From ceada41b83c9b71d4831d9e50d4c43a75b1ee531 Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Thu, 17 Apr 2014 04:15:35 +0000 Subject: [PATCH] DDC-2934 Added support for function declarations in order by. --- .../en/reference/dql-doctrine-query-language.rst | 2 +- lib/Doctrine/ORM/Query/Parser.php | 16 ++++++++++------ .../Tests/ORM/Query/SelectSqlGenerationTest.php | 8 ++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/docs/en/reference/dql-doctrine-query-language.rst b/docs/en/reference/dql-doctrine-query-language.rst index 18d87835b..0542d0191 100644 --- a/docs/en/reference/dql-doctrine-query-language.rst +++ b/docs/en/reference/dql-doctrine-query-language.rst @@ -1512,7 +1512,7 @@ Items .. code-block:: php UpdateItem ::= SingleValuedPathExpression "=" NewValue - OrderByItem ::= (SimpleArithmeticExpression | SingleValuedPathExpression | ScalarExpression | ResultVariable) ["ASC" | "DESC"] + OrderByItem ::= (SimpleArithmeticExpression | SingleValuedPathExpression | ScalarExpression | ResultVariable | FunctionDeclaration) ["ASC" | "DESC"] GroupByItem ::= IdentificationVariable | ResultVariable | SingleValuedPathExpression NewValue ::= SimpleArithmeticExpression | "NULL" diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index 9ac52a805..ba407034c 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -1456,37 +1456,41 @@ class Parser /** * OrderByItem ::= ( * SimpleArithmeticExpression | SingleValuedPathExpression | - * ScalarExpression | ResultVariable + * ScalarExpression | ResultVariable | FunctionDeclaration * ) ["ASC" | "DESC"] * * @return \Doctrine\ORM\Query\AST\OrderByItem */ public function OrderByItem() { - $this->lexer->peek(); // lookahead => '.' $this->lexer->peek(); // lookahead => token after '.' + $peek = $this->lexer->peek(); // lookahead => token after the token after the '.' + $this->lexer->resetPeek(); + $glimpse = $this->lexer->glimpse(); switch (true) { + case ($this->isFunction($peek)): + $expr = $this->FunctionDeclaration(); + break; case ($this->isMathOperator($peek)): $expr = $this->SimpleArithmeticExpression(); - break; + case ($glimpse['type'] === Lexer::T_DOT): $expr = $this->SingleValuedPathExpression(); - break; + case ($this->lexer->peek() && $this->isMathOperator($this->peekBeyondClosingParenthesis())): $expr = $this->ScalarExpression(); - break; + default: $expr = $this->ResultVariable(); - break; } diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 205189f49..e26790b9b 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -1836,6 +1836,14 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase ); } + public function testOrderByClauseSupportsFunction() + { + $this->assertSqlGeneration( + 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY CONCAT(u.username, u.name) ', + 'SELECT c0_.id AS id_0, c0_.status AS status_1, c0_.username AS username_2, c0_.name AS name_3 FROM cms_users c0_ ORDER BY c0_.username || c0_.name ASC' + ); + } + /** * @group DDC-1719 */