From ec0bad3f646d07583a06abfb577258f232b79f4d Mon Sep 17 00:00:00 2001 From: ucorina Date: Tue, 6 Sep 2011 13:28:32 +0000 Subject: [PATCH] Updates to Condition - refactored some methods to save calculated values in private properties, so that the parsing won't execute multiple times. git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@9064 201d5d3c-b55e-5fd7-737f-ddc643e51545 --- .../queryparts/condition/Condition.class.php | 154 ++++++++++-------- .../condition/ConditionGroup.class.php | 46 +++--- .../xml/xmlquery/argument/Argument.class.php | 7 +- .../db/xml_query/cubrid/CubridSelectTest.php | 2 +- .../db/xml_query/mssql/MssqlSelectTest.php | 2 +- 5 files changed, 114 insertions(+), 97 deletions(-) diff --git a/classes/db/queryparts/condition/Condition.class.php b/classes/db/queryparts/condition/Condition.class.php index b3dec7766..87b28942f 100644 --- a/classes/db/queryparts/condition/Condition.class.php +++ b/classes/db/queryparts/condition/Condition.class.php @@ -8,11 +8,16 @@ var $_value; + var $_show; + var $_value_to_string; + + function Condition($column_name, $argument, $operation, $pipe){ $this->column_name = $column_name; $this->argument = $argument; $this->operation = $operation; $this->pipe = $pipe; + } function getArgument(){ @@ -20,18 +25,21 @@ } function toString($withValue = true){ - if(!$this->show()) return ''; - if($withValue) - return $this->toStringWithValue(); - return $this->toStringWithoutValue(); + if(!isset($this->_value_to_string)){ + if(!$this->show()) { $this->_value_to_string = ''; } + else if($withValue) + $this->_value_to_string = $this->toStringWithValue(); + else $this->_value_to_string = $this->toStringWithoutValue(); + } + return $this->_value_to_string; } function toStringWithoutValue(){ - return $this->argument; + return $this->pipe . ' ' . $this->getConditionPart($this->_value); } function toStringWithValue(){ - return $this->pipe . ' ' . $this->getConditionPart($this->_value); + return $this->pipe . ' ' . $this->getConditionPart($this->_value); } function setPipe($pipe){ @@ -39,77 +47,83 @@ } function show(){ - if(is_array($this->_value) && count($this->_value) === 1 && $this->_value[0] === '') return false; - switch($this->operation) { - case 'equal' : - case 'more' : - case 'excess' : - case 'less' : - case 'below' : - case 'like_tail' : - case 'like_prefix' : - case 'like' : - case 'in' : - case 'notin' : - case 'notequal' : - // if variable is not set or is not string or number, return - if(!isset($this->_value)) return false; - if($this->_value === '') return false; - if(!in_array(gettype($this->_value), array('string', 'integer'))) return false; + if(!isset($this->_show)){ + if(is_array($this->_value) && count($this->_value) === 1 && $this->_value[0] === '') { + $this->_show = false; + } + else { + $this->_show = true; + switch($this->operation) { + case 'equal' : + case 'more' : + case 'excess' : + case 'less' : + case 'below' : + case 'like_tail' : + case 'like_prefix' : + case 'like' : + case 'in' : + case 'notin' : + case 'notequal' : + // if variable is not set or is not string or number, return + if(!isset($this->_value)) { $this->_show = false; break;} + if($this->_value === '') { $this->_show = false; break; } + if(!in_array(gettype($this->_value), array('string', 'integer'))) {$this->_show = false; break; } break; - case 'between' : - if(!is_array($this->_value)) return false; - if(count($this->_value)!=2) return false; - + case 'between' : + if(!is_array($this->_value)) { $this->_show = false; break;} + if(count($this->_value)!=2) {$this->_show = false; break;} + } + } } - return true; + return $this->_show; } function getConditionPart($value) { - $name = $this->column_name; - $operation = $this->operation; + $name = $this->column_name; + $operation = $this->operation; - switch($operation) { - case 'equal' : - return $name.' = '.$value; - break; - case 'more' : - return $name.' >= '.$value; - break; - case 'excess' : - return $name.' > '.$value; - break; - case 'less' : - return $name.' <= '.$value; - break; - case 'below' : - return $name.' < '.$value; - break; - case 'like_tail' : - case 'like_prefix' : - case 'like' : - return $name.' like '.$value; - break; - case 'in' : - return $name.' in '.$value; - break; - case 'notin' : - return $name.' not in '.$value; - break; - case 'notequal' : - return $name.' <> '.$value; - break; - case 'notnull' : - return $name.' is not null'; - break; - case 'null' : - return $name.' is null'; - break; - case 'between' : - return $name.' between ' . $value[0] . ' and ' . $value[1]; - break; + switch($operation) { + case 'equal' : + return $name.' = '.$value; + break; + case 'more' : + return $name.' >= '.$value; + break; + case 'excess' : + return $name.' > '.$value; + break; + case 'less' : + return $name.' <= '.$value; + break; + case 'below' : + return $name.' < '.$value; + break; + case 'like_tail' : + case 'like_prefix' : + case 'like' : + return $name.' like '.$value; + break; + case 'in' : + return $name.' in '.$value; + break; + case 'notin' : + return $name.' not in '.$value; + break; + case 'notequal' : + return $name.' <> '.$value; + break; + case 'notnull' : + return $name.' is not null'; + break; + case 'null' : + return $name.' is null'; + break; + case 'between' : + return $name.' between ' . $value[0] . ' and ' . $value[1]; + break; + } } - } } ?> diff --git a/classes/db/queryparts/condition/ConditionGroup.class.php b/classes/db/queryparts/condition/ConditionGroup.class.php index f801ac63c..958e5a7c9 100644 --- a/classes/db/queryparts/condition/ConditionGroup.class.php +++ b/classes/db/queryparts/condition/ConditionGroup.class.php @@ -3,47 +3,45 @@ class ConditionGroup { var $conditions; var $pipe; - + function ConditionGroup($conditions, $pipe = "") { - $this->conditions = $conditions; + $this->conditions = array(); + foreach($conditions as $condition){ + if($condition->show()) + $this->conditions[] = $condition; + } $this->pipe = $pipe; } - + function setPipe($pipe){ $this->pipe = $pipe; } - + function toString($with_value = true){ - if($this->pipe !== "") - $group = $this->pipe .' ('; - else $group = ''; - $cond_indx = 0; - + $group = ''; + foreach($this->conditions as $condition){ - if($condition->show()){ - if($cond_indx === 0) $condition->setPipe(""); - $group .= $condition->toString($with_value) . ' '; - $cond_indx++; - } + if($cond_indx === 0) $condition->setPipe(""); + $group .= $condition->toString($with_value) . ' '; + $cond_indx++; } // If the group has no conditions in it, return '' if($cond_indx === 0) return ''; - - if($this->pipe !== "") - $group .= ')'; - + + if($this->pipe !== ""){ + $group = $this->pipe . ' (' . $group . ')'; + } + return $group; } - + function getArguments(){ $args = array(); foreach($this->conditions as $condition){ - if($condition->show()){ - $arg = $condition->getArgument(); - if($arg) $args[] = $arg; - } - } + $arg = $condition->getArgument(); + if($arg) $args[] = $arg; + } return $args; } } diff --git a/classes/xml/xmlquery/argument/Argument.class.php b/classes/xml/xmlquery/argument/Argument.class.php index 0bf4f3002..b7affe15a 100644 --- a/classes/xml/xmlquery/argument/Argument.class.php +++ b/classes/xml/xmlquery/argument/Argument.class.php @@ -10,6 +10,8 @@ var $column_operation; + var $_value; // Caches escaped and toString value so that the parsing won't happen multiple times; + function Argument($name, $value){ $this->value = $value; $this->name = $name; @@ -35,8 +37,11 @@ } function getValue(){ + if(!isset($this->_value)){ $value = $this->getEscapedValue(); - return $this->toString($value); + $this->_value = $this->toString($value); + } + return $this->_value; } function getColumnOperation(){ diff --git a/test-phpUnit/db/xml_query/cubrid/CubridSelectTest.php b/test-phpUnit/db/xml_query/cubrid/CubridSelectTest.php index af5cf9af2..c9499e0c0 100644 --- a/test-phpUnit/db/xml_query/cubrid/CubridSelectTest.php +++ b/test-phpUnit/db/xml_query/cubrid/CubridSelectTest.php @@ -133,7 +133,7 @@ $args->module = \'opage\';'; $expected = 'SELECT * FROM "xe_modules" as "modules" - WHERE "module" = \'opage\' and ("browser_title" like \'%yuhuu%\') + WHERE "module" = \'opage\' and ("title" like \'%yuhuu%\') ORDER BY "module_srl" desc LIMIT 0, 20'; $this->_test($xml_file, $argsString, $expected); diff --git a/test-phpUnit/db/xml_query/mssql/MssqlSelectTest.php b/test-phpUnit/db/xml_query/mssql/MssqlSelectTest.php index 5c080b1d5..2cea817d3 100644 --- a/test-phpUnit/db/xml_query/mssql/MssqlSelectTest.php +++ b/test-phpUnit/db/xml_query/mssql/MssqlSelectTest.php @@ -133,7 +133,7 @@ $args->module = \'opage\';'; $expected = 'SELECT TOP 20 * FROM [xe_modules] as [modules] - WHERE [module] = ? and ([browser_title] like ?) + WHERE [module] = ? and ([title] like ?) ORDER BY [module_srl] desc'; $this->_test($xml_file, $argsString, $expected, array("'opage'", "'%yuhuu%'")); }