Skip to content

Commit

Permalink
test sql generation
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioBatSilva committed Sep 29, 2012
1 parent ddb2651 commit e5e45a3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 49 deletions.
10 changes: 4 additions & 6 deletions lib/Doctrine/ORM/Query/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @author Jonathan Wage <[email protected]>
* @author Roman Borschel <[email protected]>
* @author Janne Vanhala <[email protected]>
* @author Fabio B. Silva <[email protected]>
*/
class Parser
{
Expand Down Expand Up @@ -1641,12 +1642,12 @@ public function NewObjectExpression()
$className = $this->_lexer->token['value'];

if ( ! class_exists($className, true)) {
$this->semanticalError("Class '$className' is not defined.", $this->_lexer->token);
$this->semanticalError("Class \"$className\" is not defined.", $this->_lexer->token);
}

$class = new \ReflectionClass($className);
if($class->getConstructor() === null) {
$this->semanticalError("Class '$className' has not a valid contructor.", $this->_lexer->token);
$this->semanticalError("Class \"$className\" has not a valid contructor.", $this->_lexer->token);
}

$this->match(Lexer::T_OPEN_PARENTHESIS);
Expand All @@ -1661,7 +1662,7 @@ public function NewObjectExpression()
$this->match(Lexer::T_CLOSE_PARENTHESIS);

if($class->getConstructor()->getNumberOfRequiredParameters() > sizeof($args)) {
$this->semanticalError("Number of arguments does not match definition.", $this->_lexer->token);
$this->semanticalError("Number of arguments does not match.", $this->_lexer->token);
}

return new AST\NewObjectExpression($className, $args);;
Expand All @@ -1680,9 +1681,6 @@ public function NewObjectArg()

return new AST\SimpleSelectExpression($expression);

case ($this->_lexer->lookahead['type'] === Lexer::T_NEW):
return $this->NewObjectExpression();

default:
if ( ! ($this->_isFunction() || $this->_isAggregateFunction($this->_lexer->lookahead))) {
$this->syntaxError();
Expand Down
9 changes: 5 additions & 4 deletions lib/Doctrine/ORM/Query/SqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @author Roman Borschel <[email protected]>
* @author Benjamin Eberlei <[email protected]>
* @author Alexander <[email protected]>
* @author Fabio B. Silva <[email protected]>
* @since 2.0
* @todo Rename: SQLWalker
*/
Expand Down Expand Up @@ -1410,21 +1411,21 @@ public function walkNewObject($newObjectExpression)
foreach ($newObjectExpression->args as $argIndex => $e) {

$resultAlias = $this->scalarResultCounter++;
$columnAlias = $this->getSQLColumnAlias('sclr') . $resultAlias;
$columnAlias = $this->getSQLColumnAlias('sclr');

switch (true) {
case $e instanceof AST\NewObjectExpression:
$sqlSelectExpressions[] = $e->dispatch($this);
break;

default:
$sqlSelectExpressions[] = $e->dispatch($this) . ' AS ' . $columnAlias;
$sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' . $columnAlias;
break;
}


$this->scalarResultAliasMap[$resultAlias] = $columnAlias;
$this->rsm->addScalarResult($columnAlias, $resultAlias, 'string');
$this->rsm->addScalarResult($columnAlias, $resultAlias);

$this->rsm->newObjectMappings[$columnAlias] = array(
'className' => $newObjectExpression->className,
Expand All @@ -1433,7 +1434,7 @@ public function walkNewObject($newObjectExpression)
);
}

return implode(',', $sqlSelectExpressions);
return implode(', ', $sqlSelectExpressions);
}

/**
Expand Down
38 changes: 0 additions & 38 deletions tests/Doctrine/Tests/ORM/Functional/NewOperatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Common\Collections\ArrayCollection;

use Doctrine\DBAL\Connection;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\Models\CMS\CmsEmail;
use Doctrine\Tests\Models\CMS\CmsArticle;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\CMS\CmsPhonenumber;

Expand Down Expand Up @@ -133,40 +129,6 @@ public function testShouldSupportsBasicUsage()
$this->assertEquals($this->fixtures[2]->address->city, $result[2]->address);
}

public function testShouldSupportNestedOperators()
{
$this->markTestIncomplete();
$dql = "
SELECT
new Doctrine\Tests\Models\CMS\CmsUserDTO(
u.name,
e.email,
new Doctrine\Tests\Models\CMS\CmsAddressDTO(
a.country,
a.city,
a.zip
)
)
FROM
Doctrine\Tests\Models\CMS\CmsUser u
JOIN
u.email e
JOIN
u.address a
ORDER BY
u.name";

$query = $this->_em->createQuery($dql);
$result = $query->getResult();

$this->assertCount(3, $result);

$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUserDTO', $result[0]);
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUserDTO', $result[1]);
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUserDTO', $result[2]);

}

public function testShouldSupportAggregateFunctions()
{
$dql = "
Expand Down
12 changes: 11 additions & 1 deletion tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,17 @@ public function testSupportsNewOperator()
{
$this->assertSqlGeneration(
'SELECT new Doctrine\Tests\Models\CMS\CmsUserDTO(u.name, e.email, a.city) FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.email e JOIN u.address a',
'SELECT c0_.name AS sclr01, c1_.email AS sclr12, c2_.city AS sclr23 FROM cms_users c0_ INNER JOIN cms_emails c1_ ON c0_.email_id = c1_.id INNER JOIN cms_addresses c2_ ON c0_.id = c2_.user_id'
'SELECT c0_.name AS sclr0, c1_.email AS sclr1, c2_.city AS sclr2 FROM cms_users c0_ INNER JOIN cms_emails c1_ ON c0_.email_id = c1_.id INNER JOIN cms_addresses c2_ ON c0_.id = c2_.user_id'
);

$this->assertSqlGeneration(
'SELECT new Doctrine\Tests\Models\CMS\CmsUserDTO(u.name, e.email, a.city, COUNT(p)) FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.email e JOIN u.address a JOIN u.phonenumbers p',
'SELECT c0_.name AS sclr0, c1_.email AS sclr1, c2_.city AS sclr2, COUNT(c3_.phonenumber) AS sclr3 FROM cms_users c0_ INNER JOIN cms_emails c1_ ON c0_.email_id = c1_.id INNER JOIN cms_addresses c2_ ON c0_.id = c2_.user_id INNER JOIN cms_phonenumbers c3_ ON c0_.id = c3_.user_id'
);

$this->assertSqlGeneration(
'SELECT new Doctrine\Tests\Models\CMS\CmsUserDTO(u.name, e.email, a.city, COUNT(p) + u.id) FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.email e JOIN u.address a JOIN u.phonenumbers p',
'SELECT c0_.name AS sclr0, c1_.email AS sclr1, c2_.city AS sclr2, COUNT(c3_.phonenumber) + c0_.id AS sclr3 FROM cms_users c0_ INNER JOIN cms_emails c1_ ON c0_.email_id = c1_.id INNER JOIN cms_addresses c2_ ON c0_.id = c2_.user_id INNER JOIN cms_phonenumbers c3_ ON c0_.id = c3_.user_id'
);
}

Expand Down

0 comments on commit e5e45a3

Please sign in to comment.