Completed
Pull Request — master (#6365)
by Daniel Tome
10:44
created

SqlWalker::walkOrderByItem()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 10
nc 4
nop 1
crap 3
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\Query;
21
22
use Doctrine\DBAL\LockMode;
23
use Doctrine\DBAL\Types\Type;
24
use Doctrine\ORM\Mapping\ClassMetadata;
25
use Doctrine\ORM\Mapping\ClassMetadataInfo;
26
use Doctrine\ORM\OptimisticLockException;
27
use Doctrine\ORM\Query;
28
use Doctrine\ORM\Utility\PersisterHelper;
29
30
/**
31
 * The SqlWalker is a TreeWalker that walks over a DQL AST and constructs
32
 * the corresponding SQL.
33
 *
34
 * @author Guilherme Blanco <[email protected]>
35
 * @author Roman Borschel <[email protected]>
36
 * @author Benjamin Eberlei <[email protected]>
37
 * @author Alexander <[email protected]>
38
 * @author Fabio B. Silva <[email protected]>
39
 * @since  2.0
40
 * @todo Rename: SQLWalker
41
 */
42
class SqlWalker implements TreeWalker
43
{
44
    /**
45
     * @var string
46
     */
47
    const HINT_DISTINCT = 'doctrine.distinct';
48
49
    /**
50
     * @var ResultSetMapping
51
     */
52
    private $rsm;
53
54
    /**
55
     * Counter for generating unique column aliases.
56
     *
57
     * @var integer
58
     */
59
    private $aliasCounter = 0;
60
61
    /**
62
     * Counter for generating unique table aliases.
63
     *
64
     * @var integer
65
     */
66
    private $tableAliasCounter = 0;
67
68
    /**
69
     * Counter for generating unique scalar result.
70
     *
71
     * @var integer
72
     */
73
    private $scalarResultCounter = 1;
74
75
    /**
76
     * Counter for generating unique parameter indexes.
77
     *
78
     * @var integer
79
     */
80
    private $sqlParamIndex = 0;
81
82
    /**
83
     * Counter for generating indexes.
84
     *
85
     * @var integer
86
     */
87
    private $newObjectCounter = 0;
88
89
    /**
90
     * @var ParserResult
91
     */
92
    private $parserResult;
93
94
    /**
95
     * @var \Doctrine\ORM\EntityManager
96
     */
97
    private $em;
98
99
    /**
100
     * @var \Doctrine\DBAL\Connection
101
     */
102
    private $conn;
103
104
    /**
105
     * @var \Doctrine\ORM\AbstractQuery
106
     */
107
    private $query;
108
109
    /**
110
     * @var array
111
     */
112
    private $tableAliasMap = [];
113
114
    /**
115
     * Map from result variable names to their SQL column alias names.
116
     *
117
     * @var array
118
     */
119
    private $scalarResultAliasMap = [];
120
121
    /**
122
     * Map from Table-Alias + Column-Name to OrderBy-Direction.
123
     *
124
     * @var array
125
     */
126
    private $orderedColumnsMap = [];
127
128
    /**
129
     * Map from DQL-Alias + Field-Name to SQL Column Alias.
130
     *
131
     * @var array
132
     */
133
    private $scalarFields = [];
134
135
    /**
136
     * Map of all components/classes that appear in the DQL query.
137
     *
138
     * @var array
139
     */
140
    private $queryComponents;
141
142
    /**
143
     * A list of classes that appear in non-scalar SelectExpressions.
144
     *
145
     * @var array
146
     */
147
    private $selectedClasses = [];
148
149
    /**
150
     * The DQL alias of the root class of the currently traversed query.
151
     *
152
     * @var array
153
     */
154
    private $rootAliases = [];
155
156
    /**
157
     * Flag that indicates whether to generate SQL table aliases in the SQL.
158
     * These should only be generated for SELECT queries, not for UPDATE/DELETE.
159
     *
160
     * @var boolean
161
     */
162
    private $useSqlTableAliases = true;
163
164
    /**
165
     * The database platform abstraction.
166
     *
167
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
168
     */
169
    private $platform;
170
171
    /**
172
     * The quote strategy.
173
     *
174
     * @var \Doctrine\ORM\Mapping\QuoteStrategy
175
     */
176
    private $quoteStrategy;
177
178
    /**
179
     * {@inheritDoc}
180
     */
181 346
    public function __construct($query, $parserResult, array $queryComponents)
182
    {
183 346
        $this->query            = $query;
184 346
        $this->parserResult     = $parserResult;
185 346
        $this->queryComponents  = $queryComponents;
186 346
        $this->rsm              = $parserResult->getResultSetMapping();
187 346
        $this->em               = $query->getEntityManager();
188 346
        $this->conn             = $this->em->getConnection();
189 346
        $this->platform         = $this->conn->getDatabasePlatform();
190 346
        $this->quoteStrategy    = $this->em->getConfiguration()->getQuoteStrategy();
191 346
    }
192
193
    /**
194
     * Gets the Query instance used by the walker.
195
     *
196
     * @return Query.
0 ignored issues
show
Documentation introduced by
The doc-type Query. could not be parsed: Unknown type name "Query." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
197
     */
198
    public function getQuery()
199
    {
200
        return $this->query;
201
    }
202
203
    /**
204
     * Gets the Connection used by the walker.
205
     *
206
     * @return \Doctrine\DBAL\Connection
207
     */
208 18
    public function getConnection()
209
    {
210 18
        return $this->conn;
211
    }
212
213
    /**
214
     * Gets the EntityManager used by the walker.
215
     *
216
     * @return \Doctrine\ORM\EntityManager
217
     */
218 18
    public function getEntityManager()
219
    {
220 18
        return $this->em;
221
    }
222
223
    /**
224
     * Gets the information about a single query component.
225
     *
226
     * @param string $dqlAlias The DQL alias.
227
     *
228
     * @return array
229
     */
230 16
    public function getQueryComponent($dqlAlias)
231
    {
232 16
        return $this->queryComponents[$dqlAlias];
233
    }
234
235
    /**
236
     * {@inheritdoc}
237
     */
238
    public function getQueryComponents()
239
    {
240
        return $this->queryComponents;
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     */
246 1
    public function setQueryComponent($dqlAlias, array $queryComponent)
247
    {
248 1
        $requiredKeys = ['metadata', 'parent', 'relation', 'map', 'nestingLevel', 'token'];
249
250 1
        if (array_diff($requiredKeys, array_keys($queryComponent))) {
251 1
            throw QueryException::invalidQueryComponent($dqlAlias);
252
        }
253
254
        $this->queryComponents[$dqlAlias] = $queryComponent;
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260 340
    public function getExecutor($AST)
261
    {
262
        switch (true) {
263 340
            case ($AST instanceof AST\DeleteStatement):
264 32
                $primaryClass = $this->em->getClassMetadata($AST->deleteClause->abstractSchemaName);
265
266 32
                return ($primaryClass->isInheritanceTypeJoined())
267
                    ? new Exec\MultiTableDeleteExecutor($AST, $this)
268 32
                    : new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
269
270 287
            case ($AST instanceof AST\UpdateStatement):
271 23
                $primaryClass = $this->em->getClassMetadata($AST->updateClause->abstractSchemaName);
272
273 23
                return ($primaryClass->isInheritanceTypeJoined())
274 2
                    ? new Exec\MultiTableUpdateExecutor($AST, $this)
275 23
                    : new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
276
277
            default:
278 287
                return new Exec\SingleSelectExecutor($AST, $this);
279
        }
280
    }
281
282
    /**
283
     * Generates a unique, short SQL table alias.
284
     *
285
     * @param string $tableName Table name
286
     * @param string $dqlAlias  The DQL alias.
287
     *
288
     * @return string Generated table alias.
289
     */
290 298
    public function getSQLTableAlias($tableName, $dqlAlias = '')
291
    {
292 298
        $tableName .= ($dqlAlias) ? '@[' . $dqlAlias . ']' : '';
293
294 298
        if ( ! isset($this->tableAliasMap[$tableName])) {
295 298
            $this->tableAliasMap[$tableName] = (preg_match('/[a-z]/i', $tableName[0]) ? strtolower($tableName[0]) : 't')
296 298
                . $this->tableAliasCounter++ . '_';
297
        }
298
299 298
        return $this->tableAliasMap[$tableName];
300
    }
301
302
    /**
303
     * Forces the SqlWalker to use a specific alias for a table name, rather than
304
     * generating an alias on its own.
305
     *
306
     * @param string $tableName
307
     * @param string $alias
308
     * @param string $dqlAlias
309
     *
310
     * @return string
311
     */
312 54
    public function setSQLTableAlias($tableName, $alias, $dqlAlias = '')
313
    {
314 54
        $tableName .= ($dqlAlias) ? '@[' . $dqlAlias . ']' : '';
315
316 54
        $this->tableAliasMap[$tableName] = $alias;
317
318 54
        return $alias;
319
    }
320
321
    /**
322
     * Gets an SQL column alias for a column name.
323
     *
324
     * @param string $columnName
325
     *
326
     * @return string
327
     */
328 287
    public function getSQLColumnAlias($columnName)
329
    {
330 287
        return $this->quoteStrategy->getColumnAlias($columnName, $this->aliasCounter++, $this->platform);
331
    }
332
333
    /**
334
     * Generates the SQL JOINs that are necessary for Class Table Inheritance
335
     * for the given class.
336
     *
337
     * @param ClassMetadata $class    The class for which to generate the joins.
338
     * @param string        $dqlAlias The DQL alias of the class.
339
     *
340
     * @return string The SQL.
341
     */
342 35
    private function _generateClassTableInheritanceJoins($class, $dqlAlias)
343
    {
344 35
        $sql = '';
345
346 35
        $baseTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
347
348
        // INNER JOIN parent class tables
349 35
        foreach ($class->parentClasses as $parentClassName) {
350 23
            $parentClass = $this->em->getClassMetadata($parentClassName);
351 23
            $tableAlias  = $this->getSQLTableAlias($parentClass->getTableName(), $dqlAlias);
352
353
            // If this is a joined association we must use left joins to preserve the correct result.
354 23
            $sql .= isset($this->queryComponents[$dqlAlias]['relation']) ? ' LEFT ' : ' INNER ';
355 23
            $sql .= 'JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON ';
356
357 23
            $sqlParts = [];
358
359 23
            foreach ($this->quoteStrategy->getIdentifierColumnNames($class, $this->platform) as $columnName) {
360 23
                $sqlParts[] = $baseTableAlias . '.' . $columnName . ' = ' . $tableAlias . '.' . $columnName;
361
            }
362
363
            // Add filters on the root class
364 23
            if ($filterSql = $this->generateFilterConditionSQL($parentClass, $tableAlias)) {
365
                $sqlParts[] = $filterSql;
366
            }
367
368 23
            $sql .= implode(' AND ', $sqlParts);
369
        }
370
371
        // Ignore subclassing inclusion if partial objects is disallowed
372 35
        if ($this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
373 21
            return $sql;
374
        }
375
376
        // LEFT JOIN child class tables
377 14
        foreach ($class->subClasses as $subClassName) {
378 10
            $subClass   = $this->em->getClassMetadata($subClassName);
379 10
            $tableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
380
381 10
            $sql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->platform) . ' ' . $tableAlias . ' ON ';
382
383 10
            $sqlParts = [];
384
385 10
            foreach ($this->quoteStrategy->getIdentifierColumnNames($subClass, $this->platform) as $columnName) {
386 10
                $sqlParts[] = $baseTableAlias . '.' . $columnName . ' = ' . $tableAlias . '.' . $columnName;
387
            }
388
389 10
            $sql .= implode(' AND ', $sqlParts);
390
        }
391
392 14
        return $sql;
393
    }
394
395
    /**
396
     * @return string
397
     */
398 281
    private function _generateOrderedCollectionOrderByItems()
399
    {
400 281
        $orderedColumns = [];
401
402 281
        foreach ($this->selectedClasses as $selectedClass) {
403 200
            $dqlAlias  = $selectedClass['dqlAlias'];
404 200
            $qComp     = $this->queryComponents[$dqlAlias];
405
406 200
            if ( ! isset($qComp['relation']['orderBy'])) {
407 200
                continue;
408
            }
409
410 2
            $persister = $this->em->getUnitOfWork()->getEntityPersister($qComp['metadata']->name);
411
412 2
            foreach ($qComp['relation']['orderBy'] as $fieldName => $orientation) {
413 2
                $columnName = $this->quoteStrategy->getColumnName($fieldName, $qComp['metadata'], $this->platform);
414 2
                $tableName  = ($qComp['metadata']->isInheritanceTypeJoined())
415
                    ? $persister->getOwningTable($fieldName)
416 2
                    : $qComp['metadata']->getTableName();
417
418 2
                $orderedColumn = $this->getSQLTableAlias($tableName, $dqlAlias) . '.' . $columnName;
419
420
                // OrderByClause should replace an ordered relation. see - DDC-2475
421 2
                if (isset($this->orderedColumnsMap[$orderedColumn])) {
422 1
                    continue;
423
                }
424
425 2
                $this->orderedColumnsMap[$orderedColumn] = $orientation;
426 2
                $orderedColumns[] = $orderedColumn . ' ' . $orientation;
427
            }
428
        }
429
430 281
        return implode(', ', $orderedColumns);
431
    }
432
433
    /**
434
     * Generates a discriminator column SQL condition for the class with the given DQL alias.
435
     *
436
     * @param array $dqlAliases List of root DQL aliases to inspect for discriminator restrictions.
437
     *
438
     * @return string
439
     */
440 335
    private function _generateDiscriminatorColumnConditionSQL(array $dqlAliases)
441
    {
442 335
        $sqlParts = [];
443
444 335
        foreach ($dqlAliases as $dqlAlias) {
445 335
            $class = $this->queryComponents[$dqlAlias]['metadata'];
446
447 335
            if ( ! $class->isInheritanceTypeSingleTable()) continue;
448
449 15
            $conn   = $this->em->getConnection();
450 15
            $values = [];
451
452 15
            if ($class->discriminatorValue !== null) { // discriminators can be 0
453 7
                $values[] = $conn->quote($class->discriminatorValue);
454
            }
455
456 15
            foreach ($class->subClasses as $subclassName) {
457 11
                $values[] = $conn->quote($this->em->getClassMetadata($subclassName)->discriminatorValue);
458
            }
459
460 15
            $sqlTableAlias = ($this->useSqlTableAliases)
461 14
                ? $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.'
462 15
                : '';
463
464 15
            $sqlParts[] = $sqlTableAlias . $class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')';
465
        }
466
467 335
        $sql = implode(' AND ', $sqlParts);
468
469 335
        return (count($sqlParts) > 1) ? '(' . $sql . ')' : $sql;
470
    }
471
472
    /**
473
     * Generates the filter SQL for a given entity and table alias.
474
     *
475
     * @param ClassMetadata $targetEntity     Metadata of the target entity.
476
     * @param string        $targetTableAlias The table alias of the joined/selected table.
477
     *
478
     * @return string The SQL query part to add to a query.
479
     */
480 103
    private function generateFilterConditionSQL(ClassMetadata $targetEntity, $targetTableAlias)
481
    {
482 103
        if (!$this->em->hasFilters()) {
483 101
            return '';
484
        }
485
486 2
        switch($targetEntity->inheritanceType) {
487 2
            case ClassMetadata::INHERITANCE_TYPE_NONE:
488 2
                break;
489
            case ClassMetadata::INHERITANCE_TYPE_JOINED:
490
                // The classes in the inheritance will be added to the query one by one,
491
                // but only the root node is getting filtered
492
                if ($targetEntity->name !== $targetEntity->rootEntityName) {
493
                    return '';
494
                }
495
                break;
496
            case ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE:
497
                // With STI the table will only be queried once, make sure that the filters
498
                // are added to the root entity
499
                $targetEntity = $this->em->getClassMetadata($targetEntity->rootEntityName);
500
                break;
501
            default:
502
                //@todo: throw exception?
503
                return '';
504
        }
505
506 2
        $filterClauses = [];
507 2
        foreach ($this->em->getFilters()->getEnabledFilters() as $filter) {
508 2
            if ('' !== $filterExpr = $filter->addFilterConstraint($targetEntity, $targetTableAlias)) {
509 2
                $filterClauses[] = '(' . $filterExpr . ')';
510
            }
511
        }
512
513 2
        return implode(' AND ', $filterClauses);
514
    }
515
516
    /**
517
     * {@inheritdoc}
518
     */
519 287
    public function walkSelectStatement(AST\SelectStatement $AST)
520
    {
521 287
        $limit    = $this->query->getMaxResults();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Doctrine\ORM\AbstractQuery as the method getMaxResults() does only exist in the following sub-classes of Doctrine\ORM\AbstractQuery: Doctrine\ORM\Query. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
522 287
        $offset   = $this->query->getFirstResult();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Doctrine\ORM\AbstractQuery as the method getFirstResult() does only exist in the following sub-classes of Doctrine\ORM\AbstractQuery: Doctrine\ORM\Query. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
523 287
        $lockMode = $this->query->getHint(Query::HINT_LOCK_MODE);
524 287
        $sql      = $this->walkSelectClause($AST->selectClause)
525 287
            . $this->walkFromClause($AST->fromClause)
526 284
            . $this->walkWhereClause($AST->whereClause);
0 ignored issues
show
Bug introduced by
It seems like $AST->whereClause can be null; however, walkWhereClause() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
527
528 282
        if ($AST->groupByClause) {
529 13
            $sql .= $this->walkGroupByClause($AST->groupByClause);
530
        }
531
532 282
        if ($AST->havingClause) {
533 10
            $sql .= $this->walkHavingClause($AST->havingClause);
534
        }
535
536 282
        if ($AST->orderByClause) {
537 23
            $sql .= $this->walkOrderByClause($AST->orderByClause);
538
        }
539
540 281
        if ( ! $AST->orderByClause && ($orderBySql = $this->_generateOrderedCollectionOrderByItems())) {
541 2
            $sql .= ' ORDER BY ' . $orderBySql;
542
        }
543
544 281
        if ($limit !== null || $offset !== null) {
545 5
            $sql = $this->platform->modifyLimitQuery($sql, $limit, $offset);
546
        }
547
548 281
        if ($lockMode === null || $lockMode === false || $lockMode === LockMode::NONE) {
549 276
            return $sql;
550
        }
551
552 5
        if ($lockMode === LockMode::PESSIMISTIC_READ) {
553 3
            return $sql . ' ' . $this->platform->getReadLockSQL();
554
        }
555
556 2
        if ($lockMode === LockMode::PESSIMISTIC_WRITE) {
557 1
            return $sql . ' ' . $this->platform->getWriteLockSQL();
558
        }
559
560 1
        if ($lockMode !== LockMode::OPTIMISTIC) {
561
            throw QueryException::invalidLockMode();
562
        }
563
564 1
        foreach ($this->selectedClasses as $selectedClass) {
565 1
            if ( ! $selectedClass['class']->isVersioned) {
566 1
                throw OptimisticLockException::lockFailed($selectedClass['class']->name);
567
            }
568
        }
569
570
        return $sql;
571
    }
572
573
    /**
574
     * {@inheritdoc}
575
     */
576 21
    public function walkUpdateStatement(AST\UpdateStatement $AST)
577
    {
578 21
        $this->useSqlTableAliases = false;
579 21
        $this->rsm->isSelect      = false;
580
581 21
        return $this->walkUpdateClause($AST->updateClause)
582 21
            . $this->walkWhereClause($AST->whereClause);
0 ignored issues
show
Bug introduced by
It seems like $AST->whereClause can be null; however, walkWhereClause() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
583
    }
584
585
    /**
586
     * {@inheritdoc}
587
     */
588 32
    public function walkDeleteStatement(AST\DeleteStatement $AST)
589
    {
590 32
        $this->useSqlTableAliases = false;
591 32
        $this->rsm->isSelect      = false;
592
593 32
        return $this->walkDeleteClause($AST->deleteClause)
594 32
            . $this->walkWhereClause($AST->whereClause);
0 ignored issues
show
Bug introduced by
It seems like $AST->whereClause can be null; however, walkWhereClause() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
595
    }
596
597
    /**
598
     * Walks down an IdentificationVariable AST node, thereby generating the appropriate SQL.
599
     * This one differs of ->walkIdentificationVariable() because it generates the entity identifiers.
600
     *
601
     * @param string $identVariable
602
     *
603
     * @return string
604
     */
605 2
    public function walkEntityIdentificationVariable($identVariable)
606
    {
607 2
        $class      = $this->queryComponents[$identVariable]['metadata'];
608 2
        $tableAlias = $this->getSQLTableAlias($class->getTableName(), $identVariable);
609 2
        $sqlParts   = [];
610
611 2
        foreach ($this->quoteStrategy->getIdentifierColumnNames($class, $this->platform) as $columnName) {
612 2
            $sqlParts[] = $tableAlias . '.' . $columnName;
613
        }
614
615 2
        return implode(', ', $sqlParts);
616
    }
617
618
    /**
619
     * Walks down an IdentificationVariable (no AST node associated), thereby generating the SQL.
620
     *
621
     * @param string $identificationVariable
622
     * @param string $fieldName
623
     *
624
     * @return string The SQL.
625
     */
626 180
    public function walkIdentificationVariable($identificationVariable, $fieldName = null)
627
    {
628 180
        $class = $this->queryComponents[$identificationVariable]['metadata'];
629
630
        if (
631 180
            $fieldName !== null && $class->isInheritanceTypeJoined() &&
632 180
            isset($class->fieldMappings[$fieldName]['inherited'])
633
        ) {
634 7
            $class = $this->em->getClassMetadata($class->fieldMappings[$fieldName]['inherited']);
635
        }
636
637 180
        return $this->getSQLTableAlias($class->getTableName(), $identificationVariable);
638
    }
639
640
    /**
641
     * {@inheritdoc}
642
     */
643 234
    public function walkPathExpression($pathExpr)
644
    {
645 234
        $sql = '';
646
647 234
        switch ($pathExpr->type) {
648 234
            case AST\PathExpression::TYPE_STATE_FIELD:
649 223
                $fieldName = $pathExpr->field;
650 223
                $dqlAlias = $pathExpr->identificationVariable;
651 223
                $class = $this->queryComponents[$dqlAlias]['metadata'];
652
653 223
                if ($this->useSqlTableAliases) {
654 180
                    $sql .= $this->walkIdentificationVariable($dqlAlias, $fieldName) . '.';
655
                }
656
657 223
                $sql .= $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform);
658 223
                break;
659
660 35
            case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION:
661
                // 1- the owning side:
662
                //    Just use the foreign key, i.e. u.group_id
663 35
                $fieldName = $pathExpr->field;
664 35
                $dqlAlias = $pathExpr->identificationVariable;
665 35
                $class = $this->queryComponents[$dqlAlias]['metadata'];
666
667 35
                if (isset($class->associationMappings[$fieldName]['inherited'])) {
668 1
                    $class = $this->em->getClassMetadata($class->associationMappings[$fieldName]['inherited']);
669
                }
670
671 35
                $assoc = $class->associationMappings[$fieldName];
672
673 35
                if ( ! $assoc['isOwningSide']) {
674 2
                    throw QueryException::associationPathInverseSideNotSupported();
675
                }
676
677
                // COMPOSITE KEYS NOT (YET?) SUPPORTED
678 33
                if (count($assoc['sourceToTargetKeyColumns']) > 1) {
679
                    throw QueryException::associationPathCompositeKeyNotSupported();
680
                }
681
682 33
                if ($this->useSqlTableAliases) {
683 31
                    $sql .= $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.';
684
                }
685
686 33
                $sql .= reset($assoc['targetToSourceKeyColumns']);
687 33
                break;
688
689
            default:
690
                throw QueryException::invalidPathExpression($pathExpr);
691
        }
692
693 232
        return $sql;
694
    }
695
696
    /**
697
     * {@inheritdoc}
698
     */
699 287
    public function walkSelectClause($selectClause)
700
    {
701 287
        $sql = 'SELECT ' . (($selectClause->isDistinct) ? 'DISTINCT ' : '');
702 287
        $sqlSelectExpressions = array_filter(array_map([$this, 'walkSelectExpression'], $selectClause->selectExpressions));
703
704 287
        if ($this->query->getHint(Query::HINT_INTERNAL_ITERATION) == true && $selectClause->isDistinct) {
705 1
            $this->query->setHint(self::HINT_DISTINCT, true);
706
        }
707
708 287
        $addMetaColumns = ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD) &&
709 126
            $this->query->getHydrationMode() == Query::HYDRATE_OBJECT
710
            ||
711 167
            $this->query->getHydrationMode() != Query::HYDRATE_OBJECT &&
712 287
            $this->query->getHint(Query::HINT_INCLUDE_META_COLUMNS);
713
714 287
        foreach ($this->selectedClasses as $selectedClass) {
715 206
            $class       = $selectedClass['class'];
716 206
            $dqlAlias    = $selectedClass['dqlAlias'];
717 206
            $resultAlias = $selectedClass['resultAlias'];
718
719
            // Register as entity or joined entity result
720 206
            if ($this->queryComponents[$dqlAlias]['relation'] === null) {
721 206
                $this->rsm->addEntityResult($class->name, $dqlAlias, $resultAlias);
722
            } else {
723 45
                $this->rsm->addJoinedEntityResult(
724 45
                    $class->name,
725
                    $dqlAlias,
726 45
                    $this->queryComponents[$dqlAlias]['parent'],
727 45
                    $this->queryComponents[$dqlAlias]['relation']['fieldName']
728
                );
729
            }
730
731 206
            if ($class->isInheritanceTypeSingleTable() || $class->isInheritanceTypeJoined()) {
732
                // Add discriminator columns to SQL
733 33
                $rootClass   = $this->em->getClassMetadata($class->rootEntityName);
734 33
                $tblAlias    = $this->getSQLTableAlias($rootClass->getTableName(), $dqlAlias);
735 33
                $discrColumn = $rootClass->discriminatorColumn;
736 33
                $columnAlias = $this->getSQLColumnAlias($discrColumn['name']);
737
738 33
                $sqlSelectExpressions[] = $tblAlias . '.' . $discrColumn['name'] . ' AS ' . $columnAlias;
739
740 33
                $this->rsm->setDiscriminatorColumn($dqlAlias, $columnAlias);
741 33
                $this->rsm->addMetaResult($dqlAlias, $columnAlias, $discrColumn['fieldName'], false, $discrColumn['type']);
742
            }
743
744
            // Add foreign key columns to SQL, if necessary
745 206
            if ( ! $addMetaColumns && ! $class->containsForeignIdentifier) {
746 103
                continue;
747
            }
748
749
            // Add foreign key columns of class and also parent classes
750 104
            foreach ($class->associationMappings as $assoc) {
751 92
                if ( ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE)) {
752 70
                    continue;
753 71
                } else if ( !$addMetaColumns && !isset($assoc['id'])) {
754
                    continue;
755
                }
756
757 71
                $targetClass   = $this->em->getClassMetadata($assoc['targetEntity']);
758 71
                $isIdentifier  = (isset($assoc['id']) && $assoc['id'] === true);
759 71
                $owningClass   = (isset($assoc['inherited'])) ? $this->em->getClassMetadata($assoc['inherited']) : $class;
760 71
                $sqlTableAlias = $this->getSQLTableAlias($owningClass->getTableName(), $dqlAlias);
761
762 71
                foreach ($assoc['joinColumns'] as $joinColumn) {
763 71
                    $columnName  = $joinColumn['name'];
764 71
                    $columnAlias = $this->getSQLColumnAlias($columnName);
765 71
                    $columnType  = PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em);
766
767 71
                    $sqlSelectExpressions[] = $sqlTableAlias . '.' . $columnName . ' AS ' . $columnAlias;
768
769 71
                    $this->rsm->addMetaResult($dqlAlias, $columnAlias, $columnName, $isIdentifier, $columnType);
770
                }
771
            }
772
773
            // Add foreign key columns to SQL, if necessary
774 104
            if ( ! $addMetaColumns) {
775 2
                continue;
776
            }
777
778
            // Add foreign key columns of subclasses
779 102
            foreach ($class->subClasses as $subClassName) {
780 6
                $subClass      = $this->em->getClassMetadata($subClassName);
781 6
                $sqlTableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
782
783 6
                foreach ($subClass->associationMappings as $assoc) {
784
                    // Skip if association is inherited
785 6
                    if (isset($assoc['inherited'])) continue;
786
787 5
                    if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) {
788 4
                        $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
789
790 4
                        foreach ($assoc['joinColumns'] as $joinColumn) {
791 4
                            $columnName  = $joinColumn['name'];
792 4
                            $columnAlias = $this->getSQLColumnAlias($columnName);
793 4
                            $columnType  = PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em);
794
795 4
                            $sqlSelectExpressions[] = $sqlTableAlias . '.' . $columnName . ' AS ' . $columnAlias;
796
797 102
                            $this->rsm->addMetaResult($dqlAlias, $columnAlias, $columnName, $subClass->isIdentifier($columnName), $columnType);
798
                        }
799
                    }
800
                }
801
            }
802
        }
803
804 287
        $sql .= implode(', ', $sqlSelectExpressions);
805
806 287
        return $sql;
807
    }
808
809
    /**
810
     * {@inheritdoc}
811
     */
812 288
    public function walkFromClause($fromClause)
813
    {
814 288
        $identificationVarDecls = $fromClause->identificationVariableDeclarations;
815 288
        $sqlParts = [];
816
817 288
        foreach ($identificationVarDecls as $identificationVariableDecl) {
818 288
            $sqlParts[] = $this->walkIdentificationVariableDeclaration($identificationVariableDecl);
819
        }
820
821 285
        return ' FROM ' . implode(', ', $sqlParts);
822
    }
823
824
    /**
825
     * Walks down a IdentificationVariableDeclaration AST node, thereby generating the appropriate SQL.
826
     *
827
     * @param AST\IdentificationVariableDeclaration $identificationVariableDecl
828
     *
829
     * @return string
830
     */
831 289
    public function walkIdentificationVariableDeclaration($identificationVariableDecl)
832
    {
833 289
        $sql = $this->walkRangeVariableDeclaration($identificationVariableDecl->rangeVariableDeclaration);
0 ignored issues
show
Bug introduced by
It seems like $identificationVariableD...angeVariableDeclaration can be null; however, walkRangeVariableDeclaration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
834
835 289
        if ($identificationVariableDecl->indexBy) {
836 6
            $this->walkIndexBy($identificationVariableDecl->indexBy);
837
        }
838
839 289
        foreach ($identificationVariableDecl->joins as $join) {
840 90
            $sql .= $this->walkJoin($join);
841
        }
842
843 286
        return $sql;
844
    }
845
846
    /**
847
     * Walks down a IndexBy AST node.
848
     *
849
     * @param AST\IndexBy $indexBy
850
     *
851
     * @return void
852
     */
853 8
    public function walkIndexBy($indexBy)
854
    {
855 8
        $pathExpression = $indexBy->simpleStateFieldPathExpression;
856 8
        $alias          = $pathExpression->identificationVariable;
857 8
        $field          = $pathExpression->field;
858
859 8
        if (isset($this->scalarFields[$alias][$field])) {
860
            $this->rsm->addIndexByScalar($this->scalarFields[$alias][$field]);
861
862
            return;
863
        }
864
865 8
        $this->rsm->addIndexBy($alias, $field);
866 8
    }
867
868
    /**
869
     * Walks down a RangeVariableDeclaration AST node, thereby generating the appropriate SQL.
870
     *
871
     * @param AST\RangeVariableDeclaration $rangeVariableDeclaration
872
     *
873
     * @return string
874
     */
875 289
    public function walkRangeVariableDeclaration($rangeVariableDeclaration)
876
    {
877 289
        $class    = $this->em->getClassMetadata($rangeVariableDeclaration->abstractSchemaName);
878 289
        $dqlAlias = $rangeVariableDeclaration->aliasIdentificationVariable;
879
880 289
        if ($rangeVariableDeclaration->isRoot) {
881 289
            $this->rootAliases[] = $dqlAlias;
882
        }
883
884 289
        $sql = $this->platform->appendLockHint(
885 289
            $this->quoteStrategy->getTableName($class, $this->platform) . ' ' .
886 289
            $this->getSQLTableAlias($class->getTableName(), $dqlAlias),
887 289
            $this->query->getHint(Query::HINT_LOCK_MODE)
888
        );
889
890 289
        if ($class->isInheritanceTypeJoined()) {
891 33
            $sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias);
892
        }
893
894 289
        return $sql;
895
    }
896
897
    /**
898
     * Walks down a JoinAssociationDeclaration AST node, thereby generating the appropriate SQL.
899
     *
900
     * @param AST\JoinAssociationDeclaration $joinAssociationDeclaration
901
     * @param int                            $joinType
902
     * @param AST\ConditionalExpression      $condExpr
903
     *
904
     * @return string
905
     *
906
     * @throws QueryException
907
     */
908 75
    public function walkJoinAssociationDeclaration($joinAssociationDeclaration, $joinType = AST\Join::JOIN_TYPE_INNER, $condExpr = null)
909
    {
910 75
        $sql = '';
911
912 75
        $associationPathExpression = $joinAssociationDeclaration->joinAssociationPathExpression;
913 75
        $joinedDqlAlias            = $joinAssociationDeclaration->aliasIdentificationVariable;
914 75
        $indexBy                   = $joinAssociationDeclaration->indexBy;
915
916 75
        $relation        = $this->queryComponents[$joinedDqlAlias]['relation'];
917 75
        $targetClass     = $this->em->getClassMetadata($relation['targetEntity']);
918 75
        $sourceClass     = $this->em->getClassMetadata($relation['sourceEntity']);
919 75
        $targetTableName = $this->quoteStrategy->getTableName($targetClass, $this->platform);
920
921 75
        $targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName(), $joinedDqlAlias);
922 75
        $sourceTableAlias = $this->getSQLTableAlias($sourceClass->getTableName(), $associationPathExpression->identificationVariable);
923
924
        // Ensure we got the owning side, since it has all mapping info
925 75
        $assoc = ( ! $relation['isOwningSide']) ? $targetClass->associationMappings[$relation['mappedBy']] : $relation;
926
927 75
        if ($this->query->getHint(Query::HINT_INTERNAL_ITERATION) == true && (!$this->query->getHint(self::HINT_DISTINCT) || isset($this->selectedClasses[$joinedDqlAlias]))) {
928 3
            if ($relation['type'] == ClassMetadata::ONE_TO_MANY || $relation['type'] == ClassMetadata::MANY_TO_MANY) {
929 3
                throw QueryException::iterateWithFetchJoinNotAllowed($assoc);
930
            }
931
        }
932
933 72
        $targetTableJoin = null;
0 ignored issues
show
Unused Code introduced by
$targetTableJoin is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
934
935
        // This condition is not checking ClassMetadata::MANY_TO_ONE, because by definition it cannot
936
        // be the owning side and previously we ensured that $assoc is always the owning side of the associations.
937
        // The owning side is necessary at this point because only it contains the JoinColumn information.
938
        switch (true) {
939 72
            case ($assoc['type'] & ClassMetadata::TO_ONE):
940 52
                $conditions = [];
941
942 52
                foreach ($assoc['joinColumns'] as $joinColumn) {
943 52
                    $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
944 52
                    $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
945
946 52
                    if ($relation['isOwningSide']) {
947 29
                        $conditions[] = $sourceTableAlias . '.' . $quotedSourceColumn . ' = ' . $targetTableAlias . '.' . $quotedTargetColumn;
948
949 29
                        continue;
950
                    }
951
952 27
                    $conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $targetTableAlias . '.' . $quotedSourceColumn;
953
                }
954
955
                // Apply remaining inheritance restrictions
956 52
                $discrSql = $this->_generateDiscriminatorColumnConditionSQL([$joinedDqlAlias]);
957
958 52
                if ($discrSql) {
959
                    $conditions[] = $discrSql;
960
                }
961
962
                // Apply the filters
963 52
                $filterExpr = $this->generateFilterConditionSQL($targetClass, $targetTableAlias);
964
965 52
                if ($filterExpr) {
966
                    $conditions[] = $filterExpr;
967
                }
968
969
                $targetTableJoin = [
970 52
                    'table' => $targetTableName . ' ' . $targetTableAlias,
971 52
                    'condition' => implode(' AND ', $conditions),
972
                ];
973 52
                break;
974
975 21
            case ($assoc['type'] == ClassMetadata::MANY_TO_MANY):
976
                // Join relation table
977 21
                $joinTable      = $assoc['joinTable'];
978 21
                $joinTableAlias = $this->getSQLTableAlias($joinTable['name'], $joinedDqlAlias);
979 21
                $joinTableName  = $this->quoteStrategy->getJoinTableName($assoc, $sourceClass, $this->platform);
980
981 21
                $conditions      = [];
982 21
                $relationColumns = ($relation['isOwningSide'])
983 16
                    ? $assoc['joinTable']['joinColumns']
984 21
                    : $assoc['joinTable']['inverseJoinColumns'];
985
986 21
                foreach ($relationColumns as $joinColumn) {
987 21
                    $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
988 21
                    $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
989
990 21
                    $conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $quotedSourceColumn;
991
                }
992
993 21
                $sql .= $joinTableName . ' ' . $joinTableAlias . ' ON ' . implode(' AND ', $conditions);
994
995
                // Join target table
996 21
                $sql .= ($joinType == AST\Join::JOIN_TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE_LEFTOUTER) ? ' LEFT JOIN ' : ' INNER JOIN ';
997
998 21
                $conditions      = [];
999 21
                $relationColumns = ($relation['isOwningSide'])
1000 16
                    ? $assoc['joinTable']['inverseJoinColumns']
1001 21
                    : $assoc['joinTable']['joinColumns'];
1002
1003 21
                foreach ($relationColumns as $joinColumn) {
1004 21
                    $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
1005 21
                    $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
1006
1007 21
                    $conditions[] = $targetTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $quotedSourceColumn;
1008
                }
1009
1010
                // Apply remaining inheritance restrictions
1011 21
                $discrSql = $this->_generateDiscriminatorColumnConditionSQL([$joinedDqlAlias]);
1012
1013 21
                if ($discrSql) {
1014 1
                    $conditions[] = $discrSql;
1015
                }
1016
1017
                // Apply the filters
1018 21
                $filterExpr = $this->generateFilterConditionSQL($targetClass, $targetTableAlias);
1019
1020 21
                if ($filterExpr) {
1021
                    $conditions[] = $filterExpr;
1022
                }
1023
1024
                $targetTableJoin = [
1025 21
                    'table' => $targetTableName . ' ' . $targetTableAlias,
1026 21
                    'condition' => implode(' AND ', $conditions),
1027
                ];
1028 21
                break;
1029
1030
            default:
1031
                throw new \BadMethodCallException('Type of association must be one of *_TO_ONE or MANY_TO_MANY');
1032
        }
1033
1034
        // Handle WITH clause
1035 72
        $withCondition = (null === $condExpr) ? '' : ('(' . $this->walkConditionalExpression($condExpr) . ')');
1036
1037 72
        if ($targetClass->isInheritanceTypeJoined()) {
1038 3
            $ctiJoins = $this->_generateClassTableInheritanceJoins($targetClass, $joinedDqlAlias);
1039
            // If we have WITH condition, we need to build nested joins for target class table and cti joins
1040 3
            if ($withCondition) {
1041 1
                $sql .= '(' . $targetTableJoin['table'] . $ctiJoins . ') ON ' . $targetTableJoin['condition'];
1042
            } else {
1043 3
                $sql .= $targetTableJoin['table'] . ' ON ' . $targetTableJoin['condition'] . $ctiJoins;
1044
            }
1045
        } else {
1046 69
            $sql .= $targetTableJoin['table'] . ' ON ' . $targetTableJoin['condition'];
1047
        }
1048
1049 72
        if ($withCondition) {
1050 4
            $sql .= ' AND ' . $withCondition;
1051
        }
1052
1053
        // Apply the indexes
1054 72
        if ($indexBy) {
1055
            // For Many-To-One or One-To-One associations this obviously makes no sense, but is ignored silently.
1056 4
            $this->walkIndexBy($indexBy);
1057 68
        } else if (isset($relation['indexBy'])) {
1058
            $this->rsm->addIndexBy($joinedDqlAlias, $relation['indexBy']);
1059
        }
1060
1061 72
        return $sql;
1062
    }
1063
1064
    /**
1065
     * {@inheritdoc}
1066
     */
1067 43
    public function walkFunction($function)
1068
    {
1069 43
        return $function->getSql($this);
1070
    }
1071
1072
    /**
1073
     * {@inheritdoc}
1074
     */
1075 34
    public function walkOrderByClause($orderByClause)
1076
    {
1077 34
        $orderByItems = array_map([$this, 'walkOrderByItem'], $orderByClause->orderByItems);
1078
1079 33
        if (($collectionOrderByItems = $this->_generateOrderedCollectionOrderByItems()) !== '') {
1080
            $orderByItems = array_merge($orderByItems, (array) $collectionOrderByItems);
1081
        }
1082
1083 33
        return ' ORDER BY ' . implode(', ', $orderByItems);
1084
    }
1085
1086
    /**
1087
     * {@inheritdoc}
1088
     */
1089 44
    public function walkOrderByItem($orderByItem)
1090
    {
1091 44
        $type = strtoupper($orderByItem->type);
1092 44
        $expr = $orderByItem->expression;
1093 44
        $sql  = ($expr instanceof AST\Node)
1094 41
            ? $expr->dispatch($this)
1095 43
            : $this->walkResultVariable($this->queryComponents[$expr]['token']['value']);
1096
1097 43
        $this->orderedColumnsMap[$sql] = $type;
1098
1099 43
        if ($expr instanceof AST\Subselect) {
1100 2
            return '(' . $sql . ') ' . $type;
1101
        }
1102
1103 41
        return $sql . ' ' . $type;
1104
    }
1105
1106
    /**
1107
     * {@inheritdoc}
1108
     */
1109 10
    public function walkHavingClause($havingClause)
1110
    {
1111 10
        return ' HAVING ' . $this->walkConditionalExpression($havingClause->conditionalExpression);
1112
    }
1113
1114
    /**
1115
     * {@inheritdoc}
1116
     */
1117 90
    public function walkJoin($join)
1118
    {
1119 90
        $joinType        = $join->joinType;
1120 90
        $joinDeclaration = $join->joinAssociationDeclaration;
1121
1122 90
        $sql = ($joinType == AST\Join::JOIN_TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE_LEFTOUTER)
1123 28
            ? ' LEFT JOIN '
1124 90
            : ' INNER JOIN ';
1125
1126
        switch (true) {
1127 90
            case ($joinDeclaration instanceof \Doctrine\ORM\Query\AST\RangeVariableDeclaration):
1128 15
                $class      = $this->em->getClassMetadata($joinDeclaration->abstractSchemaName);
1129 15
                $dqlAlias   = $joinDeclaration->aliasIdentificationVariable;
1130 15
                $tableAlias = $this->getSQLTableAlias($class->table['name'], $dqlAlias);
1131 15
                $conditions = [];
1132
1133 15
                if ($join->conditionalExpression) {
1134 13
                    $conditions[] = '(' . $this->walkConditionalExpression($join->conditionalExpression) . ')';
1135
                }
1136
1137 15
                $condExprConjunction = ($class->isInheritanceTypeJoined() && $joinType != AST\Join::JOIN_TYPE_LEFT && $joinType != AST\Join::JOIN_TYPE_LEFTOUTER)
1138 3
                    ? ' AND '
1139 15
                    : ' ON ';
1140
1141 15
                $sql .= $this->walkRangeVariableDeclaration($joinDeclaration);
1142
1143
                // Apply remaining inheritance restrictions
1144 15
                $discrSql = $this->_generateDiscriminatorColumnConditionSQL([$dqlAlias]);
1145
1146 15
                if ($discrSql) {
1147 3
                    $conditions[] = $discrSql;
1148
                }
1149
1150
                // Apply the filters
1151 15
                $filterExpr = $this->generateFilterConditionSQL($class, $tableAlias);
1152
1153 15
                if ($filterExpr) {
1154
                    $conditions[] = $filterExpr;
1155
                }
1156
1157 15
                if ($conditions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $conditions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1158 13
                    $sql .= $condExprConjunction . implode(' AND ', $conditions);
1159
                }
1160
1161 15
                break;
1162
1163
            case ($joinDeclaration instanceof \Doctrine\ORM\Query\AST\JoinAssociationDeclaration):
1164 75
                $sql .= $this->walkJoinAssociationDeclaration($joinDeclaration, $joinType, $join->conditionalExpression);
1165 72
                break;
1166
        }
1167
1168 87
        return $sql;
1169
    }
1170
1171
    /**
1172
     * Walks down a CoalesceExpression AST node and generates the corresponding SQL.
1173
     *
1174
     * @param AST\CoalesceExpression $coalesceExpression
1175
     *
1176
     * @return string The SQL.
1177
     */
1178 2
    public function walkCoalesceExpression($coalesceExpression)
1179
    {
1180 2
        $sql = 'COALESCE(';
1181
1182 2
        $scalarExpressions = [];
1183
1184 2
        foreach ($coalesceExpression->scalarExpressions as $scalarExpression) {
1185 2
            $scalarExpressions[] = $this->walkSimpleArithmeticExpression($scalarExpression);
1186
        }
1187
1188 2
        $sql .= implode(', ', $scalarExpressions) . ')';
1189
1190 2
        return $sql;
1191
    }
1192
1193
    /**
1194
     * Walks down a NullIfExpression AST node and generates the corresponding SQL.
1195
     *
1196
     * @param AST\NullIfExpression $nullIfExpression
1197
     *
1198
     * @return string The SQL.
1199
     */
1200 3
    public function walkNullIfExpression($nullIfExpression)
1201
    {
1202 3
        $firstExpression = is_string($nullIfExpression->firstExpression)
1203
            ? $this->conn->quote($nullIfExpression->firstExpression)
1204 3
            : $this->walkSimpleArithmeticExpression($nullIfExpression->firstExpression);
1205
1206 3
        $secondExpression = is_string($nullIfExpression->secondExpression)
1207
            ? $this->conn->quote($nullIfExpression->secondExpression)
1208 3
            : $this->walkSimpleArithmeticExpression($nullIfExpression->secondExpression);
1209
1210 3
        return 'NULLIF(' . $firstExpression . ', ' . $secondExpression . ')';
1211
    }
1212
1213
    /**
1214
     * Walks down a GeneralCaseExpression AST node and generates the corresponding SQL.
1215
     *
1216
     * @param AST\GeneralCaseExpression $generalCaseExpression
1217
     *
1218
     * @return string The SQL.
1219
     */
1220 7
    public function walkGeneralCaseExpression(AST\GeneralCaseExpression $generalCaseExpression)
1221
    {
1222 7
        $sql = 'CASE';
1223
1224 7
        foreach ($generalCaseExpression->whenClauses as $whenClause) {
1225 7
            $sql .= ' WHEN ' . $this->walkConditionalExpression($whenClause->caseConditionExpression);
1226 7
            $sql .= ' THEN ' . $this->walkSimpleArithmeticExpression($whenClause->thenScalarExpression);
1227
        }
1228
1229 7
        $sql .= ' ELSE ' . $this->walkSimpleArithmeticExpression($generalCaseExpression->elseScalarExpression) . ' END';
1230
1231 7
        return $sql;
1232
    }
1233
1234
    /**
1235
     * Walks down a SimpleCaseExpression AST node and generates the corresponding SQL.
1236
     *
1237
     * @param AST\SimpleCaseExpression $simpleCaseExpression
1238
     *
1239
     * @return string The SQL.
1240
     */
1241 5
    public function walkSimpleCaseExpression($simpleCaseExpression)
1242
    {
1243 5
        $sql = 'CASE ' . $this->walkStateFieldPathExpression($simpleCaseExpression->caseOperand);
1244
1245 5
        foreach ($simpleCaseExpression->simpleWhenClauses as $simpleWhenClause) {
1246 5
            $sql .= ' WHEN ' . $this->walkSimpleArithmeticExpression($simpleWhenClause->caseScalarExpression);
1247 5
            $sql .= ' THEN ' . $this->walkSimpleArithmeticExpression($simpleWhenClause->thenScalarExpression);
1248
        }
1249
1250 5
        $sql .= ' ELSE ' . $this->walkSimpleArithmeticExpression($simpleCaseExpression->elseScalarExpression) . ' END';
1251
1252 5
        return $sql;
1253
    }
1254
1255
    /**
1256
     * {@inheritdoc}
1257
     */
1258 287
    public function walkSelectExpression($selectExpression)
1259
    {
1260 287
        $sql    = '';
1261 287
        $expr   = $selectExpression->expression;
1262 287
        $hidden = $selectExpression->hiddenAliasResultVariable;
1263
1264
        switch (true) {
1265 287
            case ($expr instanceof AST\PathExpression):
1266 61
                if ($expr->type !== AST\PathExpression::TYPE_STATE_FIELD) {
1267
                    throw QueryException::invalidPathExpression($expr);
1268
                }
1269
1270 61
                $fieldName = $expr->field;
1271 61
                $dqlAlias  = $expr->identificationVariable;
1272 61
                $qComp     = $this->queryComponents[$dqlAlias];
1273 61
                $class     = $qComp['metadata'];
1274
1275 61
                $resultAlias = $selectExpression->fieldIdentificationVariable ?: $fieldName;
1276 61
                $tableName   = ($class->isInheritanceTypeJoined())
1277 5
                    ? $this->em->getUnitOfWork()->getEntityPersister($class->name)->getOwningTable($fieldName)
1278 61
                    : $class->getTableName();
1279
1280 61
                $sqlTableAlias = $this->getSQLTableAlias($tableName, $dqlAlias);
1281 61
                $fieldMapping  = $class->fieldMappings[$fieldName];
1282 61
                $columnName    = $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform);
1283 61
                $columnAlias   = $this->getSQLColumnAlias($fieldMapping['columnName']);
1284 61
                $col           = $sqlTableAlias . '.' . $columnName;
1285
1286 61
                if (isset($fieldMapping['requireSQLConversion'])) {
1287 1
                    $type = Type::getType($fieldMapping['type']);
1288 1
                    $col  = $type->convertToPHPValueSQL($col, $this->conn->getDatabasePlatform());
1289
                }
1290
1291 61
                $sql .= $col . ' AS ' . $columnAlias;
1292
1293 61
                $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
1294
1295 61
                if ( ! $hidden) {
1296 61
                    $this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldMapping['type']);
1297 61
                    $this->scalarFields[$dqlAlias][$fieldName] = $columnAlias;
1298
                }
1299
1300 61
                break;
1301
1302
            case ($expr instanceof AST\AggregateExpression):
1303
            case ($expr instanceof AST\Functions\FunctionNode):
1304
            case ($expr instanceof AST\SimpleArithmeticExpression):
1305
            case ($expr instanceof AST\ArithmeticTerm):
1306 12
            case ($expr instanceof AST\ArithmeticFactor):
1307
            case ($expr instanceof AST\ParenthesisExpression):
1308
            case ($expr instanceof AST\Literal):
1309
            case ($expr instanceof AST\NullIfExpression):
1310
            case ($expr instanceof AST\CoalesceExpression):
1311 6
            case ($expr instanceof AST\GeneralCaseExpression):
1312 6
            case ($expr instanceof AST\SimpleCaseExpression):
1313 62
                $columnAlias = $this->getSQLColumnAlias('sclr');
1314 62
                $resultAlias = $selectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
1315
1316 62
                $sql .= $expr->dispatch($this) . ' AS ' . $columnAlias;
1317
1318 62
                $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
1319
1320 62
                if ( ! $hidden) {
1321
                    // We cannot resolve field type here; assume 'string'.
1322 62
                    $this->rsm->addScalarResult($columnAlias, $resultAlias, 'string');
1323
                }
1324 62
                break;
1325
1326
            case ($expr instanceof AST\Subselect):
1327 10
                $columnAlias = $this->getSQLColumnAlias('sclr');
1328 10
                $resultAlias = $selectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
1329
1330 10
                $sql .= '(' . $this->walkSubselect($expr) . ') AS ' . $columnAlias;
1331
1332 10
                $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
1333
1334 10
                if ( ! $hidden) {
1335
                    // We cannot resolve field type here; assume 'string'.
1336 10
                    $this->rsm->addScalarResult($columnAlias, $resultAlias, 'string');
1337
                }
1338 10
                break;
1339
1340 206
            case ($expr instanceof AST\NewObjectExpression):
1341 1
                $sql .= $this->walkNewObject($expr,$selectExpression->fieldIdentificationVariable);
1342 1
                break;
1343
1344
            default:
1345
                // IdentificationVariable or PartialObjectExpression
1346 206
                if ($expr instanceof AST\PartialObjectExpression) {
1347 5
                    $dqlAlias = $expr->identificationVariable;
1348 5
                    $partialFieldSet = $expr->partialFieldSet;
1349
                } else {
1350 204
                    $dqlAlias = $expr;
1351 204
                    $partialFieldSet = [];
1352
                }
1353
1354 206
                $queryComp   = $this->queryComponents[$dqlAlias];
1355 206
                $class       = $queryComp['metadata'];
1356 206
                $resultAlias = $selectExpression->fieldIdentificationVariable ?: null;
1357
1358 206
                if ( ! isset($this->selectedClasses[$dqlAlias])) {
1359 206
                    $this->selectedClasses[$dqlAlias] = [
1360 206
                        'class'       => $class,
1361 206
                        'dqlAlias'    => $dqlAlias,
1362 206
                        'resultAlias' => $resultAlias
1363
                    ];
1364
                }
1365
1366 206
                $sqlParts = [];
1367
1368
                // Select all fields from the queried class
1369 206
                foreach ($class->fieldMappings as $fieldName => $mapping) {
1370 205
                    if ($partialFieldSet && ! in_array($fieldName, $partialFieldSet)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $partialFieldSet of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1371 4
                        continue;
1372
                    }
1373
1374 205
                    $tableName = (isset($mapping['inherited']))
1375 17
                        ? $this->em->getClassMetadata($mapping['inherited'])->getTableName()
1376 205
                        : $class->getTableName();
1377
1378 205
                    $sqlTableAlias    = $this->getSQLTableAlias($tableName, $dqlAlias);
1379 205
                    $columnAlias      = $this->getSQLColumnAlias($mapping['columnName']);
1380 205
                    $quotedColumnName = $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform);
1381
1382 205
                    $col = $sqlTableAlias . '.' . $quotedColumnName;
1383
1384 205
                    if (isset($mapping['requireSQLConversion'])) {
1385 4
                        $type = Type::getType($mapping['type']);
1386 4
                        $col = $type->convertToPHPValueSQL($col, $this->platform);
1387
                    }
1388
1389 205
                    $sqlParts[] = $col . ' AS '. $columnAlias;
1390
1391 205
                    $this->scalarResultAliasMap[$resultAlias][] = $columnAlias;
1392
1393 205
                    $this->rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $class->name);
1394
                }
1395
1396
                // Add any additional fields of subclasses (excluding inherited fields)
1397
                // 1) on Single Table Inheritance: always, since its marginal overhead
1398
                // 2) on Class Table Inheritance only if partial objects are disallowed,
1399
                //    since it requires outer joining subtables.
1400 206
                if ($class->isInheritanceTypeSingleTable() || ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
1401 114
                    foreach ($class->subClasses as $subClassName) {
1402 14
                        $subClass      = $this->em->getClassMetadata($subClassName);
1403 14
                        $sqlTableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
1404
1405 14
                        foreach ($subClass->fieldMappings as $fieldName => $mapping) {
1406 14
                            if (isset($mapping['inherited']) || ($partialFieldSet && !in_array($fieldName, $partialFieldSet))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $partialFieldSet of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1407 14
                                continue;
1408
                            }
1409
1410 14
                            $columnAlias      = $this->getSQLColumnAlias($mapping['columnName']);
1411 14
                            $quotedColumnName = $this->quoteStrategy->getColumnName($fieldName, $subClass, $this->platform);
1412
1413 14
                            $col = $sqlTableAlias . '.' . $quotedColumnName;
1414
1415 14
                            if (isset($mapping['requireSQLConversion'])) {
1416
                                $type = Type::getType($mapping['type']);
1417
                                $col = $type->convertToPHPValueSQL($col, $this->platform);
1418
                            }
1419
1420 14
                            $sqlParts[] = $col . ' AS ' . $columnAlias;
1421
1422 14
                            $this->scalarResultAliasMap[$resultAlias][] = $columnAlias;
1423
1424 14
                            $this->rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $subClassName);
1425
                        }
1426
                    }
1427
                }
1428
1429 206
                $sql .= implode(', ', $sqlParts);
1430
        }
1431
1432 287
        return $sql;
1433
    }
1434
1435
    /**
1436
     * {@inheritdoc}
1437
     */
1438
    public function walkQuantifiedExpression($qExpr)
1439
    {
1440
        return ' ' . strtoupper($qExpr->type) . '(' . $this->walkSubselect($qExpr->subselect) . ')';
1441
    }
1442
1443
    /**
1444
     * {@inheritdoc}
1445
     */
1446 25
    public function walkSubselect($subselect)
1447
    {
1448 25
        $useAliasesBefore  = $this->useSqlTableAliases;
1449 25
        $rootAliasesBefore = $this->rootAliases;
1450
1451 25
        $this->rootAliases = []; // reset the rootAliases for the subselect
1452 25
        $this->useSqlTableAliases = true;
1453
1454 25
        $sql  = $this->walkSimpleSelectClause($subselect->simpleSelectClause);
1455 25
        $sql .= $this->walkSubselectFromClause($subselect->subselectFromClause);
1456 25
        $sql .= $this->walkWhereClause($subselect->whereClause);
0 ignored issues
show
Bug introduced by
It seems like $subselect->whereClause can be null; however, walkWhereClause() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
1457
1458 25
        $sql .= $subselect->groupByClause ? $this->walkGroupByClause($subselect->groupByClause) : '';
1459 25
        $sql .= $subselect->havingClause ? $this->walkHavingClause($subselect->havingClause) : '';
1460 25
        $sql .= $subselect->orderByClause ? $this->walkOrderByClause($subselect->orderByClause) : '';
1461
1462 25
        $this->rootAliases        = $rootAliasesBefore; // put the main aliases back
1463 25
        $this->useSqlTableAliases = $useAliasesBefore;
1464
1465 25
        return $sql;
1466
    }
1467
1468
    /**
1469
     * {@inheritdoc}
1470
     */
1471 25
    public function walkSubselectFromClause($subselectFromClause)
1472
    {
1473 25
        $identificationVarDecls = $subselectFromClause->identificationVariableDeclarations;
1474 25
        $sqlParts               = [];
1475
1476 25
        foreach ($identificationVarDecls as $subselectIdVarDecl) {
1477 25
            $sqlParts[] = $this->walkIdentificationVariableDeclaration($subselectIdVarDecl);
1478
        }
1479
1480 25
        return ' FROM ' . implode(', ', $sqlParts);
1481
    }
1482
1483
    /**
1484
     * {@inheritdoc}
1485
     */
1486 25
    public function walkSimpleSelectClause($simpleSelectClause)
1487
    {
1488 25
        return 'SELECT' . ($simpleSelectClause->isDistinct ? ' DISTINCT' : '')
1489 25
            . $this->walkSimpleSelectExpression($simpleSelectClause->simpleSelectExpression);
1490
    }
1491
1492
    /**
1493
     * @param \Doctrine\ORM\Query\AST\ParenthesisExpression $parenthesisExpression
1494
     *
1495
     * @return string.
0 ignored issues
show
Documentation introduced by
The doc-type string. could not be parsed: Unknown type name "string." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1496
     */
1497 18
    public function walkParenthesisExpression(AST\ParenthesisExpression $parenthesisExpression)
1498
    {
1499 18
        return sprintf('(%s)', $parenthesisExpression->expression->dispatch($this));
1500
    }
1501
1502
    /**
1503
     * @param AST\NewObjectExpression $newObjectExpression
1504
     *
1505
     * @return string The SQL.
1506
     */
1507 1
    public function walkNewObject($newObjectExpression, $newObjectResultAlias=null)
1508
    {
1509 1
        $sqlSelectExpressions = [];
1510 1
        $objIndex             = $newObjectResultAlias?:$this->newObjectCounter++;
1511
1512 1
        foreach ($newObjectExpression->args as $argIndex => $e) {
1513 1
            $resultAlias = $this->scalarResultCounter++;
1514 1
            $columnAlias = $this->getSQLColumnAlias('sclr');
1515 1
            $fieldType   = 'string';
1516
1517
            switch (true) {
1518 1
                case ($e instanceof AST\NewObjectExpression):
1519
                    $sqlSelectExpressions[] = $e->dispatch($this);
1520
                    break;
1521
1522
                case ($e instanceof AST\Subselect):
1523 1
                    $sqlSelectExpressions[] = '(' . $e->dispatch($this) . ') AS ' . $columnAlias;
1524 1
                    break;
1525
1526
                case ($e instanceof AST\PathExpression):
1527 1
                    $dqlAlias  = $e->identificationVariable;
1528 1
                    $qComp     = $this->queryComponents[$dqlAlias];
1529 1
                    $class     = $qComp['metadata'];
1530 1
                    $fieldType = $class->fieldMappings[$e->field]['type'];
1531
1532 1
                    $sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' . $columnAlias;
1533 1
                    break;
1534
1535 1
                case ($e instanceof AST\Literal):
1536
                    switch ($e->type) {
1537
                        case AST\Literal::BOOLEAN:
1538
                            $fieldType = 'boolean';
1539
                            break;
1540
1541
                        case AST\Literal::NUMERIC:
1542
                            $fieldType = is_float($e->value) ? 'float' : 'integer';
1543
                            break;
1544
                    }
1545
1546
                    $sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' . $columnAlias;
1547
                    break;
1548
1549
                default:
1550 1
                    $sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' . $columnAlias;
1551 1
                    break;
1552
            }
1553
1554 1
            $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
1555 1
            $this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldType);
1556
1557 1
            $this->rsm->newObjectMappings[$columnAlias] = [
1558 1
                'className' => $newObjectExpression->className,
1559 1
                'objIndex'  => $objIndex,
1560 1
                'argIndex'  => $argIndex
1561
            ];
1562
        }
1563
1564 1
        return implode(', ', $sqlSelectExpressions);
1565
    }
1566
1567
    /**
1568
     * {@inheritdoc}
1569
     */
1570 25
    public function walkSimpleSelectExpression($simpleSelectExpression)
1571
    {
1572 25
        $expr = $simpleSelectExpression->expression;
1573 25
        $sql  = ' ';
1574
1575
        switch (true) {
1576 25
            case ($expr instanceof AST\PathExpression):
1577 7
                $sql .= $this->walkPathExpression($expr);
1578 7
                break;
1579
1580
            case ($expr instanceof AST\AggregateExpression):
1581 8
                $alias = $simpleSelectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
1582
1583 8
                $sql .= $this->walkAggregateExpression($expr) . ' AS dctrn__' . $alias;
1584 8
                break;
1585
1586
            case ($expr instanceof AST\Subselect):
1587
                $alias = $simpleSelectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
1588
1589
                $columnAlias = 'sclr' . $this->aliasCounter++;
1590
                $this->scalarResultAliasMap[$alias] = $columnAlias;
1591
1592
                $sql .= '(' . $this->walkSubselect($expr) . ') AS ' . $columnAlias;
1593
                break;
1594
1595
            case ($expr instanceof AST\Functions\FunctionNode):
1596
            case ($expr instanceof AST\SimpleArithmeticExpression):
1597
            case ($expr instanceof AST\ArithmeticTerm):
1598 5
            case ($expr instanceof AST\ArithmeticFactor):
1599
            case ($expr instanceof AST\Literal):
1600
            case ($expr instanceof AST\NullIfExpression):
1601
            case ($expr instanceof AST\CoalesceExpression):
1602
            case ($expr instanceof AST\GeneralCaseExpression):
1603
            case ($expr instanceof AST\SimpleCaseExpression):
1604 8
                $alias = $simpleSelectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
1605
1606 8
                $columnAlias = $this->getSQLColumnAlias('sclr');
1607 8
                $this->scalarResultAliasMap[$alias] = $columnAlias;
1608
1609 8
                $sql .= $expr->dispatch($this) . ' AS ' . $columnAlias;
1610 8
                break;
1611
1612 2
            case ($expr instanceof AST\ParenthesisExpression):
1613 1
                $sql .= $this->walkParenthesisExpression($expr);
1614 1
                break;
1615
1616
            default: // IdentificationVariable
1617 2
                $sql .= $this->walkEntityIdentificationVariable($expr);
1618 2
                break;
1619
        }
1620
1621 25
        return $sql;
1622
    }
1623
1624
    /**
1625
     * {@inheritdoc}
1626
     */
1627 47
    public function walkAggregateExpression($aggExpression)
1628
    {
1629 47
        return $aggExpression->functionName . '(' . ($aggExpression->isDistinct ? 'DISTINCT ' : '')
1630 47
            . $this->walkSimpleArithmeticExpression($aggExpression->pathExpression) . ')';
0 ignored issues
show
Bug introduced by
It seems like $aggExpression->pathExpression can also be of type object<Doctrine\ORM\Query\AST\PathExpression>; however, Doctrine\ORM\Query\SqlWa...eArithmeticExpression() does only seem to accept object<Doctrine\ORM\Quer...leArithmeticExpression>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
1631
    }
1632
1633
    /**
1634
     * {@inheritdoc}
1635
     */
1636 13
    public function walkGroupByClause($groupByClause)
1637
    {
1638 13
        $sqlParts = [];
1639
1640 13
        foreach ($groupByClause->groupByItems as $groupByItem) {
1641 13
            $sqlParts[] = $this->walkGroupByItem($groupByItem);
1642
        }
1643
1644 13
        return ' GROUP BY ' . implode(', ', $sqlParts);
1645
    }
1646
1647
    /**
1648
     * {@inheritdoc}
1649
     */
1650 13
    public function walkGroupByItem($groupByItem)
1651
    {
1652
        // StateFieldPathExpression
1653 13
        if ( ! is_string($groupByItem)) {
1654 7
            return $this->walkPathExpression($groupByItem);
1655
        }
1656
1657
        // ResultVariable
1658 6
        if (isset($this->queryComponents[$groupByItem]['resultVariable'])) {
1659 2
            $resultVariable = $this->queryComponents[$groupByItem]['resultVariable'];
1660
1661 2
            if ($resultVariable instanceof AST\PathExpression) {
1662 1
                return $this->walkPathExpression($resultVariable);
1663
            }
1664
1665 1
            if (isset($resultVariable->pathExpression)) {
1666
                return $this->walkPathExpression($resultVariable->pathExpression);
1667
            }
1668
1669 1
            return $this->walkResultVariable($groupByItem);
1670
        }
1671
1672
        // IdentificationVariable
1673 4
        $sqlParts = [];
1674
1675 4
        foreach ($this->queryComponents[$groupByItem]['metadata']->fieldNames as $field) {
1676 4
            $item       = new AST\PathExpression(AST\PathExpression::TYPE_STATE_FIELD, $groupByItem, $field);
1677 4
            $item->type = AST\PathExpression::TYPE_STATE_FIELD;
1678
1679 4
            $sqlParts[] = $this->walkPathExpression($item);
1680
        }
1681
1682 4
        foreach ($this->queryComponents[$groupByItem]['metadata']->associationMappings as $mapping) {
1683 4
            if ($mapping['isOwningSide'] && $mapping['type'] & ClassMetadataInfo::TO_ONE) {
1684 2
                $item       = new AST\PathExpression(AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $groupByItem, $mapping['fieldName']);
1685 2
                $item->type = AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
1686
1687 4
                $sqlParts[] = $this->walkPathExpression($item);
1688
            }
1689
        }
1690
1691 4
        return implode(', ', $sqlParts);
1692
    }
1693
1694
    /**
1695
     * {@inheritdoc}
1696
     */
1697 32
    public function walkDeleteClause(AST\DeleteClause $deleteClause)
1698
    {
1699 32
        $class     = $this->em->getClassMetadata($deleteClause->abstractSchemaName);
1700 32
        $tableName = $class->getTableName();
1701 32
        $sql       = 'DELETE FROM ' . $this->quoteStrategy->getTableName($class, $this->platform);
1702
1703 32
        $this->setSQLTableAlias($tableName, $tableName, $deleteClause->aliasIdentificationVariable);
1704 32
        $this->rootAliases[] = $deleteClause->aliasIdentificationVariable;
1705
1706 32
        return $sql;
1707
    }
1708
1709
    /**
1710
     * {@inheritdoc}
1711
     */
1712 21
    public function walkUpdateClause($updateClause)
1713
    {
1714 21
        $class     = $this->em->getClassMetadata($updateClause->abstractSchemaName);
1715 21
        $tableName = $class->getTableName();
1716 21
        $sql       = 'UPDATE ' . $this->quoteStrategy->getTableName($class, $this->platform);
1717
1718 21
        $this->setSQLTableAlias($tableName, $tableName, $updateClause->aliasIdentificationVariable);
1719 21
        $this->rootAliases[] = $updateClause->aliasIdentificationVariable;
1720
1721 21
        $sql .= ' SET ' . implode(', ', array_map([$this, 'walkUpdateItem'], $updateClause->updateItems));
1722
1723 21
        return $sql;
1724
    }
1725
1726
    /**
1727
     * {@inheritdoc}
1728
     */
1729 23
    public function walkUpdateItem($updateItem)
1730
    {
1731 23
        $useTableAliasesBefore = $this->useSqlTableAliases;
1732 23
        $this->useSqlTableAliases = false;
1733
1734 23
        $sql      = $this->walkPathExpression($updateItem->pathExpression) . ' = ';
1735 23
        $newValue = $updateItem->newValue;
1736
1737
        switch (true) {
1738 23
            case ($newValue instanceof AST\Node):
1739 22
                $sql .= $newValue->dispatch($this);
1740 22
                break;
1741
1742 1
            case ($newValue === null):
1743 1
                $sql .= 'NULL';
1744 1
                break;
1745
1746
            default:
1747
                $sql .= $this->conn->quote($newValue);
1748
                break;
1749
        }
1750
1751 23
        $this->useSqlTableAliases = $useTableAliasesBefore;
1752
1753 23
        return $sql;
1754
    }
1755
1756
    /**
1757
     * {@inheritdoc}
1758
     */
1759 337
    public function walkWhereClause($whereClause)
1760
    {
1761 337
        $condSql  = null !== $whereClause ? $this->walkConditionalExpression($whereClause->conditionalExpression) : '';
1762 335
        $discrSql = $this->_generateDiscriminatorColumnConditionSql($this->rootAliases);
1763
1764 335
        if ($this->em->hasFilters()) {
1765 2
            $filterClauses = [];
1766 2
            foreach ($this->rootAliases as $dqlAlias) {
1767 2
                $class = $this->queryComponents[$dqlAlias]['metadata'];
1768 2
                $tableAlias = $this->getSQLTableAlias($class->table['name'], $dqlAlias);
1769
1770 2
                if ($filterExpr = $this->generateFilterConditionSQL($class, $tableAlias)) {
1771 2
                    $filterClauses[] = $filterExpr;
1772
                }
1773
            }
1774
1775 2
            if (count($filterClauses)) {
1776 1
                if ($condSql) {
1777
                    $condSql = '(' . $condSql . ') AND ';
1778
                }
1779
1780 1
                $condSql .= implode(' AND ', $filterClauses);
1781
            }
1782
        }
1783
1784 335
        if ($condSql) {
1785 171
            return ' WHERE ' . (( ! $discrSql) ? $condSql : '(' . $condSql . ') AND ' . $discrSql);
1786
        }
1787
1788 184
        if ($discrSql) {
1789 8
            return ' WHERE ' . $discrSql;
1790
        }
1791
1792 177
        return '';
1793
    }
1794
1795
    /**
1796
     * {@inheritdoc}
1797
     */
1798 197
    public function walkConditionalExpression($condExpr)
1799
    {
1800
        // Phase 2 AST optimization: Skip processing of ConditionalExpression
1801
        // if only one ConditionalTerm is defined
1802 197
        if ( ! ($condExpr instanceof AST\ConditionalExpression)) {
1803 187
            return $this->walkConditionalTerm($condExpr);
1804
        }
1805
1806 20
        return implode(' OR ', array_map([$this, 'walkConditionalTerm'], $condExpr->conditionalTerms));
1807
    }
1808
1809
    /**
1810
     * {@inheritdoc}
1811
     */
1812 197
    public function walkConditionalTerm($condTerm)
1813
    {
1814
        // Phase 2 AST optimization: Skip processing of ConditionalTerm
1815
        // if only one ConditionalFactor is defined
1816 197
        if ( ! ($condTerm instanceof AST\ConditionalTerm)) {
1817 182
            return $this->walkConditionalFactor($condTerm);
1818
        }
1819
1820 30
        return implode(' AND ', array_map([$this, 'walkConditionalFactor'], $condTerm->conditionalFactors));
1821
    }
1822
1823
    /**
1824
     * {@inheritdoc}
1825
     */
1826 197
    public function walkConditionalFactor($factor)
1827
    {
1828
        // Phase 2 AST optimization: Skip processing of ConditionalFactor
1829
        // if only one ConditionalPrimary is defined
1830 197
        return ( ! ($factor instanceof AST\ConditionalFactor))
1831 194
            ? $this->walkConditionalPrimary($factor)
1832 195
            : ($factor->not ? 'NOT ' : '') . $this->walkConditionalPrimary($factor->conditionalPrimary);
1833
    }
1834
1835
    /**
1836
     * {@inheritdoc}
1837
     */
1838 197
    public function walkConditionalPrimary($primary)
1839
    {
1840 197
        if ($primary->isSimpleConditionalExpression()) {
1841 197
            return $primary->simpleConditionalExpression->dispatch($this);
1842
        }
1843
1844 21
        if ($primary->isConditionalExpression()) {
1845 21
            $condExpr = $primary->conditionalExpression;
1846
1847 21
            return '(' . $this->walkConditionalExpression($condExpr) . ')';
0 ignored issues
show
Bug introduced by
It seems like $condExpr defined by $primary->conditionalExpression on line 1845 can be null; however, Doctrine\ORM\Query\SqlWa...ConditionalExpression() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
1848
        }
1849
    }
1850
1851
    /**
1852
     * {@inheritdoc}
1853
     */
1854 5
    public function walkExistsExpression($existsExpr)
1855
    {
1856 5
        $sql = ($existsExpr->not) ? 'NOT ' : '';
1857
1858 5
        $sql .= 'EXISTS (' . $this->walkSubselect($existsExpr->subselect) . ')';
1859
1860 5
        return $sql;
1861
    }
1862
1863
    /**
1864
     * {@inheritdoc}
1865
     */
1866 6
    public function walkCollectionMemberExpression($collMemberExpr)
1867
    {
1868 6
        $sql = $collMemberExpr->not ? 'NOT ' : '';
1869 6
        $sql .= 'EXISTS (SELECT 1 FROM ';
1870
1871 6
        $entityExpr   = $collMemberExpr->entityExpression;
1872 6
        $collPathExpr = $collMemberExpr->collectionValuedPathExpression;
1873
1874 6
        $fieldName = $collPathExpr->field;
1875 6
        $dqlAlias  = $collPathExpr->identificationVariable;
1876
1877 6
        $class = $this->queryComponents[$dqlAlias]['metadata'];
1878
1879
        switch (true) {
1880
            // InputParameter
1881 6
            case ($entityExpr instanceof AST\InputParameter):
1882 4
                $dqlParamKey = $entityExpr->name;
1883 4
                $entitySql   = '?';
1884 4
                break;
1885
1886
            // SingleValuedAssociationPathExpression | IdentificationVariable
1887
            case ($entityExpr instanceof AST\PathExpression):
1888 2
                $entitySql = $this->walkPathExpression($entityExpr);
1889 2
                break;
1890
1891
            default:
1892
                throw new \BadMethodCallException("Not implemented");
1893
        }
1894
1895 6
        $assoc = $class->associationMappings[$fieldName];
1896
1897 6
        if ($assoc['type'] == ClassMetadata::ONE_TO_MANY) {
1898 1
            $targetClass      = $this->em->getClassMetadata($assoc['targetEntity']);
1899 1
            $targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName());
1900 1
            $sourceTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
1901
1902 1
            $sql .= $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' ' . $targetTableAlias . ' WHERE ';
1903
1904 1
            $owningAssoc = $targetClass->associationMappings[$assoc['mappedBy']];
1905 1
            $sqlParts    = [];
1906
1907 1
            foreach ($owningAssoc['targetToSourceKeyColumns'] as $targetColumn => $sourceColumn) {
1908 1
                $targetColumn = $this->quoteStrategy->getColumnName($class->fieldNames[$targetColumn], $class, $this->platform);
1909
1910 1
                $sqlParts[] = $sourceTableAlias . '.' . $targetColumn . ' = ' . $targetTableAlias . '.' . $sourceColumn;
1911
            }
1912
1913 1
            foreach ($this->quoteStrategy->getIdentifierColumnNames($targetClass, $this->platform) as $targetColumnName) {
1914 1
                if (isset($dqlParamKey)) {
1915 1
                    $this->parserResult->addParameterMapping($dqlParamKey, $this->sqlParamIndex++);
1916
                }
1917
1918 1
                $sqlParts[] = $targetTableAlias . '.'  . $targetColumnName . ' = ' . $entitySql;
1919
            }
1920
1921 1
            $sql .= implode(' AND ', $sqlParts);
1922
        } else { // many-to-many
1923 5
            $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
1924
1925 5
            $owningAssoc = $assoc['isOwningSide'] ? $assoc : $targetClass->associationMappings[$assoc['mappedBy']];
1926 5
            $joinTable = $owningAssoc['joinTable'];
1927
1928
            // SQL table aliases
1929 5
            $joinTableAlias   = $this->getSQLTableAlias($joinTable['name']);
1930 5
            $targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName());
1931 5
            $sourceTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
1932
1933
            // join to target table
1934 5
            $sql .= $this->quoteStrategy->getJoinTableName($owningAssoc, $targetClass, $this->platform) . ' ' . $joinTableAlias
1935 5
                . ' INNER JOIN ' . $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' ' . $targetTableAlias . ' ON ';
1936
1937
            // join conditions
1938 5
            $joinColumns  = $assoc['isOwningSide'] ? $joinTable['inverseJoinColumns'] : $joinTable['joinColumns'];
1939 5
            $joinSqlParts = [];
1940
1941 5
            foreach ($joinColumns as $joinColumn) {
1942 5
                $targetColumn = $this->quoteStrategy->getColumnName($targetClass->fieldNames[$joinColumn['referencedColumnName']], $targetClass, $this->platform);
1943
1944 5
                $joinSqlParts[] = $joinTableAlias . '.' . $joinColumn['name'] . ' = ' . $targetTableAlias . '.' . $targetColumn;
1945
            }
1946
1947 5
            $sql .= implode(' AND ', $joinSqlParts);
1948 5
            $sql .= ' WHERE ';
1949
1950 5
            $joinColumns = $assoc['isOwningSide'] ? $joinTable['joinColumns'] : $joinTable['inverseJoinColumns'];
1951 5
            $sqlParts    = [];
1952
1953 5
            foreach ($joinColumns as $joinColumn) {
1954 5
                $targetColumn = $this->quoteStrategy->getColumnName($class->fieldNames[$joinColumn['referencedColumnName']], $class, $this->platform);
1955
1956 5
                $sqlParts[] = $joinTableAlias . '.' . $joinColumn['name'] . ' = ' . $sourceTableAlias . '.' . $targetColumn;
1957
            }
1958
1959 5
            foreach ($this->quoteStrategy->getIdentifierColumnNames($targetClass, $this->platform) as $targetColumnName) {
1960 5
                if (isset($dqlParamKey)) {
1961 3
                    $this->parserResult->addParameterMapping($dqlParamKey, $this->sqlParamIndex++);
1962
                }
1963
1964 5
                $sqlParts[] = $targetTableAlias . '.' . $targetColumnName . ' IN (' . $entitySql . ')';
1965
            }
1966
1967 5
            $sql .= implode(' AND ', $sqlParts);
1968
        }
1969
1970 6
        return $sql . ')';
1971
    }
1972
1973
    /**
1974
     * {@inheritdoc}
1975
     */
1976 2
    public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr)
1977
    {
1978 2
        $sizeFunc = new AST\Functions\SizeFunction('size');
1979 2
        $sizeFunc->collectionPathExpression = $emptyCollCompExpr->expression;
1980
1981 2
        return $sizeFunc->getSql($this) . ($emptyCollCompExpr->not ? ' > 0' : ' = 0');
1982
    }
1983
1984
    /**
1985
     * {@inheritdoc}
1986
     */
1987 9
    public function walkNullComparisonExpression($nullCompExpr)
1988
    {
1989 9
        $expression = $nullCompExpr->expression;
1990 9
        $comparison = ' IS' . ($nullCompExpr->not ? ' NOT' : '') . ' NULL';
1991
1992
        // Handle ResultVariable
1993 9
        if (is_string($expression) && isset($this->queryComponents[$expression]['resultVariable'])) {
1994 2
            return $this->walkResultVariable($expression) . $comparison;
1995
        }
1996
1997
        // Handle InputParameter mapping inclusion to ParserResult
1998 7
        if ($expression instanceof AST\InputParameter) {
1999
            return $this->walkInputParameter($expression) . $comparison;
2000
        }
2001
2002 7
        return $expression->dispatch($this) . $comparison;
2003
    }
2004
2005
    /**
2006
     * {@inheritdoc}
2007
     */
2008 29
    public function walkInExpression($inExpr)
2009
    {
2010 29
        $sql = $this->walkArithmeticExpression($inExpr->expression) . ($inExpr->not ? ' NOT' : '') . ' IN (';
2011
2012 28
        $sql .= ($inExpr->subselect)
2013 6
            ? $this->walkSubselect($inExpr->subselect)
2014 28
            : implode(', ', array_map([$this, 'walkInParameter'], $inExpr->literals));
2015
2016 28
        $sql .= ')';
2017
2018 28
        return $sql;
2019
    }
2020
2021
    /**
2022
     * {@inheritdoc}
2023
     */
2024 7
    public function walkInstanceOfExpression($instanceOfExpr)
2025
    {
2026 7
        $sql = '';
2027
2028 7
        $dqlAlias = $instanceOfExpr->identificationVariable;
2029 7
        $discrClass = $class = $this->queryComponents[$dqlAlias]['metadata'];
2030
2031 7
        if ($class->discriminatorColumn) {
2032 7
            $discrClass = $this->em->getClassMetadata($class->rootEntityName);
2033
        }
2034
2035 7
        if ($this->useSqlTableAliases) {
2036 7
            $sql .= $this->getSQLTableAlias($discrClass->getTableName(), $dqlAlias) . '.';
2037
        }
2038
2039 7
        $sql .= $class->discriminatorColumn['name'] . ($instanceOfExpr->not ? ' NOT IN ' : ' IN ');
2040
2041 7
        $sqlParameterList = [];
2042
2043 7
        foreach ($instanceOfExpr->value as $parameter) {
2044 7
            if ($parameter instanceof AST\InputParameter) {
2045 1
                $this->rsm->addMetadataParameterMapping($parameter->name, 'discriminatorValue');
2046
2047 1
                $sqlParameterList[] = $this->walkInputParameter($parameter);
2048
2049 1
                continue;
2050
            }
2051
2052
            // Get name from ClassMetadata to resolve aliases.
2053 6
            $entityClassName    = $this->em->getClassMetadata($parameter)->name;
2054 6
            $discriminatorValue = $class->discriminatorValue;
2055
2056 6
            if ($entityClassName !== $class->name) {
2057 5
                $discrMap = array_flip($class->discriminatorMap);
2058
2059 5
                if ( ! isset($discrMap[$entityClassName])) {
2060 1
                    throw QueryException::instanceOfUnrelatedClass($entityClassName, $class->rootEntityName);
2061
                }
2062
2063 4
                $discriminatorValue = $discrMap[$entityClassName];
2064
            }
2065
2066 5
            $sqlParameterList[] = $this->conn->quote($discriminatorValue);
2067
        }
2068
2069 6
        $sql .= '(' . implode(', ', $sqlParameterList) . ')';
2070
2071 6
        return $sql;
2072
    }
2073
2074
    /**
2075
     * {@inheritdoc}
2076
     */
2077 22
    public function walkInParameter($inParam)
2078
    {
2079 22
        return $inParam instanceof AST\InputParameter
2080 15
            ? $this->walkInputParameter($inParam)
2081 22
            : $this->walkLiteral($inParam);
2082
    }
2083
2084
    /**
2085
     * {@inheritdoc}
2086
     */
2087 98
    public function walkLiteral($literal)
2088
    {
2089 98
        switch ($literal->type) {
2090 98
            case AST\Literal::STRING:
2091 33
                return $this->conn->quote($literal->value);
2092
2093 75
            case AST\Literal::BOOLEAN:
2094 4
                return $this->conn->getDatabasePlatform()->convertBooleans('true' === strtolower($literal->value));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->conn->getD...ower($literal->value)); (object|integer|double|string|null|array) is incompatible with the return type declared by the interface Doctrine\ORM\Query\TreeWalker::walkLiteral of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
2095
2096 71
            case AST\Literal::NUMERIC:
2097 71
                return $literal->value;
2098
2099
            default:
2100
                throw QueryException::invalidLiteral($literal);
2101
        }
2102
    }
2103
2104
    /**
2105
     * {@inheritdoc}
2106
     */
2107 6
    public function walkBetweenExpression($betweenExpr)
2108
    {
2109 6
        $sql = $this->walkArithmeticExpression($betweenExpr->expression);
2110
2111 6
        if ($betweenExpr->not) {
2112 2
            $sql .= ' NOT';
2113
        }
2114
2115 6
        $sql .= ' BETWEEN ' . $this->walkArithmeticExpression($betweenExpr->leftBetweenExpression)
2116 6
            . ' AND ' . $this->walkArithmeticExpression($betweenExpr->rightBetweenExpression);
2117
2118 6
        return $sql;
2119
    }
2120
2121
    /**
2122
     * {@inheritdoc}
2123
     */
2124 8
    public function walkLikeExpression($likeExpr)
2125
    {
2126 8
        $stringExpr = $likeExpr->stringExpression;
2127 8
        $leftExpr   = (is_string($stringExpr) && isset($this->queryComponents[$stringExpr]['resultVariable']))
2128 1
            ? $this->walkResultVariable($stringExpr)
2129 8
            : $stringExpr->dispatch($this);
2130
2131 8
        $sql = $leftExpr . ($likeExpr->not ? ' NOT' : '') . ' LIKE ';
2132
2133 8
        if ($likeExpr->stringPattern instanceof AST\InputParameter) {
2134 3
            $sql .= $this->walkInputParameter($likeExpr->stringPattern);
2135 6
        } elseif ($likeExpr->stringPattern instanceof AST\Functions\FunctionNode) {
2136 2
            $sql .= $this->walkFunction($likeExpr->stringPattern);
2137 6
        } elseif ($likeExpr->stringPattern instanceof AST\PathExpression) {
2138 2
            $sql .= $this->walkPathExpression($likeExpr->stringPattern);
2139
        } else {
2140 6
            $sql .= $this->walkLiteral($likeExpr->stringPattern);
2141
        }
2142
2143 8
        if ($likeExpr->escapeChar) {
2144 1
            $sql .= ' ESCAPE ' . $this->walkLiteral($likeExpr->escapeChar);
2145
        }
2146
2147 8
        return $sql;
2148
    }
2149
2150
    /**
2151
     * {@inheritdoc}
2152
     */
2153 5
    public function walkStateFieldPathExpression($stateFieldPathExpression)
2154
    {
2155 5
        return $this->walkPathExpression($stateFieldPathExpression);
2156
    }
2157
2158
    /**
2159
     * {@inheritdoc}
2160
     */
2161 148
    public function walkComparisonExpression($compExpr)
2162
    {
2163 148
        $leftExpr  = $compExpr->leftExpression;
2164 148
        $rightExpr = $compExpr->rightExpression;
2165 148
        $sql       = '';
2166
2167 148
        $sql .= ($leftExpr instanceof AST\Node)
2168 148
            ? $leftExpr->dispatch($this)
2169 148
            : (is_numeric($leftExpr) ? $leftExpr : $this->conn->quote($leftExpr));
2170
2171 148
        $sql .= ' ' . $compExpr->operator . ' ';
2172
2173 148
        $sql .= ($rightExpr instanceof AST\Node)
2174 146
            ? $rightExpr->dispatch($this)
2175 148
            : (is_numeric($rightExpr) ? $rightExpr : $this->conn->quote($rightExpr));
2176
2177 148
        return $sql;
2178
    }
2179
2180
    /**
2181
     * {@inheritdoc}
2182
     */
2183 86
    public function walkInputParameter($inputParam)
2184
    {
2185 86
        $this->parserResult->addParameterMapping($inputParam->name, $this->sqlParamIndex++);
2186
2187 86
        $parameter = $this->query->getParameter($inputParam->name);
2188
2189 86
        if ($parameter && Type::hasType($type = $parameter->getType())) {
2190 6
            return Type::getType($type)->convertToDatabaseValueSQL('?', $this->platform);
2191
        }
2192
2193 81
        return '?';
2194
    }
2195
2196
    /**
2197
     * {@inheritdoc}
2198
     */
2199 166
    public function walkArithmeticExpression($arithmeticExpr)
2200
    {
2201 166
        return ($arithmeticExpr->isSimpleArithmeticExpression())
2202 166
            ? $this->walkSimpleArithmeticExpression($arithmeticExpr->simpleArithmeticExpression)
0 ignored issues
show
Bug introduced by
It seems like $arithmeticExpr->simpleArithmeticExpression can be null; however, walkSimpleArithmeticExpression() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
2203 165
            : '(' . $this->walkSubselect($arithmeticExpr->subselect) . ')';
0 ignored issues
show
Bug introduced by
It seems like $arithmeticExpr->subselect can be null; however, walkSubselect() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
2204
    }
2205
2206
    /**
2207
     * {@inheritdoc}
2208
     */
2209 201
    public function walkSimpleArithmeticExpression($simpleArithmeticExpr)
2210
    {
2211 201
        if ( ! ($simpleArithmeticExpr instanceof AST\SimpleArithmeticExpression)) {
2212 197
            return $this->walkArithmeticTerm($simpleArithmeticExpr);
2213
        }
2214
2215 23
        return implode(' ', array_map([$this, 'walkArithmeticTerm'], $simpleArithmeticExpr->arithmeticTerms));
2216
    }
2217
2218
    /**
2219
     * {@inheritdoc}
2220
     */
2221 205
    public function walkArithmeticTerm($term)
2222
    {
2223 205
        if (is_string($term)) {
2224 17
            return (isset($this->queryComponents[$term]))
2225 6
                ? $this->walkResultVariable($this->queryComponents[$term]['token']['value'])
2226 17
                : $term;
2227
        }
2228
2229
        // Phase 2 AST optimization: Skip processing of ArithmeticTerm
2230
        // if only one ArithmeticFactor is defined
2231 204
        if ( ! ($term instanceof AST\ArithmeticTerm)) {
2232 200
            return $this->walkArithmeticFactor($term);
2233
        }
2234
2235 22
        return implode(' ', array_map([$this, 'walkArithmeticFactor'], $term->arithmeticFactors));
2236
    }
2237
2238
    /**
2239
     * {@inheritdoc}
2240
     */
2241 205
    public function walkArithmeticFactor($factor)
2242
    {
2243 205
        if (is_string($factor)) {
2244 22
            return (isset($this->queryComponents[$factor]))
2245 2
                ? $this->walkResultVariable($this->queryComponents[$factor]['token']['value'])
2246 22
                : $factor;
2247
        }
2248
2249
        // Phase 2 AST optimization: Skip processing of ArithmeticFactor
2250
        // if only one ArithmeticPrimary is defined
2251 205
        if ( ! ($factor instanceof AST\ArithmeticFactor)) {
2252 204
            return $this->walkArithmeticPrimary($factor);
2253
        }
2254
2255 2
        $sign = $factor->isNegativeSigned() ? '-' : ($factor->isPositiveSigned() ? '+' : '');
2256
2257 2
        return $sign . $this->walkArithmeticPrimary($factor->arithmeticPrimary);
2258
    }
2259
2260
    /**
2261
     * Walks down an ArithmeticPrimary that represents an AST node, thereby generating the appropriate SQL.
2262
     *
2263
     * @param mixed $primary
2264
     *
2265
     * @return string The SQL.
2266
     */
2267 205
    public function walkArithmeticPrimary($primary)
2268
    {
2269 205
        if ($primary instanceof AST\SimpleArithmeticExpression) {
2270
            return '(' . $this->walkSimpleArithmeticExpression($primary) . ')';
2271
        }
2272
2273 205
        if ($primary instanceof AST\Node) {
2274 205
            return $primary->dispatch($this);
2275
        }
2276
2277
        return $this->walkEntityIdentificationVariable($primary);
2278
    }
2279
2280
    /**
2281
     * {@inheritdoc}
2282
     */
2283 12
    public function walkStringPrimary($stringPrimary)
2284
    {
2285 12
        return (is_string($stringPrimary))
2286
            ? $this->conn->quote($stringPrimary)
2287 12
            : $stringPrimary->dispatch($this);
2288
    }
2289
2290
    /**
2291
     * {@inheritdoc}
2292
     */
2293 25
    public function walkResultVariable($resultVariable)
2294
    {
2295 25
        $resultAlias = $this->scalarResultAliasMap[$resultVariable];
2296
2297 25
        if (is_array($resultAlias)) {
2298 1
            return implode(', ', $resultAlias);
2299
        }
2300
2301 24
        return $resultAlias;
2302
    }
2303
}
2304