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
CountOutputWalker.php
78 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Tools\Pagination; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\DBAL\Platforms\AbstractPlatform; |
| 6 | use MailPoetVendor\Doctrine\DBAL\Platforms\SQLServerPlatform; |
| 7 | use MailPoetVendor\Doctrine\ORM\Query; |
| 8 | use MailPoetVendor\Doctrine\ORM\Query\AST\SelectStatement; |
| 9 | use MailPoetVendor\Doctrine\ORM\Query\Parser; |
| 10 | use MailPoetVendor\Doctrine\ORM\Query\ParserResult; |
| 11 | use MailPoetVendor\Doctrine\ORM\Query\ResultSetMapping; |
| 12 | use MailPoetVendor\Doctrine\ORM\Query\SqlOutputWalker; |
| 13 | use RuntimeException; |
| 14 | use function array_diff; |
| 15 | use function array_keys; |
| 16 | use function count; |
| 17 | use function implode; |
| 18 | use function reset; |
| 19 | use function sprintf; |
| 20 | class CountOutputWalker extends SqlOutputWalker |
| 21 | { |
| 22 | private $platform; |
| 23 | private $rsm; |
| 24 | public function __construct($query, $parserResult, array $queryComponents) |
| 25 | { |
| 26 | $this->platform = $query->getEntityManager()->getConnection()->getDatabasePlatform(); |
| 27 | $this->rsm = $parserResult->getResultSetMapping(); |
| 28 | parent::__construct($query, $parserResult, $queryComponents); |
| 29 | } |
| 30 | protected function createSqlForFinalizer(SelectStatement $AST) : string |
| 31 | { |
| 32 | if ($this->platform instanceof SQLServerPlatform) { |
| 33 | $AST->orderByClause = null; |
| 34 | } |
| 35 | $sql = parent::createSqlForFinalizer($AST); |
| 36 | if ($AST->groupByClause) { |
| 37 | return sprintf('SELECT COUNT(*) AS dctrn_count FROM (%s) dctrn_table', $sql); |
| 38 | } |
| 39 | // Find out the SQL alias of the identifier column of the root entity |
| 40 | // It may be possible to make this work with multiple root entities but that |
| 41 | // would probably require issuing multiple queries or doing a UNION SELECT |
| 42 | // so for now, It's not supported. |
| 43 | // Get the root entity and alias from the AST fromClause |
| 44 | $from = $AST->fromClause->identificationVariableDeclarations; |
| 45 | if (count($from) > 1) { |
| 46 | throw new RuntimeException('Cannot count query which selects two FROM components, cannot make distinction'); |
| 47 | } |
| 48 | $fromRoot = reset($from); |
| 49 | $rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable; |
| 50 | $rootClass = $this->getMetadataForDqlAlias($rootAlias); |
| 51 | $rootIdentifier = $rootClass->identifier; |
| 52 | // For every identifier, find out the SQL alias by combing through the ResultSetMapping |
| 53 | $sqlIdentifier = []; |
| 54 | foreach ($rootIdentifier as $property) { |
| 55 | if (isset($rootClass->fieldMappings[$property])) { |
| 56 | foreach (array_keys($this->rsm->fieldMappings, $property, \true) as $alias) { |
| 57 | if ($this->rsm->columnOwnerMap[$alias] === $rootAlias) { |
| 58 | $sqlIdentifier[$property] = $alias; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | if (isset($rootClass->associationMappings[$property])) { |
| 63 | $joinColumn = $rootClass->associationMappings[$property]['joinColumns'][0]['name']; |
| 64 | foreach (array_keys($this->rsm->metaMappings, $joinColumn, \true) as $alias) { |
| 65 | if ($this->rsm->columnOwnerMap[$alias] === $rootAlias) { |
| 66 | $sqlIdentifier[$property] = $alias; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | if (count($rootIdentifier) !== count($sqlIdentifier)) { |
| 72 | throw new RuntimeException(sprintf('Not all identifier properties can be found in the ResultSetMapping: %s', implode(', ', array_diff($rootIdentifier, array_keys($sqlIdentifier))))); |
| 73 | } |
| 74 | // Build the counter query |
| 75 | return sprintf('SELECT COUNT(*) AS dctrn_count FROM (SELECT DISTINCT %s FROM (%s) dctrn_result) dctrn_table', implode(', ', $sqlIdentifier), $sql); |
| 76 | } |
| 77 | } |
| 78 |