From 5a8a017a66403a009533a29ea4416723a7e5e691 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 5 Dec 2014 14:53:42 +0100 Subject: [PATCH 1/5] DDC-3336 - importing platform classes --- .../LimitSubqueryOutputWalkerTest.php | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php index 0c8ddb9b1..0ec56a3da 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php @@ -2,6 +2,8 @@ namespace Doctrine\Tests\ORM\Tools\Pagination; +use Doctrine\DBAL\Platforms\OraclePlatform; +use Doctrine\DBAL\Platforms\PostgreSqlPlatform; use Doctrine\ORM\Query; class LimitSubqueryOutputWalkerTest extends PaginationTestCase @@ -22,7 +24,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase public function testLimitSubqueryWithSortPg() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new 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'); @@ -39,7 +41,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase public function testLimitSubqueryWithScalarSortPg() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new 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' @@ -58,7 +60,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase public function testLimitSubqueryWithMixedSortPg() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new 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' @@ -77,7 +79,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase public function testLimitSubqueryWithHiddenScalarSortPg() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform); $query = $this->entityManager->createQuery( 'SELECT u, g, COUNT(g.id) AS hidden g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC' @@ -96,7 +98,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase public function testLimitSubqueryPg() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform); $this->testLimitSubquery(); @@ -106,7 +108,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase public function testLimitSubqueryWithSortOracle() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new 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'); @@ -124,7 +126,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase public function testLimitSubqueryWithScalarSortOracle() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new 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' @@ -144,7 +146,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase public function testLimitSubqueryWithMixedSortOracle() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new 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' @@ -164,7 +166,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase public function testLimitSubqueryOracle() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new 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'); @@ -179,7 +181,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase $this->entityManager->getConnection()->setDatabasePlatform($odp); } - public function testCountQuery_MixedResultsWithName() + public function testCountQueryMixedResultsWithName() { $query = $this->entityManager->createQuery( 'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a'); From 60462919f2e93d9b84c483e8e4360ad90b565bea Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 5 Dec 2014 14:54:26 +0100 Subject: [PATCH 2/5] DDC-3336 - adding failing test case: scalar expressions in the `ORDER BY` clause crash the `LimitSubqueryOutputWalker` --- .../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 0ec56a3da..a4f02ab7d 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\ORM\Tools\Pagination; +use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\OraclePlatform; use Doctrine\DBAL\Platforms\PostgreSqlPlatform; use Doctrine\ORM\Query; @@ -192,5 +193,23 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase "SELECT DISTINCT id0 FROM (SELECT a0_.id AS id0, a0_.name AS name1, sum(a0_.name) AS sclr2 FROM Author a0_) dctrn_result", $limitQuery->getSql() ); } + + /** + * @group DDC-3336 + */ + public function testCountQueryWithComplexOrderByCondition() + { + $query = $this->entityManager->createQuery( + 'SELECT a FROM Doctrine\\Tests\\ORM\\Tools\\Pagination\\Author a ORDER BY (1 - 1000) * 1 DESC' + ); + $this->entityManager->getConnection()->setDatabasePlatform(new MySqlPlatform()); + + $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker'); + + $this->assertSame( + 'SELECT DISTINCT id_0 FROM (SELECT a0_.id AS id_0, a0_.name AS name_1 FROM Author a0_ ORDER BY (1 - 1000) * 1 DESC) dctrn_result', + $query->getSQL() + ); + } } From bdc54d481cb17f5c0bb1d33e619c54d21bf6da79 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 5 Dec 2014 14:55:26 +0100 Subject: [PATCH 3/5] DDC-3336 - renamed test method for clarity --- .../ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php index a4f02ab7d..7bb684ca0 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php @@ -197,7 +197,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase /** * @group DDC-3336 */ - public function testCountQueryWithComplexOrderByCondition() + public function testCountQueryWithArithmeticOrderByCondition() { $query = $this->entityManager->createQuery( 'SELECT a FROM Doctrine\\Tests\\ORM\\Tools\\Pagination\\Author a ORDER BY (1 - 1000) * 1 DESC' From 34fad084a7c13a12865618b2cb176ea6b2bf2494 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 5 Dec 2014 14:55:56 +0100 Subject: [PATCH 4/5] DDC-3336 - adding missing type-hint docblock --- .../Doctrine/Tests/ORM/Tools/Pagination/PaginationTestCase.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/PaginationTestCase.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/PaginationTestCase.php index d503e81aa..d3e77f6f0 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/PaginationTestCase.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/PaginationTestCase.php @@ -6,6 +6,9 @@ use Doctrine\Tests\OrmTestCase; abstract class PaginationTestCase extends OrmTestCase { + /** + * @var \Doctrine\ORM\EntityManagerInterface + */ public $entityManager; public function setUp() From 060bbb13661e58d2a14c76244580e354693585e7 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 5 Dec 2014 14:56:47 +0100 Subject: [PATCH 5/5] DDC-3336 - applied hotfix: only `PathExpression` instances have a `$field` property --- .../Tools/Pagination/LimitSubqueryOutputWalker.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php index 6598cf2b1..25cce9eeb 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php @@ -13,6 +13,7 @@ namespace Doctrine\ORM\Tools\Pagination; +use Doctrine\ORM\Query\AST\PathExpression; use Doctrine\ORM\Query\SqlWalker; use Doctrine\ORM\Query\AST\SelectStatement; use Doctrine\DBAL\Platforms\PostgreSqlPlatform; @@ -196,12 +197,14 @@ class LimitSubqueryOutputWalker extends SqlWalker $orderBy = array(); if (isset($AST->orderByClause)) { foreach ($AST->orderByClause->orderByItems as $item) { - $possibleAliases = (is_object($item->expression)) - ? array_keys($this->rsm->fieldMappings, $item->expression->field) - : array_keys($this->rsm->scalarMappings, $item->expression); + $expression = $item->expression; + + $possibleAliases = $expression instanceof PathExpression + ? array_keys($this->rsm->fieldMappings, $expression->field) + : array_keys($this->rsm->scalarMappings, $expression); foreach ($possibleAliases as $alias) { - if (!is_object($item->expression) || $this->rsm->columnOwnerMap[$alias] == $item->expression->identificationVariable) { + if (!is_object($expression) || $this->rsm->columnOwnerMap[$alias] == $expression->identificationVariable) { $sqlOrderColumns[] = $alias; $orderBy[] = $alias . ' ' . $item->type; break;