issue 2662 queryparts, condition coding convention

git-svn-id: http://xe-core.googlecode.com/svn/branches/maserati@12214 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ovclas 2012-11-14 06:46:49 +00:00
parent c7eef605f8
commit f1f7d80540
7 changed files with 1024 additions and 921 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,65 +1,73 @@
<?php <?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts
* @version 0.1
*/
class Subquery extends Query
{
/** /**
* @author NHN (developers@xpressengine.com) * table alias
* @package /classes/db/queryparts * @var string
* @version 0.1
*/ */
class Subquery extends Query { var $alias;
/** /**
* table alias * join type
* @var string * @var string
*/ */
var $alias; var $join_type;
/**
* join type
* @var string
*/
var $join_type;
/** /**
* constructor * constructor
* @param string $alias * @param string $alias
* @param string|array $columns * @param string|array $columns
* @param string|array $tables * @param string|array $tables
* @param string|array $conditions * @param string|array $conditions
* @param string|array $groups * @param string|array $groups
* @param string|array $orderby * @param string|array $orderby
* @param int $limit * @param int $limit
* @param string $join_type * @param string $join_type
* @return void * @return void
*/ */
function Subquery($alias, $columns, $tables, $conditions, $groups, $orderby, $limit, $join_type = null){ function Subquery($alias, $columns, $tables, $conditions, $groups, $orderby, $limit, $join_type = null)
$this->alias = $alias; {
$this->alias = $alias;
$this->queryID = null; $this->queryID = null;
$this->action = "select"; $this->action = "select";
$this->columns = $columns; $this->columns = $columns;
$this->tables = $tables; $this->tables = $tables;
$this->conditions = $conditions; $this->conditions = $conditions;
$this->groups = $groups; $this->groups = $groups;
$this->orderby = $orderby; $this->orderby = $orderby;
$this->limit = $limit; $this->limit = $limit;
$this->join_type = $join_type; $this->join_type = $join_type;
}
function getAlias(){
return $this->alias;
}
function isJoinTable(){
if($this->join_type) return true;
return false;
}
function toString($with_values = true){
$oDB = &DB::getInstance();
return '(' .$oDB->getSelectSql($this, $with_values) . ')';
}
function isSubquery(){
return true;
}
} }
function getAlias()
{
return $this->alias;
}
function isJoinTable()
{
if($this->join_type) return true;
return false;
}
function toString($with_values = true)
{
$oDB = &DB::getInstance();
return '(' .$oDB->getSelectSql($this, $with_values) . ')';
}
function isSubquery()
{
return true;
}
}
/* End of file Subquery.class.php */
/* Location: ./classes/db/queryparts/Subquery.class.php */

View file

@ -1,223 +1,237 @@
<?php <?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class Condition
{
/** /**
* @author NHN (developers@xpressengine.com) * column name
* @package /classes/db/queryparts/condition * @var string
* @version 0.1
*/ */
class Condition { var $column_name;
/** var $argument;
* column name /**
* @var string * operation can use 'equal', 'more', 'excess', 'less', 'below', 'like_tail', 'like_prefix', 'like', 'notlike_tail',
*/ * 'notlike_prefix', 'notlike', 'in', 'notin', 'not_in', 'and', 'or', 'xor', 'not', 'notequal', 'between'
var $column_name; * 'null', 'notnull'
var $argument; * @var string
/** */
* operation can use 'equal', 'more', 'excess', 'less', 'below', 'like_tail', 'like_prefix', 'like', 'notlike_tail', var $operation;
* 'notlike_prefix', 'notlike', 'in', 'notin', 'not_in', 'and', 'or', 'xor', 'not', 'notequal', 'between' /**
* 'null', 'notnull' * pipe can use 'and', 'or'...
* @var string * @var string
*/ */
var $operation; var $pipe;
/**
* pipe can use 'and', 'or'...
* @var string
*/
var $pipe;
var $_value; var $_value;
var $_show; var $_show;
var $_value_to_string; var $_value_to_string;
/** /**
* constructor * constructor
* @param string $column_name * @param string $column_name
* @param mixed $argument * @param mixed $argument
* @param string $operation * @param string $operation
* @param string $pipe * @param string $pipe
* @return void * @return void
*/ */
function Condition($column_name, $argument, $operation, $pipe){ function Condition($column_name, $argument, $operation, $pipe)
$this->column_name = $column_name; {
$this->argument = $argument; $this->column_name = $column_name;
$this->operation = $operation; $this->argument = $argument;
$this->pipe = $pipe; $this->operation = $operation;
$this->pipe = $pipe;
}
function getArgument(){
return null;
}
/**
* value to string
* @param boolean $withValue
* @return string
*/
function toString($withValue = true){
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;
}
/**
* change string without value
* @return string
*/
function toStringWithoutValue(){
return $this->pipe . ' ' . $this->getConditionPart($this->_value);
}
/**
* change string with value
* @return string
*/
function toStringWithValue(){
return $this->pipe . ' ' . $this->getConditionPart($this->_value);
}
function setPipe($pipe){
$this->pipe = $pipe;
}
/**
* @return boolean
*/
function show(){
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 'notlike_tail' :
case 'notlike_prefix' :
case 'notlike' :
case 'in' :
case 'notin' :
case 'not_in' :
case 'and':
case 'or':
case 'xor':
case 'not':
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; }
$tmpArray = array('string'=>1, 'integer'=>1);
if(!isset($tmpArray[gettype($this->_value)]))
{
$this->_show = false; break;
}
break;
case 'between' :
if(!is_array($this->_value)) { $this->_show = false; break;}
if(count($this->_value)!=2) {$this->_show = false; break;}
case 'null':
case 'notnull':
break;
default:
// If operation is not one of the above, means the condition is invalid
$this->_show = false;
}
}
}
return $this->_show;
}
/**
* Return condition string
* @param int|string|array $value
* @return string
*/
function getConditionPart($value) {
$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' :
if(defined('__CUBRID_VERSION__')
&& __CUBRID_VERSION__ >= '8.4.1')
return $name.' rlike '.$value;
else
return $name.' like '.$value;
break;
case 'notlike_tail' :
case 'notlike_prefix' :
case 'notlike' :
return $name.' not like '.$value;
break;
case 'in' :
return $name.' in '.$value;
break;
case 'notin' :
case 'not_in' :
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 'and' :
return $name.' & '.$value;
break;
case 'or' :
return $name.' | '.$value;
break;
case 'xor' :
return $name.' ^ '.$value;
break;
case 'not' :
return $name.' ~ '.$value;
break;
case 'between' :
return $name.' between ' . $value[0] . ' and ' . $value[1];
break;
}
}
} }
?> function getArgument()
{
return null;
}
/**
* value to string
* @param boolean $withValue
* @return string
*/
function toString($withValue = true)
{
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;
}
/**
* change string without value
* @return string
*/
function toStringWithoutValue()
{
return $this->pipe . ' ' . $this->getConditionPart($this->_value);
}
/**
* change string with value
* @return string
*/
function toStringWithValue()
{
return $this->pipe . ' ' . $this->getConditionPart($this->_value);
}
function setPipe($pipe)
{
$this->pipe = $pipe;
}
/**
* @return boolean
*/
function show()
{
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 'notlike_tail' :
case 'notlike_prefix' :
case 'notlike' :
case 'in' :
case 'notin' :
case 'not_in' :
case 'and':
case 'or':
case 'xor':
case 'not':
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; }
$tmpArray = array('string'=>1, 'integer'=>1);
if(!isset($tmpArray[gettype($this->_value)]))
{
$this->_show = false; break;
}
break;
case 'between' :
if(!is_array($this->_value)) { $this->_show = false; break;}
if(count($this->_value)!=2) {$this->_show = false; break;}
case 'null':
case 'notnull':
break;
default:
// If operation is not one of the above, means the condition is invalid
$this->_show = false;
}
}
}
return $this->_show;
}
/**
* Return condition string
* @param int|string|array $value
* @return string
*/
function getConditionPart($value)
{
$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' :
if(defined('__CUBRID_VERSION__')
&& __CUBRID_VERSION__ >= '8.4.1')
return $name.' rlike '.$value;
else
return $name.' like '.$value;
break;
case 'notlike_tail' :
case 'notlike_prefix' :
case 'notlike' :
return $name.' not like '.$value;
break;
case 'in' :
return $name.' in '.$value;
break;
case 'notin' :
case 'not_in' :
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 'and' :
return $name.' & '.$value;
break;
case 'or' :
return $name.' | '.$value;
break;
case 'xor' :
return $name.' ^ '.$value;
break;
case 'not' :
return $name.' ~ '.$value;
break;
case 'between' :
return $name.' between ' . $value[0] . ' and ' . $value[1];
break;
}
}
}
/* End of file Condition.class.php */
/* Location: ./classes/db/queryparts/condition/Condition.class.php */

View file

@ -1,87 +1,99 @@
<?php <?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionGroup
{
/** /**
* @author NHN (developers@xpressengine.com) * condition list
* @package /classes/db/queryparts/condition * @var array
* @version 0.1
*/ */
class ConditionGroup { var $conditions;
/** /**
* condition list * pipe can use 'and', 'or'...
* @var array * @var string
*/ */
var $conditions; var $pipe;
/**
* pipe can use 'and', 'or'...
* @var string
*/
var $pipe;
var $_group; var $_group;
var $_show; var $_show;
/** /**
* constructor * constructor
* @param array $conditions * @param array $conditions
* @param string $pipe * @param string $pipe
* @return void * @return void
*/ */
function ConditionGroup($conditions, $pipe = "") { function ConditionGroup($conditions, $pipe = "")
$this->conditions = array(); {
foreach($conditions as $condition){ $this->conditions = array();
if($condition->show()) foreach($conditions as $condition)
$this->conditions[] = $condition; {
} if($condition->show())
if(count($this->conditions) === 0) $this->_show = false; $this->conditions[] = $condition;
else $this->_show = true;
$this->pipe = $pipe;
} }
if(count($this->conditions) === 0) $this->_show = false;
else $this->_show = true;
function show(){ $this->pipe = $pipe;
return $this->_show; }
}
function setPipe($pipe){ function show()
if($this->pipe !== $pipe) $this->_group = null; {
$this->pipe = $pipe; return $this->_show;
} }
/** function setPipe($pipe)
* value to string {
* @param boolean $with_value if($this->pipe !== $pipe) $this->_group = null;
* @return string $this->pipe = $pipe;
*/ }
function toString($with_value = true){
if(!isset($this->_group)){ /**
* value to string
* @param boolean $with_value
* @return string
*/
function toString($with_value = true)
{
if(!isset($this->_group))
{
$cond_indx = 0; $cond_indx = 0;
$group = ''; $group = '';
foreach($this->conditions as $condition){ foreach($this->conditions as $condition)
if($cond_indx === 0) $condition->setPipe(""); {
$group .= $condition->toString($with_value) . ' '; if($cond_indx === 0) $condition->setPipe("");
$cond_indx++; $group .= $condition->toString($with_value) . ' ';
$cond_indx++;
} }
if($this->pipe !== "" && trim($group) !== ''){ if($this->pipe !== "" && trim($group) !== '')
$group = $this->pipe . ' (' . $group . ')'; {
} $group = $this->pipe . ' (' . $group . ')';
}
$this->_group = $group; $this->_group = $group;
}
return $this->_group;
}
/**
* return argument list
* @return array
*/
function getArguments(){
$args = array();
foreach($this->conditions as $condition){
$arg = $condition->getArgument();
if($arg) $args[] = $arg;
}
return $args;
} }
return $this->_group;
} }
?>
/**
* return argument list
* @return array
*/
function getArguments()
{
$args = array();
foreach($this->conditions as $condition)
{
$arg = $condition->getArgument();
if($arg) $args[] = $arg;
}
return $args;
}
}
/* End of file ConditionGroup.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionGroup.class.php */

View file

@ -1,23 +1,24 @@
<?php <?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionSubquery extends Condition
{
/** /**
* @author NHN (developers@xpressengine.com) * constructor
* @package /classes/db/queryparts/condition * @param string $column_name
* @version 0.1 * @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/ */
class ConditionSubquery extends Condition { function ConditionSubquery($column_name, $argument, $operation, $pipe = "")
{
/** parent::Condition($column_name, $argument, $operation, $pipe);
* constructor $this->_value = $this->argument->toString();
* @param string $column_name
* @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/
function ConditionSubquery($column_name, $argument, $operation, $pipe = ""){
parent::Condition($column_name, $argument, $operation, $pipe);
$this->_value = $this->argument->toString();
}
} }
}
?> /* End of file ConditionSubquery.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionSubquery.class.php */

View file

@ -1,71 +1,78 @@
<?php <?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionWithArgument extends Condition
{
/** /**
* @author NHN (developers@xpressengine.com) * constructor
* @package /classes/db/queryparts/condition * @param string $column_name
* @version 0.1 * @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/ */
class ConditionWithArgument extends Condition { function ConditionWithArgument($column_name, $argument, $operation, $pipe = "")
{
/** if($argument === null) { $this->_show = false; return; }
* constructor parent::Condition($column_name, $argument, $operation, $pipe);
* @param string $column_name $this->_value = $argument->getValue();
* @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/
function ConditionWithArgument($column_name, $argument, $operation, $pipe = ""){
if($argument === null) { $this->_show = false; return; }
parent::Condition($column_name, $argument, $operation, $pipe);
$this->_value = $argument->getValue();
}
function getArgument(){
if(!$this->show()) return;
return $this->argument;
}
/**
* change string without value
* @return string
*/
function toStringWithoutValue(){
$value = $this->argument->getUnescapedValue();
if(is_array($value)){
$q = '';
foreach ($value as $v) $q .= '?,';
if($q !== '') $q = substr($q, 0, -1);
$q = '(' . $q . ')';
}
else
{
// Prepared statements: column names should not be sent as query arguments, but instead concatenated to query string
if($this->argument->isColumnName())
{
$q = $value;
}
else
{
$q = '?';
}
}
return $this->pipe . ' ' . $this->getConditionPart($q);
}
/**
* @return boolean
*/
function show(){
if(!isset($this->_show)){
if(!$this->argument->isValid()) $this->_show = false;
if($this->_value === '\'\'') $this->_show = false;
if(!isset($this->_show)){
return parent::show();
}
}
return $this->_show;
}
} }
?> function getArgument()
{
if(!$this->show()) return;
return $this->argument;
}
/**
* change string without value
* @return string
*/
function toStringWithoutValue()
{
$value = $this->argument->getUnescapedValue();
if(is_array($value))
{
$q = '';
foreach ($value as $v) $q .= '?,';
if($q !== '') $q = substr($q, 0, -1);
$q = '(' . $q . ')';
}
else
{
// Prepared statements: column names should not be sent as query arguments, but instead concatenated to query string
if($this->argument->isColumnName())
{
$q = $value;
}
else
{
$q = '?';
}
}
return $this->pipe . ' ' . $this->getConditionPart($q);
}
/**
* @return boolean
*/
function show()
{
if(!isset($this->_show))
{
if(!$this->argument->isValid()) $this->_show = false;
if($this->_value === '\'\'') $this->_show = false;
if(!isset($this->_show))
{
return parent::show();
}
}
return $this->_show;
}
}
/* End of file ConditionWithArgument.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionWithArgument.class.php */

View file

@ -1,29 +1,32 @@
<?php <?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionWithoutArgument extends Condition
{
/** /**
* @author NHN (developers@xpressengine.com) * constructor
* @package /classes/db/queryparts/condition * @param string $column_name
* @version 0.1 * @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/ */
class ConditionWithoutArgument extends Condition { function ConditionWithoutArgument($column_name, $argument, $operation, $pipe = "")
/** {
* constructor parent::Condition($column_name, $argument, $operation, $pipe);
* @param string $column_name $tmpArray = array('in'=>1, 'notin'=>1, 'not_in'=>1);
* @param mixed $argument if(isset($tmpArray[$operation]))
* @param string $operation {
* @param string $pipe if(is_array($argument)) $argument = implode($argument, ',');
* @return void $this->_value = '('. $argument .')';
*/
function ConditionWithoutArgument($column_name, $argument, $operation, $pipe = ""){
parent::Condition($column_name, $argument, $operation, $pipe);
$tmpArray = array('in'=>1, 'notin'=>1, 'not_in'=>1);
if(isset($tmpArray[$operation])){
if(is_array($argument)) $argument = implode($argument, ',');
$this->_value = '('. $argument .')';
}
else
$this->_value = $argument;
} }
else
$this->_value = $argument;
} }
}
?> /* End of file ConditionWithoutArgument.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionWithoutArgument.class.php */