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
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts
* @version 0.1
*/
class Subquery extends Query
{
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts
* @version 0.1
* table alias
* @var string
*/
class Subquery extends Query {
/**
* table alias
* @var string
*/
var $alias;
/**
* join type
* @var string
*/
var $join_type;
var $alias;
/**
* join type
* @var string
*/
var $join_type;
/**
* constructor
* @param string $alias
* @param string|array $columns
* @param string|array $tables
* @param string|array $conditions
* @param string|array $groups
* @param string|array $orderby
* @param int $limit
* @param string $join_type
* @return void
*/
function Subquery($alias, $columns, $tables, $conditions, $groups, $orderby, $limit, $join_type = null){
$this->alias = $alias;
/**
* constructor
* @param string $alias
* @param string|array $columns
* @param string|array $tables
* @param string|array $conditions
* @param string|array $groups
* @param string|array $orderby
* @param int $limit
* @param string $join_type
* @return void
*/
function Subquery($alias, $columns, $tables, $conditions, $groups, $orderby, $limit, $join_type = null)
{
$this->alias = $alias;
$this->queryID = null;
$this->action = "select";
$this->queryID = null;
$this->action = "select";
$this->columns = $columns;
$this->tables = $tables;
$this->conditions = $conditions;
$this->groups = $groups;
$this->orderby = $orderby;
$this->limit = $limit;
$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;
}
$this->columns = $columns;
$this->tables = $tables;
$this->conditions = $conditions;
$this->groups = $groups;
$this->orderby = $orderby;
$this->limit = $limit;
$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;
}
}
/* End of file Subquery.class.php */
/* Location: ./classes/db/queryparts/Subquery.class.php */

View file

@ -1,223 +1,237 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class Condition
{
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
* column name
* @var string
*/
class Condition {
/**
* column name
* @var string
*/
var $column_name;
var $argument;
/**
* 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'
* 'null', 'notnull'
* @var string
*/
var $operation;
/**
* pipe can use 'and', 'or'...
* @var string
*/
var $pipe;
var $column_name;
var $argument;
/**
* 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'
* 'null', 'notnull'
* @var string
*/
var $operation;
/**
* pipe can use 'and', 'or'...
* @var string
*/
var $pipe;
var $_value;
var $_value;
var $_show;
var $_value_to_string;
var $_show;
var $_value_to_string;
/**
* constructor
* @param string $column_name
* @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/
function Condition($column_name, $argument, $operation, $pipe){
$this->column_name = $column_name;
$this->argument = $argument;
$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;
}
}
/**
* constructor
* @param string $column_name
* @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/
function Condition($column_name, $argument, $operation, $pipe)
{
$this->column_name = $column_name;
$this->argument = $argument;
$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;
}
}
}
/* End of file Condition.class.php */
/* Location: ./classes/db/queryparts/condition/Condition.class.php */

View file

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

View file

@ -1,23 +1,24 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionSubquery extends Condition
{
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
* constructor
* @param string $column_name
* @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/
class ConditionSubquery extends Condition {
/**
* constructor
* @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();
}
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
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionWithArgument extends Condition
{
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
* constructor
* @param string $column_name
* @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/
class ConditionWithArgument extends Condition {
/**
* constructor
* @param string $column_name
* @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 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;
}
}
/* End of file ConditionWithArgument.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionWithArgument.class.php */

View file

@ -1,29 +1,32 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionWithoutArgument extends Condition
{
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
* @version 0.1
* constructor
* @param string $column_name
* @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/
class ConditionWithoutArgument extends Condition {
/**
* constructor
* @param string $column_name
* @param mixed $argument
* @param string $operation
* @param string $pipe
* @return void
*/
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;
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;
}
}
?>
/* End of file ConditionWithoutArgument.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionWithoutArgument.class.php */