diff --git a/UPGRADE.md b/UPGRADE.md
index a44bbd5a35b..9de8af04d38 100644
--- a/UPGRADE.md
+++ b/UPGRADE.md
@@ -1,5 +1,14 @@
# Upgrade to 2.20
+## Add `Doctrine\ORM\Query\OutputWalker` interface, deprecate `Doctrine\ORM\Query\SqlWalker::getExecutor()`
+
+Output walkers should implement the new `\Doctrine\ORM\Query\OutputWalker` interface and create
+`Doctrine\ORM\Query\Exec\SqlFinalizer` instances instead of `Doctrine\ORM\Query\Exec\AbstractSqlExecutor`s.
+The output walker must not base its workings on the query `firstResult`/`maxResult` values, so that the
+`SqlFinalizer` can be kept in the query cache and used regardless of the actual `firstResult`/`maxResult` values.
+Any operation dependent on `firstResult`/`maxResult` should take place within the `SqlFinalizer::createExecutor()`
+method. Details can be found at https://github.com/doctrine/orm/pull/11188.
+
## Explictly forbid property hooks
Property hooks are not supported yet by Doctrine ORM. Until support is added,
diff --git a/psalm-baseline.xml b/psalm-baseline.xml
index 2d96702abe2..28ed8aab049 100644
--- a/psalm-baseline.xml
+++ b/psalm-baseline.xml
@@ -1868,6 +1868,12 @@
_sqlStatements = &$this->sqlStatements]]>
+
+
+
+
+
+
@@ -1996,6 +2002,9 @@
+
+
+
@@ -2057,11 +2066,6 @@
-
-
-
-
-
parameters)]]>
diff --git a/src/Query.php b/src/Query.php
index 2654e04b0cc..48464e52631 100644
--- a/src/Query.php
+++ b/src/Query.php
@@ -18,6 +18,8 @@
use Doctrine\ORM\Query\AST\SelectStatement;
use Doctrine\ORM\Query\AST\UpdateStatement;
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
+use Doctrine\ORM\Query\Exec\SqlFinalizer;
+use Doctrine\ORM\Query\OutputWalker;
use Doctrine\ORM\Query\Parameter;
use Doctrine\ORM\Query\ParameterTypeInferer;
use Doctrine\ORM\Query\Parser;
@@ -33,6 +35,7 @@
use function count;
use function get_debug_type;
use function in_array;
+use function is_a;
use function is_int;
use function ksort;
use function md5;
@@ -196,7 +199,7 @@ class Query extends AbstractQuery
*/
public function getSQL()
{
- return $this->parse()->getSqlExecutor()->getSqlStatements();
+ return $this->getSqlExecutor()->getSqlStatements();
}
/**
@@ -285,7 +288,7 @@ private function parse(): ParserResult
*/
protected function _doExecute()
{
- $executor = $this->parse()->getSqlExecutor();
+ $executor = $this->getSqlExecutor();
if ($this->_queryCacheProfile) {
$executor->setQueryCacheProfile($this->_queryCacheProfile);
@@ -813,11 +816,31 @@ protected function getQueryCacheId(): string
{
ksort($this->_hints);
+ if (! $this->hasHint(self::HINT_CUSTOM_OUTPUT_WALKER)) {
+ // Assume Parser will create the SqlOutputWalker; save is_a call, which might trigger a class load
+ $firstAndMaxResult = '';
+ } else {
+ $outputWalkerClass = $this->getHint(self::HINT_CUSTOM_OUTPUT_WALKER);
+ if (is_a($outputWalkerClass, OutputWalker::class, true)) {
+ $firstAndMaxResult = '';
+ } else {
+ Deprecation::trigger(
+ 'doctrine/orm',
+ 'https://github.com/doctrine/orm/pull/11188/',
+ 'Your output walker class %s should implement %s in order to provide a %s. This also means the output walker should not use the query firstResult/maxResult values, which should be read from the query by the SqlFinalizer only.',
+ $outputWalkerClass,
+ OutputWalker::class,
+ SqlFinalizer::class
+ );
+ $firstAndMaxResult = '&firstResult=' . $this->firstResult . '&maxResult=' . $this->maxResults;
+ }
+ }
+
return md5(
$this->getDQL() . serialize($this->_hints) .
'&platform=' . get_debug_type($this->getEntityManager()->getConnection()->getDatabasePlatform()) .
($this->_em->hasFilters() ? $this->_em->getFilters()->getHash() : '') .
- '&firstResult=' . $this->firstResult . '&maxResult=' . $this->maxResults .
+ $firstAndMaxResult .
'&hydrationMode=' . $this->_hydrationMode . '&types=' . serialize($this->parsedTypes) . 'DOCTRINE_QUERY_CACHE_SALT'
);
}
@@ -836,4 +859,9 @@ public function __clone()
$this->state = self::STATE_DIRTY;
}
+
+ private function getSqlExecutor(): AbstractSqlExecutor
+ {
+ return $this->parse()->prepareSqlExecutor($this);
+ }
}
diff --git a/src/Query/Exec/FinalizedSelectExecutor.php b/src/Query/Exec/FinalizedSelectExecutor.php
new file mode 100644
index 00000000000..a97e6f428c5
--- /dev/null
+++ b/src/Query/Exec/FinalizedSelectExecutor.php
@@ -0,0 +1,33 @@
+sqlStatements = $sql;
+ }
+
+ /**
+ * @param list|array $params
+ * @param array|array $types
+ */
+ public function execute(Connection $conn, array $params, array $types): Result
+ {
+ return $conn->executeQuery($this->getSqlStatements(), $params, $types, $this->queryCacheProfile);
+ }
+}
diff --git a/src/Query/Exec/PreparedExecutorFinalizer.php b/src/Query/Exec/PreparedExecutorFinalizer.php
new file mode 100644
index 00000000000..b89b124e352
--- /dev/null
+++ b/src/Query/Exec/PreparedExecutorFinalizer.php
@@ -0,0 +1,28 @@
+executor = $exeutor;
+ }
+
+ public function createExecutor(Query $query): AbstractSqlExecutor
+ {
+ return $this->executor;
+ }
+}
diff --git a/src/Query/Exec/SingleSelectSqlFinalizer.php b/src/Query/Exec/SingleSelectSqlFinalizer.php
new file mode 100644
index 00000000000..5180be9e7b2
--- /dev/null
+++ b/src/Query/Exec/SingleSelectSqlFinalizer.php
@@ -0,0 +1,64 @@
+sql = $sql;
+ }
+
+ /**
+ * This method exists temporarily to support old SqlWalker interfaces.
+ *
+ * @internal
+ *
+ * @psalm-internal Doctrine\ORM
+ */
+ public function finalizeSql(Query $query): string
+ {
+ $platform = $query->getEntityManager()->getConnection()->getDatabasePlatform();
+
+ $sql = $platform->modifyLimitQuery($this->sql, $query->getMaxResults(), $query->getFirstResult());
+
+ $lockMode = $query->getHint(Query::HINT_LOCK_MODE) ?: LockMode::NONE;
+
+ if ($lockMode !== LockMode::NONE && $lockMode !== LockMode::OPTIMISTIC && $lockMode !== LockMode::PESSIMISTIC_READ && $lockMode !== LockMode::PESSIMISTIC_WRITE) {
+ throw QueryException::invalidLockMode();
+ }
+
+ if ($lockMode === LockMode::PESSIMISTIC_READ) {
+ $sql .= ' ' . $this->getReadLockSQL($platform);
+ } elseif ($lockMode === LockMode::PESSIMISTIC_WRITE) {
+ $sql .= ' ' . $this->getWriteLockSQL($platform);
+ }
+
+ return $sql;
+ }
+
+ /** @return FinalizedSelectExecutor */
+ public function createExecutor(Query $query): AbstractSqlExecutor
+ {
+ return new FinalizedSelectExecutor($this->finalizeSql($query));
+ }
+}
diff --git a/src/Query/Exec/SqlFinalizer.php b/src/Query/Exec/SqlFinalizer.php
new file mode 100644
index 00000000000..cddad84e8a3
--- /dev/null
+++ b/src/Query/Exec/SqlFinalizer.php
@@ -0,0 +1,26 @@
+queryComponents = $treeWalkerChain->getQueryComponents();
}
- $outputWalkerClass = $this->customOutputWalker ?: SqlWalker::class;
+ $outputWalkerClass = $this->customOutputWalker ?: SqlOutputWalker::class;
$outputWalker = new $outputWalkerClass($this->query, $this->parserResult, $this->queryComponents);
- // Assign an SQL executor to the parser result
- $this->parserResult->setSqlExecutor($outputWalker->getExecutor($AST));
+ if ($outputWalker instanceof OutputWalker) {
+ $finalizer = $outputWalker->getFinalizer($AST);
+ $this->parserResult->setSqlFinalizer($finalizer);
+ } else {
+ Deprecation::trigger(
+ 'doctrine/orm',
+ 'https://github.com/doctrine/orm/pull/11188/',
+ 'Your output walker class %s should implement %s in order to provide a %s. This also means the output walker should not use the query firstResult/maxResult values, which should be read from the query by the SqlFinalizer only.',
+ $outputWalkerClass,
+ OutputWalker::class,
+ SqlFinalizer::class
+ );
+ // @phpstan-ignore method.deprecated
+ $executor = $outputWalker->getExecutor($AST);
+ // @phpstan-ignore method.deprecated
+ $this->parserResult->setSqlExecutor($executor);
+ }
return $this->parserResult;
}
diff --git a/src/Query/ParserResult.php b/src/Query/ParserResult.php
index e5d36c9bbe3..86a4e4d3b64 100644
--- a/src/Query/ParserResult.php
+++ b/src/Query/ParserResult.php
@@ -4,7 +4,10 @@
namespace Doctrine\ORM\Query;
+use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
+use Doctrine\ORM\Query\Exec\SqlFinalizer;
+use LogicException;
use function sprintf;
@@ -20,15 +23,23 @@ class ParserResult
'sqlExecutor' => '_sqlExecutor',
'resultSetMapping' => '_resultSetMapping',
'parameterMappings' => '_parameterMappings',
+ 'sqlFinalizer' => 'sqlFinalizer',
];
/**
* The SQL executor used for executing the SQL.
*
- * @var AbstractSqlExecutor
+ * @var ?AbstractSqlExecutor
*/
private $sqlExecutor;
+ /**
+ * The SQL executor used for executing the SQL.
+ *
+ * @var ?SqlFinalizer
+ */
+ private $sqlFinalizer;
+
/**
* The ResultSetMapping that describes how to map the SQL result set.
*
@@ -75,6 +86,8 @@ public function setResultSetMapping(ResultSetMapping $rsm)
/**
* Sets the SQL executor that should be used for this ParserResult.
*
+ * @deprecated
+ *
* @param AbstractSqlExecutor $executor
*
* @return void
@@ -87,13 +100,33 @@ public function setSqlExecutor($executor)
/**
* Gets the SQL executor used by this ParserResult.
*
- * @return AbstractSqlExecutor
+ * @deprecated
+ *
+ * @return ?AbstractSqlExecutor
*/
public function getSqlExecutor()
{
return $this->sqlExecutor;
}
+ public function setSqlFinalizer(SqlFinalizer $finalizer): void
+ {
+ $this->sqlFinalizer = $finalizer;
+ }
+
+ public function prepareSqlExecutor(Query $query): AbstractSqlExecutor
+ {
+ if ($this->sqlFinalizer !== null) {
+ return $this->sqlFinalizer->createExecutor($query);
+ }
+
+ if ($this->sqlExecutor !== null) {
+ return $this->sqlExecutor;
+ }
+
+ throw new LogicException('This ParserResult lacks both the SqlFinalizer as well as the (legacy) SqlExecutor');
+ }
+
/**
* Adds a DQL to SQL parameter mapping. One DQL parameter name/position can map to
* several SQL parameter positions.
diff --git a/src/Query/SqlOutputWalker.php b/src/Query/SqlOutputWalker.php
new file mode 100644
index 00000000000..e737e1c9c53
--- /dev/null
+++ b/src/Query/SqlOutputWalker.php
@@ -0,0 +1,29 @@
+createSqlForFinalizer($AST));
+
+ case $AST instanceof AST\UpdateStatement:
+ return new PreparedExecutorFinalizer($this->createUpdateStatementExecutor($AST));
+
+ case $AST instanceof AST\DeleteStatement:
+ return new PreparedExecutorFinalizer($this->createDeleteStatementExecutor($AST));
+ }
+
+ throw new LogicException('Unexpected AST node type');
+ }
+}
diff --git a/src/Query/SqlWalker.php b/src/Query/SqlWalker.php
index 4c25fb63a68..e4e2de5c5c1 100644
--- a/src/Query/SqlWalker.php
+++ b/src/Query/SqlWalker.php
@@ -16,7 +16,6 @@
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\Query;
use Doctrine\ORM\Utility\HierarchyDiscriminatorResolver;
-use Doctrine\ORM\Utility\LockSqlHelper;
use Doctrine\ORM\Utility\PersisterHelper;
use InvalidArgumentException;
use LogicException;
@@ -49,8 +48,6 @@
*/
class SqlWalker implements TreeWalker
{
- use LockSqlHelper;
-
public const HINT_DISTINCT = 'doctrine.distinct';
/**
@@ -278,34 +275,48 @@ public function setQueryComponent($dqlAlias, array $queryComponent)
/**
* Gets an executor that can be used to execute the result of this walker.
*
+ * @deprecated Output walkers should no longer create the executor directly, but instead provide
+ * a SqlFinalizer by implementing the `OutputWalker` interface. Thus, this method is
+ * no longer needed and will be removed in 4.0.
+ *
* @param AST\DeleteStatement|AST\UpdateStatement|AST\SelectStatement $AST
*
* @return Exec\AbstractSqlExecutor
- *
- * @not-deprecated
*/
public function getExecutor($AST)
{
switch (true) {
case $AST instanceof AST\DeleteStatement:
- $primaryClass = $this->em->getClassMetadata($AST->deleteClause->abstractSchemaName);
-
- return $primaryClass->isInheritanceTypeJoined()
- ? new Exec\MultiTableDeleteExecutor($AST, $this)
- : new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
+ return $this->createDeleteStatementExecutor($AST);
case $AST instanceof AST\UpdateStatement:
- $primaryClass = $this->em->getClassMetadata($AST->updateClause->abstractSchemaName);
-
- return $primaryClass->isInheritanceTypeJoined()
- ? new Exec\MultiTableUpdateExecutor($AST, $this)
- : new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
+ return $this->createUpdateStatementExecutor($AST);
default:
return new Exec\SingleSelectExecutor($AST, $this);
}
}
+ /** @psalm-internal Doctrine\ORM */
+ protected function createUpdateStatementExecutor(AST\UpdateStatement $AST): Exec\AbstractSqlExecutor
+ {
+ $primaryClass = $this->em->getClassMetadata($AST->updateClause->abstractSchemaName);
+
+ return $primaryClass->isInheritanceTypeJoined()
+ ? new Exec\MultiTableUpdateExecutor($AST, $this)
+ : new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
+ }
+
+ /** @psalm-internal Doctrine\ORM */
+ protected function createDeleteStatementExecutor(AST\DeleteStatement $AST): Exec\AbstractSqlExecutor
+ {
+ $primaryClass = $this->em->getClassMetadata($AST->deleteClause->abstractSchemaName);
+
+ return $primaryClass->isInheritanceTypeJoined()
+ ? new Exec\MultiTableDeleteExecutor($AST, $this)
+ : new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
+ }
+
/**
* Generates a unique, short SQL table alias.
*
@@ -561,10 +572,15 @@ private function generateFilterConditionSQL(
*/
public function walkSelectStatement(AST\SelectStatement $AST)
{
- $limit = $this->query->getMaxResults();
- $offset = $this->query->getFirstResult();
- $lockMode = $this->query->getHint(Query::HINT_LOCK_MODE) ?: LockMode::NONE;
- $sql = $this->walkSelectClause($AST->selectClause)
+ $sql = $this->createSqlForFinalizer($AST);
+ $finalizer = new Exec\SingleSelectSqlFinalizer($sql);
+
+ return $finalizer->finalizeSql($this->query);
+ }
+
+ protected function createSqlForFinalizer(AST\SelectStatement $AST): string
+ {
+ $sql = $this->walkSelectClause($AST->selectClause)
. $this->walkFromClause($AST->fromClause)
. $this->walkWhereClause($AST->whereClause);
@@ -585,31 +601,22 @@ public function walkSelectStatement(AST\SelectStatement $AST)
$sql .= ' ORDER BY ' . $orderBySql;
}
- $sql = $this->platform->modifyLimitQuery($sql, $limit, $offset);
-
- if ($lockMode === LockMode::NONE) {
- return $sql;
- }
+ $this->assertOptimisticLockingHasAllClassesVersioned();
- if ($lockMode === LockMode::PESSIMISTIC_READ) {
- return $sql . ' ' . $this->getReadLockSQL($this->platform);
- }
-
- if ($lockMode === LockMode::PESSIMISTIC_WRITE) {
- return $sql . ' ' . $this->getWriteLockSQL($this->platform);
- }
+ return $sql;
+ }
- if ($lockMode !== LockMode::OPTIMISTIC) {
- throw QueryException::invalidLockMode();
- }
+ private function assertOptimisticLockingHasAllClassesVersioned(): void
+ {
+ $lockMode = $this->query->getHint(Query::HINT_LOCK_MODE) ?: LockMode::NONE;
- foreach ($this->selectedClasses as $selectedClass) {
- if (! $selectedClass['class']->isVersioned) {
- throw OptimisticLockException::lockFailed($selectedClass['class']->name);
+ if ($lockMode === LockMode::OPTIMISTIC) {
+ foreach ($this->selectedClasses as $selectedClass) {
+ if (! $selectedClass['class']->isVersioned) {
+ throw OptimisticLockException::lockFailed($selectedClass['class']->name);
+ }
}
}
-
- return $sql;
}
/**
diff --git a/src/Tools/Pagination/CountOutputWalker.php b/src/Tools/Pagination/CountOutputWalker.php
index 0929e2ab065..92615f8aab5 100644
--- a/src/Tools/Pagination/CountOutputWalker.php
+++ b/src/Tools/Pagination/CountOutputWalker.php
@@ -11,7 +11,7 @@
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\ParserResult;
use Doctrine\ORM\Query\ResultSetMapping;
-use Doctrine\ORM\Query\SqlWalker;
+use Doctrine\ORM\Query\SqlOutputWalker;
use RuntimeException;
use function array_diff;
@@ -36,7 +36,7 @@
*
* @psalm-import-type QueryComponent from Parser
*/
-class CountOutputWalker extends SqlWalker
+class CountOutputWalker extends SqlOutputWalker
{
/** @var AbstractPlatform */
private $platform;
@@ -62,16 +62,13 @@ public function __construct($query, $parserResult, array $queryComponents)
parent::__construct($query, $parserResult, $queryComponents);
}
- /**
- * {@inheritDoc}
- */
- public function walkSelectStatement(SelectStatement $AST)
+ protected function createSqlForFinalizer(SelectStatement $AST): string
{
if ($this->platform instanceof SQLServerPlatform) {
$AST->orderByClause = null;
}
- $sql = parent::walkSelectStatement($AST);
+ $sql = parent::createSqlForFinalizer($AST);
if ($AST->groupByClause) {
return sprintf(
diff --git a/src/Tools/Pagination/LimitSubqueryOutputWalker.php b/src/Tools/Pagination/LimitSubqueryOutputWalker.php
index f92c1145d4f..0e858ce0ecb 100644
--- a/src/Tools/Pagination/LimitSubqueryOutputWalker.php
+++ b/src/Tools/Pagination/LimitSubqueryOutputWalker.php
@@ -14,15 +14,20 @@
use Doctrine\ORM\Mapping\QuoteStrategy;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\Query;
+use Doctrine\ORM\Query\AST\DeleteStatement;
use Doctrine\ORM\Query\AST\OrderByClause;
use Doctrine\ORM\Query\AST\PathExpression;
use Doctrine\ORM\Query\AST\SelectExpression;
use Doctrine\ORM\Query\AST\SelectStatement;
+use Doctrine\ORM\Query\AST\UpdateStatement;
+use Doctrine\ORM\Query\Exec\SingleSelectSqlFinalizer;
+use Doctrine\ORM\Query\Exec\SqlFinalizer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\ParserResult;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\Query\ResultSetMapping;
-use Doctrine\ORM\Query\SqlWalker;
+use Doctrine\ORM\Query\SqlOutputWalker;
+use LogicException;
use RuntimeException;
use function array_diff;
@@ -50,7 +55,7 @@
*
* @psalm-import-type QueryComponent from Parser
*/
-class LimitSubqueryOutputWalker extends SqlWalker
+class LimitSubqueryOutputWalker extends SqlOutputWalker
{
private const ORDER_BY_PATH_EXPRESSION = '/(?platform = $query->getEntityManager()->getConnection()->getDatabasePlatform();
$this->rsm = $parserResult->getResultSetMapping();
+ $query = clone $query;
+
// Reset limit and offset
$this->firstResult = $query->getFirstResult();
$this->maxResults = $query->getMaxResults();
@@ -158,11 +165,33 @@ private function rebuildOrderByForRowNumber(SelectStatement $AST): void
*/
public function walkSelectStatement(SelectStatement $AST)
{
+ $sqlFinalizer = $this->getFinalizer($AST);
+
+ $query = $this->getQuery();
+
+ $abstractSqlExecutor = $sqlFinalizer->createExecutor($query);
+
+ return $abstractSqlExecutor->getSqlStatements();
+ }
+
+ /**
+ * @param DeleteStatement|UpdateStatement|SelectStatement $AST
+ *
+ * @return SingleSelectSqlFinalizer
+ */
+ public function getFinalizer($AST): SqlFinalizer
+ {
+ if (! $AST instanceof SelectStatement) {
+ throw new LogicException(self::class . ' is to be used on SelectStatements only');
+ }
+
if ($this->platformSupportsRowNumber()) {
- return $this->walkSelectStatementWithRowNumber($AST);
+ $sql = $this->createSqlWithRowNumber($AST);
+ } else {
+ $sql = $this->createSqlWithoutRowNumber($AST);
}
- return $this->walkSelectStatementWithoutRowNumber($AST);
+ return new SingleSelectSqlFinalizer($sql);
}
/**
@@ -174,6 +203,16 @@ public function walkSelectStatement(SelectStatement $AST)
* @throws RuntimeException
*/
public function walkSelectStatementWithRowNumber(SelectStatement $AST)
+ {
+ // Apply the limit and offset.
+ return $this->platform->modifyLimitQuery(
+ $this->createSqlWithRowNumber($AST),
+ $this->maxResults,
+ $this->firstResult
+ );
+ }
+
+ private function createSqlWithRowNumber(SelectStatement $AST): string
{
$hasOrderBy = false;
$outerOrderBy = ' ORDER BY dctrn_minrownum ASC';
@@ -203,13 +242,6 @@ public function walkSelectStatementWithRowNumber(SelectStatement $AST)
$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
@@ -232,6 +264,16 @@ public function walkSelectStatementWithRowNumber(SelectStatement $AST)
* @throws RuntimeException
*/
public function walkSelectStatementWithoutRowNumber(SelectStatement $AST, $addMissingItemsFromOrderByToSelect = true)
+ {
+ // Apply the limit and offset.
+ return $this->platform->modifyLimitQuery(
+ $this->createSqlWithoutRowNumber($AST, $addMissingItemsFromOrderByToSelect),
+ $this->maxResults,
+ $this->firstResult
+ );
+ }
+
+ private function createSqlWithoutRowNumber(SelectStatement $AST, bool $addMissingItemsFromOrderByToSelect = true): string
{
// We don't want to call this recursively!
if ($AST->orderByClause instanceof OrderByClause && $addMissingItemsFromOrderByToSelect) {
@@ -260,13 +302,6 @@ public function walkSelectStatementWithoutRowNumber(SelectStatement $AST, $addMi
// https://github.com/doctrine/orm/issues/2630
$sql = $this->preserveSqlOrdering($sqlIdentifier, $innerSql, $sql, $orderByClause);
- // 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
diff --git a/src/Tools/Pagination/RootTypeWalker.php b/src/Tools/Pagination/RootTypeWalker.php
index dc1d77a51c7..ec45dbd6fc1 100644
--- a/src/Tools/Pagination/RootTypeWalker.php
+++ b/src/Tools/Pagination/RootTypeWalker.php
@@ -5,7 +5,10 @@
namespace Doctrine\ORM\Tools\Pagination;
use Doctrine\ORM\Query\AST;
-use Doctrine\ORM\Query\SqlWalker;
+use Doctrine\ORM\Query\Exec\FinalizedSelectExecutor;
+use Doctrine\ORM\Query\Exec\PreparedExecutorFinalizer;
+use Doctrine\ORM\Query\Exec\SqlFinalizer;
+use Doctrine\ORM\Query\SqlOutputWalker;
use Doctrine\ORM\Utility\PersisterHelper;
use RuntimeException;
@@ -22,7 +25,7 @@
* Returning the type instead of a "real" SQL statement is a slight hack. However, it has the
* benefit that the DQL -> root entity id type resolution can be cached in the query cache.
*/
-final class RootTypeWalker extends SqlWalker
+final class RootTypeWalker extends SqlOutputWalker
{
public function walkSelectStatement(AST\SelectStatement $AST): string
{
@@ -45,4 +48,13 @@ public function walkSelectStatement(AST\SelectStatement $AST): string
->getEntityManager()
)[0];
}
+
+ public function getFinalizer($AST): SqlFinalizer
+ {
+ if (! $AST instanceof AST\SelectStatement) {
+ throw new RuntimeException(self::class . ' is to be used on SelectStatements only');
+ }
+
+ return new PreparedExecutorFinalizer(new FinalizedSelectExecutor($this->walkSelectStatement($AST)));
+ }
}
diff --git a/tests/Tests/Mocks/NullSqlWalker.php b/tests/Tests/Mocks/NullSqlWalker.php
index 9d54e703547..90cf0c6e926 100644
--- a/tests/Tests/Mocks/NullSqlWalker.php
+++ b/tests/Tests/Mocks/NullSqlWalker.php
@@ -7,12 +7,14 @@
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Query\AST;
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
-use Doctrine\ORM\Query\SqlWalker;
+use Doctrine\ORM\Query\Exec\PreparedExecutorFinalizer;
+use Doctrine\ORM\Query\Exec\SqlFinalizer;
+use Doctrine\ORM\Query\SqlOutputWalker;
/**
* SqlWalker implementation that does not produce SQL.
*/
-class NullSqlWalker extends SqlWalker
+class NullSqlWalker extends SqlOutputWalker
{
public function walkSelectStatement(AST\SelectStatement $AST): string
{
@@ -29,13 +31,15 @@ public function walkDeleteStatement(AST\DeleteStatement $AST): string
return '';
}
- public function getExecutor($AST): AbstractSqlExecutor
+ public function getFinalizer($AST): SqlFinalizer
{
- return new class extends AbstractSqlExecutor {
- public function execute(Connection $conn, array $params, array $types): int
- {
- return 0;
+ return new PreparedExecutorFinalizer(
+ new class extends AbstractSqlExecutor {
+ public function execute(Connection $conn, array $params, array $types): int
+ {
+ return 0;
+ }
}
- };
+ );
}
}
diff --git a/tests/Tests/ORM/Functional/ParserResultSerializationTest.php b/tests/Tests/ORM/Functional/ParserResultSerializationTest.php
index 0841c5ff3a0..7b2ae59119a 100644
--- a/tests/Tests/ORM/Functional/ParserResultSerializationTest.php
+++ b/tests/Tests/ORM/Functional/ParserResultSerializationTest.php
@@ -6,6 +6,8 @@
use Closure;
use Doctrine\ORM\Query;
+use Doctrine\ORM\Query\Exec\FinalizedSelectExecutor;
+use Doctrine\ORM\Query\Exec\PreparedExecutorFinalizer;
use Doctrine\ORM\Query\Exec\SingleSelectExecutor;
use Doctrine\ORM\Query\ParserResult;
use Doctrine\ORM\Query\ResultSetMapping;
@@ -35,18 +37,40 @@ protected function setUp(): void
*
* @dataProvider provideToSerializedAndBack
*/
- public function testSerializeParserResult(Closure $toSerializedAndBack): void
+ public function testSerializeParserResultForQueryWithSqlWalker(Closure $toSerializedAndBack): void
{
$query = $this->_em
->createQuery('SELECT u FROM Doctrine\Tests\Models\Company\CompanyEmployee u WHERE u.name = :name');
+ // Use the (legacy) SqlWalker which directly puts an SqlExecutor instance into the parser result
+ $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, Query\SqlWalker::class);
+
$parserResult = self::parseQuery($query);
$unserialized = $toSerializedAndBack($parserResult);
$this->assertInstanceOf(ParserResult::class, $unserialized);
$this->assertInstanceOf(ResultSetMapping::class, $unserialized->getResultSetMapping());
$this->assertEquals(['name' => [0]], $unserialized->getParameterMappings());
- $this->assertInstanceOf(SingleSelectExecutor::class, $unserialized->getSqlExecutor());
+ $this->assertNotNull($unserialized->prepareSqlExecutor($query));
+ }
+
+ /**
+ * @param Closure(ParserResult): ParserResult $toSerializedAndBack
+ *
+ * @dataProvider provideToSerializedAndBack
+ */
+ public function testSerializeParserResultForQueryWithSqlOutputWalker(Closure $toSerializedAndBack): void
+ {
+ $query = $this->_em
+ ->createQuery('SELECT u FROM Doctrine\Tests\Models\Company\CompanyEmployee u WHERE u.name = :name');
+
+ $parserResult = self::parseQuery($query);
+ $unserialized = $toSerializedAndBack($parserResult);
+
+ $this->assertInstanceOf(ParserResult::class, $unserialized);
+ $this->assertInstanceOf(ResultSetMapping::class, $unserialized->getResultSetMapping());
+ $this->assertEquals(['name' => [0]], $unserialized->getParameterMappings());
+ $this->assertNotNull($unserialized->prepareSqlExecutor($query));
}
/** @return Generator */
@@ -75,6 +99,9 @@ public function testItSerializesParserResultWithAForwardCompatibleFormat(): void
$query = $this->_em
->createQuery('SELECT u FROM Doctrine\Tests\Models\Company\CompanyEmployee u WHERE u.name = :name');
+ // Use the (legacy) SqlWalker which directly puts an SqlExecutor instance into the parser result
+ $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, Query\SqlWalker::class);
+
$parserResult = self::parseQuery($query);
$serialized = serialize($parserResult);
$this->assertStringNotContainsString(
@@ -118,11 +145,12 @@ public static function provideSerializedSingleSelectResults(): Generator
public function testSymfony44ProvidedData(): void
{
- $sqlExecutor = $this->createMock(SingleSelectExecutor::class);
+ $sqlExecutor = new FinalizedSelectExecutor('test');
+ $sqlFinalizer = new PreparedExecutorFinalizer($sqlExecutor);
$resultSetMapping = $this->createMock(ResultSetMapping::class);
$parserResult = new ParserResult();
- $parserResult->setSqlExecutor($sqlExecutor);
+ $parserResult->setSqlFinalizer($sqlFinalizer);
$parserResult->setResultSetMapping($resultSetMapping);
$parserResult->addParameterMapping('name', 0);
@@ -132,7 +160,7 @@ public function testSymfony44ProvidedData(): void
$this->assertInstanceOf(ParserResult::class, $unserialized);
$this->assertInstanceOf(ResultSetMapping::class, $unserialized->getResultSetMapping());
$this->assertEquals(['name' => [0]], $unserialized->getParameterMappings());
- $this->assertInstanceOf(SingleSelectExecutor::class, $unserialized->getSqlExecutor());
+ $this->assertEquals($sqlExecutor, $unserialized->prepareSqlExecutor($this->createMock(Query::class)));
}
private static function parseQuery(Query $query): ParserResult
diff --git a/tests/Tests/ORM/Functional/QueryCacheTest.php b/tests/Tests/ORM/Functional/QueryCacheTest.php
index 4935c3d78ee..3894ecef697 100644
--- a/tests/Tests/ORM/Functional/QueryCacheTest.php
+++ b/tests/Tests/ORM/Functional/QueryCacheTest.php
@@ -43,7 +43,7 @@ public function testQueryCacheDependsOnHints(): array
}
/** @depends testQueryCacheDependsOnHints */
- public function testQueryCacheDependsOnFirstResult(array $previous): void
+ public function testQueryCacheDoesNotDependOnFirstResultForDefaultOutputWalker(array $previous): void
{
[$query, $cache] = $previous;
assert($query instanceof Query);
@@ -55,11 +55,11 @@ public function testQueryCacheDependsOnFirstResult(array $previous): void
$query->setMaxResults(9999);
$query->getResult();
- self::assertCount($cacheCount + 1, $cache->getValues());
+ self::assertCount($cacheCount, $cache->getValues());
}
/** @depends testQueryCacheDependsOnHints */
- public function testQueryCacheDependsOnMaxResults(array $previous): void
+ public function testQueryCacheDoesNotDependOnMaxResultsForDefaultOutputWalker(array $previous): void
{
[$query, $cache] = $previous;
assert($query instanceof Query);
@@ -70,7 +70,7 @@ public function testQueryCacheDependsOnMaxResults(array $previous): void
$query->setMaxResults(10);
$query->getResult();
- self::assertCount($cacheCount + 1, $cache->getValues());
+ self::assertCount($cacheCount, $cache->getValues());
}
/** @depends testQueryCacheDependsOnHints */
diff --git a/tests/Tests/ORM/Functional/Ticket/GH11112Test.php b/tests/Tests/ORM/Functional/Ticket/GH11112Test.php
new file mode 100644
index 00000000000..3dbdb533b33
--- /dev/null
+++ b/tests/Tests/ORM/Functional/Ticket/GH11112Test.php
@@ -0,0 +1,79 @@
+useModelSet('cms');
+ self::$queryCache = new ArrayAdapter();
+
+ parent::setUp();
+ }
+
+ public function testSimpleQueryHasLimitAndOffsetApplied(): void
+ {
+ $platform = $this->_em->getConnection()->getDatabasePlatform();
+ $query = $this->_em->createQuery('SELECT u FROM ' . CmsUser::class . ' u');
+ $originalSql = $query->getSQL();
+
+ $query->setMaxResults(10);
+ $query->setFirstResult(20);
+ $sqlMax10First20 = $query->getSQL();
+
+ $query->setMaxResults(30);
+ $query->setFirstResult(40);
+ $sqlMax30First40 = $query->getSQL();
+
+ // The SQL is platform specific and may even be something with outer SELECTS being added. So,
+ // derive the expected value at runtime through the platform.
+ self::assertSame($platform->modifyLimitQuery($originalSql, 10, 20), $sqlMax10First20);
+ self::assertSame($platform->modifyLimitQuery($originalSql, 30, 40), $sqlMax30First40);
+
+ $cacheEntries = self::$queryCache->getValues();
+ self::assertCount(1, $cacheEntries);
+ }
+
+ public function testSubqueryLimitAndOffsetAreIgnored(): void
+ {
+ // Not sure what to do about this test. Basically, I want to make sure that
+ // firstResult/maxResult for subqueries are not relevant, they do not make it
+ // into the final query at all. That would give us the guarantee that the
+ // "sql finalizer" step is sufficient for the final, "outer" query and we
+ // do not need to run finalizers for the subqueries.
+
+ // This DQL/query makes no sense, it's just about creating a subquery in the first place
+ $queryBuilder = $this->_em->createQueryBuilder();
+ $queryBuilder
+ ->select('o')
+ ->from(CmsUser::class, 'o')
+ ->where($queryBuilder->expr()->exists(
+ $this->_em->createQueryBuilder()
+ ->select('u')
+ ->from(CmsUser::class, 'u')
+ ->setFirstResult(10)
+ ->setMaxResults(20)
+ ));
+
+ $query = $queryBuilder->getQuery();
+ $originalSql = $query->getSQL();
+
+ $clone = clone $query;
+ $clone->setFirstResult(24);
+ $clone->setMaxResults(42);
+ $limitedSql = $clone->getSQL();
+
+ $platform = $this->_em->getConnection()->getDatabasePlatform();
+
+ // The SQL is platform specific and may even be something with outer SELECTS being added. So,
+ // derive the expected value at runtime through the platform.
+ self::assertSame($platform->modifyLimitQuery($originalSql, 42, 24), $limitedSql);
+ }
+}
diff --git a/tests/Tests/ORM/Query/CustomTreeWalkersTest.php b/tests/Tests/ORM/Query/CustomTreeWalkersTest.php
index b2267078ff5..eb27b6113a3 100644
--- a/tests/Tests/ORM/Query/CustomTreeWalkersTest.php
+++ b/tests/Tests/ORM/Query/CustomTreeWalkersTest.php
@@ -6,6 +6,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
+use Doctrine\ORM\Query\AST;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\TreeWalker;
@@ -110,13 +111,13 @@ public function testSupportsSeveralHintsQueries(): void
}
}
-class AddUnknownQueryComponentWalker extends Query\SqlWalker
+class AddUnknownQueryComponentWalker extends Query\SqlOutputWalker
{
- public function walkSelectStatement(Query\AST\SelectStatement $selectStatement): void
+ protected function createSqlForFinalizer(AST\SelectStatement $AST): string
{
- parent::walkSelectStatement($selectStatement);
-
$this->setQueryComponent('x', []);
+
+ return parent::createSqlForFinalizer($AST);
}
}
diff --git a/tests/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php b/tests/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php
index cb69a3a5674..95d2047d593 100644
--- a/tests/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php
+++ b/tests/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php
@@ -10,6 +10,7 @@
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Query;
use Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker;
+use Symfony\Component\Cache\Adapter\ArrayAdapter;
use function class_exists;
@@ -385,6 +386,28 @@ public function testLimitSubqueryOrderBySubSelectOrderByExpressionOracle(): void
);
}
+ public function testParsingQueryWithDifferentLimitOffsetValuesTakesOnlyOneCacheEntry(): void
+ {
+ $queryCache = new ArrayAdapter();
+ $this->entityManager->getConfiguration()->setQueryCache($queryCache);
+
+ $query = $this->createQuery('SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a');
+
+ self::assertSame(
+ 'SELECT DISTINCT id_0 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 LIMIT 20 OFFSET 10',
+ $query->getSQL()
+ );
+
+ $query->setFirstResult(30)->setMaxResults(40);
+
+ self::assertSame(
+ 'SELECT DISTINCT id_0 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 LIMIT 40 OFFSET 30',
+ $query->getSQL()
+ );
+
+ self::assertCount(1, $queryCache->getValues());
+ }
+
private function createQuery(string $dql): Query
{
$query = $this->entityManager->createQuery($dql);
diff --git a/tests/Tests/ORM/Tools/Pagination/PaginatorTest.php b/tests/Tests/ORM/Tools/Pagination/PaginatorTest.php
index 1c8b47ac0cf..23ff3b90be4 100644
--- a/tests/Tests/ORM/Tools/Pagination/PaginatorTest.php
+++ b/tests/Tests/ORM/Tools/Pagination/PaginatorTest.php
@@ -87,7 +87,8 @@ public function testExtraParametersAreStrippedWhenWalkerRemovingOriginalSelectEl
public function testPaginatorNotCaringAboutExtraParametersWithoutOutputWalkers(): void
{
- $this->connection->expects(self::exactly(3))->method('executeQuery');
+ $result = $this->getMockBuilder(Result::class)->disableOriginalConstructor()->getMock();
+ $this->connection->expects(self::exactly(3))->method('executeQuery')->willReturn($result);
$this->createPaginatorWithExtraParametersWithoutOutputWalkers([])->count();
$this->createPaginatorWithExtraParametersWithoutOutputWalkers([[10]])->count();
@@ -96,7 +97,8 @@ public function testPaginatorNotCaringAboutExtraParametersWithoutOutputWalkers()
public function testgetIteratorDoesCareAboutExtraParametersWithoutOutputWalkersWhenResultIsNotEmpty(): void
{
- $this->connection->expects(self::exactly(1))->method('executeQuery');
+ $result = $this->getMockBuilder(Result::class)->disableOriginalConstructor()->getMock();
+ $this->connection->expects(self::exactly(1))->method('executeQuery')->willReturn($result);
$this->expectException(Query\QueryException::class);
$this->expectExceptionMessage('Too many parameters: the query defines 1 parameters and you bound 2');
diff --git a/tests/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php b/tests/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php
index 1f1710d2cf1..6f5c9c6dd22 100644
--- a/tests/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php
+++ b/tests/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php
@@ -21,9 +21,10 @@ public function testDqlQueryTransformation(string $dql, string $expectedSql): vo
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [WhereInWalker::class]);
$query->setHint(WhereInWalker::HINT_PAGINATOR_HAS_IDS, true);
- $result = (new Parser($query))->parse();
+ $result = (new Parser($query))->parse();
+ $executor = $result->prepareSqlExecutor($query);
- self::assertEquals($expectedSql, $result->getSqlExecutor()->getSqlStatements());
+ self::assertEquals($expectedSql, $executor->getSqlStatements());
self::assertEquals([0], $result->getSqlParameterPositions(WhereInWalker::PAGINATOR_ID_ALIAS));
}