From bbe95e94ff0290e847e4e9852dd1a0b0e620ac88 Mon Sep 17 00:00:00 2001 From: ucorina Date: Mon, 1 Aug 2011 14:36:13 +0000 Subject: [PATCH] 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 --- classes/db/DB.class.php | 4 +- classes/db/queryparts/Query.class.php | 147 +++++++++--------- .../cubrid/CubridSelectOnlineTest.php | 14 ++ 3 files changed, 90 insertions(+), 75 deletions(-) diff --git a/classes/db/DB.class.php b/classes/db/DB.class.php index 0c75b09ad..c3300fa7c 100644 --- a/classes/db/DB.class.php +++ b/classes/db/DB.class.php @@ -395,8 +395,8 @@ $output = $this->_executeDeleteAct($output); break; case 'select' : - // TODO Add property for Query object for Arg_columns - $output->arg_columns = is_array($arg_columns)?$arg_columns:array(); + $arg_columns = is_array($arg_columns)?$arg_columns:array(); + $output->setColumnList($arg_columns); $output = $this->_executeSelectAct($output); break; } diff --git a/classes/db/queryparts/Query.class.php b/classes/db/queryparts/Query.class.php index 4943a39a0..c507637d0 100644 --- a/classes/db/queryparts/Query.class.php +++ b/classes/db/queryparts/Query.class.php @@ -1,20 +1,20 @@ -queryID = $queryID; $this->action = $action; - + if(!isset($tables)) return; $this->columns = $this->setColumns($columns); $this->tables = $this->setTables($tables); @@ -34,114 +34,115 @@ $this->orderby = $this->setOrder($orderby); $this->limit = $this->setLimit($limit); } - + function show(){ return true; } - + function setQueryId($queryID){ $this->queryID = $queryID; } - + function setAction($action){ $this->action = $action; } - + function setColumnList($columnList){ - $this->columnList = $columnList; + if(count($columnList) > 0) + $this->columnList = $columnList; } - + function setColumns($columns){ if(!isset($columns) || count($columns) === 0){ $this->columns = array(new StarExpression()); return; } - + if(!is_array($columns)) $columns = array($columns); - - $this->columns = $columns; + + $this->columns = $columns; } - + function setTables($tables){ if(!isset($tables) || count($tables) === 0){ $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; } 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; + + $this->conditions = $conditions; } - + function setGroups($groups){ if(!isset($groups) || count($groups) === 0) return; if(!is_array($groups)) $groups = array($groups); - - $this->groups = $groups; + + $this->groups = $groups; } - + function setOrder($order){ if(!isset($order) || count($order) === 0) return; if(!is_array($order)) $order = array($order); - - $this->orderby = $order; + + $this->orderby = $order; } - + function setLimit($limit = NULL){ if(!isset($limit)) return; $this->limit = $limit; } - + // START Fluent interface function select($columns= null){ $this->action = 'select'; - $this->setColumns($columns); + $this->setColumns($columns); return $this; } - + function from($tables){ $this->setTables($tables); return $this; } - + function where($conditions){ $this->setConditions($conditions); - return $this; + return $this; } - + function groupBy($groups){ $this->setGroups($groups); - return $this; + return $this; } - + function orderBy($order){ $this->setOrder($order); - return $this; + return $this; } - + function limit($limit){ $this->setLimit($limit); - return $this; + return $this; } // END Fluent interface - + function getAction(){ return $this->action; } - - function getSelectString($with_values = true){ + + function getSelectString($with_values = true){ if(isset($this->columnList)){ $selectColumns = array(); $dbParser = XmlQueryParser::getDBParser(); - + foreach($this->columnList as $columnName){ $columnName = $dbParser->escapeColumn($columnName); $selectColumns[] = new SelectExpression($columnName); @@ -149,7 +150,7 @@ } else $selectColumns = $this->columns; - + $select = ''; foreach($selectColumns as $column){ if($column->show()) @@ -163,12 +164,12 @@ $select = substr($select, 0, -2); return $select; } - - function getUpdateString($with_values = true){ + + function getUpdateString($with_values = true){ return $this->getSelectString($with_values); - } - - function getInsertString($with_values = true){ + } + + function getInsertString($with_values = true){ $columnsList = ''; $valuesList = ''; foreach($this->columns as $column){ @@ -179,14 +180,14 @@ } $columnsList = substr($columnsList, 0, -2); $valuesList = substr($valuesList, 0, -2); - + return "($columnsList) \n VALUES ($valuesList)"; - } - + } + function getTables(){ return $this->tables; } - + // from table_a // from table_a inner join table_b on x=y // from (select * from table a) as x @@ -197,15 +198,15 @@ foreach($this->tables as $table){ if($table->isJoinTable() || !$simple_table_count) $from .= $table->toString($with_values) . ' '; else $from .= ', '.$table->toString($with_values) . ' '; - + if(is_a($table, 'Subquery')) $from .= $table->getAlias() ? ' as ' . $table->getAlias() . ' ' : ' '; - + $simple_table_count++; } if(trim($from) == '') return ''; return $from; } - + function getWhereString($with_values = true){ $where = ''; if(count($this->conditions) > 0){ @@ -213,25 +214,25 @@ foreach($this->conditions as $conditionGroup){ $condition_string = $conditionGroup->toString($with_values); if($condition_string !== '') $condition_count++; - if($condition_count === 1){ + if($condition_count === 1){ $conditionGroup->setPipe(""); $condition_string = $conditionGroup->toString($with_values); } $where .= $condition_string; } if(trim($where) == '') return ''; - + } return $where; } - + function getGroupByString(){ $groupBy = ''; if($this->groups) if($this->groups[0] !== "") $groupBy = implode(', ', $this->groups); - return $groupBy; + return $groupBy; } - + function getOrderByString(){ if(count($this->orderby) === 0) return ''; $orderBy = ''; @@ -241,28 +242,28 @@ $orderBy = substr($orderBy, 0, -2); return $orderBy; } - + function getLimit(){ return $this->limit; } - + function getLimitString(){ $limit = ''; if(count($this->limit) > 0){ $limit = ''; $limit .= $this->limit->toString(); - } - return $limit; + } + return $limit; } - + function getFirstTableName(){ - return $this->tables[0]->getName(); + return $this->tables[0]->getName(); } - + function getArguments(){ if(!isset($this->arguments)){ $this->arguments = array(); - + // Column arguments if(count($this->columns) > 0){ // The if is for delete statements, all others must have columns foreach($this->columns as $column){ @@ -270,16 +271,16 @@ $arg = $column->getArgument(); if($arg) $this->arguments[] = $arg; } - } + } } - + // Condition arguments if(count($this->conditions) > 0) 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){ diff --git a/test-phpUnit/db/xml_query/cubrid/CubridSelectOnlineTest.php b/test-phpUnit/db/xml_query/cubrid/CubridSelectOnlineTest.php index 1cc4b5e09..601d0f9b1 100644 --- a/test-phpUnit/db/xml_query/cubrid/CubridSelectOnlineTest.php +++ b/test-phpUnit/db/xml_query/cubrid/CubridSelectOnlineTest.php @@ -11,6 +11,20 @@ $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(){ $args->site_srl = 0; $output = executeQuery('module.getSiteInfo', $args);