From d9c1782a4f6d46f66e9deb2c375830f9192d4482 Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Sat, 3 Aug 2013 17:38:55 -0400 Subject: [PATCH] Properly fixed DDC-1858. Added support for ResultVariable in NullComparisons while using HavingClause. --- lib/Doctrine/ORM/Query/Parser.php | 26 +++++++++++++++++-- lib/Doctrine/ORM/Query/SqlWalker.php | 10 +++++-- .../ORM/Query/SelectSqlGenerationTest.php | 11 ++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index b64785dfd..f873f4d01 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -3108,7 +3108,7 @@ class Parser } /** - * NullComparisonExpression ::= (InputParameter | NullIfExpression | CoalesceExpression | SingleValuedPathExpression) "IS" ["NOT"] "NULL" + * NullComparisonExpression ::= (InputParameter | NullIfExpression | CoalesceExpression | SingleValuedPathExpression | ResultVariable) "IS" ["NOT"] "NULL" * * @return \Doctrine\ORM\Query\AST\NullComparisonExpression */ @@ -3134,7 +3134,29 @@ class Parser break; default: - $expr = $this->SingleValuedPathExpression(); + // We need to check if we are in a IdentificationVariable or SingleValuedPathExpression + $glimpse = $this->lexer->glimpse(); + + if ($glimpse['type'] === Lexer::T_DOT) { + $expr = $this->SingleValuedPathExpression(); + + // Leave switch statement + break; + } + + $lookaheadValue = $this->lexer->lookahead['value']; + + // Validate existing component + if ( ! isset($this->queryComponents[$lookaheadValue])) { + $this->semanticalError('Cannot add having condition on undefined result variable.'); + } + + // Validating ResultVariable + if ( ! isset($this->queryComponents[$lookaheadValue]['resultVariable'])) { + $this->semanticalError('Cannot add having condition on a non result variable.'); + } + + $expr = $this->ResultVariable(); break; } diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 5eae0bf9a..fe923c953 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -1921,9 +1921,15 @@ class SqlWalker implements TreeWalker */ public function walkNullComparisonExpression($nullCompExpr) { - $expression = $nullCompExpr->expression; - $comparison = ' IS' . ($nullCompExpr->not ? ' NOT' : '') . ' NULL'; + $expression = $nullCompExpr->expression; + $comparison = ' IS' . ($nullCompExpr->not ? ' NOT' : '') . ' NULL'; + // Handle ResultVariable + if (is_string($expression) && isset($this->queryComponents[$expression]['resultVariable'])) { + return $this->walkResultVariable($expression) . $comparison; + } + + // Handle InputParameter mapping inclusion to ParserResult if ($expression instanceof AST\InputParameter) { $this->parserResult->addParameterMapping($expression->name, $this->sqlParamIndex++); diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index dbb303323..a444a2e87 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -2050,6 +2050,17 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase 'SELECT c0_.name AS name0 FROM cms_users c0_ HAVING name0 IN (?)' ); } + + /** + * @group DDC-1858 + */ + public function testHavingSupportResultVariableInAggregateFunction() + { + $this->assertSqlGeneration( + 'SELECT COUNT(u.name) AS countName FROM Doctrine\Tests\Models\CMS\CmsUser u HAVING countName IS NULL', + 'SELECT COUNT(c0_.name) AS sclr0 FROM cms_users c0_ HAVING sclr0 IS NULL' + ); + } } class MyAbsFunction extends \Doctrine\ORM\Query\AST\Functions\FunctionNode