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
This commit is contained in:
ucorina 2011-09-06 13:28:32 +00:00
parent 385d704388
commit ec0bad3f64
5 changed files with 114 additions and 97 deletions

View file

@ -8,11 +8,16 @@
var $_value; var $_value;
var $_show;
var $_value_to_string;
function Condition($column_name, $argument, $operation, $pipe){ function Condition($column_name, $argument, $operation, $pipe){
$this->column_name = $column_name; $this->column_name = $column_name;
$this->argument = $argument; $this->argument = $argument;
$this->operation = $operation; $this->operation = $operation;
$this->pipe = $pipe; $this->pipe = $pipe;
} }
function getArgument(){ function getArgument(){
@ -20,18 +25,21 @@
} }
function toString($withValue = true){ function toString($withValue = true){
if(!$this->show()) return ''; if(!isset($this->_value_to_string)){
if($withValue) if(!$this->show()) { $this->_value_to_string = ''; }
return $this->toStringWithValue(); else if($withValue)
return $this->toStringWithoutValue(); $this->_value_to_string = $this->toStringWithValue();
else $this->_value_to_string = $this->toStringWithoutValue();
}
return $this->_value_to_string;
} }
function toStringWithoutValue(){ function toStringWithoutValue(){
return $this->argument; return $this->pipe . ' ' . $this->getConditionPart($this->_value);
} }
function toStringWithValue(){ function toStringWithValue(){
return $this->pipe . ' ' . $this->getConditionPart($this->_value); return $this->pipe . ' ' . $this->getConditionPart($this->_value);
} }
function setPipe($pipe){ function setPipe($pipe){
@ -39,77 +47,83 @@
} }
function show(){ function show(){
if(is_array($this->_value) && count($this->_value) === 1 && $this->_value[0] === '') return false; if(!isset($this->_show)){
switch($this->operation) { if(is_array($this->_value) && count($this->_value) === 1 && $this->_value[0] === '') {
case 'equal' : $this->_show = false;
case 'more' : }
case 'excess' : else {
case 'less' : $this->_show = true;
case 'below' : switch($this->operation) {
case 'like_tail' : case 'equal' :
case 'like_prefix' : case 'more' :
case 'like' : case 'excess' :
case 'in' : case 'less' :
case 'notin' : case 'below' :
case 'notequal' : case 'like_tail' :
// if variable is not set or is not string or number, return case 'like_prefix' :
if(!isset($this->_value)) return false; case 'like' :
if($this->_value === '') return false; case 'in' :
if(!in_array(gettype($this->_value), array('string', 'integer'))) return false; 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; break;
case 'between' : case 'between' :
if(!is_array($this->_value)) return false; if(!is_array($this->_value)) { $this->_show = false; break;}
if(count($this->_value)!=2) return false; if(count($this->_value)!=2) {$this->_show = false; break;}
}
}
} }
return true; return $this->_show;
} }
function getConditionPart($value) { function getConditionPart($value) {
$name = $this->column_name; $name = $this->column_name;
$operation = $this->operation; $operation = $this->operation;
switch($operation) { switch($operation) {
case 'equal' : case 'equal' :
return $name.' = '.$value; return $name.' = '.$value;
break; break;
case 'more' : case 'more' :
return $name.' >= '.$value; return $name.' >= '.$value;
break; break;
case 'excess' : case 'excess' :
return $name.' > '.$value; return $name.' > '.$value;
break; break;
case 'less' : case 'less' :
return $name.' <= '.$value; return $name.' <= '.$value;
break; break;
case 'below' : case 'below' :
return $name.' < '.$value; return $name.' < '.$value;
break; break;
case 'like_tail' : case 'like_tail' :
case 'like_prefix' : case 'like_prefix' :
case 'like' : case 'like' :
return $name.' like '.$value; return $name.' like '.$value;
break; break;
case 'in' : case 'in' :
return $name.' in '.$value; return $name.' in '.$value;
break; break;
case 'notin' : case 'notin' :
return $name.' not in '.$value; return $name.' not in '.$value;
break; break;
case 'notequal' : case 'notequal' :
return $name.' <> '.$value; return $name.' <> '.$value;
break; break;
case 'notnull' : case 'notnull' :
return $name.' is not null'; return $name.' is not null';
break; break;
case 'null' : case 'null' :
return $name.' is null'; return $name.' is null';
break; break;
case 'between' : case 'between' :
return $name.' between ' . $value[0] . ' and ' . $value[1]; return $name.' between ' . $value[0] . ' and ' . $value[1];
break; break;
}
} }
}
} }
?> ?>

View file

@ -3,47 +3,45 @@
class ConditionGroup { class ConditionGroup {
var $conditions; var $conditions;
var $pipe; var $pipe;
function ConditionGroup($conditions, $pipe = "") { function ConditionGroup($conditions, $pipe = "") {
$this->conditions = $conditions; $this->conditions = array();
foreach($conditions as $condition){
if($condition->show())
$this->conditions[] = $condition;
}
$this->pipe = $pipe; $this->pipe = $pipe;
} }
function setPipe($pipe){ function setPipe($pipe){
$this->pipe = $pipe; $this->pipe = $pipe;
} }
function toString($with_value = true){ function toString($with_value = true){
if($this->pipe !== "")
$group = $this->pipe .' (';
else $group = '';
$cond_indx = 0; $cond_indx = 0;
$group = '';
foreach($this->conditions as $condition){ foreach($this->conditions as $condition){
if($condition->show()){ if($cond_indx === 0) $condition->setPipe("");
if($cond_indx === 0) $condition->setPipe(""); $group .= $condition->toString($with_value) . ' ';
$group .= $condition->toString($with_value) . ' '; $cond_indx++;
$cond_indx++;
}
} }
// If the group has no conditions in it, return '' // If the group has no conditions in it, return ''
if($cond_indx === 0) return ''; if($cond_indx === 0) return '';
if($this->pipe !== "") if($this->pipe !== ""){
$group .= ')'; $group = $this->pipe . ' (' . $group . ')';
}
return $group; return $group;
} }
function getArguments(){ function getArguments(){
$args = array(); $args = array();
foreach($this->conditions as $condition){ foreach($this->conditions as $condition){
if($condition->show()){ $arg = $condition->getArgument();
$arg = $condition->getArgument(); if($arg) $args[] = $arg;
if($arg) $args[] = $arg; }
}
}
return $args; return $args;
} }
} }

View file

@ -10,6 +10,8 @@
var $column_operation; var $column_operation;
var $_value; // Caches escaped and toString value so that the parsing won't happen multiple times;
function Argument($name, $value){ function Argument($name, $value){
$this->value = $value; $this->value = $value;
$this->name = $name; $this->name = $name;
@ -35,8 +37,11 @@
} }
function getValue(){ function getValue(){
if(!isset($this->_value)){
$value = $this->getEscapedValue(); $value = $this->getEscapedValue();
return $this->toString($value); $this->_value = $this->toString($value);
}
return $this->_value;
} }
function getColumnOperation(){ function getColumnOperation(){

View file

@ -133,7 +133,7 @@
$args->module = \'opage\';'; $args->module = \'opage\';';
$expected = 'SELECT * $expected = 'SELECT *
FROM "xe_modules" as "modules" 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 ORDER BY "module_srl" desc
LIMIT 0, 20'; LIMIT 0, 20';
$this->_test($xml_file, $argsString, $expected); $this->_test($xml_file, $argsString, $expected);

View file

@ -133,7 +133,7 @@
$args->module = \'opage\';'; $args->module = \'opage\';';
$expected = 'SELECT TOP 20 * $expected = 'SELECT TOP 20 *
FROM [xe_modules] as [modules] FROM [xe_modules] as [modules]
WHERE [module] = ? and ([browser_title] like ?) WHERE [module] = ? and ([title] like ?)
ORDER BY [module_srl] desc'; ORDER BY [module_srl] desc';
$this->_test($xml_file, $argsString, $expected, array("'opage'", "'%yuhuu%'")); $this->_test($xml_file, $argsString, $expected, array("'opage'", "'%yuhuu%'"));
} }