Exception
9 months ago
CountOutputWalker.php
9 months ago
CountWalker.php
9 months ago
LimitSubqueryOutputWalker.php
9 months ago
LimitSubqueryWalker.php
9 months ago
Paginator.php
9 months ago
RootTypeWalker.php
9 months ago
RowNumberOverFunction.php
9 months ago
WhereInWalker.php
9 months ago
index.php
9 months ago
CountWalker.php
42 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Tools\Pagination; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\ORM\Query\AST\AggregateExpression; |
| 6 | use MailPoetVendor\Doctrine\ORM\Query\AST\PathExpression; |
| 7 | use MailPoetVendor\Doctrine\ORM\Query\AST\SelectExpression; |
| 8 | use MailPoetVendor\Doctrine\ORM\Query\AST\SelectStatement; |
| 9 | use MailPoetVendor\Doctrine\ORM\Query\TreeWalkerAdapter; |
| 10 | use RuntimeException; |
| 11 | use function count; |
| 12 | use function reset; |
| 13 | class CountWalker extends TreeWalkerAdapter |
| 14 | { |
| 15 | public const HINT_DISTINCT = 'doctrine_paginator.distinct'; |
| 16 | public function walkSelectStatement(SelectStatement $AST) |
| 17 | { |
| 18 | if ($AST->havingClause) { |
| 19 | throw new RuntimeException('Cannot count query that uses a HAVING clause. Use the output walkers for pagination'); |
| 20 | } |
| 21 | // Get the root entity and alias from the AST fromClause |
| 22 | $from = $AST->fromClause->identificationVariableDeclarations; |
| 23 | if (count($from) > 1) { |
| 24 | throw new RuntimeException('Cannot count query which selects two FROM components, cannot make distinction'); |
| 25 | } |
| 26 | $fromRoot = reset($from); |
| 27 | $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; |
| 28 | $rootClass = $this->getMetadataForDqlAlias($rootAlias); |
| 29 | $identifierFieldName = $rootClass->getSingleIdentifierFieldName(); |
| 30 | $pathType = PathExpression::TYPE_STATE_FIELD; |
| 31 | if (isset($rootClass->associationMappings[$identifierFieldName])) { |
| 32 | $pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION; |
| 33 | } |
| 34 | $pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $rootAlias, $identifierFieldName); |
| 35 | $pathExpression->type = $pathType; |
| 36 | $distinct = $this->_getQuery()->getHint(self::HINT_DISTINCT); |
| 37 | $AST->selectClause->selectExpressions = [new SelectExpression(new AggregateExpression('count', $pathExpression, $distinct), null)]; |
| 38 | // ORDER BY is not needed, only increases query execution through unnecessary sorting. |
| 39 | $AST->orderByClause = null; |
| 40 | } |
| 41 | } |
| 42 |