From 6ed0ff0a12f383ee192c1aca77931ad4bee90adf Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 27 Mar 2011 14:04:53 +0200 Subject: [PATCH] [DDC-1079] Bugfix for shortcut for ArithmeticExpressions in SimpleSelectExpression that lead to literals not being valid. Problem was that ScalarExpression() did not handle AggregateExpressions() at all, which is now fixed. --- lib/Doctrine/ORM/Query/Parser.php | 19 ++++++++----------- .../ORM/Query/LanguageRecognitionTest.php | 9 +++++++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index 6cd0bdad6..51d589095 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -1644,7 +1644,7 @@ class Parser return $this->StateFieldPathExpression(); } else if ($lookahead == Lexer::T_INTEGER || $lookahead == Lexer::T_FLOAT) { return $this->SimpleArithmeticExpression(); - } else if ($this->_isFunction()) { + } else if ($this->_isFunction() || $this->_isAggregateFunction($this->_lexer->lookahead['type'])) { // We may be in an ArithmeticExpression (find the matching ")" and inspect for Math operator) $this->_lexer->peek(); // "(" $peek = $this->_peekBeyondClosingParenthesis(); @@ -1652,8 +1652,12 @@ class Parser if ($this->_isMathOperator($peek)) { return $this->SimpleArithmeticExpression(); } - - return $this->FunctionDeclaration(); + + if ($this->_isAggregateFunction($this->_lexer->lookahead['type'])) { + return $this->AggregateExpression(); + } else { + return $this->FunctionDeclaration(); + } } else if ($lookahead == Lexer::T_STRING) { return $this->StringPrimary(); } else if ($lookahead == Lexer::T_INPUT_PARAMETER) { @@ -1797,15 +1801,8 @@ class Parser } $this->_lexer->peek(); - $beyond = $this->_peekBeyondClosingParenthesis(); - if ($this->_isMathOperator($beyond)) { - $expression = $this->ScalarExpression(); - } else if ($this->_isAggregateFunction($this->_lexer->lookahead['type'])) { - $expression = $this->AggregateExpression(); - } else { - $expression = $this->FunctionDeclaration(); - } + $expression = $this->ScalarExpression(); $expr = new AST\SimpleSelectExpression($expression); diff --git a/tests/Doctrine/Tests/ORM/Query/LanguageRecognitionTest.php b/tests/Doctrine/Tests/ORM/Query/LanguageRecognitionTest.php index ff9d94b1f..fa1da40d5 100644 --- a/tests/Doctrine/Tests/ORM/Query/LanguageRecognitionTest.php +++ b/tests/Doctrine/Tests/ORM/Query/LanguageRecognitionTest.php @@ -251,6 +251,15 @@ class LanguageRecognitionTest extends \Doctrine\Tests\OrmTestCase $this->assertValidDQL("SELECT (SELECT (SUM(u.id) / COUNT(u.id)) FROM Doctrine\Tests\Models\CMS\CmsUser u2) value FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = 'jon'"); } + /** + * @group DDC-1079 + */ + public function testSelectLiteralInSubselect() + { + $this->assertValidDQL('SELECT (SELECT 1 FROM Doctrine\Tests\Models\CMS\CmsUser u2) value FROM Doctrine\Tests\Models\CMS\CmsUser u'); + $this->assertValidDQL('SELECT (SELECT 0 FROM Doctrine\Tests\Models\CMS\CmsUser u2) value FROM Doctrine\Tests\Models\CMS\CmsUser u'); + } + public function testDuplicateAliasInSubselectPart() { $this->assertInvalidDQL("SELECT (SELECT SUM(u.id) / COUNT(u.id) AS foo FROM Doctrine\Tests\Models\CMS\CmsUser u2) foo FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = 'jon'");