Improvements for query classes.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@9102 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-09-07 17:10:40 +00:00
parent f1c9668478
commit 78db12a858
6 changed files with 88 additions and 76 deletions

View file

@ -582,7 +582,7 @@
} }
function getUpdateSql($query, $with_values = true){ function getUpdateSql($query, $with_values = true){
$columnsList = $query->getSelectString($with_values); $columnsList = $query->getUpdateString($with_values);
if($columnsList == '') return new Object(-1, "Invalid query"); if($columnsList == '') return new Object(-1, "Invalid query");
$tableName = $query->getFirstTableName(); $tableName = $query->getFirstTableName();

View file

@ -48,8 +48,18 @@
} }
function setColumnList($columnList){ function setColumnList($columnList){
if(count($columnList) > 0)
$this->columnList = $columnList; $this->columnList = $columnList;
if(count($this->columnList) > 0) {
$selectColumns = array();
$dbParser = XmlQueryParser::getDBParser();
foreach($this->columnList as $columnName){
$columnName = $dbParser->escapeColumn($columnName);
$selectColumns[] = new SelectExpression($columnName);
}
unset($this->columns);
$this->columns = $selectColumns;
}
} }
function setColumns($columns){ function setColumns($columns){
@ -76,10 +86,13 @@
} }
function setConditions($conditions){ function setConditions($conditions){
if(!isset($conditions) || count($conditions) === 0) return; $this->conditions = array();
if(!is_array($conditions)) $conditions = array($conditions); if(!isset($conditions) || count($conditions) === 0) return;
if(!is_array($conditions)) $conditions = array($conditions);
$this->conditions = $conditions; foreach($conditions as $conditionGroup){
if($conditionGroup->show()) $this->conditions[] = $conditionGroup;
}
} }
function setGroups($groups){ function setGroups($groups){
@ -139,34 +152,23 @@
} }
function getSelectString($with_values = true){ function getSelectString($with_values = true){
if(isset($this->columnList)){ foreach($this->columns as $column){
$selectColumns = array(); if($column->show())
$dbParser = XmlQueryParser::getDBParser(); if($column->isSubquery()){
$select[] = $column->toString($with_values) . ' as '. $column->getAlias();
foreach($this->columnList as $columnName){ }
$columnName = $dbParser->escapeColumn($columnName); else
$selectColumns[] = new SelectExpression($columnName); $select[] = $column->getExpression($with_values);
} }
} return trim(implode($select, ', '));
else
$selectColumns = $this->columns;
$select = '';
foreach($selectColumns as $column){
if($column->show())
if(is_a($column, 'Subquery')){
$select .= $column->toString($with_values) . ' as '. $column->getAlias() .', ';
}
else
$select .= $column->getExpression($with_values) . ', ';
}
if(trim($select) == '') return '';
$select = substr($select, 0, -2);
return $select;
} }
function getUpdateString($with_values = true){ function getUpdateString($with_values = true){
return $this->getSelectString($with_values); foreach($this->columns as $column){
if($column->show())
$update[] = $column->getExpression($with_values);
}
return trim(implode($update, ', '));
} }
function getInsertString($with_values = true){ function getInsertString($with_values = true){
@ -209,21 +211,16 @@
function getWhereString($with_values = true){ function getWhereString($with_values = true){
$where = ''; $where = '';
if(count($this->conditions) > 0){ $condition_count = 0;
$condition_count = 0; foreach($this->conditions as $conditionGroup){
foreach($this->conditions as $conditionGroup){ if($condition_count === 0){
$condition_string = $conditionGroup->toString($with_values); $conditionGroup->setPipe("");
if($condition_string !== '') $condition_count++; }
if($condition_count === 1){ $condition_string = $conditionGroup->toString($with_values);
$conditionGroup->setPipe(""); $where .= $condition_string;
$condition_string = $conditionGroup->toString($with_values); $condition_count++;
} }
$where .= $condition_string; return trim($where);
}
if(trim($where) == '') return '';
}
return $where;
} }
function getGroupByString(){ function getGroupByString(){

View file

@ -1,15 +1,15 @@
<?php <?php
class Subquery extends Query { class Subquery extends Query {
var $alias; var $alias;
var $join_type; var $join_type;
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;
@ -18,20 +18,24 @@
$this->limit = $limit; $this->limit = $limit;
$this->join_type = $join_type; $this->join_type = $join_type;
} }
function getAlias(){ function getAlias(){
return $this->alias; return $this->alias;
} }
function isJoinTable(){ function isJoinTable(){
if($this->join_type) return true; if($this->join_type) return true;
return false; return false;
} }
function toString($with_values = true){ function toString($with_values = true){
$oDB = &DB::getInstance(); $oDB = &DB::getInstance();
return '(' .$oDB->getSelectSql($this, $with_values) . ')'; return '(' .$oDB->getSelectSql($this, $with_values) . ')';
}
function isSubquery(){
return true;
} }
} }

View file

@ -4,20 +4,32 @@
var $conditions; var $conditions;
var $pipe; var $pipe;
var $_group;
var $_show;
function ConditionGroup($conditions, $pipe = "") { function ConditionGroup($conditions, $pipe = "") {
$this->conditions = array(); $this->conditions = array();
foreach($conditions as $condition){ foreach($conditions as $condition){
if($condition->show()) if($condition->show())
$this->conditions[] = $condition; $this->conditions[] = $condition;
} }
if(count($this->conditions) === 0) $this->_show = false;
else $this->_show = true;
$this->pipe = $pipe; $this->pipe = $pipe;
} }
function show(){
return $this->_show;
}
function setPipe($pipe){ function setPipe($pipe){
if($this->pipe !== $pipe) $this->_group = null;
$this->pipe = $pipe; $this->pipe = $pipe;
} }
function toString($with_value = true){ function toString($with_value = true){
if(!isset($this->_group)){
$cond_indx = 0; $cond_indx = 0;
$group = ''; $group = '';
@ -26,23 +38,23 @@
$group .= $condition->toString($with_value) . ' '; $group .= $condition->toString($with_value) . ' ';
$cond_indx++; $cond_indx++;
} }
// If the group has no conditions in it, return ''
if($cond_indx === 0) return '';
if($this->pipe !== ""){ if($this->pipe !== "" && trim($group) !== ''){
$group = $this->pipe . ' (' . $group . ')'; $group = $this->pipe . ' (' . $group . ')';
} }
return $group; $this->_group = $group;
}
return $this->_group;
} }
function getArguments(){ function getArguments(){
$args = array(); $args = array();
foreach($this->conditions as $condition){ foreach($this->conditions as $condition){
$arg = $condition->getArgument(); $arg = $condition->getArgument();
if($arg) $args[] = $arg; if($arg) $args[] = $arg;
} }
return $args; return $args;
} }
} }
?> ?>

View file

@ -1,11 +1,11 @@
<?php <?php
/** /**
* @class SelectExpression * @class SelectExpression
* @author Arnia Software * @author Arnia Software
* @brief Represents an expresion that appears in the select clause * @brief Represents an expresion that appears in the select clause
* *
* @remarks * @remarks
* $column_name can be: * $column_name can be:
* - a table column name * - a table column name
* - an sql function - like count(*) * - an sql function - like count(*)
@ -15,22 +15,26 @@
class SelectExpression extends Expression { class SelectExpression extends Expression {
var $column_alias; var $column_alias;
function SelectExpression($column_name, $alias = NULL){ function SelectExpression($column_name, $alias = NULL){
parent::Expression($column_name); parent::Expression($column_name);
$this->column_alias = $alias; $this->column_alias = $alias;
} }
function getExpression() { function getExpression() {
return sprintf("%s%s", $this->column_name, $this->column_alias ? " as ".$this->column_alias : ""); return sprintf("%s%s", $this->column_name, $this->column_alias ? " as ".$this->column_alias : "");
} }
function show() { function show() {
return true; return true;
} }
function getArgument(){ function getArgument(){
return null; return null;
} }
function isSubquery(){
return false;
}
} }
?> ?>

View file

@ -12,11 +12,6 @@
} }
parent::Argument($name, $value); parent::Argument($name, $value);
$this->operation = $operation; $this->operation = $operation;
if($this->type !== 'date'){
$dbParser = XmlQueryParser::getDBParser();
$this->value = $dbParser->escapeStringValue($this->value);
}
} }
function createConditionValue(){ function createConditionValue(){