From b8b7afe5762c012778bd61b1bc5b6afe29848b88 Mon Sep 17 00:00:00 2001 From: Raymond Kolbe Date: Tue, 9 Apr 2013 17:00:06 -0300 Subject: [PATCH 1/3] Fix Oracle subquery ordering lost bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See http://www.doctrine-project.org/jira/browse/DDC-1800 for a full description. --- .../Tools/Pagination/LimitSubqueryOutputWalker.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php index 3943cb5e6..274dac271 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php @@ -137,9 +137,16 @@ class LimitSubqueryOutputWalker extends SqlWalker )); } - // Build the counter query. - $sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result', - implode(', ', $sqlIdentifier), $innerSql); + // Build the counter query + if ($this->platform instanceof OraclePlatform) { + // Ordering is lost in Oracle with subqueries + // http://www.doctrine-project.org/jira/browse/DDC-1800 + $sql = sprintf('SELECT DISTINCT %s, ROWNUM FROM (%s) dctrn_result ORDER BY ROWNUM ASC', + implode(', ', $sqlIdentifier), $innerSql); + } else { + $sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result', + implode(', ', $sqlIdentifier), $innerSql); + } if ($this->platform instanceof PostgreSqlPlatform) { //http://www.doctrine-project.org/jira/browse/DDC-1958 From 4bafcc5b317a1feb8f05febf7985d8cfd87d31d7 Mon Sep 17 00:00:00 2001 From: Raymond Kolbe Date: Tue, 9 Apr 2013 17:30:54 -0300 Subject: [PATCH 2/3] Fix Oracle subquery ordering MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See http://www.doctrine-project.org/jira/browse/DDC-1800 and http://www.doctrine-project.org/jira/browse/DDC-1958#comment-19969 --- .../Pagination/LimitSubqueryOutputWalker.php | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php index 274dac271..a6c24ee34 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php @@ -16,6 +16,7 @@ namespace Doctrine\ORM\Tools\Pagination; use Doctrine\ORM\Query\SqlWalker; use Doctrine\ORM\Query\AST\SelectStatement; use Doctrine\DBAL\Platforms\PostgreSqlPlatform; +use Doctrine\DBAL\Platforms\OraclePlatform; /** * Wraps the query in order to select root entity IDs for pagination. @@ -138,19 +139,13 @@ class LimitSubqueryOutputWalker extends SqlWalker } // Build the counter query - if ($this->platform instanceof OraclePlatform) { - // Ordering is lost in Oracle with subqueries - // http://www.doctrine-project.org/jira/browse/DDC-1800 - $sql = sprintf('SELECT DISTINCT %s, ROWNUM FROM (%s) dctrn_result ORDER BY ROWNUM ASC', - implode(', ', $sqlIdentifier), $innerSql); - } else { - $sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result', - implode(', ', $sqlIdentifier), $innerSql); - } + $sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result', + implode(', ', $sqlIdentifier), $innerSql); - if ($this->platform instanceof PostgreSqlPlatform) { + if ($this->platform instanceof PostgreSqlPlatform || + $this->platform instanceof OraclePlatform) { //http://www.doctrine-project.org/jira/browse/DDC-1958 - $this->getPostgresqlSql($AST, $sqlIdentifier, $innerSql, $sql); + $this->preserveSqlOrdering($AST, $sqlIdentifier, $innerSql, $sql); } // Apply the limit and offset. @@ -168,9 +163,9 @@ class LimitSubqueryOutputWalker extends SqlWalker return $sql; } - + /** - * Generates new SQL for postgresql if necessary. + * Generates new SQL for Postgresql or Oracle if necessary. * * @param SelectStatement $AST * @param array $sqlIdentifier @@ -179,7 +174,7 @@ class LimitSubqueryOutputWalker extends SqlWalker * * @return void */ - public function getPostgresqlSql(SelectStatement $AST, array $sqlIdentifier, $innerSql, &$sql) + public function preserveSqlOrdering(SelectStatement $AST, array $sqlIdentifier, $innerSql, &$sql) { // For every order by, find out the SQL alias by inspecting the ResultSetMapping. $sqlOrderColumns = array(); From 27e23faa5a74c3eb4500d470d097e86d1962ca48 Mon Sep 17 00:00:00 2001 From: Raymond Kolbe Date: Wed, 10 Apr 2013 13:07:09 -0300 Subject: [PATCH 3/3] Accompanying tests for PR #646 --- .../LimitSubqueryOutputWalkerTest.php | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php index f19b8520b..07ce5afd9 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php @@ -83,6 +83,82 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase $this->entityManager->getConnection()->setDatabasePlatform($odp); } + + public function testLimitSubqueryWithSortOracle() + { + $odp = $this->entityManager->getConnection()->getDatabasePlatform(); + $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform); + + $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'); + $query->expireQueryCache(true); + $limitQuery = clone $query; + $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker'); + + $this->assertEquals( + "SELECT DISTINCT ID0, TITLE1 FROM (SELECT m0_.id AS ID0, m0_.title AS TITLE1, c1_.id AS ID2, a2_.id AS ID3, a2_.name AS NAME4, m0_.author_id AS AUTHOR_ID5, m0_.category_id AS CATEGORY_ID6 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id ORDER BY m0_.title ASC) dctrn_result ORDER BY TITLE1 ASC", $limitQuery->getSql() + ); + + $this->entityManager->getConnection()->setDatabasePlatform($odp); + } + + public function testLimitSubqueryWithScalarSortOracle() + { + $odp = $this->entityManager->getConnection()->getDatabasePlatform(); + $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform); + + $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' + ); + $query->expireQueryCache(true); + $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 testLimitSubqueryWithMixedSortOracle() + { + $odp = $this->entityManager->getConnection()->getDatabasePlatform(); + $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform); + + $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' + ); + $query->expireQueryCache(true); + $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 testLimitSubqueryOracle() + { + $odp = $this->entityManager->getConnection()->getDatabasePlatform(); + $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform); + + $query = $this->entityManager->createQuery( + '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'); + + $this->assertEquals( + "SELECT DISTINCT ID0 FROM (SELECT m0_.id AS ID0, m0_.title AS TITLE1, c1_.id AS ID2, a2_.id AS ID3, a2_.name AS NAME4, m0_.author_id AS AUTHOR_ID5, m0_.category_id AS CATEGORY_ID6 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id) dctrn_result", $limitQuery->getSql() + ); + + $this->entityManager->getConnection()->setDatabasePlatform($odp); + } public function testCountQuery_MixedResultsWithName() {