Removed legacy methods from DB and DBCubrid classes.

Added Query object to hold together query parts. 
Updated structure of query cache file to return Query object. 
Added support for array query arguments - used in the IN clause.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8438 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-06-02 13:06:08 +00:00
parent c384f6c753
commit 0cd318fcf5
9 changed files with 210 additions and 891 deletions

View file

@ -1,72 +1,172 @@
<?php
class Query extends Object {
var $queryID;
var $action;
var $columns;
var $tables;
var $conditions;
var $groups;
var $orderby;
function select($columns= null){
$this->action = 'select';
function setQueryId($queryID){
$this->queryID = $queryID;
}
function setAction($action){
$this->action = $action;
}
function setColumns($columns){
if(!isset($columns) || count($columns) === 0){
$this->columns = array(new StarExpression());
return $this;
return;
}
if(!is_array($columns)) $columns = array($columns);
$this->columns = $columns;
return $this;
$this->columns = $columns;
}
function from($tables){
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 $this;
return;
}
if(!is_array($tables)) $tables = array($tables);
$this->tables = $tables;
$this->tables = $tables;
}
function setConditions($conditions){
if(!isset($conditions) || count($conditions) === 0) return;
if(!is_array($conditions)) $conditions = array($conditions);
$this->conditions = $conditions;
}
function setGroups($groups){
if(!isset($groups) || count($groups) === 0) return;
if(!is_array($groups)) $groups = array($groups);
$this->groups = $groups;
}
function setOrder($order){
if(!isset($order) || count($order) === 0) return;
if(!is_array($order)) $order = array($order);
$this->orderby = $order;
}
function setLimit($limit){
if(!isset($limit)) return;
$this->limit = $limit;
}
// START Fluent interface
function select($columns= null){
$this->action = 'select';
$this->setColumns($columns);
return $this;
}
function from($tables){
$this->setTables($tables);
return $this;
}
function where($conditions){
if(!isset($conditions) || count($conditions) === 0) return $this;
if(!is_array($conditions)) $conditions = array($conditions);
$this->conditions = $conditions;
return $this;
$this->setConditions($conditions);
return $this;
}
function groupBy($groups){
if(!isset($groups) || count($groups) === 0) return $this;
if(!is_array($groups)) $groups = array($groups);
$this->groups = $groups;
$this->setGroups($groups);
return $this;
}
function orderBy($order){
if(!isset($order) || count($order) === 0) return $this;
if(!is_array($order)) $order = array($order);
$this->orderby = $order;
return $this;
$this->setOrder($order);
return $this;
}
function limit($limit){
if(!isset($limit)) return $this;
$this->limit = $limit;
return $this;
$this->setLimit($limit);
return $this;
}
function getSql(){
if($this->action == 'select') return $this->getSelectSql();
// END Fluent interface
function getAction(){
return $this->action;
}
function getSelect(){
$select = '';
foreach($this->columns as $column){
if($column->show())
$select .= $column->getExpression() . ', ';
}
if(trim($select) == '') return '';
$select = substr($select, 0, -2);
return $select;
}
function getFrom(){
$from = '';
$simple_table_count = 0;
foreach($this->tables as $table){
if($table->isJoinTable() || !$simple_table_count) $from .= $table->toString() . ' ';
else $from .= ', '.$table->toString() . ' ';
$simple_table_count++;
}
if(trim($from) == '') return '';
return $from;
}
function getWhere(){
$where = '';
if(count($this->conditions) > 0){
foreach($this->conditions as $conditionGroup){
$where .= $conditionGroup->toString();
}
if(trim($where) == '') return '';
}
return $where;
}
function getGroupBy(){
$groupBy = '';
if($this->groups) if($this->groups[0] !== "")
$groupBy = implode(', ', $this->groups);
return $groupBy;
}
function getOrderBy(){
if(count($this->orderby) === 0) return '';
$orderBy = '';
foreach($this->orderby as $order){
$orderBy .= $order->toString() .', ';
}
$orderBy = substr($orderBy, 0, -2);
return $orderBy;
}
function getLimit(){
$limit = '';
if(count($this->limit) > 0){
$limit = '';
$limit .= $this->limit->toString();
}
return $limit;
}
}