1
0
mirror of synced 2024-12-05 03:06:05 +03:00

Merge branch 'hotfix/#1337-fix-oracle-postgresql-paginator-sorting-issues' into hotfix/#1342-paginator-functional-test-integration

This commit is contained in:
Marco Pivetta 2015-03-24 00:03:51 +00:00
commit 6dfff304f3
3 changed files with 330 additions and 55 deletions

View File

@ -13,10 +13,15 @@
namespace Doctrine\ORM\Tools\Pagination;
use Doctrine\ORM\Query\AST\ArithmeticExpression;
use Doctrine\ORM\Query\AST\ArithmeticTerm;
use Doctrine\ORM\Query\AST\OrderByClause;
use Doctrine\ORM\Query\AST\PartialObjectExpression;
use Doctrine\ORM\Query\AST\PathExpression;
use Doctrine\ORM\Query\AST\SelectExpression;
use Doctrine\ORM\Query\Expr\OrderBy;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\AST\SelectStatement;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
/**
* Wraps the query in order to select root entity IDs for pagination.
@ -56,6 +61,23 @@ class LimitSubqueryOutputWalker extends SqlWalker
*/
private $maxResults;
/**
* @var \Doctrine\ORM\EntityManager
*/
private $em;
/**
* @var array
*/
private $orderByPathExpressions = [];
/**
* The quote strategy.
*
* @var \Doctrine\ORM\Mapping\QuoteStrategy
*/
private $quoteStrategy;
/**
* Constructor.
*
@ -78,6 +100,9 @@ class LimitSubqueryOutputWalker extends SqlWalker
$this->maxResults = $query->getMaxResults();
$query->setFirstResult(null)->setMaxResults(null);
$this->em = $query->getEntityManager();
$this->quoteStrategy = $this->em->getConfiguration()->getQuoteStrategy();
parent::__construct($query, $parserResult, $queryComponents);
}
@ -85,13 +110,27 @@ class LimitSubqueryOutputWalker extends SqlWalker
* Walks down a SelectStatement AST node, wrapping it in a SELECT DISTINCT.
*
* @param SelectStatement $AST
* @param bool $addMissingItemsFromOrderByToSelect
*
* @return string
*
* @throws \RuntimeException
*/
public function walkSelectStatement(SelectStatement $AST)
public function walkSelectStatement(SelectStatement $AST, $addMissingItemsFromOrderByToSelect = true)
{
// We don't want to call this recursively!
if ($AST->orderByClause instanceof OrderByClause && $addMissingItemsFromOrderByToSelect) {
// In the case of ordering a query by columns from joined tables, we
// must add those columns to the select clause of the query BEFORE
// the SQL is generated.
$this->addMissingItemsFromOrderByToSelect($AST);
}
// Remove order by clause from the inner query
// It will be re-appended in the outer select generated by this method
$orderByClause = $AST->orderByClause;
$AST->orderByClause = null;
// Set every select expression as visible(hidden = false) to
// make $AST have scalar mappings properly - this is relevant for referencing selected
// fields from outside the subquery, for example in the ORDER BY segment
@ -104,6 +143,9 @@ class LimitSubqueryOutputWalker extends SqlWalker
$innerSql = parent::walkSelectStatement($AST);
// Restore orderByClause
$AST->orderByClause = $orderByClause;
// Restore hiddens
foreach ($AST->selectClause->selectExpressions as $idx => $expr) {
$expr->hiddenAliasResultVariable = $hiddens[$idx];
@ -163,7 +205,7 @@ class LimitSubqueryOutputWalker extends SqlWalker
implode(', ', $sqlIdentifier), $innerSql);
// http://www.doctrine-project.org/jira/browse/DDC-1958
$sql = $this->preserveSqlOrdering($AST, $sqlIdentifier, $innerSql, $sql);
$sql = $this->preserveSqlOrdering($sqlIdentifier, $innerSql, $sql, $orderByClause);
// Apply the limit and offset.
$sql = $this->platform->modifyLimitQuery(
@ -182,49 +224,182 @@ class LimitSubqueryOutputWalker extends SqlWalker
}
/**
* Generates new SQL for Postgresql or Oracle if necessary.
* Finds all PathExpressions in an AST's OrderByClause, and ensures that
* the referenced fields are present in the SelectClause of the passed AST.
*
* @param SelectStatement $AST
*/
private function addMissingItemsFromOrderByToSelect(SelectStatement $AST)
{
$this->orderByPathExpressions = [];
// We need to do this in another walker because otherwise we'll end up
// polluting the state of this one.
$walker = clone $this;
// This will populate $orderByPathExpressions via
// LimitSubqueryOutputWalker::walkPathExpression, which will be called
// as the select statement is walked. We'll end up with an array of all
// path expressions referenced in the query.
$walker->walkSelectStatement($AST, false);
$orderByPathExpressions = $walker->getOrderByPathExpressions();
// Get a map of referenced identifiers to field names.
$selects = [];
foreach ($orderByPathExpressions as $pathExpression) {
$idVar = $pathExpression->identificationVariable;
$field = $pathExpression->field;
if (!isset($selects[$idVar])) {
$selects[$idVar] = [];
}
$selects[$idVar][$field] = true;
}
// Loop the select clause of the AST and exclude items from $select
// that are already being selected in the query.
foreach ($AST->selectClause->selectExpressions as $selectExpression) {
if ($selectExpression instanceof SelectExpression) {
$idVar = $selectExpression->expression;
if (!is_string($idVar)) {
continue;
}
$field = $selectExpression->fieldIdentificationVariable;
if ($field === null) {
// No need to add this select, as we're already fetching the whole object.
unset($selects[$idVar]);
} else {
unset($selects[$idVar][$field]);
}
}
}
// Add select items which were not excluded to the AST's select clause.
foreach ($selects as $idVar => $fields) {
$AST->selectClause->selectExpressions[] = new SelectExpression(new PartialObjectExpression($idVar, array_keys($fields)), null, true);
}
}
/**
* Generates new SQL for statements with an order by clause
*
* @param array $sqlIdentifier
* @param string $innerSql
* @param string $sql
* @param OrderByClause $orderByClause
*
* @return void
* @return string
*/
public function preserveSqlOrdering(SelectStatement $AST, array $sqlIdentifier, $innerSql, $sql)
public function preserveSqlOrdering(array $sqlIdentifier, $innerSql, $sql, $orderByClause)
{
// For every order by, find out the SQL alias by inspecting the ResultSetMapping.
$sqlOrderColumns = array();
$orderBy = array();
if (isset($AST->orderByClause)) {
foreach ($AST->orderByClause->orderByItems as $item) {
$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($expression) || $this->rsm->columnOwnerMap[$alias] == $expression->identificationVariable) {
$sqlOrderColumns[] = $alias;
$orderBy[] = $alias . ' ' . $item->type;
break;
}
}
}
// remove identifier aliases
$sqlOrderColumns = array_diff($sqlOrderColumns, $sqlIdentifier);
// If the sql statement has an order by clause, we need to wrap it in a new select distinct
// statement
if (!$orderByClause instanceof OrderByClause) {
return $sql;
}
if (count($orderBy)) {
$sql = sprintf(
'SELECT DISTINCT %s FROM (%s) dctrn_result ORDER BY %s',
implode(', ', array_merge($sqlIdentifier, $sqlOrderColumns)),
$innerSql,
implode(', ', $orderBy)
);
}
// Rebuild the order by clause to work in the scope of the new select statement
/* @var array $sqlOrderColumns an array of items that need to be included in the select list */
/* @var array $orderBy an array of rebuilt order by items */
list($sqlOrderColumns, $orderBy) = $this->rebuildOrderByClauseForOuterScope($orderByClause);
// Identifiers are always included in the select list, so there's no need to include them twice
$sqlOrderColumns = array_diff($sqlOrderColumns, $sqlIdentifier);
// Build the select distinct statement
$sql = sprintf(
'SELECT DISTINCT %s FROM (%s) dctrn_result ORDER BY %s',
implode(', ', array_merge($sqlIdentifier, $sqlOrderColumns)),
$innerSql,
implode(', ', $orderBy)
);
return $sql;
}
/**
* Generates a new order by clause that works in the scope of a select query wrapping the original
*
* @param OrderByClause $orderByClause
* @return array
*/
private function rebuildOrderByClauseForOuterScope(OrderByClause $orderByClause) {
$dqlAliasToSqlTableAliasMap
= $searchPatterns
= $replacements
= $dqlAliasToClassMap
= $selectListAdditions
= $orderByItems
= [];
// Generate DQL alias -> SQL table alias mapping
foreach(array_keys($this->rsm->aliasMap) as $dqlAlias) {
$dqlAliasToClassMap[$dqlAlias] = $class = $this->queryComponents[$dqlAlias]['metadata'];
$dqlAliasToSqlTableAliasMap[$dqlAlias] = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
}
// Pattern to find table path expressions in the order by clause
$fieldSearchPattern = '/(?<![a-z0-9_])%s\.%s(?![a-z0-9_])/i';
// Generate search patterns for each field's path expression in the order by clause
foreach($this->rsm->fieldMappings as $fieldAlias => $columnName) {
$dqlAliasForFieldAlias = $this->rsm->columnOwnerMap[$fieldAlias];
$columnName = $this->quoteStrategy->getColumnName(
$columnName,
$dqlAliasToClassMap[$dqlAliasForFieldAlias],
$this->em->getConnection()->getDatabasePlatform()
);
$sqlTableAliasForFieldAlias = $dqlAliasToSqlTableAliasMap[$dqlAliasForFieldAlias];
$searchPatterns[] = sprintf($fieldSearchPattern, $sqlTableAliasForFieldAlias, $columnName);
$replacements[] = $fieldAlias;
}
$complexAddedOrderByAliases = 0;
foreach($orderByClause->orderByItems as $orderByItem) {
// Walk order by item to get string representation of it
$orderByItemString = $this->walkOrderByItem($orderByItem);
// Replace path expressions in the order by clause with their column alias
$orderByItemString = preg_replace($searchPatterns, $replacements, $orderByItemString);
// The order by items are not required to be in the select list on Oracle and PostgreSQL, but
// for the sake of simplicity, order by items will be included in the select list on all platforms.
// This doesn't impact functionality.
$selectListAddition = trim(preg_replace('/([^ ]+) (?:asc|desc)/i', '$1', $orderByItemString));
// If the expression is an arithmetic expression, we need to create an alias for it.
if ($orderByItem->expression instanceof ArithmeticTerm) {
$orderByAlias = "ordr_" . $complexAddedOrderByAliases++;
$orderByItemString = $orderByAlias . " " . $orderByItem->type;
$selectListAddition .= " AS $orderByAlias";
}
$selectListAdditions[] = $selectListAddition;
$orderByItems[] = $orderByItemString;
}
return array($selectListAdditions, $orderByItems);
}
/**
* {@inheritdoc}
*/
public function walkPathExpression($pathExpr)
{
if (!in_array($pathExpr, $this->orderByPathExpressions)) {
$this->orderByPathExpressions[] = $pathExpr;
}
return parent::walkPathExpression($pathExpr);
}
/**
* getter for $orderByPathExpressions
*
* @return array
*/
public function getOrderByPathExpressions()
{
return $this->orderByPathExpressions;
}
}

View File

@ -5,6 +5,7 @@ namespace Doctrine\Tests\ORM\Tools\Pagination;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\ORM\Query;
class LimitSubqueryOutputWalkerTest extends PaginationTestCase
@ -33,7 +34,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
$limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
$this->assertEquals(
"SELECT DISTINCT id_0, title_1 FROM (SELECT m0_.id AS id_0, m0_.title AS title_1, c1_.id AS id_2, a2_.id AS id_3, a2_.name AS name_4, m0_.author_id AS author_id_5, m0_.category_id AS category_id_6 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 title_1 ASC", $limitQuery->getSql()
"SELECT DISTINCT id_0, title_1 FROM (SELECT m0_.id AS id_0, m0_.title AS title_1, c1_.id AS id_2, a2_.id AS id_3, a2_.name AS name_4, m0_.author_id AS author_id_5, m0_.category_id AS category_id_6 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 ORDER BY title_1 ASC", $limitQuery->getSql()
);
$this->entityManager->getConnection()->setDatabasePlatform($odp);
@ -51,7 +52,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
$limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
$this->assertEquals(
"SELECT DISTINCT id_1, sclr_0 FROM (SELECT COUNT(g0_.id) AS sclr_0, u1_.id AS id_1, g0_.id AS id_2 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 sclr_0 ASC) dctrn_result ORDER BY sclr_0 ASC",
"SELECT DISTINCT id_1, sclr_0 FROM (SELECT COUNT(g0_.id) AS sclr_0, u1_.id AS id_1, g0_.id AS id_2 FROM User u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id) dctrn_result ORDER BY sclr_0 ASC",
$limitQuery->getSql()
);
@ -70,7 +71,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
$limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
$this->assertEquals(
"SELECT DISTINCT id_1, sclr_0 FROM (SELECT COUNT(g0_.id) AS sclr_0, u1_.id AS id_1, g0_.id AS id_2 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 sclr_0 ASC, u1_.id DESC) dctrn_result ORDER BY sclr_0 ASC, id_1 DESC",
"SELECT DISTINCT id_1, sclr_0 FROM (SELECT COUNT(g0_.id) AS sclr_0, u1_.id AS id_1, g0_.id AS id_2 FROM User u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id) dctrn_result ORDER BY sclr_0 ASC, id_1 DESC",
$limitQuery->getSql()
);
@ -89,7 +90,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
$limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
$this->assertEquals(
"SELECT DISTINCT id_1, sclr_0 FROM (SELECT COUNT(g0_.id) AS sclr_0, u1_.id AS id_1, g0_.id AS id_2 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 sclr_0 ASC, u1_.id DESC) dctrn_result ORDER BY sclr_0 ASC, id_1 DESC",
"SELECT DISTINCT id_1, sclr_0 FROM (SELECT COUNT(g0_.id) AS sclr_0, u1_.id AS id_1, g0_.id AS id_2 FROM User u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id) dctrn_result ORDER BY sclr_0 ASC, id_1 DESC",
$limitQuery->getSql()
);
@ -118,7 +119,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
$limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
$this->assertEquals(
"SELECT DISTINCT ID_0, TITLE_1 FROM (SELECT m0_.id AS ID_0, m0_.title AS TITLE_1, c1_.id AS ID_2, a2_.id AS ID_3, a2_.name AS NAME_4, m0_.author_id AS AUTHOR_ID_5, m0_.category_id AS CATEGORY_ID_6 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 TITLE_1 ASC", $limitQuery->getSql()
"SELECT DISTINCT ID_0, TITLE_1 FROM (SELECT m0_.id AS ID_0, m0_.title AS TITLE_1, c1_.id AS ID_2, a2_.id AS ID_3, a2_.name AS NAME_4, m0_.author_id AS AUTHOR_ID_5, m0_.category_id AS CATEGORY_ID_6 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 ORDER BY TITLE_1 ASC", $limitQuery->getSql()
);
$this->entityManager->getConnection()->setDatabasePlatform($odp);
@ -137,7 +138,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
$limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
$this->assertEquals(
"SELECT DISTINCT ID_1, SCLR_0 FROM (SELECT COUNT(g0_.id) AS SCLR_0, u1_.id AS ID_1, g0_.id AS ID_2 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 SCLR_0 ASC) dctrn_result ORDER BY SCLR_0 ASC",
"SELECT DISTINCT ID_1, SCLR_0 FROM (SELECT COUNT(g0_.id) AS SCLR_0, u1_.id AS ID_1, g0_.id AS ID_2 FROM User u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id) dctrn_result ORDER BY SCLR_0 ASC",
$limitQuery->getSql()
);
@ -157,7 +158,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
$limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
$this->assertEquals(
"SELECT DISTINCT ID_1, SCLR_0 FROM (SELECT COUNT(g0_.id) AS SCLR_0, u1_.id AS ID_1, g0_.id AS ID_2 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 SCLR_0 ASC, u1_.id DESC) dctrn_result ORDER BY SCLR_0 ASC, ID_1 DESC",
"SELECT DISTINCT ID_1, SCLR_0 FROM (SELECT COUNT(g0_.id) AS SCLR_0, u1_.id AS ID_1, g0_.id AS ID_2 FROM User u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id) dctrn_result ORDER BY SCLR_0 ASC, ID_1 DESC",
$limitQuery->getSql()
);
@ -207,7 +208,67 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
$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',
'SELECT DISTINCT id_0, (1 - 1000) * 1 AS ordr_0 FROM (SELECT a0_.id AS id_0, a0_.name AS name_1 FROM Author a0_) dctrn_result ORDER BY ordr_0 DESC',
$query->getSQL()
);
}
public function testCountQueryWithComplexScalarOrderByItem()
{
$query = $this->entityManager->createQuery(
'SELECT a FROM Doctrine\Tests\ORM\Tools\Pagination\Avatar a ORDER BY a.image_height * a.image_width 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, image_height_2 * image_width_3 AS ordr_0 FROM (SELECT a0_.id AS id_0, a0_.image AS image_1, a0_.image_height AS image_height_2, a0_.image_width AS image_width_3, a0_.image_alt_desc AS image_alt_desc_4, a0_.user_id AS user_id_5 FROM Avatar a0_) dctrn_result ORDER BY ordr_0 DESC',
$query->getSQL()
);
}
public function testCountQueryWithComplexScalarOrderByItemJoined()
{
$query = $this->entityManager->createQuery(
'SELECT u FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.avatar a ORDER BY a.image_height * a.image_width 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, image_height_1 * image_width_2 AS ordr_0 FROM (SELECT u0_.id AS id_0, a1_.image_height AS image_height_1, a1_.image_width AS image_width_2, a1_.user_id AS user_id_3 FROM User u0_ INNER JOIN Avatar a1_ ON u0_.id = a1_.user_id) dctrn_result ORDER BY ordr_0 DESC',
$query->getSQL()
);
}
public function testCountQueryWithComplexScalarOrderByItemJoinedWithPartial()
{
$query = $this->entityManager->createQuery(
'SELECT u, partial a.{id, image_alt_desc} FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.avatar a ORDER BY a.image_height * a.image_width 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, image_height_3 * image_width_4 AS ordr_0 FROM (SELECT u0_.id AS id_0, a1_.id AS id_1, a1_.image_alt_desc AS image_alt_desc_2, a1_.image_height AS image_height_3, a1_.image_width AS image_width_4, a1_.user_id AS user_id_5 FROM User u0_ INNER JOIN Avatar a1_ ON u0_.id = a1_.user_id) dctrn_result ORDER BY ordr_0 DESC',
$query->getSQL()
);
}
public function testCountQueryWithComplexScalarOrderByItemOracle()
{
$query = $this->entityManager->createQuery(
'SELECT a FROM Doctrine\Tests\ORM\Tools\Pagination\Avatar a ORDER BY a.image_height * a.image_width DESC'
);
$this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform());
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
$this->assertSame(
'SELECT DISTINCT ID_0, IMAGE_HEIGHT_2 * IMAGE_WIDTH_3 AS ordr_0 FROM (SELECT a0_.id AS ID_0, a0_.image AS IMAGE_1, a0_.image_height AS IMAGE_HEIGHT_2, a0_.image_width AS IMAGE_WIDTH_3, a0_.image_alt_desc AS IMAGE_ALT_DESC_4, a0_.user_id AS USER_ID_5 FROM Avatar a0_) dctrn_result ORDER BY ordr_0 DESC',
$query->getSQL()
);
}
@ -224,11 +285,26 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
$this->assertEquals(
'SELECT DISTINCT id_0, name_2 FROM (SELECT a0_.id AS id_0, a0_.name AS name_1, a0_.name AS name_2 FROM Author a0_ ORDER BY name_2 DESC) dctrn_result ORDER BY name_2 DESC',
'SELECT DISTINCT id_0, name_2 FROM (SELECT a0_.id AS id_0, a0_.name AS name_1, a0_.name AS name_2 FROM Author a0_) dctrn_result ORDER BY name_2 DESC',
$query->getSql()
);
}
public function testLimitSubqueryWithColumnWithSortDirectionInName()
{
$query = $this->entityManager->createQuery(
'SELECT a FROM Doctrine\Tests\ORM\Tools\Pagination\Avatar a ORDER BY a.image_alt_desc 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, image_alt_desc_4 FROM (SELECT a0_.id AS id_0, a0_.image AS image_1, a0_.image_height AS image_height_2, a0_.image_width AS image_width_3, a0_.image_alt_desc AS image_alt_desc_4, a0_.user_id AS user_id_5 FROM Avatar a0_) dctrn_result ORDER BY image_alt_desc_4 DESC',
$query->getSQL()
);
}
public function testLimitSubqueryWithOrderByInnerJoined()
{
$query = $this->entityManager->createQuery(
@ -238,7 +314,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
$this->assertEquals(
'SELECT DISTINCT id_0 FROM (SELECT b0_.id AS id_0, b0_.author_id AS author_id_1, b0_.category_id AS category_id_2 FROM BlogPost b0_ INNER JOIN Author a1_ ON b0_.author_id = a1_.id ORDER BY a1_.name ASC) dctrn_result',
'SELECT DISTINCT id_0, name_1 FROM (SELECT b0_.id AS id_0, a1_.name AS name_1, b0_.author_id AS author_id_2, b0_.category_id AS category_id_3 FROM BlogPost b0_ INNER JOIN Author a1_ ON b0_.author_id = a1_.id) dctrn_result ORDER BY name_1 ASC',
$query->getSQL()
);
}

View File

@ -24,7 +24,7 @@ abstract class PaginationTestCase extends OrmTestCase
class MyBlogPost
{
/** @Id @column(type="integer") @generatedValue */
/** @Id @Column(type="integer") @GeneratedValue */
public $id;
/**
* @ManyToOne(targetEntity="Author")
@ -34,7 +34,7 @@ class MyBlogPost
* @ManyToOne(targetEntity="Category")
*/
public $category;
/** @column(type="string") */
/** @Column(type="string") */
public $title;
}
@ -44,7 +44,7 @@ class MyBlogPost
class MyAuthor
{
/** @Id @column(type="integer") @generatedValue */
/** @Id @Column(type="integer") @GeneratedValue */
public $id;
}
@ -55,7 +55,7 @@ class MyAuthor
class MyCategory
{
/** @id @column(type="integer") @generatedValue */
/** @Id @Column(type="integer") @GeneratedValue */
public $id;
}
@ -67,7 +67,7 @@ class MyCategory
class BlogPost
{
/** @Id @column(type="integer") @generatedValue */
/** @Id @Column(type="integer") @GeneratedValue */
public $id;
/**
* @ManyToOne(targetEntity="Author")
@ -85,7 +85,7 @@ class BlogPost
class Author
{
/** @Id @column(type="integer") @generatedValue */
/** @Id @Column(type="integer") @GeneratedValue */
public $id;
/** @Column(type="string") */
public $name;
@ -98,7 +98,7 @@ class Author
class Person
{
/** @Id @column(type="integer") @generatedValue */
/** @Id @Column(type="integer") @GeneratedValue */
public $id;
/** @Column(type="string") */
public $name;
@ -113,7 +113,7 @@ class Person
class Category
{
/** @id @column(type="integer") @generatedValue */
/** @Id @Column(type="integer") @GeneratedValue */
public $id;
}
@ -123,7 +123,7 @@ class Category
class Group
{
/** @Id @column(type="integer") @generatedValue */
/** @Id @Column(type="integer") @GeneratedValue */
public $id;
/** @ManyToMany(targetEntity="User", mappedBy="groups") */
public $users;
@ -133,7 +133,7 @@ class Group
class User
{
/** @Id @column(type="integer") @generatedValue */
/** @Id @Column(type="integer") @GeneratedValue */
public $id;
/**
* @ManyToMany(targetEntity="Group", inversedBy="users")
@ -144,4 +144,28 @@ class User
* )
*/
public $groups;
/**
* @OneToOne(targetEntity="Avatar", mappedBy="user")
*/
public $avatar;
}
/** @Entity */
class Avatar
{
/** @Id @Column(type="integer") @GeneratedValue */
public $id;
/**
* @OneToOne(targetEntity="User", inversedBy="avatar")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
public $user;
/** @Column(type="string", length=255) */
public $image;
/** @Column(type="integer") */
public $image_height;
/** @Column(type="integer") */
public $image_width;
/** @Column(type="string", length=255) */
public $image_alt_desc;
}