Update for query column list argument (selecting just certain columns from a table instead of all).

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@8709 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-08-01 14:36:13 +00:00
parent c2f8aeb167
commit bbe95e94ff
3 changed files with 90 additions and 75 deletions

View file

@ -395,8 +395,8 @@
$output = $this->_executeDeleteAct($output); $output = $this->_executeDeleteAct($output);
break; break;
case 'select' : case 'select' :
// TODO Add property for Query object for Arg_columns $arg_columns = is_array($arg_columns)?$arg_columns:array();
$output->arg_columns = is_array($arg_columns)?$arg_columns:array(); $output->setColumnList($arg_columns);
$output = $this->_executeSelectAct($output); $output = $this->_executeSelectAct($output);
break; break;
} }

View file

@ -1,20 +1,20 @@
<?php <?php
class Query extends Object { class Query extends Object {
var $queryID; var $queryID;
var $action; var $action;
var $columns; var $columns;
var $tables; var $tables;
var $conditions; var $conditions;
var $groups; var $groups;
var $orderby; var $orderby;
var $limit; var $limit;
var $arguments = null; var $arguments = null;
var $columnList = null; var $columnList = null;
function Query($queryID = null function Query($queryID = null
, $action = null , $action = null
, $columns = null , $columns = null
@ -25,7 +25,7 @@
, $limit = null){ , $limit = null){
$this->queryID = $queryID; $this->queryID = $queryID;
$this->action = $action; $this->action = $action;
if(!isset($tables)) return; if(!isset($tables)) return;
$this->columns = $this->setColumns($columns); $this->columns = $this->setColumns($columns);
$this->tables = $this->setTables($tables); $this->tables = $this->setTables($tables);
@ -34,114 +34,115 @@
$this->orderby = $this->setOrder($orderby); $this->orderby = $this->setOrder($orderby);
$this->limit = $this->setLimit($limit); $this->limit = $this->setLimit($limit);
} }
function show(){ function show(){
return true; return true;
} }
function setQueryId($queryID){ function setQueryId($queryID){
$this->queryID = $queryID; $this->queryID = $queryID;
} }
function setAction($action){ function setAction($action){
$this->action = $action; $this->action = $action;
} }
function setColumnList($columnList){ function setColumnList($columnList){
$this->columnList = $columnList; if(count($columnList) > 0)
$this->columnList = $columnList;
} }
function setColumns($columns){ function setColumns($columns){
if(!isset($columns) || count($columns) === 0){ if(!isset($columns) || count($columns) === 0){
$this->columns = array(new StarExpression()); $this->columns = array(new StarExpression());
return; return;
} }
if(!is_array($columns)) $columns = array($columns); if(!is_array($columns)) $columns = array($columns);
$this->columns = $columns; $this->columns = $columns;
} }
function setTables($tables){ function setTables($tables){
if(!isset($tables) || count($tables) === 0){ if(!isset($tables) || count($tables) === 0){
$this->setError(true); $this->setError(true);
$this->setMessage("You must provide at least one table for the query."); $this->setMessage("You must provide at least one table for the query.");
return; return;
} }
if(!is_array($tables)) $tables = array($tables); if(!is_array($tables)) $tables = array($tables);
$this->tables = $tables; $this->tables = $tables;
} }
function setConditions($conditions){ function setConditions($conditions){
if(!isset($conditions) || count($conditions) === 0) return; if(!isset($conditions) || count($conditions) === 0) return;
if(!is_array($conditions)) $conditions = array($conditions); if(!is_array($conditions)) $conditions = array($conditions);
$this->conditions = $conditions; $this->conditions = $conditions;
} }
function setGroups($groups){ function setGroups($groups){
if(!isset($groups) || count($groups) === 0) return; if(!isset($groups) || count($groups) === 0) return;
if(!is_array($groups)) $groups = array($groups); if(!is_array($groups)) $groups = array($groups);
$this->groups = $groups; $this->groups = $groups;
} }
function setOrder($order){ function setOrder($order){
if(!isset($order) || count($order) === 0) return; if(!isset($order) || count($order) === 0) return;
if(!is_array($order)) $order = array($order); if(!is_array($order)) $order = array($order);
$this->orderby = $order; $this->orderby = $order;
} }
function setLimit($limit = NULL){ function setLimit($limit = NULL){
if(!isset($limit)) return; if(!isset($limit)) return;
$this->limit = $limit; $this->limit = $limit;
} }
// START Fluent interface // START Fluent interface
function select($columns= null){ function select($columns= null){
$this->action = 'select'; $this->action = 'select';
$this->setColumns($columns); $this->setColumns($columns);
return $this; return $this;
} }
function from($tables){ function from($tables){
$this->setTables($tables); $this->setTables($tables);
return $this; return $this;
} }
function where($conditions){ function where($conditions){
$this->setConditions($conditions); $this->setConditions($conditions);
return $this; return $this;
} }
function groupBy($groups){ function groupBy($groups){
$this->setGroups($groups); $this->setGroups($groups);
return $this; return $this;
} }
function orderBy($order){ function orderBy($order){
$this->setOrder($order); $this->setOrder($order);
return $this; return $this;
} }
function limit($limit){ function limit($limit){
$this->setLimit($limit); $this->setLimit($limit);
return $this; return $this;
} }
// END Fluent interface // END Fluent interface
function getAction(){ function getAction(){
return $this->action; return $this->action;
} }
function getSelectString($with_values = true){ function getSelectString($with_values = true){
if(isset($this->columnList)){ if(isset($this->columnList)){
$selectColumns = array(); $selectColumns = array();
$dbParser = XmlQueryParser::getDBParser(); $dbParser = XmlQueryParser::getDBParser();
foreach($this->columnList as $columnName){ foreach($this->columnList as $columnName){
$columnName = $dbParser->escapeColumn($columnName); $columnName = $dbParser->escapeColumn($columnName);
$selectColumns[] = new SelectExpression($columnName); $selectColumns[] = new SelectExpression($columnName);
@ -149,7 +150,7 @@
} }
else else
$selectColumns = $this->columns; $selectColumns = $this->columns;
$select = ''; $select = '';
foreach($selectColumns as $column){ foreach($selectColumns as $column){
if($column->show()) if($column->show())
@ -163,12 +164,12 @@
$select = substr($select, 0, -2); $select = substr($select, 0, -2);
return $select; return $select;
} }
function getUpdateString($with_values = true){ function getUpdateString($with_values = true){
return $this->getSelectString($with_values); return $this->getSelectString($with_values);
} }
function getInsertString($with_values = true){ function getInsertString($with_values = true){
$columnsList = ''; $columnsList = '';
$valuesList = ''; $valuesList = '';
foreach($this->columns as $column){ foreach($this->columns as $column){
@ -179,14 +180,14 @@
} }
$columnsList = substr($columnsList, 0, -2); $columnsList = substr($columnsList, 0, -2);
$valuesList = substr($valuesList, 0, -2); $valuesList = substr($valuesList, 0, -2);
return "($columnsList) \n VALUES ($valuesList)"; return "($columnsList) \n VALUES ($valuesList)";
} }
function getTables(){ function getTables(){
return $this->tables; return $this->tables;
} }
// from table_a // from table_a
// from table_a inner join table_b on x=y // from table_a inner join table_b on x=y
// from (select * from table a) as x // from (select * from table a) as x
@ -197,15 +198,15 @@
foreach($this->tables as $table){ foreach($this->tables as $table){
if($table->isJoinTable() || !$simple_table_count) $from .= $table->toString($with_values) . ' '; if($table->isJoinTable() || !$simple_table_count) $from .= $table->toString($with_values) . ' ';
else $from .= ', '.$table->toString($with_values) . ' '; else $from .= ', '.$table->toString($with_values) . ' ';
if(is_a($table, 'Subquery')) $from .= $table->getAlias() ? ' as ' . $table->getAlias() . ' ' : ' '; if(is_a($table, 'Subquery')) $from .= $table->getAlias() ? ' as ' . $table->getAlias() . ' ' : ' ';
$simple_table_count++; $simple_table_count++;
} }
if(trim($from) == '') return ''; if(trim($from) == '') return '';
return $from; return $from;
} }
function getWhereString($with_values = true){ function getWhereString($with_values = true){
$where = ''; $where = '';
if(count($this->conditions) > 0){ if(count($this->conditions) > 0){
@ -213,25 +214,25 @@
foreach($this->conditions as $conditionGroup){ foreach($this->conditions as $conditionGroup){
$condition_string = $conditionGroup->toString($with_values); $condition_string = $conditionGroup->toString($with_values);
if($condition_string !== '') $condition_count++; if($condition_string !== '') $condition_count++;
if($condition_count === 1){ if($condition_count === 1){
$conditionGroup->setPipe(""); $conditionGroup->setPipe("");
$condition_string = $conditionGroup->toString($with_values); $condition_string = $conditionGroup->toString($with_values);
} }
$where .= $condition_string; $where .= $condition_string;
} }
if(trim($where) == '') return ''; if(trim($where) == '') return '';
} }
return $where; return $where;
} }
function getGroupByString(){ function getGroupByString(){
$groupBy = ''; $groupBy = '';
if($this->groups) if($this->groups[0] !== "") if($this->groups) if($this->groups[0] !== "")
$groupBy = implode(', ', $this->groups); $groupBy = implode(', ', $this->groups);
return $groupBy; return $groupBy;
} }
function getOrderByString(){ function getOrderByString(){
if(count($this->orderby) === 0) return ''; if(count($this->orderby) === 0) return '';
$orderBy = ''; $orderBy = '';
@ -241,28 +242,28 @@
$orderBy = substr($orderBy, 0, -2); $orderBy = substr($orderBy, 0, -2);
return $orderBy; return $orderBy;
} }
function getLimit(){ function getLimit(){
return $this->limit; return $this->limit;
} }
function getLimitString(){ function getLimitString(){
$limit = ''; $limit = '';
if(count($this->limit) > 0){ if(count($this->limit) > 0){
$limit = ''; $limit = '';
$limit .= $this->limit->toString(); $limit .= $this->limit->toString();
} }
return $limit; return $limit;
} }
function getFirstTableName(){ function getFirstTableName(){
return $this->tables[0]->getName(); return $this->tables[0]->getName();
} }
function getArguments(){ function getArguments(){
if(!isset($this->arguments)){ if(!isset($this->arguments)){
$this->arguments = array(); $this->arguments = array();
// Column arguments // Column arguments
if(count($this->columns) > 0){ // The if is for delete statements, all others must have columns if(count($this->columns) > 0){ // The if is for delete statements, all others must have columns
foreach($this->columns as $column){ foreach($this->columns as $column){
@ -270,16 +271,16 @@
$arg = $column->getArgument(); $arg = $column->getArgument();
if($arg) $this->arguments[] = $arg; if($arg) $this->arguments[] = $arg;
} }
} }
} }
// Condition arguments // Condition arguments
if(count($this->conditions) > 0) if(count($this->conditions) > 0)
foreach($this->conditions as $conditionGroup){ foreach($this->conditions as $conditionGroup){
$args = $conditionGroup->getArguments(); $args = $conditionGroup->getArguments();
if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args); if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args);
} }
// Navigation arguments // Navigation arguments
if(count($this->orderby) > 0) if(count($this->orderby) > 0)
foreach($this->orderby as $order){ foreach($this->orderby as $order){

View file

@ -11,6 +11,20 @@
$this->assertEquals($output->data->module_srl, 111); $this->assertEquals($output->data->module_srl, 111);
} }
/**
* Tests that when a column list is given, the query only selects those columns from the database
* insetad of retrieving all table columns (as specified in the xml query file)
*/
function test_get_module_by_mid_columnList(){
$args->mid = 'test_4l8ci4vv0n';
$args->site_srl = 0;
$output = executeQuery('module.getMidInfo', $args, array('module_srl'));
$this->assertNotNull($output);
$this->assertNotNull($output->data, $output->message . PHP_EOL . $output->variables["_query"]);
$this->assertEquals($output->data->module_srl, 111);
$this->assertEquals($output->data->module, null);
}
function test_module_getInfo(){ function test_module_getInfo(){
$args->site_srl = 0; $args->site_srl = 0;
$output = executeQuery('module.getSiteInfo', $args); $output = executeQuery('module.getSiteInfo', $args);