Started unit tests for correlated subqueries - select, from, where.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8557 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-06-30 18:14:24 +00:00
parent 1353ade0c2
commit e313076cc8
7 changed files with 147 additions and 14 deletions

View file

@ -1,7 +1,6 @@
<?php
/**
*
* @class TableTag
* @author Arnia Sowftare
* @brief Models the <table> tag inside an XML Query file
@ -26,16 +25,21 @@
var $join_type;
var $conditions;
/**
* @brief Initialises Table Tag properties
* @param XML <table> tag $table
*/
function TableTag($table){
$this->unescaped_name = $table->attrs->name;
$dbParser = XmlQueryParser::getDBParser();
$this->unescaped_name = $table->attrs->name;
$this->name = $dbParser->parseTableName($table->attrs->name);
$this->alias = $table->attrs->alias;
if(!$this->alias) $this->alias = $table->attrs->name;
//if(!$this->alias) $this->alias = $alias;
$this->join_type = $table->attrs->type;
$this->conditions = $table->conditions;
}
@ -53,6 +57,12 @@
return $this->unescaped_name;
}
/**
* @brief Returns string for printing in PHP query cache file
* The string contains code for instantiation of either
* a Table or a JoinTable object
* @return string
*/
function getTableString(){
$dbParser = XmlQueryParser::getDBParser();
if($this->isJoinTable()){

View file

@ -1,5 +1,21 @@
<?php
/**
* @class TablesTag
* @author Arnia Sowftare
* @brief Models the <tables> tag inside an XML Query file
*
* @abstract
* Example
* <tables>
* <table name="documents" alias="doc" />
* </tables>
* Attributes
* None.
* Children
* Can have children of type <table> or <query>
*/
class TablesTag {
var $tables;
@ -16,8 +32,7 @@
if(count($xml_tables)) require_once(_XE_PATH_.'classes/xml/xmlquery/tags/table/TableTag.class.php');
foreach($xml_tables as $table){
if($table->name === 'query') $this->tables[] = new QueryTag($table, true);
else $this->tables[] = new TableTag($table);
$this->tables[] = new TableTag($table);
}
}
if(!$xml_queries) return;

View file

@ -2,8 +2,13 @@
class DBTest extends PHPUnit_Framework_TestCase {
function _testQuery($xml_file, $argsString, $expected, $methodName){
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

@ -18,7 +18,6 @@
function testSelectUncorrelated1(){
$xml_file = $this->xmlPath . "select_uncorrelated1.xml";
echo $xml_file;
$argsString = '$args->user_id = 4;
';
$expected = 'select "column_a" as "value_a"
@ -32,7 +31,6 @@
function testSelectUncorrelated2(){
$xml_file = $this->xmlPath . "select_uncorrelated2.xml";
echo $xml_file;
$argsString = '$args->user_id = 4;
$args->user_name = 7;
';
@ -49,7 +47,6 @@
function testFromUncorrelated(){
$xml_file = $this->xmlPath . "from_uncorrelated1.xml";
echo $xml_file;
$argsString = '$args->user_id = 4;
$args->user_name = 7;
';
@ -65,7 +62,6 @@
function testWhereUncorrelated(){
$xml_file = $this->xmlPath . "where_uncorrelated1.xml";
echo $xml_file;
$argsString = '';
$expected = 'select * from
"xe_member" as "member"
@ -73,5 +69,41 @@
from "xe_documents" as "documents")';
$this->_test($xml_file, $argsString, $expected);
}
function testSelectCorrelated1(){
$xml_file = $this->xmlPath . "select_correlated1.xml";
$argsString = '$args->user_id = 7;';
$expected = 'select *,
(select count(*) as "count"
from "xe_documents" as "documents"
where "documents"."user_id" = "member"."user_id"
) as "totaldocumentcount"
from "xe_member" as "member"
where "user_id" = \'7\'';
$this->_test($xml_file, $argsString, $expected);
}
function testWhereCorrelated1(){
$xml_file = $this->xmlPath . "where_correlated1.xml";
$argsString = '';
$expected = ' SELECT *
FROM xe_member as member
WHERE regdate = (SELECT MAX(regdate) as regdate
FROM xe_documents as documents
WHERE documents.user_id = member.user_id)';
$this->_test($xml_file, $argsString, $expected);
}
function testFromCorrelated1(){
$xml_file = $this->xmlPath . "from_correlated1.xml";
$argsString = '';
$expected = 'SELECT m.member_srl, m.nickname, m.regdate, a.count
FROM (
SELECT documents.member_srl as member_srl, count(*) as count
FROM xe_documents as documents
GROUP BY documents.member_srl) a
INNER JOIN xe_members m on m.member_srl = a.member_srl';
$this->_test($xml_file, $argsString, $expected);
}
}
?>

View file

@ -0,0 +1,27 @@
<query id="getMemberInfo" action="select">
<tables>
<query alias="a">
<tables>
<table name="documents" alias="documents" />
</tables>
<columns>
<column name="member_srl" alias="member_srl" />
<column name="count(*)" alias="count" />
</columns>
<groups>
<group column="member_srl" />
</groups>
</query>
<table name="member" alias="m" type="inner 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>
</query>

View file

@ -0,0 +1,22 @@
<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" />
</conditions>
</query>
</columns>
<conditions>
<condition operation="equal" column="user_id" var="user_id" notnull="notnull" />
</conditions>
</query>

View file

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