Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SqlWalker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SqlWalker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 | 699 | public function __construct($query, $parserResult, array $queryComponents) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Gets the Query instance used by the walker. |
||
| 195 | * |
||
| 196 | * @return Query. |
||
|
|
|||
| 197 | */ |
||
| 198 | public function getQuery() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Gets the Connection used by the walker. |
||
| 205 | * |
||
| 206 | * @return \Doctrine\DBAL\Connection |
||
| 207 | */ |
||
| 208 | 35 | public function getConnection() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Gets the EntityManager used by the walker. |
||
| 215 | * |
||
| 216 | * @return \Doctrine\ORM\EntityManager |
||
| 217 | */ |
||
| 218 | 22 | public function getEntityManager() |
|
| 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 | 17 | public function getQueryComponent($dqlAlias) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritdoc} |
||
| 237 | */ |
||
| 238 | public function getQueryComponents() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritdoc} |
||
| 245 | */ |
||
| 246 | 1 | View Code Duplication | 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 | 693 | public function getExecutor($AST) |
|
| 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 | 645 | public function getSQLTableAlias($tableName, $dqlAlias = '') |
|
| 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 | 65 | public function setSQLTableAlias($tableName, $alias, $dqlAlias = '') |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Gets an SQL column alias for a column name. |
||
| 323 | * |
||
| 324 | * @param string $columnName |
||
| 325 | * |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | 634 | public function getSQLColumnAlias($columnName) |
|
| 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 | 100 | private function _generateClassTableInheritanceJoins($class, $dqlAlias) |
|
| 343 | { |
||
| 344 | 100 | $sql = ''; |
|
| 345 | |||
| 346 | 100 | $baseTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias); |
|
| 347 | |||
| 348 | // INNER JOIN parent class tables |
||
| 349 | 100 | foreach ($class->parentClasses as $parentClassName) { |
|
| 350 | 67 | $parentClass = $this->em->getClassMetadata($parentClassName); |
|
| 351 | 67 | $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 | 67 | $sql .= isset($this->queryComponents[$dqlAlias]['relation']) ? ' LEFT ' : ' INNER '; |
|
| 355 | 67 | $sql .= 'JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON '; |
|
| 356 | |||
| 357 | 67 | $sqlParts = []; |
|
| 358 | |||
| 359 | 67 | View Code Duplication | foreach ($this->quoteStrategy->getIdentifierColumnNames($class, $this->platform) as $columnName) { |
| 360 | 67 | $sqlParts[] = $baseTableAlias . '.' . $columnName . ' = ' . $tableAlias . '.' . $columnName; |
|
| 361 | } |
||
| 362 | |||
| 363 | // Add filters on the root class |
||
| 364 | 67 | if ($filterSql = $this->generateFilterConditionSQL($parentClass, $tableAlias)) { |
|
| 365 | 1 | $sqlParts[] = $filterSql; |
|
| 366 | } |
||
| 367 | |||
| 368 | 67 | $sql .= implode(' AND ', $sqlParts); |
|
| 369 | } |
||
| 370 | |||
| 371 | // Ignore subclassing inclusion if partial objects is disallowed |
||
| 372 | 100 | if ($this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) { |
|
| 373 | 21 | return $sql; |
|
| 374 | } |
||
| 375 | |||
| 376 | // LEFT JOIN child class tables |
||
| 377 | 79 | foreach ($class->subClasses as $subClassName) { |
|
| 378 | 39 | $subClass = $this->em->getClassMetadata($subClassName); |
|
| 379 | 39 | $tableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias); |
|
| 380 | |||
| 381 | 39 | $sql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->platform) . ' ' . $tableAlias . ' ON '; |
|
| 382 | |||
| 383 | 39 | $sqlParts = []; |
|
| 384 | |||
| 385 | 39 | View Code Duplication | foreach ($this->quoteStrategy->getIdentifierColumnNames($subClass, $this->platform) as $columnName) { |
| 386 | 39 | $sqlParts[] = $baseTableAlias . '.' . $columnName . ' = ' . $tableAlias . '.' . $columnName; |
|
| 387 | } |
||
| 388 | |||
| 389 | 39 | $sql .= implode(' AND ', $sqlParts); |
|
| 390 | } |
||
| 391 | |||
| 392 | 79 | return $sql; |
|
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | 628 | private function _generateOrderedCollectionOrderByItems() |
|
| 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 | 688 | private function _generateDiscriminatorColumnConditionSQL(array $dqlAliases) |
|
| 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 | 322 | private function generateFilterConditionSQL(ClassMetadata $targetEntity, $targetTableAlias) |
|
| 481 | { |
||
| 482 | 322 | if (!$this->em->hasFilters()) { |
|
| 483 | 284 | return ''; |
|
| 484 | } |
||
| 485 | |||
| 486 | 43 | switch($targetEntity->inheritanceType) { |
|
| 487 | 43 | case ClassMetadata::INHERITANCE_TYPE_NONE: |
|
| 488 | 33 | break; |
|
| 489 | 10 | 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 | 6 | if ($targetEntity->name !== $targetEntity->rootEntityName) { |
|
| 493 | 4 | return ''; |
|
| 494 | } |
||
| 495 | 6 | break; |
|
| 496 | 4 | 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 | 4 | $targetEntity = $this->em->getClassMetadata($targetEntity->rootEntityName); |
|
| 500 | 4 | break; |
|
| 501 | default: |
||
| 502 | //@todo: throw exception? |
||
| 503 | return ''; |
||
| 504 | } |
||
| 505 | |||
| 506 | 43 | $filterClauses = []; |
|
| 507 | 43 | foreach ($this->em->getFilters()->getEnabledFilters() as $filter) { |
|
| 508 | 10 | if ('' !== $filterExpr = $filter->addFilterConstraint($targetEntity, $targetTableAlias)) { |
|
| 509 | 10 | $filterClauses[] = '(' . $filterExpr . ')'; |
|
| 510 | } |
||
| 511 | } |
||
| 512 | |||
| 513 | 43 | return implode(' AND ', $filterClauses); |
|
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * {@inheritdoc} |
||
| 518 | */ |
||
| 519 | 634 | public function walkSelectStatement(AST\SelectStatement $AST) |
|
| 520 | { |
||
| 521 | 634 | $limit = $this->query->getMaxResults(); |
|
| 522 | 634 | $offset = $this->query->getFirstResult(); |
|
| 523 | 634 | $lockMode = $this->query->getHint(Query::HINT_LOCK_MODE); |
|
| 524 | 634 | $sql = $this->walkSelectClause($AST->selectClause) |
|
| 525 | 634 | . $this->walkFromClause($AST->fromClause) |
|
| 526 | 632 | . $this->walkWhereClause($AST->whereClause); |
|
| 527 | |||
| 528 | 629 | if ($AST->groupByClause) { |
|
| 529 | 24 | $sql .= $this->walkGroupByClause($AST->groupByClause); |
|
| 530 | } |
||
| 531 | |||
| 532 | 629 | if ($AST->havingClause) { |
|
| 533 | 14 | $sql .= $this->walkHavingClause($AST->havingClause); |
|
| 534 | } |
||
| 535 | |||
| 536 | 629 | if ($AST->orderByClause) { |
|
| 537 | 142 | $sql .= $this->walkOrderByClause($AST->orderByClause); |
|
| 538 | } |
||
| 539 | |||
| 540 | 628 | if ( ! $AST->orderByClause && ($orderBySql = $this->_generateOrderedCollectionOrderByItems())) { |
|
| 541 | 6 | $sql .= ' ORDER BY ' . $orderBySql; |
|
| 542 | } |
||
| 543 | |||
| 544 | 628 | View Code Duplication | if ($limit !== null || $offset !== null) { |
| 545 | 39 | $sql = $this->platform->modifyLimitQuery($sql, $limit, $offset); |
|
| 546 | } |
||
| 547 | |||
| 548 | 628 | if ($lockMode === null || $lockMode === false || $lockMode === LockMode::NONE) { |
|
| 549 | 623 | 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 | 25 | public function walkUpdateStatement(AST\UpdateStatement $AST) |
|
| 584 | |||
| 585 | /** |
||
| 586 | * {@inheritdoc} |
||
| 587 | */ |
||
| 588 | 36 | public function walkDeleteStatement(AST\DeleteStatement $AST) |
|
| 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) |
|
| 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 | 427 | public function walkIdentificationVariable($identificationVariable, $fieldName = null) |
|
| 639 | |||
| 640 | /** |
||
| 641 | * {@inheritdoc} |
||
| 642 | */ |
||
| 643 | 496 | public function walkPathExpression($pathExpr) |
|
| 644 | { |
||
| 645 | 496 | $sql = ''; |
|
| 646 | |||
| 647 | /* @var $pathExpr Query\AST\PathExpression */ |
||
| 648 | 496 | switch ($pathExpr->type) { |
|
| 649 | 496 | case AST\PathExpression::TYPE_STATE_FIELD: |
|
| 650 | 475 | $fieldName = $pathExpr->field; |
|
| 651 | 475 | $dqlAlias = $pathExpr->identificationVariable; |
|
| 652 | 475 | $class = $this->queryComponents[$dqlAlias]['metadata']; |
|
| 653 | |||
| 654 | 475 | if ($this->useSqlTableAliases) { |
|
| 655 | 427 | $sql .= $this->walkIdentificationVariable($dqlAlias, $fieldName) . '.'; |
|
| 656 | } |
||
| 657 | |||
| 658 | 475 | $sql .= $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform); |
|
| 659 | 475 | break; |
|
| 660 | |||
| 661 | 62 | case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION: |
|
| 662 | // 1- the owning side: |
||
| 663 | // Just use the foreign key, i.e. u.group_id |
||
| 664 | 62 | $fieldName = $pathExpr->field; |
|
| 665 | 62 | $dqlAlias = $pathExpr->identificationVariable; |
|
| 666 | 62 | $class = $this->queryComponents[$dqlAlias]['metadata']; |
|
| 667 | |||
| 668 | 62 | if (isset($class->associationMappings[$fieldName]['inherited'])) { |
|
| 669 | 2 | $class = $this->em->getClassMetadata($class->associationMappings[$fieldName]['inherited']); |
|
| 670 | } |
||
| 671 | |||
| 672 | 62 | $assoc = $class->associationMappings[$fieldName]; |
|
| 673 | |||
| 674 | 62 | if ( ! $assoc['isOwningSide']) { |
|
| 675 | 2 | throw QueryException::associationPathInverseSideNotSupported($pathExpr); |
|
| 676 | } |
||
| 677 | |||
| 678 | // COMPOSITE KEYS NOT (YET?) SUPPORTED |
||
| 679 | 60 | if (count($assoc['sourceToTargetKeyColumns']) > 1) { |
|
| 680 | 1 | throw QueryException::associationPathCompositeKeyNotSupported(); |
|
| 681 | } |
||
| 682 | |||
| 683 | 59 | if ($this->useSqlTableAliases) { |
|
| 684 | 56 | $sql .= $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.'; |
|
| 685 | } |
||
| 686 | |||
| 687 | 59 | $sql .= reset($assoc['targetToSourceKeyColumns']); |
|
| 688 | 59 | break; |
|
| 689 | |||
| 690 | default: |
||
| 691 | throw QueryException::invalidPathExpression($pathExpr); |
||
| 692 | } |
||
| 693 | |||
| 694 | 493 | return $sql; |
|
| 695 | } |
||
| 696 | |||
| 697 | /** |
||
| 698 | * {@inheritdoc} |
||
| 699 | */ |
||
| 700 | 634 | public function walkSelectClause($selectClause) |
|
| 701 | { |
||
| 702 | 634 | $sql = 'SELECT ' . (($selectClause->isDistinct) ? 'DISTINCT ' : ''); |
|
| 703 | 634 | $sqlSelectExpressions = array_filter(array_map([$this, 'walkSelectExpression'], $selectClause->selectExpressions)); |
|
| 704 | |||
| 705 | 634 | if ($this->query->getHint(Query::HINT_INTERNAL_ITERATION) == true && $selectClause->isDistinct) { |
|
| 706 | 1 | $this->query->setHint(self::HINT_DISTINCT, true); |
|
| 707 | } |
||
| 708 | |||
| 709 | 634 | $addMetaColumns = ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD) && |
|
| 710 | 471 | $this->query->getHydrationMode() == Query::HYDRATE_OBJECT |
|
| 711 | || |
||
| 712 | 290 | $this->query->getHydrationMode() != Query::HYDRATE_OBJECT && |
|
| 713 | 634 | $this->query->getHint(Query::HINT_INCLUDE_META_COLUMNS); |
|
| 714 | |||
| 715 | 634 | foreach ($this->selectedClasses as $selectedClass) { |
|
| 716 | 496 | $class = $selectedClass['class']; |
|
| 717 | 496 | $dqlAlias = $selectedClass['dqlAlias']; |
|
| 718 | 496 | $resultAlias = $selectedClass['resultAlias']; |
|
| 719 | |||
| 720 | // Register as entity or joined entity result |
||
| 721 | 496 | if ($this->queryComponents[$dqlAlias]['relation'] === null) { |
|
| 722 | 496 | $this->rsm->addEntityResult($class->name, $dqlAlias, $resultAlias); |
|
| 723 | } else { |
||
| 724 | 159 | $this->rsm->addJoinedEntityResult( |
|
| 725 | 159 | $class->name, |
|
| 726 | 159 | $dqlAlias, |
|
| 727 | 159 | $this->queryComponents[$dqlAlias]['parent'], |
|
| 728 | 159 | $this->queryComponents[$dqlAlias]['relation']['fieldName'] |
|
| 729 | ); |
||
| 730 | } |
||
| 731 | |||
| 732 | 496 | if ($class->isInheritanceTypeSingleTable() || $class->isInheritanceTypeJoined()) { |
|
| 733 | // Add discriminator columns to SQL |
||
| 734 | 97 | $rootClass = $this->em->getClassMetadata($class->rootEntityName); |
|
| 735 | 97 | $tblAlias = $this->getSQLTableAlias($rootClass->getTableName(), $dqlAlias); |
|
| 736 | 97 | $discrColumn = $rootClass->discriminatorColumn; |
|
| 737 | 97 | $columnAlias = $this->getSQLColumnAlias($discrColumn['name']); |
|
| 738 | |||
| 739 | 97 | $sqlSelectExpressions[] = $tblAlias . '.' . $discrColumn['name'] . ' AS ' . $columnAlias; |
|
| 740 | |||
| 741 | 97 | $this->rsm->setDiscriminatorColumn($dqlAlias, $columnAlias); |
|
| 742 | 97 | $this->rsm->addMetaResult($dqlAlias, $columnAlias, $discrColumn['fieldName'], false, $discrColumn['type']); |
|
| 743 | } |
||
| 744 | |||
| 745 | // Add foreign key columns to SQL, if necessary |
||
| 746 | 496 | if ( ! $addMetaColumns && ! $class->containsForeignIdentifier) { |
|
| 747 | 181 | continue; |
|
| 748 | } |
||
| 749 | |||
| 750 | // Add foreign key columns of class and also parent classes |
||
| 751 | 365 | foreach ($class->associationMappings as $assoc) { |
|
| 752 | 319 | if ( ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) |
|
| 753 | 319 | || ( ! $addMetaColumns && !isset($assoc['id']))) { |
|
| 754 | 268 | continue; |
|
| 755 | } |
||
| 756 | |||
| 757 | 286 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
|
| 758 | 286 | $isIdentifier = (isset($assoc['id']) && $assoc['id'] === true); |
|
| 759 | 286 | $owningClass = (isset($assoc['inherited'])) ? $this->em->getClassMetadata($assoc['inherited']) : $class; |
|
| 760 | 286 | $sqlTableAlias = $this->getSQLTableAlias($owningClass->getTableName(), $dqlAlias); |
|
| 761 | |||
| 762 | 286 | View Code Duplication | foreach ($assoc['joinColumns'] as $joinColumn) { |
| 763 | 286 | $columnName = $joinColumn['name']; |
|
| 764 | 286 | $columnAlias = $this->getSQLColumnAlias($columnName); |
|
| 765 | 286 | $columnType = PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em); |
|
| 766 | |||
| 767 | 286 | $quotedColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); |
|
| 768 | 286 | $sqlSelectExpressions[] = $sqlTableAlias . '.' . $quotedColumnName . ' AS ' . $columnAlias; |
|
| 769 | |||
| 770 | 286 | $this->rsm->addMetaResult($dqlAlias, $columnAlias, $columnName, $isIdentifier, $columnType); |
|
| 771 | } |
||
| 772 | } |
||
| 773 | |||
| 774 | // Add foreign key columns to SQL, if necessary |
||
| 775 | 365 | if ( ! $addMetaColumns) { |
|
| 776 | 8 | continue; |
|
| 777 | } |
||
| 778 | |||
| 779 | // Add foreign key columns of subclasses |
||
| 780 | 360 | foreach ($class->subClasses as $subClassName) { |
|
| 781 | 37 | $subClass = $this->em->getClassMetadata($subClassName); |
|
| 782 | 37 | $sqlTableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias); |
|
| 783 | |||
| 784 | 37 | foreach ($subClass->associationMappings as $assoc) { |
|
| 785 | // Skip if association is inherited |
||
| 786 | 27 | if (isset($assoc['inherited'])) continue; |
|
| 787 | |||
| 788 | 16 | if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) { |
|
| 789 | 14 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
|
| 790 | |||
| 791 | 14 | View Code Duplication | foreach ($assoc['joinColumns'] as $joinColumn) { |
| 792 | 14 | $columnName = $joinColumn['name']; |
|
| 793 | 14 | $columnAlias = $this->getSQLColumnAlias($columnName); |
|
| 794 | 14 | $columnType = PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em); |
|
| 795 | |||
| 796 | 14 | $quotedColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $subClass, $this->platform); |
|
| 797 | 14 | $sqlSelectExpressions[] = $sqlTableAlias . '.' . $quotedColumnName . ' AS ' . $columnAlias; |
|
| 798 | |||
| 799 | 360 | $this->rsm->addMetaResult($dqlAlias, $columnAlias, $columnName, $subClass->isIdentifier($columnName), $columnType); |
|
| 800 | } |
||
| 801 | } |
||
| 802 | } |
||
| 803 | } |
||
| 804 | } |
||
| 805 | |||
| 806 | 634 | $sql .= implode(', ', $sqlSelectExpressions); |
|
| 807 | |||
| 808 | 634 | return $sql; |
|
| 809 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * {@inheritdoc} |
||
| 813 | */ |
||
| 814 | 636 | View Code Duplication | public function walkFromClause($fromClause) |
| 815 | { |
||
| 816 | 636 | $identificationVarDecls = $fromClause->identificationVariableDeclarations; |
|
| 817 | 636 | $sqlParts = []; |
|
| 818 | |||
| 819 | 636 | foreach ($identificationVarDecls as $identificationVariableDecl) { |
|
| 820 | 636 | $sqlParts[] = $this->walkIdentificationVariableDeclaration($identificationVariableDecl); |
|
| 821 | } |
||
| 822 | |||
| 823 | 634 | return ' FROM ' . implode(', ', $sqlParts); |
|
| 824 | } |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Walks down a IdentificationVariableDeclaration AST node, thereby generating the appropriate SQL. |
||
| 828 | * |
||
| 829 | * @param AST\IdentificationVariableDeclaration $identificationVariableDecl |
||
| 830 | * |
||
| 831 | * @return string |
||
| 832 | */ |
||
| 833 | 637 | public function walkIdentificationVariableDeclaration($identificationVariableDecl) |
|
| 847 | |||
| 848 | /** |
||
| 849 | * Walks down a IndexBy AST node. |
||
| 850 | * |
||
| 851 | * @param AST\IndexBy $indexBy |
||
| 852 | * |
||
| 853 | * @return void |
||
| 854 | */ |
||
| 855 | 9 | public function walkIndexBy($indexBy) |
|
| 869 | |||
| 870 | /** |
||
| 871 | * Walks down a RangeVariableDeclaration AST node, thereby generating the appropriate SQL. |
||
| 872 | * |
||
| 873 | * @param AST\RangeVariableDeclaration $rangeVariableDeclaration |
||
| 874 | * |
||
| 875 | * @return string |
||
| 876 | */ |
||
| 877 | 637 | public function walkRangeVariableDeclaration($rangeVariableDeclaration) |
|
| 881 | |||
| 882 | /** |
||
| 883 | * Generate appropriate SQL for RangeVariableDeclaration AST node |
||
| 884 | * |
||
| 885 | * @param AST\RangeVariableDeclaration $rangeVariableDeclaration |
||
| 886 | * @param bool $buildNestedJoins |
||
| 887 | * |
||
| 888 | * @return string |
||
| 889 | */ |
||
| 890 | 637 | private function generateRangeVariableDeclarationSQL($rangeVariableDeclaration, bool $buildNestedJoins) : string |
|
| 915 | |||
| 916 | /** |
||
| 917 | * Walks down a JoinAssociationDeclaration AST node, thereby generating the appropriate SQL. |
||
| 918 | * |
||
| 919 | * @param AST\JoinAssociationDeclaration $joinAssociationDeclaration |
||
| 920 | * @param int $joinType |
||
| 921 | * @param AST\ConditionalExpression $condExpr |
||
| 922 | * |
||
| 923 | * @return string |
||
| 924 | * |
||
| 925 | * @throws QueryException |
||
| 926 | */ |
||
| 927 | 229 | public function walkJoinAssociationDeclaration($joinAssociationDeclaration, $joinType = AST\Join::JOIN_TYPE_INNER, $condExpr = null) |
|
| 1082 | |||
| 1083 | /** |
||
| 1084 | * {@inheritdoc} |
||
| 1085 | */ |
||
| 1086 | 118 | public function walkFunction($function) |
|
| 1090 | |||
| 1091 | /** |
||
| 1092 | * {@inheritdoc} |
||
| 1093 | */ |
||
| 1094 | 153 | public function walkOrderByClause($orderByClause) |
|
| 1104 | |||
| 1105 | /** |
||
| 1106 | * {@inheritdoc} |
||
| 1107 | */ |
||
| 1108 | 171 | public function walkOrderByItem($orderByItem) |
|
| 1124 | |||
| 1125 | /** |
||
| 1126 | * {@inheritdoc} |
||
| 1127 | */ |
||
| 1128 | 14 | public function walkHavingClause($havingClause) |
|
| 1132 | |||
| 1133 | /** |
||
| 1134 | * {@inheritdoc} |
||
| 1135 | */ |
||
| 1136 | 247 | public function walkJoin($join) |
|
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Walks down a CoalesceExpression AST node and generates the corresponding SQL. |
||
| 1193 | * |
||
| 1194 | * @param AST\CoalesceExpression $coalesceExpression |
||
| 1195 | * |
||
| 1196 | * @return string The SQL. |
||
| 1197 | */ |
||
| 1198 | 2 | public function walkCoalesceExpression($coalesceExpression) |
|
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Walks down a NullIfExpression AST node and generates the corresponding SQL. |
||
| 1215 | * |
||
| 1216 | * @param AST\NullIfExpression $nullIfExpression |
||
| 1217 | * |
||
| 1218 | * @return string The SQL. |
||
| 1219 | */ |
||
| 1220 | 3 | public function walkNullIfExpression($nullIfExpression) |
|
| 1232 | |||
| 1233 | /** |
||
| 1234 | * Walks down a GeneralCaseExpression AST node and generates the corresponding SQL. |
||
| 1235 | * |
||
| 1236 | * @param AST\GeneralCaseExpression $generalCaseExpression |
||
| 1237 | * |
||
| 1238 | * @return string The SQL. |
||
| 1239 | */ |
||
| 1240 | 9 | View Code Duplication | public function walkGeneralCaseExpression(AST\GeneralCaseExpression $generalCaseExpression) |
| 1253 | |||
| 1254 | /** |
||
| 1255 | * Walks down a SimpleCaseExpression AST node and generates the corresponding SQL. |
||
| 1256 | * |
||
| 1257 | * @param AST\SimpleCaseExpression $simpleCaseExpression |
||
| 1258 | * |
||
| 1259 | * @return string The SQL. |
||
| 1260 | */ |
||
| 1261 | 5 | View Code Duplication | public function walkSimpleCaseExpression($simpleCaseExpression) |
| 1274 | |||
| 1275 | /** |
||
| 1276 | * {@inheritdoc} |
||
| 1277 | */ |
||
| 1278 | 634 | public function walkSelectExpression($selectExpression) |
|
| 1454 | |||
| 1455 | /** |
||
| 1456 | * {@inheritdoc} |
||
| 1457 | */ |
||
| 1458 | public function walkQuantifiedExpression($qExpr) |
||
| 1462 | |||
| 1463 | /** |
||
| 1464 | * {@inheritdoc} |
||
| 1465 | */ |
||
| 1466 | 33 | public function walkSubselect($subselect) |
|
| 1487 | |||
| 1488 | /** |
||
| 1489 | * {@inheritdoc} |
||
| 1490 | */ |
||
| 1491 | 33 | View Code Duplication | public function walkSubselectFromClause($subselectFromClause) |
| 1502 | |||
| 1503 | /** |
||
| 1504 | * {@inheritdoc} |
||
| 1505 | */ |
||
| 1506 | 33 | public function walkSimpleSelectClause($simpleSelectClause) |
|
| 1511 | |||
| 1512 | /** |
||
| 1513 | * @param \Doctrine\ORM\Query\AST\ParenthesisExpression $parenthesisExpression |
||
| 1514 | * |
||
| 1515 | * @return string. |
||
| 1516 | */ |
||
| 1517 | 22 | public function walkParenthesisExpression(AST\ParenthesisExpression $parenthesisExpression) |
|
| 1521 | |||
| 1522 | /** |
||
| 1523 | * @param AST\NewObjectExpression $newObjectExpression |
||
| 1524 | * @param null|string $newObjectResultAlias |
||
| 1525 | * @return string The SQL. |
||
| 1526 | */ |
||
| 1527 | 22 | public function walkNewObject($newObjectExpression, $newObjectResultAlias=null) |
|
| 1586 | |||
| 1587 | /** |
||
| 1588 | * {@inheritdoc} |
||
| 1589 | */ |
||
| 1590 | 33 | public function walkSimpleSelectExpression($simpleSelectExpression) |
|
| 1637 | |||
| 1638 | /** |
||
| 1639 | * {@inheritdoc} |
||
| 1640 | */ |
||
| 1641 | 76 | public function walkAggregateExpression($aggExpression) |
|
| 1646 | |||
| 1647 | /** |
||
| 1648 | * {@inheritdoc} |
||
| 1649 | */ |
||
| 1650 | 24 | public function walkGroupByClause($groupByClause) |
|
| 1660 | |||
| 1661 | /** |
||
| 1662 | * {@inheritdoc} |
||
| 1663 | */ |
||
| 1664 | 24 | public function walkGroupByItem($groupByItem) |
|
| 1707 | |||
| 1708 | /** |
||
| 1709 | * {@inheritdoc} |
||
| 1710 | */ |
||
| 1711 | 36 | public function walkDeleteClause(AST\DeleteClause $deleteClause) |
|
| 1722 | |||
| 1723 | /** |
||
| 1724 | * {@inheritdoc} |
||
| 1725 | */ |
||
| 1726 | 25 | public function walkUpdateClause($updateClause) |
|
| 1739 | |||
| 1740 | /** |
||
| 1741 | * {@inheritdoc} |
||
| 1742 | */ |
||
| 1743 | 29 | public function walkUpdateItem($updateItem) |
|
| 1769 | |||
| 1770 | /** |
||
| 1771 | * {@inheritdoc} |
||
| 1772 | */ |
||
| 1773 | 691 | public function walkWhereClause($whereClause) |
|
| 1808 | |||
| 1809 | /** |
||
| 1810 | * {@inheritdoc} |
||
| 1811 | */ |
||
| 1812 | 372 | public function walkConditionalExpression($condExpr) |
|
| 1822 | |||
| 1823 | /** |
||
| 1824 | * {@inheritdoc} |
||
| 1825 | */ |
||
| 1826 | 372 | public function walkConditionalTerm($condTerm) |
|
| 1836 | |||
| 1837 | /** |
||
| 1838 | * {@inheritdoc} |
||
| 1839 | */ |
||
| 1840 | 372 | public function walkConditionalFactor($factor) |
|
| 1848 | |||
| 1849 | /** |
||
| 1850 | * {@inheritdoc} |
||
| 1851 | */ |
||
| 1852 | 372 | public function walkConditionalPrimary($primary) |
|
| 1864 | |||
| 1865 | /** |
||
| 1866 | * {@inheritdoc} |
||
| 1867 | */ |
||
| 1868 | 5 | public function walkExistsExpression($existsExpr) |
|
| 1876 | |||
| 1877 | /** |
||
| 1878 | * {@inheritdoc} |
||
| 1879 | */ |
||
| 1880 | 6 | public function walkCollectionMemberExpression($collMemberExpr) |
|
| 1986 | |||
| 1987 | /** |
||
| 1988 | * {@inheritdoc} |
||
| 1989 | */ |
||
| 1990 | 3 | public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr) |
|
| 1997 | |||
| 1998 | /** |
||
| 1999 | * {@inheritdoc} |
||
| 2000 | */ |
||
| 2001 | 11 | public function walkNullComparisonExpression($nullCompExpr) |
|
| 2018 | |||
| 2019 | /** |
||
| 2020 | * {@inheritdoc} |
||
| 2021 | */ |
||
| 2022 | 86 | public function walkInExpression($inExpr) |
|
| 2034 | |||
| 2035 | /** |
||
| 2036 | * {@inheritdoc} |
||
| 2037 | * @throws \Doctrine\ORM\Query\QueryException |
||
| 2038 | */ |
||
| 2039 | 14 | public function walkInstanceOfExpression($instanceOfExpr) |
|
| 2059 | |||
| 2060 | /** |
||
| 2061 | * {@inheritdoc} |
||
| 2062 | */ |
||
| 2063 | 82 | public function walkInParameter($inParam) |
|
| 2069 | |||
| 2070 | /** |
||
| 2071 | * {@inheritdoc} |
||
| 2072 | */ |
||
| 2073 | 148 | public function walkLiteral($literal) |
|
| 2089 | |||
| 2090 | /** |
||
| 2091 | * {@inheritdoc} |
||
| 2092 | */ |
||
| 2093 | 6 | public function walkBetweenExpression($betweenExpr) |
|
| 2106 | |||
| 2107 | /** |
||
| 2108 | * {@inheritdoc} |
||
| 2109 | */ |
||
| 2110 | 9 | public function walkLikeExpression($likeExpr) |
|
| 2135 | |||
| 2136 | /** |
||
| 2137 | * {@inheritdoc} |
||
| 2138 | */ |
||
| 2139 | 5 | public function walkStateFieldPathExpression($stateFieldPathExpression) |
|
| 2143 | |||
| 2144 | /** |
||
| 2145 | * {@inheritdoc} |
||
| 2146 | */ |
||
| 2147 | 263 | public function walkComparisonExpression($compExpr) |
|
| 2165 | |||
| 2166 | /** |
||
| 2167 | * {@inheritdoc} |
||
| 2168 | */ |
||
| 2169 | 219 | public function walkInputParameter($inputParam) |
|
| 2181 | |||
| 2182 | /** |
||
| 2183 | * {@inheritdoc} |
||
| 2184 | */ |
||
| 2185 | 330 | public function walkArithmeticExpression($arithmeticExpr) |
|
| 2191 | |||
| 2192 | /** |
||
| 2193 | * {@inheritdoc} |
||
| 2194 | */ |
||
| 2195 | 393 | public function walkSimpleArithmeticExpression($simpleArithmeticExpr) |
|
| 2203 | |||
| 2204 | /** |
||
| 2205 | * {@inheritdoc} |
||
| 2206 | */ |
||
| 2207 | 414 | public function walkArithmeticTerm($term) |
|
| 2223 | |||
| 2224 | /** |
||
| 2225 | * {@inheritdoc} |
||
| 2226 | */ |
||
| 2227 | 414 | public function walkArithmeticFactor($factor) |
|
| 2245 | |||
| 2246 | /** |
||
| 2247 | * Walks down an ArithmeticPrimary that represents an AST node, thereby generating the appropriate SQL. |
||
| 2248 | * |
||
| 2249 | * @param mixed $primary |
||
| 2250 | * |
||
| 2251 | * @return string The SQL. |
||
| 2252 | */ |
||
| 2253 | 414 | public function walkArithmeticPrimary($primary) |
|
| 2265 | |||
| 2266 | /** |
||
| 2267 | * {@inheritdoc} |
||
| 2268 | */ |
||
| 2269 | 18 | public function walkStringPrimary($stringPrimary) |
|
| 2275 | |||
| 2276 | /** |
||
| 2277 | * {@inheritdoc} |
||
| 2278 | */ |
||
| 2279 | 30 | public function walkResultVariable($resultVariable) |
|
| 2289 | |||
| 2290 | /** |
||
| 2291 | * @param ClassMetadataInfo $rootClass |
||
| 2292 | * @param AST\InstanceOfExpression $instanceOfExpr |
||
| 2293 | * @return string The list in parentheses of valid child discriminators from the given class |
||
| 2294 | * @throws QueryException |
||
| 2295 | */ |
||
| 2296 | 14 | private function getChildDiscriminatorsFromClassMetadata(ClassMetadataInfo $rootClass, AST\InstanceOfExpression $instanceOfExpr): string |
|
| 2322 | } |
||
| 2323 |
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.