Skip to content

Commit

Permalink
Make Paginator use simpler queries when there are no sql joins used (d…
Browse files Browse the repository at this point in the history
…octrine#8278)

This is work in progress.
  • Loading branch information
d-ph committed Nov 1, 2024
1 parent b2f087d commit c06e100
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/Tools/Pagination/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public static function newWithAutoDetection(Query|QueryBuilder $query): self

$queryAST = $query->getAST();
[
'queryHasGroupByClause' => $queryHasGroupByClause,
'queryHasHavingClause' => $queryHasHavingClause,
'hasGroupByClause' => $queryHasGroupByClause,
'hasHavingClause' => $queryHasHavingClause,
'rootEntityHasSingleIdentifierFieldName' => $rootEntityHasSingleIdentifierFieldName,
'queryCouldProduceDuplicates' => $queryCouldProduceDuplicates,
'queryCouldHaveToManyJoins' => $queryCouldHaveToManyJoins,
'couldProduceDuplicates' => $queryCouldProduceDuplicates,
'couldHaveToManyJoins' => $queryCouldHaveToManyJoins,
] = self::autoDetectQueryFeatures($query->getEntityManager(), $queryAST);

$paginator = new self($query, $queryCouldProduceDuplicates);
Expand All @@ -109,30 +109,30 @@ public static function newWithAutoDetection(Query|QueryBuilder $query): self

/**
* @return array{
* queryHasGroupByClause: bool|null,
* queryHasHavingClause: bool|null,
* hasGroupByClause: bool|null,
* hasHavingClause: bool|null,
* rootEntityHasSingleIdentifierFieldName: bool|null,
* queryCouldProduceDuplicates: bool,
* queryCouldHaveToManyJoins: bool,
* couldProduceDuplicates: bool,
* couldHaveToManyJoins: bool,
* }
*/
private static function autoDetectQueryFeatures(EntityManagerInterface $entityManager, Node $queryAST): array
{
$queryFeatures = [
// Null means undetermined
'queryHasGroupByClause' => null,
'queryHasHavingClause' => null,
'hasGroupByClause' => null,
'hasHavingClause' => null,
'rootEntityHasSingleIdentifierFieldName' => null,
'queryCouldProduceDuplicates' => true,
'queryCouldHaveToManyJoins' => true,
'couldProduceDuplicates' => true,
'couldHaveToManyJoins' => true,
];

if (! $queryAST instanceof Query\AST\SelectStatement) {
return $queryFeatures;
}

$queryFeatures['queryHasGroupByClause'] = $queryAST->groupByClause !== null;
$queryFeatures['queryHasHavingClause'] = $queryAST->havingClause !== null;
$queryFeatures['hasGroupByClause'] = $queryAST->groupByClause !== null;
$queryFeatures['hasHavingClause'] = $queryAST->havingClause !== null;

$from = $queryAST->fromClause->identificationVariableDeclarations;
if (count($from) > 1) {
Expand Down Expand Up @@ -209,7 +209,7 @@ private static function autoDetectQueryFeatures(EntityManagerInterface $entityMa
$toManyJoinsAliases[] = $joinAlias;
}

$queryFeatures['queryCouldHaveToManyJoins'] = count($toManyJoinsAliases) > 0;
$queryFeatures['couldHaveToManyJoins'] = count($toManyJoinsAliases) > 0;

// Check the Select list.
foreach ($queryAST->selectClause->selectExpressions as $selectExpression) {
Expand Down Expand Up @@ -254,7 +254,7 @@ private static function autoDetectQueryFeatures(EntityManagerInterface $entityMa
return $queryFeatures;
}

$queryFeatures['queryCouldProduceDuplicates'] = false;
$queryFeatures['couldProduceDuplicates'] = false;

return $queryFeatures;
}
Expand Down Expand Up @@ -430,11 +430,11 @@ private function cloneQuery(Query $query): Query
*/
private function useOutputWalker(Query $query, bool $forCountQuery = false): bool
{
if ($forCountQuery === false && $this->useResultQueryOutputWalker !== null) {
if (! $forCountQuery && $this->useResultQueryOutputWalker !== null) {
return $this->useResultQueryOutputWalker;
}

if ($forCountQuery === true && $this->useCountQueryOutputWalker !== null) {
if ($forCountQuery && $this->useCountQueryOutputWalker !== null) {
return $this->useCountQueryOutputWalker;
}

Expand Down

0 comments on commit c06e100

Please sign in to comment.