From 61634950f35712954e65466a795b9002f37839d9 Mon Sep 17 00:00:00 2001 From: aleks Date: Sat, 26 Jan 2013 21:21:09 +0200 Subject: [PATCH 1/5] Add test for order by scalar --- .../LimitSubqueryOutputWalkerTest.php | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php index 362ffc0c8..7735f3cd6 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php @@ -9,7 +9,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase public function testLimitSubquery() { $query = $this->entityManager->createQuery( - 'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a'); + 'SELECT p, C, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category C JOIN p.author a'); $query->expireQueryCache(true); $limitQuery = clone $query; $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker'); @@ -25,7 +25,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); $query = $this->entityManager->createQuery( - 'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a ORDER BY p.title'); + 'SELECT p, C, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category C JOIN p.author a ORDER BY p.title'); $limitQuery = clone $query; $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker'); @@ -36,6 +36,25 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase $this->entityManager->getConnection()->setDatabasePlatform($odp); } + public function testLimitSubqueryWithScalarSortPg() + { + $odp = $this->entityManager->getConnection()->getDatabasePlatform(); + $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); + + $query = $this->entityManager->createQuery( + 'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\USER u JOIN u.groups g ORDER BY g_quantity' + ); + $limitQuery = clone $query; + $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker'); + + $this->assertEquals( + "SELECT DISTINCT id1, sclr0 FROM (SELECT COUNT(g0_.id) AS sclr0, u1_.id AS id1, g0_.id AS id2 FROM USER u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id ORDER BY sclr0 ASC) dctrn_result ORDER BY sclr0 ASC", + $limitQuery->getSql() + ); + + $this->entityManager->getConnection()->setDatabasePlatform($odp); + } + public function testLimitSubqueryPg() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); From 5e6bc0847fa1a2eb39750fe2727f1faa944189d8 Mon Sep 17 00:00:00 2001 From: aleks Date: Sat, 26 Jan 2013 21:31:45 +0200 Subject: [PATCH 2/5] Added support for order by scalar --- .../ORM/Tools/Pagination/LimitSubqueryOutputWalker.php | 10 +++++++--- .../Tools/Pagination/LimitSubqueryOutputWalkerTest.php | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php index a0ccc982d..02eaeeaa5 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php @@ -179,12 +179,16 @@ class LimitSubqueryOutputWalker extends SqlWalker $orderBy = array(); if (isset($AST->orderByClause)) { foreach ($AST->orderByClause->orderByItems as $item) { - $possibleAliases = array_keys($this->rsm->fieldMappings, $item->expression->field); + if (is_object($item->expression)) { + $possibleAliases = array_keys($this->rsm->fieldMappings, $item->expression->field); + } else { + $possibleAliases = array_keys($this->rsm->scalarMappings, $item->expression); + } foreach ($possibleAliases as $alias) { - if ($this->rsm->columnOwnerMap[$alias] == $item->expression->identificationVariable) { + if (!is_object($item->expression) || $this->rsm->columnOwnerMap[$alias] == $item->expression->identificationVariable) { $sqlOrderColumns[] = $alias; - $orderBy[] = $alias . ' ' . $item->type; + $orderBy[] = $alias . ' ' . $item->type; break; } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php index 7735f3cd6..2e8e8d042 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php @@ -48,7 +48,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker'); $this->assertEquals( - "SELECT DISTINCT id1, sclr0 FROM (SELECT COUNT(g0_.id) AS sclr0, u1_.id AS id1, g0_.id AS id2 FROM USER u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id ORDER BY sclr0 ASC) dctrn_result ORDER BY sclr0 ASC", + "SELECT DISTINCT id1, sclr0 FROM (SELECT COUNT(g0_.id) AS sclr0, u1_.id AS id1, g0_.id AS id2 FROM User u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id ORDER BY sclr0 ASC) dctrn_result ORDER BY sclr0 ASC", $limitQuery->getSql() ); From 6662096ed3b49911a80ff9bfcd67d17a4600cfd1 Mon Sep 17 00:00:00 2001 From: aleks Date: Sat, 26 Jan 2013 23:27:38 +0200 Subject: [PATCH 3/5] Fix typo --- .../ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php index 2e8e8d042..6d8355525 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php @@ -9,7 +9,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase public function testLimitSubquery() { $query = $this->entityManager->createQuery( - 'SELECT p, C, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category C JOIN p.author a'); + 'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a'); $query->expireQueryCache(true); $limitQuery = clone $query; $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker'); @@ -25,7 +25,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); $query = $this->entityManager->createQuery( - 'SELECT p, C, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category C JOIN p.author a ORDER BY p.title'); + 'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a ORDER BY p.title'); $limitQuery = clone $query; $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker'); From dc190a297daaeb3e36b034eebdbf4154584b35c3 Mon Sep 17 00:00:00 2001 From: aleks Date: Sun, 27 Jan 2013 11:09:26 +0200 Subject: [PATCH 4/5] Fix typo --- .../ORM/Tools/Pagination/LimitSubqueryOutputWalker.php | 10 ++++------ .../Tools/Pagination/LimitSubqueryOutputWalkerTest.php | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php index 02eaeeaa5..3943cb5e6 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php @@ -179,16 +179,14 @@ class LimitSubqueryOutputWalker extends SqlWalker $orderBy = array(); if (isset($AST->orderByClause)) { foreach ($AST->orderByClause->orderByItems as $item) { - if (is_object($item->expression)) { - $possibleAliases = array_keys($this->rsm->fieldMappings, $item->expression->field); - } else { - $possibleAliases = array_keys($this->rsm->scalarMappings, $item->expression); - } + $possibleAliases = (is_object($item->expression)) + ? array_keys($this->rsm->fieldMappings, $item->expression->field) + : array_keys($this->rsm->scalarMappings, $item->expression); foreach ($possibleAliases as $alias) { if (!is_object($item->expression) || $this->rsm->columnOwnerMap[$alias] == $item->expression->identificationVariable) { $sqlOrderColumns[] = $alias; - $orderBy[] = $alias . ' ' . $item->type; + $orderBy[] = $alias . ' ' . $item->type; break; } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php index 6d8355525..bb69cd310 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php @@ -42,7 +42,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); $query = $this->entityManager->createQuery( - 'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\USER u JOIN u.groups g ORDER BY g_quantity' + 'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity' ); $limitQuery = clone $query; $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker'); From 206c251090dfd271e3b5bd636fd8a0a469c3ed55 Mon Sep 17 00:00:00 2001 From: aleks Date: Sun, 27 Jan 2013 11:40:40 +0200 Subject: [PATCH 5/5] Add test with mixed sort --- .../LimitSubqueryOutputWalkerTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php index bb69cd310..f19b8520b 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php @@ -55,6 +55,25 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase $this->entityManager->getConnection()->setDatabasePlatform($odp); } + public function testLimitSubqueryWithMixedSortPg() + { + $odp = $this->entityManager->getConnection()->getDatabasePlatform(); + $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); + + $query = $this->entityManager->createQuery( + 'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC' + ); + $limitQuery = clone $query; + $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker'); + + $this->assertEquals( + "SELECT DISTINCT id1, sclr0 FROM (SELECT COUNT(g0_.id) AS sclr0, u1_.id AS id1, g0_.id AS id2 FROM User u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id ORDER BY sclr0 ASC, u1_.id DESC) dctrn_result ORDER BY sclr0 ASC, id1 DESC", + $limitQuery->getSql() + ); + + $this->entityManager->getConnection()->setDatabasePlatform($odp); + } + public function testLimitSubqueryPg() { $odp = $this->entityManager->getConnection()->getDatabasePlatform();