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 $_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;
}
}
}
}
?>

View file

@ -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;
}
}