Added subquery argument support to more types of queries.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8565 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-07-05 19:49:20 +00:00
parent 4b85afd9b3
commit d248d70773
11 changed files with 232 additions and 32 deletions

View file

@ -34,10 +34,7 @@
function getArguments(){ function getArguments(){
$arguments = array(); $arguments = array();
foreach($this->conditions as $condition){ foreach($this->conditions as $condition){
if(is_a($condition, 'QueryTag'))
$arguments = array_merge($arguments, $condition->getArguments()); $arguments = array_merge($arguments, $condition->getArguments());
else
$arguments[] = $condition->getArgument();
} }
return $arguments; return $arguments;
} }

View file

@ -44,8 +44,12 @@
$this->pipe = $pipe; $this->pipe = $pipe;
} }
function getArgument(){ function getArguments(){
return $this->argument; $arguments = array();
if($this->query)
$arguments = array_merge($arguments, $this->query->getArguments());
$arguments[] = $this->argument;
return $arguments;
} }
function getConditionString(){ function getConditionString(){

View file

@ -171,6 +171,7 @@ class QueryTag {
$arguments = array(); $arguments = array();
if($this->columns) if($this->columns)
$arguments = array_merge($arguments, $this->columns->getArguments()); $arguments = array_merge($arguments, $this->columns->getArguments());
$arguments = array_merge($arguments, $this->tables->getArguments());
$arguments = array_merge($arguments, $this->conditions->getArguments()); $arguments = array_merge($arguments, $this->conditions->getArguments());
$arguments = array_merge($arguments, $this->navigation->getArguments()); $arguments = array_merge($arguments, $this->navigation->getArguments());
return $arguments; return $arguments;

View file

@ -24,7 +24,7 @@
var $alias; var $alias;
var $join_type; var $join_type;
var $conditions; var $conditions;
var $conditionsTag;
/** /**
* @brief Initialises Table Tag properties * @brief Initialises Table Tag properties
* @param XML <table> tag $table * @param XML <table> tag $table
@ -41,6 +41,9 @@
$this->join_type = $table->attrs->type; $this->join_type = $table->attrs->type;
$this->conditions = $table->conditions; $this->conditions = $table->conditions;
if($this->isJoinTable())
$this->conditionsTag = new JoinConditionsTag($this->conditions);
} }
function isJoinTable(){ function isJoinTable(){
@ -66,16 +69,20 @@
function getTableString(){ function getTableString(){
$dbParser = XmlQueryParser::getDBParser(); $dbParser = XmlQueryParser::getDBParser();
if($this->isJoinTable()){ if($this->isJoinTable()){
$conditionsTag = new JoinConditionsTag($this->conditions);
return sprintf('new JoinTable(\'%s\', \'%s\', "%s", %s)' return sprintf('new JoinTable(\'%s\', \'%s\', "%s", %s)'
, $dbParser->escape($this->name) , $dbParser->escape($this->name)
, $dbParser->escape($this->alias) , $dbParser->escape($this->alias)
, $this->join_type, $conditionsTag->toString()); , $this->join_type, $this->conditionsTag->toString());
} }
return sprintf('new Table(\'%s\'%s)' return sprintf('new Table(\'%s\'%s)'
, $dbParser->escape($this->name) , $dbParser->escape($this->name)
, $this->alias ? ', \'' . $dbParser->escape($this->alias) .'\'' : ''); , $this->alias ? ', \'' . $dbParser->escape($this->alias) .'\'' : '');
} }
function getArguments(){
if(!isset($this->conditionsTag)) return array();
return $this->conditionsTag->getArguments();
}
} }
?> ?>

View file

@ -53,5 +53,12 @@
$output_tables .= ')'; $output_tables .= ')';
return $output_tables; return $output_tables;
} }
function getArguments(){
$arguments = array();
foreach($this->tables as $table)
$arguments = array_merge($arguments, $table->getArguments());
return $arguments;
}
} }
?> ?>

View file

@ -2,13 +2,13 @@
class DBTest extends PHPUnit_Framework_TestCase { class DBTest extends PHPUnit_Framework_TestCase {
function _testQuery($xml_file, $argsString, $expected, $methodName){ function _testQuery($xml_file, $argsString, $expected, $methodName){
//echo PHP_EOL . ' ----------------------------------- ' .PHP_EOL; echo PHP_EOL . ' ----------------------------------- ' .PHP_EOL;
//echo $xml_file; echo $xml_file;
//echo PHP_EOL . ' ----------------------------------- ' .PHP_EOL; echo PHP_EOL . ' ----------------------------------- ' .PHP_EOL;
$tester = new QueryTester(); $tester = new QueryTester();
$outputString = $tester->getNewParserOutputString($xml_file, $argsString); $outputString = $tester->getNewParserOutputString($xml_file, $argsString);
//echo $outputString; echo $outputString;
$output = eval($outputString); $output = eval($outputString);
if(!is_a($output, 'Query')){ if(!is_a($output, 'Query')){
@ -18,8 +18,8 @@
$querySql = $db->{$methodName}($output); $querySql = $db->{$methodName}($output);
// Remove whitespaces, tabs and all // Remove whitespaces, tabs and all
$querySql = Helper::cleanQuery($querySql); $querySql = Helper::cleanString($querySql);
$expected = Helper::cleanQuery($expected); $expected = Helper::cleanString($expected);
} }
$this->assertEquals($expected, $querySql); $this->assertEquals($expected, $querySql);
} }
@ -37,8 +37,8 @@
$queryArguments = $output->getArguments(); $queryArguments = $output->getArguments();
// Remove whitespaces, tabs and all // Remove whitespaces, tabs and all
$querySql = Helper::cleanQuery($querySql); $querySql = Helper::cleanString($querySql);
$expected = Helper::cleanQuery($expected); $expected = Helper::cleanString($expected);
} }
// Test // Test
@ -51,9 +51,14 @@
$this->assertEquals($expectedArgs[$i], $queryArguments[$i]->getValue()); $this->assertEquals($expectedArgs[$i], $queryArguments[$i]->getValue());
} }
} }
function _testCachedOutput($expected, $actual){
$expected = Helper::cleanString($expected);
$actual = Helper::cleanString($actual);
$this->assertEquals($expected, $actual);
}
} }
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
?> ?>

View file

@ -19,7 +19,7 @@
function testSelectUncorrelated1(){ function testSelectUncorrelated1(){
$xml_file = $this->xmlPath . "select_uncorrelated1.xml"; $xml_file = $this->xmlPath . "select_uncorrelated1.xml";
$argsString = '$args->user_id = 4; $argsString = '$args->user_id = 4;
'; ';
$expected = 'select "column_a" as "value_a" $expected = 'select "column_a" as "value_a"
, (select max("column_b") as "count" , (select max("column_b") as "count"
from "xe_table_b" as "table_b" from "xe_table_b" as "table_b"
@ -45,7 +45,7 @@
$this->_test($xml_file, $argsString, $expected); $this->_test($xml_file, $argsString, $expected);
} }
function testFromUncorrelated(){ function testFromUncorrelated1(){
$xml_file = $this->xmlPath . "from_uncorrelated1.xml"; $xml_file = $this->xmlPath . "from_uncorrelated1.xml";
$argsString = '$args->user_id = 4; $argsString = '$args->user_id = 4;
$args->user_name = 7; $args->user_name = 7;
@ -60,13 +60,36 @@
$this->_test($xml_file, $argsString, $expected); $this->_test($xml_file, $argsString, $expected);
} }
function testWhereUncorrelated(){ // function testFromUncorrelated2(){
$xml_file = $this->xmlPath . "where_uncorrelated1.xml"; // $xml_file = $this->xmlPath . "from_uncorrelated1.xml";
$argsString = ''; // $argsString = '$args->user_id = 4;
$expected = 'select * from // $args->user_name = 7;
"xe_member" as "member" // ';
where "regdate" = (select max("regdate") as "maxregdate" // $expected = 'select max("documentcountbymember"."count") as "maxcount"
from "xe_documents" as "documents")'; // from (
// select "member_srl" as "member_srl"
// , count(*) as "count"
// from "xe_documents" as "documents"
// group by "member_srl"
// ) as "documentcountbymember"';
// $this->_test($xml_file, $argsString, $expected);
// }
function testFromUncorrelated2(){
$xml_file = $this->xmlPath . "from_uncorrelated2.xml";
$argsString = '$args->member_srl = 4;
$args->module_srl = 7;
';
$expected = 'select max("documentcountbymember"."count") as "maxcount"
from (
select "member_srl" as "member_srl"
, count(*) as "count"
from "xe_documents" as "documents"
where "module_srl" = 7
group by "member_srl"
) as "documentcountbymember"
where "member_srl" = 4
';
$this->_test($xml_file, $argsString, $expected); $this->_test($xml_file, $argsString, $expected);
} }
@ -83,6 +106,22 @@
$this->_test($xml_file, $argsString, $expected); $this->_test($xml_file, $argsString, $expected);
} }
function testSelectCorrelated2(){
$xml_file = $this->xmlPath . "select_correlated2.xml";
$argsString = '$args->user_id = 7;
$args->module_srl = 17;
';
$expected = 'select *,
(select count(*) as "count"
from "xe_documents" as "documents"
where "documents"."user_id" = "member"."user_id"
and "module_srl" = 17
) as "totaldocumentcount"
from "xe_member" as "member"
where "user_id" = \'7\'';
$this->_test($xml_file, $argsString, $expected);
}
function testWhereCorrelated1(){ function testWhereCorrelated1(){
$xml_file = $this->xmlPath . "where_correlated1.xml"; $xml_file = $this->xmlPath . "where_correlated1.xml";
$argsString = ''; $argsString = '';
@ -96,6 +135,22 @@
$this->_test($xml_file, $argsString, $expected); $this->_test($xml_file, $argsString, $expected);
} }
function testWhereCorrelated2(){
$xml_file = $this->xmlPath . "where_correlated2.xml";
$argsString = '$args->module_srl = 12; $args->member_srl = 19;';
$expected = 'select *
from "xe_member" as "member"
where "member_srl" = 19
and "regdate" = (
select max("regdate") as "maxregdate"
from "xe_documents" as "documents"
where "documents"."user_id" = "member"."user_id"
and "module_srl" = 12
)
';
$this->_test($xml_file, $argsString, $expected);
}
function testFromCorrelated1(){ function testFromCorrelated1(){
$xml_file = $this->xmlPath . "from_correlated1.xml"; $xml_file = $this->xmlPath . "from_correlated1.xml";
$argsString = ''; $argsString = '';
@ -112,5 +167,25 @@
left join "xe_member" as "m" on "m"."member" = "a"."member_srl"'; left join "xe_member" as "m" on "m"."member" = "a"."member_srl"';
$this->_test($xml_file, $argsString, $expected); $this->_test($xml_file, $argsString, $expected);
} }
function testFromCorrelated2(){
$xml_file = $this->xmlPath . "from_correlated2.xml";
$argsString = '$args->module_srl = 12; $args->count = 20;';
$expected = 'select "m"."member_srl"
, "m"."nickname"
, "m"."regdate"
, "a"."count"
from (
select "member_srl" as "member_srl"
, count(*) as "count"
from "xe_documents" as "documents"
where "module_srl" = 12
group by "member_srl"
) as "a"
left join "xe_member" as "m" on "m"."member" = "a"."member_srl"
where "a"."count" >= 20
';
$this->_test($xml_file, $argsString, $expected);
}
} }
?> ?>

View file

@ -0,0 +1,33 @@
<query id="getMemberInfo" action="select">
<tables>
<table query="true" alias="a">
<tables>
<table name="documents" alias="documents" />
</tables>
<columns>
<column name="member_srl" alias="member_srl" />
<column name="count(*)" alias="count" />
</columns>
<conditions>
<condition operation="equal" column="module_srl" var="module_srl" notnull="notnull" />
</conditions>
<groups>
<group column="member_srl" />
</groups>
</table>
<table name="member" alias="m" type="left join">
<conditions>
<condition operation="equal" column="m.member" default="a.member_srl" />
</conditions>
</table>
</tables>
<columns>
<column name="m.member_srl" />
<column name="m.nickname" />
<column name="m.regdate" />
<column name="a.count" />
</columns>
<conditions>
<condition operation="more" column="a.count" var="count" notnull="notnull" />
</conditions>
</query>

View file

@ -0,0 +1,25 @@
<query id="getMemberInfo" action="select">
<tables>
<table query="true" alias="documentCountByMember">
<tables>
<table name="documents" alias="documents" />
</tables>
<columns>
<column name="member_srl" alias="member_srl" />
<column name="count(*)" alias="count" />
</columns>
<conditions>
<condition operation="equal" column="module_srl" var="module_srl" notnull="notnull" />
</conditions>
<groups>
<group column="member_srl" />
</groups>
</table>
</tables>
<columns>
<column name="MAX(documentCountByMember.count)" alias="maxCount" />
</columns>
<conditions>
<condition operation="equal" column="member_srl" var="member_srl" notnull="notnull" />
</conditions>
</query>

View file

@ -0,0 +1,23 @@
<query id="getStatistics" action="select">
<tables>
<table name="member" alias="member" />
</tables>
<columns>
<column name="*" />
<query id="getMemberDocumentCount" alias="totalDocumentCount">
<tables>
<table name="documents" alias="documents" />
</tables>
<columns>
<column name="count(*)" alias="count" />
</columns>
<conditions>
<condition operation="equal" column="documents.user_id" default="member.user_id" />
<condition operation="equal" column="module_srl" var="module_srl" notnull="notnull" pipe="and" />
</conditions>
</query>
</columns>
<conditions>
<condition operation="equal" column="user_id" var="user_id" notnull="notnull" />
</conditions>
</query>

View file

@ -0,0 +1,23 @@
<query id="getMemberInfo" action="select">
<tables>
<table name="member" alias="member" />
</tables>
<columns>
<column name="*" />
</columns>
<conditions>
<condition operation="equal" column="member_srl" var="member_srl" notnull="notnull"/>
<query alias="documentMaxRegdate" operation="equal" column="regdate" notnull="notnull" pipe="and" >
<tables>
<table name="documents" alias="documents" />
</tables>
<columns>
<column name="max(regdate)" alias="maxregdate" />
</columns>
<conditions>
<condition operation="equal" column="documents.user_id" default="member.user_id" notnull="notnull" />
<condition operation="equal" column="module_srl" var="module_srl" notnull="notnull" pipe="and" />
</conditions>
</query>
</conditions>
</query>