Update to query argument naming - so that queries that have the same variable name specified in the XML will not fail.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8658 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-07-26 14:40:46 +00:00
parent 620b18e532
commit cfc7d32afd
15 changed files with 209 additions and 189 deletions

View file

@ -95,8 +95,6 @@
$this->value = $default_value;
}
function checkFilter($filter_type){
if(isset($this->value) && $this->value != ''){
$val = $this->value;

View file

@ -49,13 +49,8 @@
if($column_type === '') return;
$this->type = $column_type;
//if($column_type === '') $column_type = 'varchar';
}
}
?>

View file

@ -0,0 +1,11 @@
<?php
class SortArgument extends Argument {
function getValue(){
return $this->getUnescapedValue();
}
}
?>

View file

@ -2,15 +2,23 @@
class QueryArgument {
var $argument_name;
var $variable_name;
var $argument_validator;
var $column_name;
var $operation;
static $number_of_arguments = 0;
function QueryArgument($tag){
$this->argument_name = $tag->attrs->var;
if(!$this->argument_name) $this->argument_name = $tag->attrs->name;
if(!$this->argument_name) $this->argument_name = str_replace('.', '_',$tag->attrs->column);
$this->variable_name = $this->argument_name;
self::$number_of_arguments++;
$this->argument_name .= self::$number_of_arguments;
$name = $tag->attrs->name;
if(!$name) $name = $tag->attrs->column;
if(strpos($name, '.') === false) $this->column_name = $name;
@ -48,7 +56,7 @@
$arg = sprintf("\n$%s_argument = new ConditionArgument('%s', %s, '%s');\n"
, $this->argument_name
, $this->argument_name
, '$args->'.$this->argument_name
, '$args->'.$this->variable_name
, $this->operation
);
@ -56,8 +64,7 @@
$arg = sprintf("\n$%s_argument = new Argument('%s', %s);\n"
, $this->argument_name
, $this->argument_name
, '$args->'.$this->argument_name
, $this->ignoreValue ? 'null' : '$args->'.$this->argument_name);
, '$args->'.$this->variable_name);
$arg .= $this->argument_validator->toString();

View file

@ -0,0 +1,20 @@
<?php
class SortQueryArgument extends QueryArgument{
function toString(){
$arg = sprintf("\n$%s_argument = new SortArgument('%s', %s);\n"
, $this->argument_name
, $this->argument_name
, '$args->'.$this->variable_name);
$arg .= $this->argument_validator->toString();
$arg .= sprintf("if(!$%s_argument->isValid()) return $%s_argument->getErrorMessage();\n"
, $this->argument_name
, $this->argument_name
);
return $arg;
}
}
?>

View file

@ -20,16 +20,16 @@
// Sort order - asc / desc
$this->sort_order = $index->attrs->order;
if(!in_array($this->sort_order, array("asc", "desc"))){
$arg->var = $this->sort_order;
$arg->default = '"asc"';
$this->sort_order_argument = new QueryArgument($arg);
$this->sort_order = "\$args->".$this->sort_order;
$arg->attrs->var = $this->sort_order;
$arg->attrs->default = 'asc';
$this->sort_order_argument = new SortQueryArgument($arg);
$this->sort_order = '$'.$this->sort_order_argument->getArgumentName().'_argument';
}
else $this->sort_order = '"'.$this->sort_order.'"';
}
function toString(){
return sprintf("new OrderByColumn(\$%s_argument, %s)", $this->argument_name, $this->sort_order);
return sprintf("new OrderByColumn(\$%s_argument, %s)", $this->argument->getArgumentName(), $this->sort_order);
}
function getArguments(){

View file

@ -16,13 +16,14 @@
$this->arguments[] = new QueryArgument($index->page_count);
}
$this->list_count = $index->list_count->attrs;
$this->arguments[] = new QueryArgument($index->list_count);
$this->list_count = new QueryArgument($index->list_count);
$this->arguments[] = $this->list_count;
}
function toString(){
if ($this->page)return sprintf("new Limit(\$%s_argument, \$%s_argument, \$%s_argument)",$this->list_count->var, $this->page->var, $this->page_count->var);
else return sprintf("new Limit(\$%s_argument)", $this->list_count->var);
$name = $this->list_count->getArgumentName();
if ($this->page)return sprintf("new Limit(\$%s_argument, \$%s_argument, \$%s_argument)",$name, $name, $name);
else return sprintf("new Limit(\$%s_argument)", $name);
}
function getArguments(){

View file

@ -10,26 +10,6 @@ class QueryArgumentTest extends CubridTest {
function QueryArgumentTest(){
$this->xmlPath = str_replace('QueryArgumentTest.php', '', str_replace('\\', '/', __FILE__)) . $this->xmlPath;
}
function testNotNullConditionArgument(){
$xml_file = $this->xmlPath . "condition1.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new QueryArgument($xml_obj->condition);
$this->assertEquals("member_srl", $tag->getArgumentName());
$this->assertEquals("member_srl", $tag->getColumnName());
$this->assertEquals(true, $tag->isConditionArgument());
$actual = Helper::cleanString($tag->toString());
$expected = Helper::cleanString('$member_srl_argument = new ConditionArgument(\'member_srl\', $args->member_srl, \'equal\');
$member_srl_argument->checkNotNull();
$member_srl_argument->createConditionValue();
if(!$member_srl_argument->isValid()) return $member_srl_argument->getErrorMessage();');
$this->assertEquals($expected, $actual);
}
}
}
?>

View file

@ -19,12 +19,13 @@ class ConditionTagTest extends CubridTest {
$xml_file = $this->xmlPath . "condition1.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new ConditionTag($xml_obj->condition);
$arguments = $tag->getArguments();
$expected = "new Condition('\"user_id\"',\$user_id_argument,\"equal\")";
$expected = "new Condition('\"user_id\"',\$" . $arguments[0]->getArgumentName() . "_argument,\"equal\")";
$actual = $tag->getConditionString();
$this->assertEquals($expected, $actual);
$arguments = $tag->getArguments();
$this->assertEquals(1, count($arguments));
}
@ -54,12 +55,13 @@ class ConditionTagTest extends CubridTest {
$xml_file = $this->xmlPath . "condition2.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new ConditionTag($xml_obj->condition);
$arguments = $tag->getArguments();
$expected = "new Condition('\"type\"',\$type_argument,\"equal\", 'and')";
$expected = "new Condition('\"type\"',\$" . $arguments[0]->getArgumentName() . "_argument,\"equal\", 'and')";
$actual = $tag->getConditionString();
$this->assertEquals($expected, $actual);
$arguments = $tag->getArguments();
$this->assertEquals(1, count($arguments));
}

View file

@ -29,6 +29,7 @@
require_once(_XE_PATH_.'classes/xml/xmlquery/DBParser.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/argument/Argument.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/argument/SortArgument.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/argument/ConditionArgument.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/queryargument/DefaultValue.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/expression/Expression.class.php');
@ -48,4 +49,5 @@
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/table/TableTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/condition/ConditionTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/queryargument/QueryArgument.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/queryargument/SortQueryArgument.class.php');
?>

View file

@ -26,8 +26,13 @@
}
function _testPreparedQuery($xml_file, $argsString, $expected, $methodName, $expectedArgs = NULL){
echo PHP_EOL . ' ----------------------------------- ' .PHP_EOL;
echo $xml_file;
echo PHP_EOL . ' ----------------------------------- ' .PHP_EOL;
$tester = new QueryTester();
$outputString = $tester->getNewParserOutputString($xml_file, $argsString);
echo $outputString;
$output = eval($outputString);
if(!is_a($output, 'Query')){

View file

@ -7,7 +7,7 @@
$args->site_srl = 0;
$output = executeQuery('module.getMidInfo', $args);
$this->assertNotNull($output);
$this->assertNotNull($output->data, $output->message);
$this->assertNotNull($output->data, $output->message . PHP_EOL . $output->variables["_query"]);
$this->assertEquals($output->data->module_srl, 111);
}
@ -15,7 +15,7 @@
$args->site_srl = 0;
$output = executeQuery('module.getSiteInfo', $args);
$this->assertTrue(is_a($output, 'Object'));
$this->assertEquals(0, $output->error, $output->message);
$this->assertEquals(0, $output->error, $output->message . PHP_EOL . $output->variables["_query"]);
}
function test_document_getDocumentList_pagination(){
@ -44,7 +44,7 @@
$args->member_srl = NULL;
$output = executeQuery('document.getDocumentList', $args);
$this->assertTrue(is_int($output->page), $output->message);
$this->assertTrue(is_int($output->page), $output->message . PHP_EOL . $output->variables["_query"]);
}
function test_member_getMemberList(){
@ -56,7 +56,7 @@
$args->page_count = 10;
$output = executeQuery('member.getMemberList', $args);
$this->assertEquals(0, $output->error, $output->message);
$this->assertEquals(0, $output->error, $output->message . PHP_EOL . $output->variables["_query"]);
}
}
?>

View file

@ -146,8 +146,8 @@
$expected = 'select "module_srl"
from "xe_module_grants" as "module_grants"
where "name" in (\'access\',\'view\',\'list\')
and ("group_srl" >= -2
or "group_srl" = -2
and ("group_srl" >= 1
or "group_srl" = -1
or "group_srl" = -2)
group by "module_srl"';
$this->_test($xml_file, $argsString, $expected);

View file

@ -147,7 +147,6 @@
function test_module_getModuleSites(){
$xml_file = _XE_PATH_ . "modules/module/queries/getModuleSites.xml";
//$argsString = '$args->module_srls = array(67, 65);';
$argsString = '$args->module_srls = "67, 65";';
$expected = 'SELECT [modules].[module_srl] as [module_srl], [sites].[domain] as [domain] FROM [xe_modules] as [modules] , [xe_sites] as [sites] WHERE [modules].[module_srl] in (?,?) and [sites].[site_srl] = [modules].[site_srl]';
$this->_test($xml_file, $argsString, $expected, array(array(67, 65)));