Merge branch 'hotfix/#1353-#1347-#1351-fix-paginator-sorting-and-fetch-joining'
Close #1353
This commit is contained in:
commit
82230cc427
@ -13,13 +13,23 @@
|
||||
|
||||
namespace Doctrine\ORM\Tools\Pagination;
|
||||
|
||||
use Doctrine\DBAL\Platforms\DB2Platform;
|
||||
use Doctrine\DBAL\Platforms\OraclePlatform;
|
||||
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
|
||||
use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
|
||||
use Doctrine\DBAL\Platforms\SQLServerPlatform;
|
||||
use Doctrine\ORM\Query\AST\ArithmeticExpression;
|
||||
use Doctrine\ORM\Query\AST\ArithmeticFactor;
|
||||
use Doctrine\ORM\Query\AST\ArithmeticTerm;
|
||||
use Doctrine\ORM\Query\AST\Literal;
|
||||
use Doctrine\ORM\Query\AST\OrderByClause;
|
||||
use Doctrine\ORM\Query\AST\OrderByItem;
|
||||
use Doctrine\ORM\Query\AST\PartialObjectExpression;
|
||||
use Doctrine\ORM\Query\AST\PathExpression;
|
||||
use Doctrine\ORM\Query\AST\SelectExpression;
|
||||
use Doctrine\ORM\Query\AST\SimpleArithmeticExpression;
|
||||
use Doctrine\ORM\Query\Expr\OrderBy;
|
||||
use Doctrine\ORM\Query\Expr\Select;
|
||||
use Doctrine\ORM\Query\SqlWalker;
|
||||
use Doctrine\ORM\Query\AST\SelectStatement;
|
||||
|
||||
@ -66,11 +76,6 @@ class LimitSubqueryOutputWalker extends SqlWalker
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $orderByPathExpressions = [];
|
||||
|
||||
/**
|
||||
* The quote strategy.
|
||||
*
|
||||
@ -78,6 +83,17 @@ class LimitSubqueryOutputWalker extends SqlWalker
|
||||
*/
|
||||
private $quoteStrategy;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $orderByPathExpressions = [];
|
||||
|
||||
/**
|
||||
* @var bool We don't want to add path expressions from sub-selects into the select clause of the containing query.
|
||||
* This state flag simply keeps track on whether we are walking on a subquery or not
|
||||
*/
|
||||
private $inSubSelect = false;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
@ -106,17 +122,138 @@ class LimitSubqueryOutputWalker extends SqlWalker
|
||||
parent::__construct($query, $parserResult, $queryComponents);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the platform supports the ROW_NUMBER window function.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function platformSupportsRowNumber()
|
||||
{
|
||||
return $this->platform instanceof PostgreSqlPlatform
|
||||
|| $this->platform instanceof SQLServerPlatform
|
||||
|| $this->platform instanceof OraclePlatform
|
||||
|| $this->platform instanceof SQLAnywherePlatform
|
||||
|| $this->platform instanceof DB2Platform
|
||||
|| (method_exists($this->platform, 'supportsRowNumberFunction')
|
||||
&& $this->platform->supportsRowNumberFunction());
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuilds a select statement's order by clause for use in a
|
||||
* ROW_NUMBER() OVER() expression.
|
||||
*
|
||||
* @param SelectStatement $AST
|
||||
*/
|
||||
private function rebuildOrderByForRowNumber(SelectStatement $AST)
|
||||
{
|
||||
$orderByClause = $AST->orderByClause;
|
||||
$selectAliasToExpressionMap = [];
|
||||
// Get any aliases that are available for select expressions.
|
||||
foreach ($AST->selectClause->selectExpressions as $selectExpression) {
|
||||
$selectAliasToExpressionMap[$selectExpression->fieldIdentificationVariable] = $selectExpression->expression;
|
||||
}
|
||||
|
||||
// Rebuild string orderby expressions to use the select expression they're referencing
|
||||
foreach ($orderByClause->orderByItems as $orderByItem) {
|
||||
if (is_string($orderByItem->expression) && isset($selectAliasToExpressionMap[$orderByItem->expression])) {
|
||||
$orderByItem->expression = $selectAliasToExpressionMap[$orderByItem->expression];
|
||||
}
|
||||
}
|
||||
$func = new RowNumberOverFunction('dctrn_rownum');
|
||||
$func->orderByClause = $AST->orderByClause;
|
||||
$AST->selectClause->selectExpressions[] = new SelectExpression($func, 'dctrn_rownum', true);
|
||||
|
||||
// No need for an order by clause, we'll order by rownum in the outer query.
|
||||
$AST->orderByClause = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks down a SelectStatement AST node, wrapping it in a SELECT DISTINCT.
|
||||
*
|
||||
* @param SelectStatement $AST
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function walkSelectStatement(SelectStatement $AST)
|
||||
{
|
||||
if ($this->platformSupportsRowNumber()) {
|
||||
return $this->walkSelectStatementWithRowNumber($AST);
|
||||
}
|
||||
return $this->walkSelectStatementWithoutRowNumber($AST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks down a SelectStatement AST node, wrapping it in a SELECT DISTINCT.
|
||||
* This method is for use with platforms which support ROW_NUMBER.
|
||||
*
|
||||
* @param SelectStatement $AST
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function walkSelectStatementWithRowNumber(SelectStatement $AST)
|
||||
{
|
||||
$hasOrderBy = false;
|
||||
$outerOrderBy = ' ORDER BY dctrn_minrownum ASC';
|
||||
$orderGroupBy = '';
|
||||
if ($AST->orderByClause instanceof OrderByClause) {
|
||||
$hasOrderBy = true;
|
||||
$this->rebuildOrderByForRowNumber($AST);
|
||||
}
|
||||
|
||||
$innerSql = $this->getInnerSQL($AST);
|
||||
|
||||
$sqlIdentifier = $this->getSQLIdentifier($AST);
|
||||
|
||||
if ($hasOrderBy) {
|
||||
$orderGroupBy = ' GROUP BY ' . implode(', ', $sqlIdentifier);
|
||||
$sqlIdentifier[] = 'MIN(' . $this->walkResultVariable('dctrn_rownum') . ') AS dctrn_minrownum';
|
||||
}
|
||||
|
||||
// Build the counter query
|
||||
$sql = sprintf(
|
||||
'SELECT DISTINCT %s FROM (%s) dctrn_result',
|
||||
implode(', ', $sqlIdentifier),
|
||||
$innerSql
|
||||
);
|
||||
|
||||
if ($hasOrderBy) {
|
||||
$sql .= $orderGroupBy . $outerOrderBy;
|
||||
}
|
||||
|
||||
// Apply the limit and offset.
|
||||
$sql = $this->platform->modifyLimitQuery(
|
||||
$sql,
|
||||
$this->maxResults,
|
||||
$this->firstResult
|
||||
);
|
||||
|
||||
// Add the columns to the ResultSetMapping. It's not really nice but
|
||||
// it works. Preferably I'd clear the RSM or simply create a new one
|
||||
// but that is not possible from inside the output walker, so we dirty
|
||||
// up the one we have.
|
||||
foreach ($sqlIdentifier as $property => $alias) {
|
||||
$this->rsm->addScalarResult($alias, $property);
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks down a SelectStatement AST node, wrapping it in a SELECT DISTINCT.
|
||||
* This method is for platforms which DO NOT support ROW_NUMBER.
|
||||
*
|
||||
* @param SelectStatement $AST
|
||||
* @param bool $addMissingItemsFromOrderByToSelect
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function walkSelectStatement(SelectStatement $AST, $addMissingItemsFromOrderByToSelect = true)
|
||||
public function walkSelectStatementWithoutRowNumber(SelectStatement $AST, $addMissingItemsFromOrderByToSelect = true)
|
||||
{
|
||||
// We don't want to call this recursively!
|
||||
if ($AST->orderByClause instanceof OrderByClause && $addMissingItemsFromOrderByToSelect) {
|
||||
@ -131,74 +268,9 @@ class LimitSubqueryOutputWalker extends SqlWalker
|
||||
$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
|
||||
$hiddens = array();
|
||||
$innerSql = $this->getInnerSQL($AST);
|
||||
|
||||
foreach ($AST->selectClause->selectExpressions as $idx => $expr) {
|
||||
$hiddens[$idx] = $expr->hiddenAliasResultVariable;
|
||||
$expr->hiddenAliasResultVariable = false;
|
||||
}
|
||||
|
||||
$innerSql = parent::walkSelectStatement($AST);
|
||||
|
||||
// Restore orderByClause
|
||||
$AST->orderByClause = $orderByClause;
|
||||
|
||||
// Restore hiddens
|
||||
foreach ($AST->selectClause->selectExpressions as $idx => $expr) {
|
||||
$expr->hiddenAliasResultVariable = $hiddens[$idx];
|
||||
}
|
||||
|
||||
// Find out the SQL alias of the identifier column of the root entity.
|
||||
// It may be possible to make this work with multiple root entities but that
|
||||
// would probably require issuing multiple queries or doing a UNION SELECT.
|
||||
// So for now, it's not supported.
|
||||
|
||||
// Get the root entity and alias from the AST fromClause.
|
||||
$from = $AST->fromClause->identificationVariableDeclarations;
|
||||
if (count($from) !== 1) {
|
||||
throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction");
|
||||
}
|
||||
|
||||
$fromRoot = reset($from);
|
||||
$rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable;
|
||||
$rootClass = $this->queryComponents[$rootAlias]['metadata'];
|
||||
$rootIdentifier = $rootClass->identifier;
|
||||
|
||||
// For every identifier, find out the SQL alias by combing through the ResultSetMapping
|
||||
$sqlIdentifier = array();
|
||||
foreach ($rootIdentifier as $property) {
|
||||
if (isset($rootClass->fieldMappings[$property])) {
|
||||
foreach (array_keys($this->rsm->fieldMappings, $property) as $alias) {
|
||||
if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) {
|
||||
$sqlIdentifier[$property] = $alias;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($rootClass->associationMappings[$property])) {
|
||||
$joinColumn = $rootClass->associationMappings[$property]['joinColumns'][0]['name'];
|
||||
|
||||
foreach (array_keys($this->rsm->metaMappings, $joinColumn) as $alias) {
|
||||
if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) {
|
||||
$sqlIdentifier[$property] = $alias;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count($sqlIdentifier) === 0) {
|
||||
throw new \RuntimeException('The Paginator does not support Queries which only yield ScalarResults.');
|
||||
}
|
||||
|
||||
if (count($rootIdentifier) != count($sqlIdentifier)) {
|
||||
throw new \RuntimeException(sprintf(
|
||||
'Not all identifier properties can be found in the ResultSetMapping: %s',
|
||||
implode(', ', array_diff($rootIdentifier, array_keys($sqlIdentifier)))
|
||||
));
|
||||
}
|
||||
$sqlIdentifier = $this->getSQLIdentifier($AST);
|
||||
|
||||
// Build the counter query
|
||||
$sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result',
|
||||
@ -220,6 +292,9 @@ class LimitSubqueryOutputWalker extends SqlWalker
|
||||
$this->rsm->addScalarResult($alias, $property);
|
||||
}
|
||||
|
||||
// Restore orderByClause
|
||||
$AST->orderByClause = $orderByClause;
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
@ -241,7 +316,7 @@ class LimitSubqueryOutputWalker extends SqlWalker
|
||||
// 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);
|
||||
$walker->walkSelectStatementWithoutRowNumber($AST, false);
|
||||
$orderByPathExpressions = $walker->getOrderByPathExpressions();
|
||||
|
||||
// Get a map of referenced identifiers to field names.
|
||||
@ -298,17 +373,13 @@ class LimitSubqueryOutputWalker extends SqlWalker
|
||||
}
|
||||
|
||||
// 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);
|
||||
$orderBy = $this->rebuildOrderByClauseForOuterScope($orderByClause);
|
||||
|
||||
// Build the select distinct statement
|
||||
$sql = sprintf(
|
||||
'SELECT DISTINCT %s FROM (%s) dctrn_result ORDER BY %s',
|
||||
implode(', ', array_merge($sqlIdentifier, $sqlOrderColumns)),
|
||||
implode(', ', $sqlIdentifier),
|
||||
$innerSql,
|
||||
implode(', ', $orderBy)
|
||||
);
|
||||
@ -342,21 +413,39 @@ class LimitSubqueryOutputWalker extends SqlWalker
|
||||
$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) {
|
||||
foreach($this->rsm->fieldMappings as $fieldAlias => $fieldName) {
|
||||
$dqlAliasForFieldAlias = $this->rsm->columnOwnerMap[$fieldAlias];
|
||||
$class = $dqlAliasToClassMap[$dqlAliasForFieldAlias];
|
||||
|
||||
// If the field is from a joined child table, we won't be ordering
|
||||
// on it.
|
||||
if (!isset($class->fieldMappings[$fieldName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fieldMapping = $class->fieldMappings[$fieldName];
|
||||
|
||||
// Get the proper column name as will appear in the select list
|
||||
$columnName = $this->quoteStrategy->getColumnName(
|
||||
$columnName,
|
||||
$fieldName,
|
||||
$dqlAliasToClassMap[$dqlAliasForFieldAlias],
|
||||
$this->em->getConnection()->getDatabasePlatform()
|
||||
);
|
||||
|
||||
// Get the SQL table alias for the entity and field
|
||||
$sqlTableAliasForFieldAlias = $dqlAliasToSqlTableAliasMap[$dqlAliasForFieldAlias];
|
||||
if (isset($fieldMapping['declared']) && $fieldMapping['declared'] !== $class->name) {
|
||||
// Field was declared in a parent class, so we need to get the proper SQL table alias
|
||||
// for the joined parent table.
|
||||
$otherClassMetadata = $this->em->getClassMetadata($fieldMapping['declared']);
|
||||
$sqlTableAliasForFieldAlias = $this->getSQLTableAlias($otherClassMetadata->getTableName(), $dqlAliasForFieldAlias);
|
||||
}
|
||||
|
||||
// Compose search/replace patterns
|
||||
$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);
|
||||
@ -364,34 +453,10 @@ class LimitSubqueryOutputWalker extends SqlWalker
|
||||
// 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);
|
||||
return $orderByItems;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -403,4 +468,119 @@ class LimitSubqueryOutputWalker extends SqlWalker
|
||||
{
|
||||
return $this->orderByPathExpressions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SelectStatement $AST
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
* @throws \Doctrine\ORM\Query\QueryException
|
||||
*/
|
||||
private function getInnerSQL(SelectStatement $AST)
|
||||
{
|
||||
// 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
|
||||
$hiddens = [];
|
||||
|
||||
foreach ($AST->selectClause->selectExpressions as $idx => $expr) {
|
||||
$hiddens[$idx] = $expr->hiddenAliasResultVariable;
|
||||
$expr->hiddenAliasResultVariable = false;
|
||||
}
|
||||
|
||||
$innerSql = parent::walkSelectStatement($AST);
|
||||
|
||||
// Restore hiddens
|
||||
foreach ($AST->selectClause->selectExpressions as $idx => $expr) {
|
||||
$expr->hiddenAliasResultVariable = $hiddens[$idx];
|
||||
}
|
||||
|
||||
return $innerSql;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SelectStatement $AST
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getSQLIdentifier(SelectStatement $AST)
|
||||
{
|
||||
// Find out the SQL alias of the identifier column of the root entity.
|
||||
// It may be possible to make this work with multiple root entities but that
|
||||
// would probably require issuing multiple queries or doing a UNION SELECT.
|
||||
// So for now, it's not supported.
|
||||
|
||||
// Get the root entity and alias from the AST fromClause.
|
||||
$from = $AST->fromClause->identificationVariableDeclarations;
|
||||
if (count($from) !== 1) {
|
||||
throw new \RuntimeException('Cannot count query which selects two FROM components, cannot make distinction');
|
||||
}
|
||||
|
||||
$fromRoot = reset($from);
|
||||
$rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable;
|
||||
$rootClass = $this->queryComponents[$rootAlias]['metadata'];
|
||||
$rootIdentifier = $rootClass->identifier;
|
||||
|
||||
// For every identifier, find out the SQL alias by combing through the ResultSetMapping
|
||||
$sqlIdentifier = [];
|
||||
foreach ($rootIdentifier as $property) {
|
||||
if (isset($rootClass->fieldMappings[$property])) {
|
||||
foreach (array_keys($this->rsm->fieldMappings, $property) as $alias) {
|
||||
if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) {
|
||||
$sqlIdentifier[$property] = $alias;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($rootClass->associationMappings[$property])) {
|
||||
$joinColumn = $rootClass->associationMappings[$property]['joinColumns'][0]['name'];
|
||||
|
||||
foreach (array_keys($this->rsm->metaMappings, $joinColumn) as $alias) {
|
||||
if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) {
|
||||
$sqlIdentifier[$property] = $alias;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count($sqlIdentifier) === 0) {
|
||||
throw new \RuntimeException('The Paginator does not support Queries which only yield ScalarResults.');
|
||||
}
|
||||
|
||||
if (count($rootIdentifier) != count($sqlIdentifier)) {
|
||||
throw new \RuntimeException(sprintf(
|
||||
'Not all identifier properties can be found in the ResultSetMapping: %s',
|
||||
implode(', ', array_diff($rootIdentifier, array_keys($sqlIdentifier)))
|
||||
));
|
||||
}
|
||||
|
||||
return $sqlIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function walkPathExpression($pathExpr)
|
||||
{
|
||||
if (!$this->inSubSelect && !$this->platformSupportsRowNumber() && !in_array($pathExpr, $this->orderByPathExpressions)) {
|
||||
$this->orderByPathExpressions[] = $pathExpr;
|
||||
}
|
||||
|
||||
return parent::walkPathExpression($pathExpr);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function walkSubSelect($subselect)
|
||||
{
|
||||
$this->inSubSelect = true;
|
||||
|
||||
$sql = parent::walkSubselect($subselect);
|
||||
|
||||
$this->inSubSelect = false;
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,9 @@
|
||||
namespace Doctrine\ORM\Tools\Pagination;
|
||||
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
||||
use Doctrine\ORM\ORMException;
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\Query\TreeWalkerAdapter;
|
||||
use Doctrine\ORM\Query\AST\Functions\IdentityFunction;
|
||||
use Doctrine\ORM\Query\AST\PathExpression;
|
||||
@ -68,6 +71,8 @@ class LimitSubqueryWalker extends TreeWalkerAdapter
|
||||
$rootClass = $queryComponents[$rootAlias]['metadata'];
|
||||
$selectExpressions = array();
|
||||
|
||||
$this->validate($AST);
|
||||
|
||||
foreach ($queryComponents as $dqlAlias => $qComp) {
|
||||
// Preserve mixed data in query for ordering.
|
||||
if (isset($qComp['resultVariable'])) {
|
||||
@ -112,6 +117,41 @@ class LimitSubqueryWalker extends TreeWalkerAdapter
|
||||
|
||||
$AST->selectClause->isDistinct = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the AST to ensure that this walker is able to properly manipulate it.
|
||||
*
|
||||
* @param SelectStatement $AST
|
||||
*/
|
||||
private function validate(SelectStatement $AST)
|
||||
{
|
||||
// Prevent LimitSubqueryWalker from being used with queries that include
|
||||
// a limit, a fetched to-many join, and an order by condition that
|
||||
// references a column from the fetch joined table.
|
||||
$queryComponents = $this->getQueryComponents();
|
||||
$query = $this->_getQuery();
|
||||
$from = $AST->fromClause->identificationVariableDeclarations;
|
||||
$fromRoot = reset($from);
|
||||
|
||||
if ($query instanceof Query
|
||||
&& $query->getMaxResults()
|
||||
&& $AST->orderByClause
|
||||
&& count($fromRoot->joins)) {
|
||||
// Check each orderby item.
|
||||
// TODO: check complex orderby items too...
|
||||
foreach ($AST->orderByClause->orderByItems as $orderByItem) {
|
||||
$expression = $orderByItem->expression;
|
||||
if ($orderByItem->expression instanceof PathExpression
|
||||
&& isset($queryComponents[$expression->identificationVariable])) {
|
||||
$queryComponent = $queryComponents[$expression->identificationVariable];
|
||||
if (isset($queryComponent['parent'])
|
||||
&& $queryComponent['relation']['type'] & ClassMetadataInfo::TO_MANY) {
|
||||
throw new \RuntimeException("Cannot select distinct identifiers from query with LIMIT and ORDER BY on a column from a fetch joined to-many association. Use output walkers.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve either an IdentityFunction (IDENTITY(u.assoc)) or a state field (u.name).
|
||||
|
61
lib/Doctrine/ORM/Tools/Pagination/RowNumberOverFunction.php
Normal file
61
lib/Doctrine/ORM/Tools/Pagination/RowNumberOverFunction.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\ORM\Tools\Pagination;
|
||||
|
||||
use Doctrine\ORM\ORMException;
|
||||
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
|
||||
|
||||
|
||||
/**
|
||||
* RowNumberOverFunction
|
||||
*
|
||||
* Provides ROW_NUMBER() OVER(ORDER BY...) construct for use in LimitSubqueryOutputWalker
|
||||
*
|
||||
* @since 2.5
|
||||
* @author Bill Schaller <bill@zeroedin.com>
|
||||
*/
|
||||
class RowNumberOverFunction extends FunctionNode
|
||||
{
|
||||
/**
|
||||
* @var \Doctrine\ORM\Query\AST\OrderByClause
|
||||
*/
|
||||
public $orderByClause;
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
|
||||
{
|
||||
return 'ROW_NUMBER() OVER(' . trim($sqlWalker->walkOrderByClause(
|
||||
$this->orderByClause
|
||||
)) . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*
|
||||
* @throws ORMException
|
||||
*/
|
||||
public function parse(\Doctrine\ORM\Query\Parser $parser)
|
||||
{
|
||||
throw new ORMException("The RowNumberOverFunction is not intended for, nor is it enabled for use in DQL.");
|
||||
}
|
||||
}
|
@ -23,8 +23,18 @@ class Company
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @Column(type="string", name="jurisdiction_code", nullable=true)
|
||||
*/
|
||||
public $jurisdiction;
|
||||
|
||||
/**
|
||||
* @OneToOne(targetEntity="Logo", mappedBy="company", cascade={"persist"}, orphanRemoval=true)
|
||||
*/
|
||||
public $logo;
|
||||
|
||||
/**
|
||||
* @OneToMany(targetEntity="Department", mappedBy="company", cascade={"persist"}, orphanRemoval=true)
|
||||
*/
|
||||
public $departments;
|
||||
}
|
||||
|
30
tests/Doctrine/Tests/Models/Pagination/Department.php
Normal file
30
tests/Doctrine/Tests/Models/Pagination/Department.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace Doctrine\Tests\Models\Pagination;
|
||||
|
||||
/**
|
||||
* Department
|
||||
*
|
||||
* @package Doctrine\Tests\Models\Pagination
|
||||
*
|
||||
* @author Bill Schaller
|
||||
* @Entity
|
||||
* @Table(name="pagination_department")
|
||||
*/
|
||||
class Department
|
||||
{
|
||||
/**
|
||||
* @Id @Column(type="integer")
|
||||
* @GeneratedValue
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* @Column(type="string")
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @ManyToOne(targetEntity="Company", inversedBy="departments", cascade={"persist"})
|
||||
*/
|
||||
public $company;
|
||||
}
|
27
tests/Doctrine/Tests/Models/Pagination/User.php
Normal file
27
tests/Doctrine/Tests/Models/Pagination/User.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\Models\Pagination;
|
||||
|
||||
|
||||
/**
|
||||
* @package Doctrine\Tests\Models\Pagination
|
||||
*
|
||||
* @Entity
|
||||
* @Table(name="pagination_user")
|
||||
* @InheritanceType("SINGLE_TABLE")
|
||||
* @DiscriminatorColumn(name="type", type="string")
|
||||
* @DiscriminatorMap({"user1"="User1"})
|
||||
*/
|
||||
abstract class User
|
||||
{
|
||||
/**
|
||||
* @Id @Column(type="integer")
|
||||
* @GeneratedValue
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @Column(type="string")
|
||||
*/
|
||||
public $name;
|
||||
}
|
17
tests/Doctrine/Tests/Models/Pagination/User1.php
Normal file
17
tests/Doctrine/Tests/Models/Pagination/User1.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\Models\Pagination;
|
||||
|
||||
/**
|
||||
* Class User1
|
||||
* @package Doctrine\Tests\Models\Pagination
|
||||
*
|
||||
* @Entity()
|
||||
*/
|
||||
class User1 extends User
|
||||
{
|
||||
/**
|
||||
* @Column(type="string")
|
||||
*/
|
||||
public $email;
|
||||
}
|
@ -3,12 +3,16 @@
|
||||
namespace Doctrine\Tests\ORM\Functional;
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\Tests\Models\CMS\CmsArticle;
|
||||
use Doctrine\Tests\Models\CMS\CmsEmail;
|
||||
use Doctrine\Tests\Models\CMS\CmsUser;
|
||||
use Doctrine\Tests\Models\CMS\CmsGroup;
|
||||
use Doctrine\ORM\Tools\Pagination\Paginator;
|
||||
use Doctrine\Tests\Models\Company\CompanyManager;
|
||||
use Doctrine\Tests\Models\Pagination\Company;
|
||||
use Doctrine\Tests\Models\Pagination\Department;
|
||||
use Doctrine\Tests\Models\Pagination\Logo;
|
||||
use Doctrine\Tests\Models\Pagination\User1;
|
||||
use ReflectionMethod;
|
||||
|
||||
/**
|
||||
@ -20,6 +24,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
{
|
||||
$this->useModelSet('cms');
|
||||
$this->useModelSet('pagination');
|
||||
$this->useModelSet('company');
|
||||
parent::setUp();
|
||||
$this->populate();
|
||||
}
|
||||
@ -115,7 +120,6 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
|
||||
private function iterateWithOrderAsc($useOutputWalkers, $fetchJoinCollection, $baseDql, $checkField)
|
||||
{
|
||||
|
||||
// Ascending
|
||||
$dql = "$baseDql ASC";
|
||||
$query = $this->_em->createQuery($dql);
|
||||
@ -130,7 +134,6 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
|
||||
private function iterateWithOrderAscWithLimit($useOutputWalkers, $fetchJoinCollection, $baseDql, $checkField)
|
||||
{
|
||||
|
||||
// Ascending
|
||||
$dql = "$baseDql ASC";
|
||||
$query = $this->_em->createQuery($dql);
|
||||
@ -439,6 +442,16 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
$this->iterateWithOrderDescWithLimitAndOffset(true, $fetchJoinCollection, $dql, "name");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider fetchJoinCollection
|
||||
*/
|
||||
public function testIterateWithOutputWalkersWithFetchJoinWithComplexOrderByReferencingJoinedWithLimitAndOffsetWithInheritanceType($fetchJoinCollection)
|
||||
{
|
||||
$dql = 'SELECT u FROM Doctrine\Tests\Models\Pagination\User u ORDER BY u.id';
|
||||
$this->iterateWithOrderAscWithLimit(true, $fetchJoinCollection, $dql, "name");
|
||||
$this->iterateWithOrderDescWithLimit(true, $fetchJoinCollection, $dql, "name");
|
||||
}
|
||||
|
||||
public function testIterateComplexWithOutputWalker()
|
||||
{
|
||||
$dql = 'SELECT g, COUNT(u.id) AS userCount FROM Doctrine\Tests\Models\CMS\CmsGroup g LEFT JOIN g.users u GROUP BY g HAVING COUNT(u.id) > 0';
|
||||
@ -449,6 +462,127 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
$this->assertCount(3, $paginator->getIterator());
|
||||
}
|
||||
|
||||
public function testJoinedClassTableInheritance()
|
||||
{
|
||||
$dql = 'SELECT c FROM Doctrine\Tests\Models\Company\CompanyManager c ORDER BY c.startDate';
|
||||
$query = $this->_em->createQuery($dql);
|
||||
|
||||
$paginator = new Paginator($query);
|
||||
$this->assertCount(1, $paginator->getIterator());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider useOutputWalkers
|
||||
*/
|
||||
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromBoth($useOutputWalkers)
|
||||
{
|
||||
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY c.name';
|
||||
$dqlAsc = $dql . " ASC, d.name";
|
||||
$dqlDesc = $dql . " DESC, d.name";
|
||||
$this->iterateWithOrderAsc($useOutputWalkers, true, $dqlAsc, "name");
|
||||
$this->iterateWithOrderDesc($useOutputWalkers, true, $dqlDesc, "name");
|
||||
}
|
||||
|
||||
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromBothWithLimitWithOutputWalker()
|
||||
{
|
||||
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY c.name';
|
||||
$dqlAsc = $dql . " ASC, d.name";
|
||||
$dqlDesc = $dql . " DESC, d.name";
|
||||
$this->iterateWithOrderAscWithLimit(true, true, $dqlAsc, "name");
|
||||
$this->iterateWithOrderDescWithLimit(true, true, $dqlDesc, "name");
|
||||
}
|
||||
|
||||
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromBothWithLimitWithoutOutputWalker()
|
||||
{
|
||||
$this->setExpectedException("RuntimeException", "Cannot select distinct identifiers from query with LIMIT and ORDER BY on a column from a fetch joined to-many association. Use output walkers.");
|
||||
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY c.name';
|
||||
$dqlAsc = $dql . " ASC, d.name";
|
||||
$dqlDesc = $dql . " DESC, d.name";
|
||||
$this->iterateWithOrderAscWithLimit(false, true, $dqlAsc, "name");
|
||||
$this->iterateWithOrderDescWithLimit(false, true, $dqlDesc, "name");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider useOutputWalkers
|
||||
*/
|
||||
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromRoot($useOutputWalkers)
|
||||
{
|
||||
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY c.name';
|
||||
$this->iterateWithOrderAsc($useOutputWalkers, true, $dql, "name");
|
||||
$this->iterateWithOrderDesc($useOutputWalkers, true, $dql, "name");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider useOutputWalkers
|
||||
*/
|
||||
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromRootWithLimit($useOutputWalkers)
|
||||
{
|
||||
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY c.name';
|
||||
$this->iterateWithOrderAscWithLimit($useOutputWalkers, true, $dql, "name");
|
||||
$this->iterateWithOrderDescWithLimit($useOutputWalkers, true, $dql, "name");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider useOutputWalkers
|
||||
*/
|
||||
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromRootWithLimitAndOffset($useOutputWalkers)
|
||||
{
|
||||
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY c.name';
|
||||
$this->iterateWithOrderAscWithLimitAndOffset($useOutputWalkers, true, $dql, "name");
|
||||
$this->iterateWithOrderDescWithLimitAndOffset($useOutputWalkers, true, $dql, "name");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider useOutputWalkers
|
||||
*/
|
||||
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromJoined($useOutputWalkers)
|
||||
{
|
||||
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY d.name';
|
||||
$this->iterateWithOrderAsc($useOutputWalkers, true, $dql, "name");
|
||||
$this->iterateWithOrderDesc($useOutputWalkers, true, $dql, "name");
|
||||
}
|
||||
|
||||
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromJoinedWithLimitWithOutputWalker()
|
||||
{
|
||||
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY d.name';
|
||||
$this->iterateWithOrderAscWithLimit(true, true, $dql, "name");
|
||||
$this->iterateWithOrderDescWithLimit(true, true, $dql, "name");
|
||||
}
|
||||
|
||||
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromJoinedWithLimitWithoutOutputWalker()
|
||||
{
|
||||
$this->setExpectedException("RuntimeException", "Cannot select distinct identifiers from query with LIMIT and ORDER BY on a column from a fetch joined to-many association. Use output walkers.");
|
||||
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY d.name';
|
||||
|
||||
$this->iterateWithOrderAscWithLimit(false, true, $dql, "name");
|
||||
$this->iterateWithOrderDescWithLimit(false, true, $dql, "name");
|
||||
}
|
||||
|
||||
public function testCountWithCountSubqueryInWhereClauseWithOutputWalker()
|
||||
{
|
||||
$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE ((SELECT COUNT(s.id) FROM Doctrine\Tests\Models\CMS\CmsUser s) = 9) ORDER BY u.id desc";
|
||||
$query = $this->_em->createQuery($dql);
|
||||
|
||||
$paginator = new Paginator($query, true);
|
||||
$paginator->setUseOutputWalkers(true);
|
||||
$this->assertCount(9, $paginator);
|
||||
}
|
||||
|
||||
public function testIterateWithCountSubqueryInWhereClause()
|
||||
{
|
||||
$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE ((SELECT COUNT(s.id) FROM Doctrine\Tests\Models\CMS\CmsUser s) = 9) ORDER BY u.id desc";
|
||||
$query = $this->_em->createQuery($dql);
|
||||
|
||||
$paginator = new Paginator($query, true);
|
||||
$paginator->setUseOutputWalkers(true);
|
||||
|
||||
$users = iterator_to_array($paginator->getIterator());
|
||||
$this->assertCount(9, $users);
|
||||
foreach ($users as $i => $user) {
|
||||
$this->assertEquals("username" . (8 - $i), $user->username);
|
||||
}
|
||||
}
|
||||
|
||||
public function testDetectOutputWalker()
|
||||
{
|
||||
// This query works using the output walkers but causes an exception using the TreeWalker
|
||||
@ -468,6 +602,20 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
count($paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test using a paginator when the entity attribute name and corresponding column name are not the same.
|
||||
*/
|
||||
public function testPaginationWithColumnAttributeNameDifference()
|
||||
{
|
||||
$dql = 'SELECT c FROM Doctrine\Tests\Models\Pagination\Company c ORDER BY c.id';
|
||||
$query = $this->_em->createQuery($dql);
|
||||
|
||||
$paginator = new Paginator($query);
|
||||
$paginator->getIterator();
|
||||
|
||||
$this->assertCount(9, $paginator->getIterator());
|
||||
}
|
||||
|
||||
public function testCloneQuery()
|
||||
{
|
||||
$dql = 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u';
|
||||
@ -543,6 +691,14 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
$user->addGroup($groups[$j]);
|
||||
}
|
||||
$this->_em->persist($user);
|
||||
for ($j = 0; $j < $i + 1; $j++) {
|
||||
$article = new CmsArticle();
|
||||
$article->topic = "topic$i$j";
|
||||
$article->text = "text$i$j";
|
||||
$article->setAuthor($user);
|
||||
$article->version = 0;
|
||||
$this->_em->persist($article);
|
||||
}
|
||||
}
|
||||
|
||||
for ($i = 0; $i < 9; $i++) {
|
||||
@ -553,9 +709,30 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
$company->logo->image_width = 100 + $i;
|
||||
$company->logo->image_height = 100 + $i;
|
||||
$company->logo->company = $company;
|
||||
for($j=0;$j<3;$j++) {
|
||||
$department = new Department();
|
||||
$department->name = "name$i$j";
|
||||
$department->company = $company;
|
||||
$company->departments[] = $department;
|
||||
}
|
||||
$this->_em->persist($company);
|
||||
}
|
||||
|
||||
for ($i = 0; $i < 9; $i++) {
|
||||
$user = new User1();
|
||||
$user->name = "name$i";
|
||||
$user->email = "email$i";
|
||||
$this->_em->persist($user);
|
||||
}
|
||||
|
||||
$manager = new CompanyManager();
|
||||
$manager->setName('Roman B.');
|
||||
$manager->setTitle('Foo');
|
||||
$manager->setDepartment('IT');
|
||||
$manager->setSalary(100000);
|
||||
|
||||
$this->_em->persist($manager);
|
||||
|
||||
$this->_em->flush();
|
||||
}
|
||||
|
||||
|
@ -34,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) dctrn_result ORDER BY title_1 ASC", $limitQuery->getSql()
|
||||
"SELECT DISTINCT id_0, MIN(sclr_5) AS dctrn_minrownum 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, ROW_NUMBER() OVER(ORDER BY m0_.title ASC) AS sclr_5, m0_.author_id AS author_id_6, m0_.category_id AS category_id_7 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 GROUP BY id_0 ORDER BY dctrn_minrownum ASC", $limitQuery->getSql()
|
||||
);
|
||||
|
||||
$this->entityManager->getConnection()->setDatabasePlatform($odp);
|
||||
@ -52,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) dctrn_result ORDER BY sclr_0 ASC",
|
||||
"SELECT DISTINCT id_1, MIN(sclr_3) AS dctrn_minrownum FROM (SELECT COUNT(g0_.id) AS sclr_0, u1_.id AS id_1, g0_.id AS id_2, ROW_NUMBER() OVER(ORDER BY COUNT(g0_.id) ASC) AS sclr_3 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 GROUP BY id_1 ORDER BY dctrn_minrownum ASC",
|
||||
$limitQuery->getSql()
|
||||
);
|
||||
|
||||
@ -71,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) dctrn_result ORDER BY sclr_0 ASC, id_1 DESC",
|
||||
"SELECT DISTINCT id_1, MIN(sclr_3) AS dctrn_minrownum FROM (SELECT COUNT(g0_.id) AS sclr_0, u1_.id AS id_1, g0_.id AS id_2, ROW_NUMBER() OVER(ORDER BY COUNT(g0_.id) ASC, u1_.id DESC) AS sclr_3 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 GROUP BY id_1 ORDER BY dctrn_minrownum ASC",
|
||||
$limitQuery->getSql()
|
||||
);
|
||||
|
||||
@ -90,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) dctrn_result ORDER BY sclr_0 ASC, id_1 DESC",
|
||||
"SELECT DISTINCT id_1, MIN(sclr_3) AS dctrn_minrownum FROM (SELECT COUNT(g0_.id) AS sclr_0, u1_.id AS id_1, g0_.id AS id_2, ROW_NUMBER() OVER(ORDER BY COUNT(g0_.id) ASC, u1_.id DESC) AS sclr_3 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 GROUP BY id_1 ORDER BY dctrn_minrownum ASC",
|
||||
$limitQuery->getSql()
|
||||
);
|
||||
|
||||
@ -119,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) dctrn_result ORDER BY TITLE_1 ASC", $limitQuery->getSql()
|
||||
"SELECT DISTINCT ID_0, MIN(SCLR_5) AS dctrn_minrownum 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, ROW_NUMBER() OVER(ORDER BY m0_.title ASC) AS SCLR_5, m0_.author_id AS AUTHOR_ID_6, m0_.category_id AS CATEGORY_ID_7 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 GROUP BY ID_0 ORDER BY dctrn_minrownum ASC", $limitQuery->getSql()
|
||||
);
|
||||
|
||||
$this->entityManager->getConnection()->setDatabasePlatform($odp);
|
||||
@ -138,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) dctrn_result ORDER BY SCLR_0 ASC",
|
||||
"SELECT DISTINCT ID_1, MIN(SCLR_3) AS dctrn_minrownum FROM (SELECT COUNT(g0_.id) AS SCLR_0, u1_.id AS ID_1, g0_.id AS ID_2, ROW_NUMBER() OVER(ORDER BY COUNT(g0_.id) ASC) AS SCLR_3 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 GROUP BY ID_1 ORDER BY dctrn_minrownum ASC",
|
||||
$limitQuery->getSql()
|
||||
);
|
||||
|
||||
@ -158,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) dctrn_result ORDER BY SCLR_0 ASC, ID_1 DESC",
|
||||
"SELECT DISTINCT ID_1, MIN(SCLR_3) AS dctrn_minrownum FROM (SELECT COUNT(g0_.id) AS SCLR_0, u1_.id AS ID_1, g0_.id AS ID_2, ROW_NUMBER() OVER(ORDER BY COUNT(g0_.id) ASC, u1_.id DESC) AS SCLR_3 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 GROUP BY ID_1 ORDER BY dctrn_minrownum ASC",
|
||||
$limitQuery->getSql()
|
||||
);
|
||||
|
||||
@ -208,7 +208,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
|
||||
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
|
||||
|
||||
$this->assertSame(
|
||||
'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',
|
||||
'SELECT DISTINCT id_0 FROM (SELECT a0_.id AS id_0, a0_.name AS name_1 FROM Author a0_) dctrn_result ORDER BY (1 - 1000) * 1 DESC',
|
||||
$query->getSQL()
|
||||
);
|
||||
}
|
||||
@ -223,7 +223,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
|
||||
$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',
|
||||
'SELECT DISTINCT id_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 image_height_2 * image_width_3 DESC',
|
||||
$query->getSQL()
|
||||
);
|
||||
}
|
||||
@ -238,7 +238,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
|
||||
$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',
|
||||
'SELECT DISTINCT id_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 image_height_1 * image_width_2 DESC',
|
||||
$query->getSQL()
|
||||
);
|
||||
}
|
||||
@ -253,7 +253,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
|
||||
$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',
|
||||
'SELECT DISTINCT id_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 image_height_3 * image_width_4 DESC',
|
||||
$query->getSQL()
|
||||
);
|
||||
}
|
||||
@ -268,7 +268,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
|
||||
$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',
|
||||
'SELECT DISTINCT ID_0, MIN(SCLR_5) AS dctrn_minrownum 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, ROW_NUMBER() OVER(ORDER BY a0_.image_height * a0_.image_width DESC) AS SCLR_5, a0_.user_id AS USER_ID_6 FROM Avatar a0_) dctrn_result GROUP BY ID_0 ORDER BY dctrn_minrownum ASC',
|
||||
$query->getSQL()
|
||||
);
|
||||
}
|
||||
@ -285,7 +285,7 @@ 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_) dctrn_result ORDER BY name_2 DESC',
|
||||
'SELECT DISTINCT id_0 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()
|
||||
);
|
||||
}
|
||||
@ -300,7 +300,7 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
|
||||
$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',
|
||||
'SELECT DISTINCT id_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 image_alt_desc_4 DESC',
|
||||
$query->getSQL()
|
||||
);
|
||||
}
|
||||
@ -314,7 +314,39 @@ class LimitSubqueryOutputWalkerTest extends PaginationTestCase
|
||||
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
|
||||
|
||||
$this->assertEquals(
|
||||
'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',
|
||||
'SELECT DISTINCT id_0 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()
|
||||
);
|
||||
}
|
||||
|
||||
public function testLimitSubqueryWithOrderByAndSubSelectInWhereClauseMySql()
|
||||
{
|
||||
$this->entityManager->getConnection()->setDatabasePlatform(new MySqlPlatform());
|
||||
$query = $this->entityManager->createQuery(
|
||||
'SELECT b FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost b
|
||||
WHERE ((SELECT COUNT(simple.id) FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost simple) = 1)
|
||||
ORDER BY b.id DESC'
|
||||
);
|
||||
$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_ WHERE ((SELECT COUNT(b1_.id) AS dctrn__1 FROM BlogPost b1_) = 1)) dctrn_result ORDER BY id_0 DESC',
|
||||
$query->getSQL()
|
||||
);
|
||||
}
|
||||
|
||||
public function testLimitSubqueryWithOrderByAndSubSelectInWhereClausePgSql()
|
||||
{
|
||||
$this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform());
|
||||
$query = $this->entityManager->createQuery(
|
||||
'SELECT b FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost b
|
||||
WHERE ((SELECT COUNT(simple.id) FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost simple) = 1)
|
||||
ORDER BY b.id DESC'
|
||||
);
|
||||
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
|
||||
|
||||
$this->assertEquals(
|
||||
'SELECT DISTINCT id_0, MIN(sclr_1) AS dctrn_minrownum FROM (SELECT b0_.id AS id_0, ROW_NUMBER() OVER(ORDER BY b0_.id DESC) AS sclr_1, b0_.author_id AS author_id_2, b0_.category_id AS category_id_3 FROM BlogPost b0_ WHERE ((SELECT COUNT(b1_.id) AS dctrn__1 FROM BlogPost b1_) = 1)) dctrn_result GROUP BY id_0 ORDER BY dctrn_minrownum ASC',
|
||||
$query->getSQL()
|
||||
);
|
||||
}
|
||||
|
@ -270,6 +270,9 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
|
||||
'pagination' => array(
|
||||
'Doctrine\Tests\Models\Pagination\Company',
|
||||
'Doctrine\Tests\Models\Pagination\Logo',
|
||||
'Doctrine\Tests\Models\Pagination\Department',
|
||||
'Doctrine\Tests\Models\Pagination\User',
|
||||
'Doctrine\Tests\Models\Pagination\User1',
|
||||
),
|
||||
);
|
||||
|
||||
@ -521,7 +524,9 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
|
||||
|
||||
if (isset($this->_usedModelSets['pagination'])) {
|
||||
$conn->executeUpdate('DELETE FROM pagination_logo');
|
||||
$conn->executeUpdate('DELETE FROM pagination_department');
|
||||
$conn->executeUpdate('DELETE FROM pagination_company');
|
||||
$conn->executeUpdate('DELETE FROM pagination_user');
|
||||
}
|
||||
|
||||
$this->_em->clear();
|
||||
|
Loading…
Reference in New Issue
Block a user