Issue 1431: xml query click_count error - fixed missing class error, added unit tests

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.3.2@12017 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2012-10-31 09:54:57 +00:00
parent bbcef7ad51
commit f93d0d289d
10 changed files with 112 additions and 45 deletions

View file

@ -13,6 +13,7 @@
require(_XE_PATH_.'classes/db/queryparts/expression/InsertExpression.class.php');
require(_XE_PATH_.'classes/db/queryparts/expression/UpdateExpression.class.php');
require(_XE_PATH_.'classes/db/queryparts/expression/UpdateExpressionWithoutArgument.class.php');
require(_XE_PATH_.'classes/db/queryparts/expression/ClickCountExpression.class.php');
require(_XE_PATH_.'classes/db/queryparts/table/Table.class.php');
require(_XE_PATH_.'classes/db/queryparts/table/JoinTable.class.php');
require(_XE_PATH_.'classes/db/queryparts/table/CubridTableWithHint.class.php');

View file

@ -56,13 +56,13 @@
* argument list
* @var array
*/
var $arguments = null;
var $arguments = NULL;
/**
* column list
* @var array
*/
var $columnList = null;
var $columnList = NULL;
/**
* order by text
@ -83,15 +83,15 @@
* @param string $priority
* @return void
*/
function Query($queryID = null
, $action = null
, $columns = null
, $tables = null
, $conditions = null
, $groups = null
, $orderby = null
, $limit = null
, $priority = null){
function Query($queryID = NULL
, $action = NULL
, $columns = NULL
, $tables = NULL
, $conditions = NULL
, $groups = NULL
, $orderby = NULL
, $limit = NULL
, $priority = NULL){
$this->queryID = $queryID;
$this->action = $action;
$this->priority = $priority;
@ -106,7 +106,7 @@
}
function show(){
return true;
return TRUE;
}
function setQueryId($queryID){
@ -149,7 +149,7 @@
function setTables($tables){
if(!isset($tables) || count($tables) === 0){
$this->setError(true);
$this->setError(TRUE);
$this->setMessage("You must provide at least one table for the query.");
return;
}
@ -158,10 +158,10 @@
$this->tables = $tables;
}
function setSubquery($subquery){
$this->subquery = $subquery;
}
function setSubquery($subquery){
$this->subquery = $subquery;
}
function setConditions($conditions){
$this->conditions = array();
@ -198,7 +198,7 @@
* @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;
@ -263,12 +263,28 @@
return $this->priority?'LOW_PRIORITY':'';
}
/**
* Check if current query uses the click count attribute
* For CUBRID, this statement uses the click count feature.
* For the other databases, using this attribute causes a query
* to produce both a select and an update
*/
function usesClickCount()
{
foreach($this->columns as $column){
if($column->show() && $column instanceof ClickCountExpression)
return true;
}
return false;
}
/**
* Return select sql
* @param boolean $with_values
* @return string
*/
function getSelectString($with_values = true){
function getSelectString($with_values = TRUE){
$select = array();
foreach($this->columns as $column){
if($column->show())
if($column->isSubquery()){
@ -285,7 +301,7 @@
* @param boolean $with_values
* @return string
*/
function getUpdateString($with_values = true){
function getUpdateString($with_values = TRUE){
foreach($this->columns as $column){
if($column->show())
$update[] = $column->getExpression($with_values);
@ -298,22 +314,22 @@
* @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){
$columnsList .= $column->getColumnName() . ', ';
}
$columnsList = substr($columnsList, 0, -2);
$selectStatement = $this->subquery->toString($with_values);
$selectStatement = substr($selectStatement, 1, -1);
return "($columnsList) \n $selectStatement";
}
if($this->subquery){ // means we have insert-select
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()){
@ -339,7 +355,7 @@
* @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){
@ -360,7 +376,7 @@
* @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;

View file

@ -23,9 +23,8 @@
parent::SelectExpression($column_name, $alias);
if(!is_bool($click_count)){
error_log("Click_count value for $column_name was not boolean", 0);
// error_log("Click_count value for $column_name was not boolean", 0);
$this->click_count = false;
return;
}
$this->click_count = $click_count;
}
@ -39,7 +38,15 @@
* @return string
*/
function getExpression(){
return "$this->column_name = $this->column_name + 1";
$db_type = Context::getDBType();
if($db_type == 'cubrid')
{
return "INCR($this->column_name)";
}
else
{
return "$this->column_name";
}
}
}

View file

@ -44,9 +44,9 @@
function getExpressionString(){
if($this->name == '*') return "new StarExpression()";
if($this->click_count)
return sprintf('new ClickCountExpression(%s, %s, $args->%s)', $this->name, $this->alias,$this->click_count);
return sprintf('new ClickCountExpression(\'%s\', %s, $args->%s)', $this->name, $this->alias ? '\'' . $this->alias . '\'' : "''",$this->click_count);
if(strpos($this->name, '$') === 0)
return sprintf('new SelectExpression($args->%s)', substr($this->name, 1));
return sprintf('new SelectExpression($args->%s)', substr($this->name, 1));
$dbParser = DB::getParser();
return sprintf('new SelectExpression(\'%s\'%s)', $this->name, $this->alias ? ', \''.$dbParser->escape($this->alias) .'\'': '');
}

View file

@ -36,6 +36,7 @@
require_once(_XE_PATH_.'classes/db/queryparts/expression/InsertExpression.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/expression/UpdateExpression.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/expression/UpdateExpressionWithoutArgument.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/expression/ClickCountExpression.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/table/Table.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/table/JoinTable.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/table/CubridTableWithHint.class.php');

View file

@ -20,8 +20,12 @@ class DBTest extends PHPUnit_Framework_TestCase {
$querySql = $db->{$methodName}($output);
// Remove whitespaces, tabs and all
$querySql = Helper::cleanString($querySql);
$expected = Helper::cleanString($expected);
if(is_a($querySql, 'Object'))
{
$querySql = $querySql->getMessage();
}
$querySql = Helper::cleanString($querySql);
$expected = Helper::cleanString($expected);
}
$this->assertEquals($expected, $querySql);
}

View file

@ -439,6 +439,17 @@
$argsString = '$args->package_srl = 18325662;';
$expectedArgs = array(18325662);
$this->_testPreparedQuery($xml_file, $argsString, $expected, 'getSelectSql', $expectedArgs);
}
}
/**
* Issue 1431 - xml click count error
*/
function testClickCount()
{
$xml_file = _TEST_PATH_ . "db/xml_query/mysql/data/click_count.xml";
$argsString = '$args->incr_expose_count = true;';
$expected = 'select INCR("expose_count") from "xe_modules" as "modules"';
$this->_test($xml_file, $argsString, $expected);
}
}

View file

@ -0,0 +1,8 @@
<query id="getNewestDocuments" action="select">
<tables>
<table name="modules" />
</tables>
<columns>
<column name="expose_count" click_count="incr_expose_count"/>
</columns>
</query>

View file

@ -380,4 +380,15 @@ class MysqlSelectTest extends MysqlTest {
limit 5";
$this->_test($xml_file, $argsString, $expected);
}
/**
* Issue 1431 - xml click count error
*/
function testClickCount()
{
$xml_file = _TEST_PATH_ . "db/xml_query/mysql/data/click_count.xml";
$argsString = '$args->incr_expose_count = true;';
$expected = "select `expose_count` from `xe_modules` as `modules`";
$this->_test($xml_file, $argsString, $expected);
}
}

View file

@ -0,0 +1,8 @@
<query id="getNewestDocuments" action="select">
<tables>
<table name="modules" />
</tables>
<columns>
<column name="expose_count" click_count="incr_expose_count"/>
</columns>
</query>