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/InsertExpression.class.php');
require(_XE_PATH_.'classes/db/queryparts/expression/UpdateExpression.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/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/Table.class.php');
require(_XE_PATH_.'classes/db/queryparts/table/JoinTable.class.php'); require(_XE_PATH_.'classes/db/queryparts/table/JoinTable.class.php');
require(_XE_PATH_.'classes/db/queryparts/table/CubridTableWithHint.class.php'); require(_XE_PATH_.'classes/db/queryparts/table/CubridTableWithHint.class.php');

View file

@ -56,13 +56,13 @@
* argument list * argument list
* @var array * @var array
*/ */
var $arguments = null; var $arguments = NULL;
/** /**
* column list * column list
* @var array * @var array
*/ */
var $columnList = null; var $columnList = NULL;
/** /**
* order by text * order by text
@ -83,15 +83,15 @@
* @param string $priority * @param string $priority
* @return void * @return void
*/ */
function Query($queryID = null function Query($queryID = NULL
, $action = null , $action = NULL
, $columns = null , $columns = NULL
, $tables = null , $tables = NULL
, $conditions = null , $conditions = NULL
, $groups = null , $groups = NULL
, $orderby = null , $orderby = NULL
, $limit = null , $limit = NULL
, $priority = null){ , $priority = NULL){
$this->queryID = $queryID; $this->queryID = $queryID;
$this->action = $action; $this->action = $action;
$this->priority = $priority; $this->priority = $priority;
@ -106,7 +106,7 @@
} }
function show(){ function show(){
return true; return TRUE;
} }
function setQueryId($queryID){ function setQueryId($queryID){
@ -149,7 +149,7 @@
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;
} }
@ -158,10 +158,10 @@
$this->tables = $tables; $this->tables = $tables;
} }
function setSubquery($subquery){ function setSubquery($subquery){
$this->subquery = $subquery; $this->subquery = $subquery;
} }
function setConditions($conditions){ function setConditions($conditions){
$this->conditions = array(); $this->conditions = array();
@ -198,7 +198,7 @@
* @param string|array $columns * @param string|array $columns
* @return Query return Query instance * @return Query return Query instance
*/ */
function select($columns= null){ function select($columns= NULL){
$this->action = 'select'; $this->action = 'select';
$this->setColumns($columns); $this->setColumns($columns);
return $this; return $this;
@ -263,12 +263,28 @@
return $this->priority?'LOW_PRIORITY':''; 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 * Return select sql
* @param boolean $with_values * @param boolean $with_values
* @return string * @return string
*/ */
function getSelectString($with_values = true){ function getSelectString($with_values = TRUE){
$select = array();
foreach($this->columns as $column){ foreach($this->columns as $column){
if($column->show()) if($column->show())
if($column->isSubquery()){ if($column->isSubquery()){
@ -285,7 +301,7 @@
* @param boolean $with_values * @param boolean $with_values
* @return string * @return string
*/ */
function getUpdateString($with_values = true){ function getUpdateString($with_values = TRUE){
foreach($this->columns as $column){ foreach($this->columns as $column){
if($column->show()) if($column->show())
$update[] = $column->getExpression($with_values); $update[] = $column->getExpression($with_values);
@ -298,22 +314,22 @@
* @param boolean $with_values * @param boolean $with_values
* @return string * @return string
*/ */
function getInsertString($with_values = true){ function getInsertString($with_values = TRUE){
$columnsList = ''; $columnsList = '';
if($this->subquery){ // means we have insert-select if($this->subquery){ // means we have insert-select
foreach($this->columns as $column){ foreach($this->columns as $column){
$columnsList .= $column->getColumnName() . ', '; $columnsList .= $column->getColumnName() . ', ';
} }
$columnsList = substr($columnsList, 0, -2); $columnsList = substr($columnsList, 0, -2);
$selectStatement = $this->subquery->toString($with_values); $selectStatement = $this->subquery->toString($with_values);
$selectStatement = substr($selectStatement, 1, -1); $selectStatement = substr($selectStatement, 1, -1);
return "($columnsList) \n $selectStatement"; return "($columnsList) \n $selectStatement";
} }
$valuesList = ''; $valuesList = '';
foreach($this->columns as $column){ foreach($this->columns as $column){
if($column->show()){ if($column->show()){
@ -339,7 +355,7 @@
* @param boolean $with_values * @param boolean $with_values
* @return string * @return string
*/ */
function getFromString($with_values = true){ function getFromString($with_values = TRUE){
$from = ''; $from = '';
$simple_table_count = 0; $simple_table_count = 0;
foreach($this->tables as $table){ foreach($this->tables as $table){
@ -360,7 +376,7 @@
* @param boolean $with_optimization * @param boolean $with_optimization
* @return string * @return string
*/ */
function getWhereString($with_values = true, $with_optimization = true){ function getWhereString($with_values = TRUE, $with_optimization = TRUE){
$where = ''; $where = '';
$condition_count = 0; $condition_count = 0;

View file

@ -23,9 +23,8 @@
parent::SelectExpression($column_name, $alias); parent::SelectExpression($column_name, $alias);
if(!is_bool($click_count)){ 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; $this->click_count = false;
return;
} }
$this->click_count = $click_count; $this->click_count = $click_count;
} }
@ -39,7 +38,15 @@
* @return string * @return string
*/ */
function getExpression(){ 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(){ function getExpressionString(){
if($this->name == '*') return "new StarExpression()"; if($this->name == '*') return "new StarExpression()";
if($this->click_count) 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) 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(); $dbParser = DB::getParser();
return sprintf('new SelectExpression(\'%s\'%s)', $this->name, $this->alias ? ', \''.$dbParser->escape($this->alias) .'\'': ''); 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/InsertExpression.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/expression/UpdateExpression.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/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/Table.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/table/JoinTable.class.php'); require_once(_XE_PATH_.'classes/db/queryparts/table/JoinTable.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/table/CubridTableWithHint.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); $querySql = $db->{$methodName}($output);
// Remove whitespaces, tabs and all // Remove whitespaces, tabs and all
$querySql = Helper::cleanString($querySql); if(is_a($querySql, 'Object'))
$expected = Helper::cleanString($expected); {
$querySql = $querySql->getMessage();
}
$querySql = Helper::cleanString($querySql);
$expected = Helper::cleanString($expected);
} }
$this->assertEquals($expected, $querySql); $this->assertEquals($expected, $querySql);
} }

View file

@ -439,6 +439,17 @@
$argsString = '$args->package_srl = 18325662;'; $argsString = '$args->package_srl = 18325662;';
$expectedArgs = array(18325662); $expectedArgs = array(18325662);
$this->_testPreparedQuery($xml_file, $argsString, $expected, 'getSelectSql', $expectedArgs); $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"; limit 5";
$this->_test($xml_file, $argsString, $expected); $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>