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

View file

@ -4,7 +4,8 @@
* @package /classes/db/queryparts
* @version 0.1
*/
class Query extends Object {
class Query extends Object
{
/**
* Query id, defined in query xml file
* @var string
@ -91,7 +92,8 @@
, $groups = null
, $orderby = null
, $limit = null
, $priority = null){
, $priority = null)
{
$this->queryID = $queryID;
$this->action = $action;
$this->priority = $priority;
@ -105,29 +107,36 @@
$this->limit = $this->setLimit($limit);
}
function show(){
function show()
{
return true;
}
function setQueryId($queryID){
function setQueryId($queryID)
{
$this->queryID = $queryID;
}
function setAction($action){
function setAction($action)
{
$this->action = $action;
}
function setPriority($priority){
function setPriority($priority)
{
$this->priority = $priority;
}
function setColumnList($columnList){
function setColumnList($columnList)
{
$this->columnList = $columnList;
if(count($this->columnList) > 0) {
if(count($this->columnList) > 0)
{
$selectColumns = array();
$dbParser = DB::getParser();
foreach($this->columnList as $columnName){
foreach($this->columnList as $columnName)
{
$columnName = $dbParser->escapeColumn($columnName);
$selectColumns[] = new SelectExpression($columnName);
}
@ -136,8 +145,10 @@
}
}
function setColumns($columns){
if(!isset($columns) || count($columns) === 0){
function setColumns($columns)
{
if(!isset($columns) || count($columns) === 0)
{
$this->columns = array(new StarExpression());
return;
}
@ -147,8 +158,10 @@
$this->columns = $columns;
}
function setTables($tables){
if(!isset($tables) || count($tables) === 0){
function setTables($tables)
{
if(!isset($tables) || count($tables) === 0)
{
$this->setError(true);
$this->setMessage("You must provide at least one table for the query.");
return;
@ -159,35 +172,41 @@
$this->tables = $tables;
}
function setSubquery($subquery){
function setSubquery($subquery)
{
$this->subquery = $subquery;
}
function setConditions($conditions){
function setConditions($conditions)
{
$this->conditions = array();
if(!isset($conditions) || count($conditions) === 0) return;
if(!is_array($conditions)) $conditions = array($conditions);
foreach($conditions as $conditionGroup){
foreach($conditions as $conditionGroup)
{
if($conditionGroup->show()) $this->conditions[] = $conditionGroup;
}
}
function setGroups($groups){
function setGroups($groups)
{
if(!isset($groups) || count($groups) === 0) return;
if(!is_array($groups)) $groups = array($groups);
$this->groups = $groups;
}
function setOrder($order){
function setOrder($order)
{
if(!isset($order) || count($order) === 0) return;
if(!is_array($order)) $order = array($order);
$this->orderby = $order;
}
function setLimit($limit = NULL){
function setLimit($limit = NULL)
{
if(!isset($limit)) return;
$this->limit = $limit;
}
@ -198,7 +217,8 @@
* @param string|array $columns
* @return Query return Query instance
*/
function select($columns= null){
function select($columns= null)
{
$this->action = 'select';
$this->setColumns($columns);
return $this;
@ -209,7 +229,8 @@
* @param string|array $tables
* @return Query return Query instance
*/
function from($tables){
function from($tables)
{
$this->setTables($tables);
return $this;
}
@ -219,7 +240,8 @@
* @param string|array $conditions
* @return Query return Query instance
*/
function where($conditions){
function where($conditions)
{
$this->setConditions($conditions);
return $this;
}
@ -229,7 +251,8 @@
* @param string|array $groups
* @return Query return Query instance
*/
function groupBy($groups){
function groupBy($groups)
{
$this->setGroups($groups);
return $this;
}
@ -239,7 +262,8 @@
* @param string|array $order
* @return Query return Query instance
*/
function orderBy($order){
function orderBy($order)
{
$this->setOrder($order);
return $this;
}
@ -249,17 +273,20 @@
* @param int $limit
* @return Query return Query instance
*/
function limit($limit){
function limit($limit)
{
$this->setLimit($limit);
return $this;
}
// END Fluent interface
function getAction(){
function getAction()
{
return $this->action;
}
function getPriority(){
function getPriority()
{
return $this->priority?'LOW_PRIORITY':'';
}
@ -268,10 +295,13 @@
* @param boolean $with_values
* @return string
*/
function getSelectString($with_values = true){
foreach($this->columns as $column){
function getSelectString($with_values = true)
{
foreach($this->columns as $column)
{
if($column->show())
if($column->isSubquery()){
if($column->isSubquery())
{
$select[] = $column->toString($with_values) . ' as '. $column->getAlias();
}
else
@ -285,8 +315,10 @@
* @param boolean $with_values
* @return string
*/
function getUpdateString($with_values = true){
foreach($this->columns as $column){
function getUpdateString($with_values = true)
{
foreach($this->columns as $column)
{
if($column->show())
$update[] = $column->getExpression($with_values);
}
@ -298,25 +330,27 @@
* @param boolean $with_values
* @return string
*/
function getInsertString($with_values = true){
function getInsertString($with_values = true)
{
$columnsList = '';
if($this->subquery){ // means we have insert-select
foreach($this->columns as $column){
// means we have insert-select
if($this->subquery)
{
foreach($this->columns as $column)
{
$columnsList .= $column->getColumnName() . ', ';
}
$columnsList = substr($columnsList, 0, -2);
$selectStatement = $this->subquery->toString($with_values);
$selectStatement = substr($selectStatement, 1, -1);
return "($columnsList) \n $selectStatement";
}
$valuesList = '';
foreach($this->columns as $column){
if($column->show()){
foreach($this->columns as $column)
{
if($column->show())
{
$columnsList .= $column->getColumnName() . ', ';
$valuesList .= $column->getValue($with_values) . ', ';
}
@ -327,7 +361,8 @@
return "($columnsList) \n VALUES ($valuesList)";
}
function getTables(){
function getTables()
{
return $this->tables;
}
@ -339,10 +374,12 @@
* @param boolean $with_values
* @return string
*/
function getFromString($with_values = true){
function getFromString($with_values = true)
{
$from = '';
$simple_table_count = 0;
foreach($this->tables as $table){
foreach($this->tables as $table)
{
if($table->isJoinTable() || !$simple_table_count) $from .= $table->toString($with_values) . ' ';
else $from .= ', '.$table->toString($with_values) . ' ';
@ -360,12 +397,15 @@
* @param boolean $with_optimization
* @return string
*/
function getWhereString($with_values = true, $with_optimization = true){
function getWhereString($with_values = true, $with_optimization = true)
{
$where = '';
$condition_count = 0;
foreach ($this->conditions as $conditionGroup) {
if ($condition_count === 0) {
foreach($this->conditions as $conditionGroup)
{
if($condition_count === 0)
{
$conditionGroup->setPipe("");
}
$condition_string = $conditionGroup->toString($with_values);
@ -374,14 +414,17 @@
}
if($with_optimization &&
(strstr($this->getOrderByString(), 'list_order') || strstr($this->getOrderByString(), 'update_order'))) {
(strstr($this->getOrderByString(), 'list_order') || strstr($this->getOrderByString(), 'update_order')))
{
if($condition_count !== 0)
$where = '(' . $where . ') ';
foreach ($this->orderby as $order) {
foreach($this->orderby as $order)
{
$colName = $order->getColumnName();
if (strstr($colName, 'list_order') || strstr($colName, 'update_order')) {
if(strstr($colName, 'list_order') || strstr($colName, 'update_order'))
{
$opt_condition = new ConditionWithoutArgument($colName, 2100000000, 'less', 'and');
if($condition_count === 0)
$opt_condition->setPipe("");
@ -398,7 +441,8 @@
* Return groupby sql
* @return string
*/
function getGroupByString(){
function getGroupByString()
{
$groupBy = '';
if($this->groups) if($this->groups[0] !== "")
$groupBy = implode(', ', $this->groups);
@ -409,11 +453,14 @@
* Return orderby sql
* @return string
*/
function getOrderByString(){
if(!$this->_orderByString){
function getOrderByString()
{
if(!$this->_orderByString)
{
if(count($this->orderby) === 0) return '';
$orderBy = '';
foreach($this->orderby as $order){
foreach($this->orderby as $order)
{
$orderBy .= $order->toString() .', ';
}
$orderBy = substr($orderBy, 0, -2);
@ -422,7 +469,8 @@
return $this->_orderByString;
}
function getLimit(){
function getLimit()
{
return $this->limit;
}
@ -430,16 +478,19 @@
* Return limit sql
* @return string
*/
function getLimitString(){
function getLimitString()
{
$limit = '';
if(count($this->limit) > 0){
if(count($this->limit) > 0)
{
$limit = '';
$limit .= $this->limit->toString();
}
return $limit;
}
function getFirstTableName(){
function getFirstTableName()
{
return $this->tables[0]->getName();
}
@ -447,8 +498,10 @@
* Return argument list
* @return array
*/
function getArguments(){
if(!isset($this->arguments)){
function getArguments()
{
if(!isset($this->arguments))
{
$this->arguments = array();
// Join table arguments
@ -465,9 +518,13 @@
}
// Column arguments
if(count($this->columns) > 0){ // The if is for delete statements, all others must have columns
foreach($this->columns as $column){
if($column->show()){
// The if is for delete statements, all others must have columns
if(count($this->columns) > 0)
{
foreach($this->columns as $column)
{
if($column->show())
{
$args = $column->getArguments();
if($args) $this->arguments = array_merge($this->arguments, $args);
}
@ -476,14 +533,16 @@
// Condition arguments
if(count($this->conditions) > 0)
foreach($this->conditions as $conditionGroup){
foreach($this->conditions as $conditionGroup)
{
$args = $conditionGroup->getArguments();
if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args);
}
// Navigation arguments
if(count($this->orderby) > 0)
foreach($this->orderby as $order){
foreach($this->orderby as $order)
{
$args = $order->getArguments();
if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args);
}
@ -492,6 +551,5 @@
}
}
?>
/* End of file Query.class.php */
/* Location: ./classes/db/queryparts/Query.class.php */

View file

@ -4,7 +4,8 @@
* @package /classes/db/queryparts
* @version 0.1
*/
class Subquery extends Query {
class Subquery extends Query
{
/**
* table alias
* @var string
@ -28,7 +29,8 @@
* @param string $join_type
* @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->queryID = null;
@ -43,23 +45,29 @@
$this->join_type = $join_type;
}
function getAlias(){
function getAlias()
{
return $this->alias;
}
function isJoinTable(){
function isJoinTable()
{
if($this->join_type) return true;
return false;
}
function toString($with_values = true){
function toString($with_values = true)
{
$oDB = &DB::getInstance();
return '(' .$oDB->getSelectSql($this, $with_values) . ')';
}
function isSubquery(){
function isSubquery()
{
return true;
}
}
/* End of file Subquery.class.php */
/* Location: ./classes/db/queryparts/Subquery.class.php */

View file

@ -4,7 +4,8 @@
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class Condition {
class Condition
{
/**
* column name
* @var string
@ -37,15 +38,16 @@
* @param string $pipe
* @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->operation = $operation;
$this->pipe = $pipe;
}
function getArgument(){
function getArgument()
{
return null;
}
@ -54,8 +56,10 @@
* @param boolean $withValue
* @return string
*/
function toString($withValue = true){
if (!isset($this->_value_to_string)) {
function toString($withValue = true)
{
if (!isset($this->_value_to_string))
{
if (!$this->show())
{
$this->_value_to_string = '';
@ -76,7 +80,8 @@
* change string without value
* @return string
*/
function toStringWithoutValue(){
function toStringWithoutValue()
{
return $this->pipe . ' ' . $this->getConditionPart($this->_value);
}
@ -84,25 +89,32 @@
* change string with value
* @return string
*/
function toStringWithValue(){
function toStringWithValue()
{
return $this->pipe . ' ' . $this->getConditionPart($this->_value);
}
function setPipe($pipe){
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] === '') {
function show()
{
if(!isset($this->_show))
{
if(is_array($this->_value) && count($this->_value) === 1 && $this->_value[0] === '')
{
$this->_show = false;
}
else {
else
{
$this->_show = true;
switch($this->operation) {
switch($this->operation)
{
case 'equal' :
case 'more' :
case 'excess' :
@ -151,11 +163,13 @@
* @param int|string|array $value
* @return string
*/
function getConditionPart($value) {
function getConditionPart($value)
{
$name = $this->column_name;
$operation = $this->operation;
switch($operation) {
switch($operation)
{
case 'equal' :
return $name.' = '.$value;
break;
@ -219,5 +233,5 @@
}
}
}
?>
/* End of file Condition.class.php */
/* Location: ./classes/db/queryparts/condition/Condition.class.php */

View file

@ -4,7 +4,8 @@
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionGroup {
class ConditionGroup
{
/**
* condition list
* @var array
@ -25,9 +26,11 @@
* @param string $pipe
* @return void
*/
function ConditionGroup($conditions, $pipe = "") {
function ConditionGroup($conditions, $pipe = "")
{
$this->conditions = array();
foreach($conditions as $condition){
foreach($conditions as $condition)
{
if($condition->show())
$this->conditions[] = $condition;
}
@ -37,11 +40,13 @@
$this->pipe = $pipe;
}
function show(){
function show()
{
return $this->_show;
}
function setPipe($pipe){
function setPipe($pipe)
{
if($this->pipe !== $pipe) $this->_group = null;
$this->pipe = $pipe;
}
@ -51,18 +56,22 @@
* @param boolean $with_value
* @return string
*/
function toString($with_value = true){
if(!isset($this->_group)){
function toString($with_value = true)
{
if(!isset($this->_group))
{
$cond_indx = 0;
$group = '';
foreach($this->conditions as $condition){
foreach($this->conditions as $condition)
{
if($cond_indx === 0) $condition->setPipe("");
$group .= $condition->toString($with_value) . ' ';
$cond_indx++;
}
if($this->pipe !== "" && trim($group) !== ''){
if($this->pipe !== "" && trim($group) !== '')
{
$group = $this->pipe . ' (' . $group . ')';
}
@ -75,13 +84,16 @@
* return argument list
* @return array
*/
function getArguments(){
function getArguments()
{
$args = array();
foreach($this->conditions as $condition){
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

@ -4,8 +4,8 @@
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionSubquery extends Condition {
class ConditionSubquery extends Condition
{
/**
* constructor
* @param string $column_name
@ -14,10 +14,11 @@
* @param string $pipe
* @return void
*/
function ConditionSubquery($column_name, $argument, $operation, $pipe = ""){
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

@ -4,8 +4,8 @@
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionWithArgument extends Condition {
class ConditionWithArgument extends Condition
{
/**
* constructor
* @param string $column_name
@ -14,13 +14,15 @@
* @param string $pipe
* @return void
*/
function ConditionWithArgument($column_name, $argument, $operation, $pipe = ""){
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(){
function getArgument()
{
if(!$this->show()) return;
return $this->argument;
}
@ -29,10 +31,12 @@
* change string without value
* @return string
*/
function toStringWithoutValue(){
function toStringWithoutValue()
{
$value = $this->argument->getUnescapedValue();
if(is_array($value)){
if(is_array($value))
{
$q = '';
foreach ($value as $v) $q .= '?,';
if($q !== '') $q = substr($q, 0, -1);
@ -56,16 +60,19 @@
/**
* @return boolean
*/
function show(){
if(!isset($this->_show)){
function show()
{
if(!isset($this->_show))
{
if(!$this->argument->isValid()) $this->_show = false;
if($this->_value === '\'\'') $this->_show = false;
if(!isset($this->_show)){
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

@ -4,7 +4,8 @@
* @package /classes/db/queryparts/condition
* @version 0.1
*/
class ConditionWithoutArgument extends Condition {
class ConditionWithoutArgument extends Condition
{
/**
* constructor
* @param string $column_name
@ -13,17 +14,19 @@
* @param string $pipe
* @return void
*/
function ConditionWithoutArgument($column_name, $argument, $operation, $pipe = ""){
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(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 */