From 6bbf2d9da35aa8ce500f26d671a135a00870e33a Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Mon, 5 Sep 2011 03:16:01 -0300 Subject: [PATCH] Added support for ResultVariable referencing in ArithmeticPrimary. Fixes DDC-1346. --- lib/Doctrine/ORM/Query/Parser.php | 8 ++++++-- lib/Doctrine/ORM/Query/SqlWalker.php | 6 ++++++ .../Tests/ORM/Query/SelectSqlGenerationTest.php | 10 +++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index 699537456..a8ddfd879 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -2381,7 +2381,7 @@ class Parser /** * ArithmeticPrimary ::= SingleValuedPathExpression | Literal | "(" SimpleArithmeticExpression ")" * | FunctionsReturningNumerics | AggregateExpression | FunctionsReturningStrings - * | FunctionsReturningDatetime | IdentificationVariable | CaseExpression + * | FunctionsReturningDatetime | IdentificationVariable | ResultVariable | CaseExpression */ public function ArithmeticPrimary() { @@ -2410,7 +2410,11 @@ class Parser if ($peek['value'] == '.') { return $this->SingleValuedPathExpression(); } - + + if (isset($this->_queryComponents[$this->_lexer->lookahead['value']]['resultVariable'])) { + return $this->ResultVariable(); + } + return $this->StateFieldPathExpression(); case Lexer::T_INPUT_PARAMETER: diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 5e92b0973..46563615b 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -1971,6 +1971,12 @@ class SqlWalker implements TreeWalker public function walkArithmeticTerm($term) { if (is_string($term)) { + if (isset($this->_queryComponents[$term])) { + $columnName = $this->_queryComponents[$term]['token']['value']; + + return $this->_scalarResultAliasMap[$columnName]; + } + return $term; } diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index f132296fe..4da9cf3e3 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -38,7 +38,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase foreach ($queryHints AS $name => $value) { $query->setHint($name, $value); } - + parent::assertEquals($sqlToBeConfirmed, $query->getSQL()); $query->free(); } catch (\Exception $e) { @@ -991,6 +991,14 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase ); } + public function testSubSelectAliasesFromOuterQueryReuseInWhereClause() + { + $this->assertSqlGeneration( + "SELECT uo, (SELECT ui.name FROM Doctrine\Tests\Models\CMS\CmsUser ui WHERE ui.id = uo.id) AS bar FROM Doctrine\Tests\Models\CMS\CmsUser uo WHERE bar = ?0", + "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, (SELECT c1_.name FROM cms_users c1_ WHERE c1_.id = c0_.id) AS sclr4 FROM cms_users c0_ WHERE sclr4 = ?" + ); + } + /** * @group DDC-1298 */