Completed
Pull Request — master (#6500)
by Mathew
17:49 queued 08:41
created

CountWalker::walkSelectStatement()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4.0092

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 22
cts 24
cp 0.9167
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 23
nc 4
nop 1
crap 4.0092
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Tools\Pagination;
21
22
use Doctrine\ORM\Query\TreeWalkerAdapter;
23
use Doctrine\ORM\Query\AST\SelectStatement;
24
use Doctrine\ORM\Query\AST\SelectExpression;
25
use Doctrine\ORM\Query\AST\PathExpression;
26
use Doctrine\ORM\Query\AST\AggregateExpression;
27
28
/**
29
 * Replaces the selectClause of the AST with a COUNT statement.
30
 *
31
 * @category    DoctrineExtensions
32
 * @package     DoctrineExtensions\Paginate
33
 * @author      David Abdemoulaie <[email protected]>
34
 * @copyright   Copyright (c) 2010 David Abdemoulaie (http://hobodave.com/)
35
 * @license     http://hobodave.com/license.txt New BSD License
36
 */
37
class CountWalker extends TreeWalkerAdapter
38
{
39
    /**
40
     * Distinct mode hint name.
41
     */
42
    const HINT_DISTINCT = 'doctrine_paginator.distinct';
43
44
    /**
45
     * Walks down a SelectStatement AST node, modifying it to retrieve a COUNT.
46
     *
47
     * @param SelectStatement $AST
48
     *
49
     * @return void
50
     *
51
     * @throws \RuntimeException
52
     */
53 10
    public function walkSelectStatement(SelectStatement $AST)
54
    {
55 10
        if ($AST->havingClause) {
56
            throw new \RuntimeException('Cannot count query that uses a HAVING clause. Use the output walkers for pagination');
57
        }
58
59 10
        $queryComponents = $this->_getQueryComponents();
60
        // Get the root entity and alias from the AST fromClause
61 10
        $from = $AST->fromClause->identificationVariableDeclarations;
62
63 10
        if (count($from) > 1) {
64
            throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction");
65
        }
66
67 10
        $fromRoot            = reset($from);
68 10
        $rootAlias           = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable;
69 10
        $rootClass           = $queryComponents[$rootAlias]['metadata'];
70 10
        $identifierFieldName = $rootClass->getSingleIdentifierFieldName();
71
72 10
        $pathType = PathExpression::TYPE_STATE_FIELD;
73 10
        if (isset($rootClass->associationMappings[$identifierFieldName])) {
74 1
            $pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
75
        }
76
77 10
        $pathExpression = new PathExpression(
78 10
            PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $rootAlias,
79 10
            $identifierFieldName
80
        );
81 10
        $pathExpression->type = $pathType;
82
83 10
        $distinct = $this->_getQuery()->getHint(self::HINT_DISTINCT);
84 10
        $AST->selectClause->selectExpressions = [
85 10
            new SelectExpression(
86 10
                new AggregateExpression('count', $pathExpression, $distinct), null
87
            )
88
        ];
89
90
        // ORDER BY is not needed, only increases query execution through unnecessary sorting.
91 10
        $AST->orderByClause = null;
92 10
    }
93
}
94
95