From df0632258a2204bc5f81fb835c7f7ff4af79b3f7 Mon Sep 17 00:00:00 2001
From: "Fabio B. Silva" <fabio.bat.silva@gmail.com>
Date: Wed, 15 Feb 2012 19:24:06 -0200
Subject: [PATCH] Order by clause support case expressions

---
 lib/Doctrine/ORM/Query/Parser.php                     |  8 ++++++++
 lib/Doctrine/ORM/Query/SqlWalker.php                  |  4 ++++
 .../Tests/ORM/Query/SelectSqlGenerationTest.php       | 11 ++++++++---
 3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php
index 4d097b5ff..4d1bba2fd 100644
--- a/lib/Doctrine/ORM/Query/Parser.php
+++ b/lib/Doctrine/ORM/Query/Parser.php
@@ -1347,6 +1347,14 @@ class Parser
                 $expr = $this->SingleValuedPathExpression();
                 break;
 
+            case ($this->_lexer->lookahead['type'] === Lexer::T_CASE):
+            case ($this->_lexer->lookahead['type'] === Lexer::T_COALESCE):
+            case ($this->_lexer->lookahead['type'] === Lexer::T_NULLIF):
+                // Since NULLIF and COALESCE can be identified as a function,
+                // we need to check if before check for FunctionDeclaration
+                $expr = $this->CaseExpression();
+                break;
+
             case ($this->_isFunction()):
                 $this->_lexer->peek(); // "("
                 
diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php
index 8426dc124..1e095ff5a 100644
--- a/lib/Doctrine/ORM/Query/SqlWalker.php
+++ b/lib/Doctrine/ORM/Query/SqlWalker.php
@@ -786,6 +786,10 @@ class SqlWalker implements TreeWalker
             case ($expr instanceof AST\AggregateExpression):
             case ($expr instanceof AST\Functions\FunctionNode):
             case ($expr instanceof AST\SimpleArithmeticExpression):
+            case ($expr instanceof AST\NullIfExpression):
+            case ($expr instanceof AST\CoalesceExpression):
+            case ($expr instanceof AST\SimpleCaseExpression):
+            case ($expr instanceof AST\GeneralCaseExpression):
                 $sql = $expr->dispatch($this);
                 break;
             default:
diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
index e77fa6447..314f998b4 100644
--- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
+++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
@@ -1608,15 +1608,20 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
      */
     public function testOrderByClauseSupportNullIfAndCoalesce()
     {
-        $this->markTestIncomplete();
+
         $this->assertSqlGeneration(
             'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY NULLIF(u.name, u.username)',
             'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY NULLIF(c0_.name, c0_.username) ASC'
         );
 
         $this->assertSqlGeneration(
-            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY NULLIF(u.name, u.username)',
-            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY COALESCE(NULLIF(u.name, \'\'), u.username) ASC'
+            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY COALESCE(NULLIF(u.name, u.username), u.id)',
+            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY COALESCE(NULLIF(c0_.name, c0_.username), c0_.id) ASC'
+        );
+
+        $this->assertSqlGeneration(
+            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY CASE u.id WHEN 1 THEN 1 ELSE 0 END',
+            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY CASE c0_.id WHEN 1 THEN 1 ELSE 0 END ASC'
         );
     }
 }