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 | 346 | 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 | 18 | public function getConnection() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Gets the EntityManager used by the walker. |
||
| 215 | * |
||
| 216 | * @return \Doctrine\ORM\EntityManager |
||
| 217 | */ |
||
| 218 | 18 | 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 | 16 | public function getQueryComponent($dqlAlias) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritdoc} |
||
| 237 | */ |
||
| 238 | public function getQueryComponents() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritdoc} |
||
| 245 | */ |
||
| 246 | 1 | public function setQueryComponent($dqlAlias, array $queryComponent) |
|
| 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 = '') |
|
| 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 = '') |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 572 | |||
| 573 | /** |
||
| 574 | * {@inheritdoc} |
||
| 575 | */ |
||
| 576 | 21 | public function walkUpdateStatement(AST\UpdateStatement $AST) |
|
| 584 | |||
| 585 | /** |
||
| 586 | * {@inheritdoc} |
||
| 587 | */ |
||
| 588 | 32 | 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 | 180 | public function walkIdentificationVariable($identificationVariable, $fieldName = null) |
|
| 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) |
|
| 808 | |||
| 809 | /** |
||
| 810 | * {@inheritdoc} |
||
| 811 | */ |
||
| 812 | 288 | public function walkFromClause($fromClause) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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; |
|
| 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) |
|
| 1071 | |||
| 1072 | /** |
||
| 1073 | * {@inheritdoc} |
||
| 1074 | */ |
||
| 1075 | 34 | public function walkOrderByClause($orderByClause) |
|
| 1085 | |||
| 1086 | /** |
||
| 1087 | * {@inheritdoc} |
||
| 1088 | */ |
||
| 1089 | 44 | public function walkOrderByItem($orderByItem) |
|
| 1105 | |||
| 1106 | /** |
||
| 1107 | * {@inheritdoc} |
||
| 1108 | */ |
||
| 1109 | 10 | public function walkHavingClause($havingClause) |
|
| 1113 | |||
| 1114 | /** |
||
| 1115 | * {@inheritdoc} |
||
| 1116 | */ |
||
| 1117 | 90 | public function walkJoin($join) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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)) { |
|
| 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))) { |
|
| 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) |
||
| 1442 | |||
| 1443 | /** |
||
| 1444 | * {@inheritdoc} |
||
| 1445 | */ |
||
| 1446 | 25 | public function walkSubselect($subselect) |
|
| 1467 | |||
| 1468 | /** |
||
| 1469 | * {@inheritdoc} |
||
| 1470 | */ |
||
| 1471 | 25 | public function walkSubselectFromClause($subselectFromClause) |
|
| 1482 | |||
| 1483 | /** |
||
| 1484 | * {@inheritdoc} |
||
| 1485 | */ |
||
| 1486 | 25 | public function walkSimpleSelectClause($simpleSelectClause) |
|
| 1491 | |||
| 1492 | /** |
||
| 1493 | * @param \Doctrine\ORM\Query\AST\ParenthesisExpression $parenthesisExpression |
||
| 1494 | * |
||
| 1495 | * @return string. |
||
| 1496 | */ |
||
| 1497 | 18 | public function walkParenthesisExpression(AST\ParenthesisExpression $parenthesisExpression) |
|
| 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) |
|
| 1632 | |||
| 1633 | /** |
||
| 1634 | * {@inheritdoc} |
||
| 1635 | */ |
||
| 1636 | 13 | public function walkGroupByClause($groupByClause) |
|
| 1646 | |||
| 1647 | /** |
||
| 1648 | * {@inheritdoc} |
||
| 1649 | */ |
||
| 1650 | 13 | public function walkGroupByItem($groupByItem) |
|
| 1693 | |||
| 1694 | /** |
||
| 1695 | * {@inheritdoc} |
||
| 1696 | */ |
||
| 1697 | 32 | public function walkDeleteClause(AST\DeleteClause $deleteClause) |
|
| 1708 | |||
| 1709 | /** |
||
| 1710 | * {@inheritdoc} |
||
| 1711 | */ |
||
| 1712 | 21 | public function walkUpdateClause($updateClause) |
|
| 1725 | |||
| 1726 | /** |
||
| 1727 | * {@inheritdoc} |
||
| 1728 | */ |
||
| 1729 | 23 | public function walkUpdateItem($updateItem) |
|
| 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) |
|
| 1808 | |||
| 1809 | /** |
||
| 1810 | * {@inheritdoc} |
||
| 1811 | */ |
||
| 1812 | 197 | public function walkConditionalTerm($condTerm) |
|
| 1822 | |||
| 1823 | /** |
||
| 1824 | * {@inheritdoc} |
||
| 1825 | */ |
||
| 1826 | 197 | public function walkConditionalFactor($factor) |
|
| 1834 | |||
| 1835 | /** |
||
| 1836 | * {@inheritdoc} |
||
| 1837 | */ |
||
| 1838 | 197 | public function walkConditionalPrimary($primary) |
|
| 1850 | |||
| 1851 | /** |
||
| 1852 | * {@inheritdoc} |
||
| 1853 | */ |
||
| 1854 | 5 | public function walkExistsExpression($existsExpr) |
|
| 1862 | |||
| 1863 | /** |
||
| 1864 | * {@inheritdoc} |
||
| 1865 | */ |
||
| 1866 | 6 | public function walkCollectionMemberExpression($collMemberExpr) |
|
| 1972 | |||
| 1973 | /** |
||
| 1974 | * {@inheritdoc} |
||
| 1975 | */ |
||
| 1976 | 2 | public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr) |
|
| 1983 | |||
| 1984 | /** |
||
| 1985 | * {@inheritdoc} |
||
| 1986 | */ |
||
| 1987 | 9 | public function walkNullComparisonExpression($nullCompExpr) |
|
| 2004 | |||
| 2005 | /** |
||
| 2006 | * {@inheritdoc} |
||
| 2007 | */ |
||
| 2008 | 29 | public function walkInExpression($inExpr) |
|
| 2020 | |||
| 2021 | /** |
||
| 2022 | * {@inheritdoc} |
||
| 2023 | */ |
||
| 2024 | 7 | public function walkInstanceOfExpression($instanceOfExpr) |
|
| 2073 | |||
| 2074 | /** |
||
| 2075 | * {@inheritdoc} |
||
| 2076 | */ |
||
| 2077 | 22 | public function walkInParameter($inParam) |
|
| 2083 | |||
| 2084 | /** |
||
| 2085 | * {@inheritdoc} |
||
| 2086 | */ |
||
| 2087 | 98 | public function walkLiteral($literal) |
|
| 2103 | |||
| 2104 | /** |
||
| 2105 | * {@inheritdoc} |
||
| 2106 | */ |
||
| 2107 | 6 | public function walkBetweenExpression($betweenExpr) |
|
| 2120 | |||
| 2121 | /** |
||
| 2122 | * {@inheritdoc} |
||
| 2123 | */ |
||
| 2124 | 8 | public function walkLikeExpression($likeExpr) |
|
| 2149 | |||
| 2150 | /** |
||
| 2151 | * {@inheritdoc} |
||
| 2152 | */ |
||
| 2153 | 5 | public function walkStateFieldPathExpression($stateFieldPathExpression) |
|
| 2157 | |||
| 2158 | /** |
||
| 2159 | * {@inheritdoc} |
||
| 2160 | */ |
||
| 2161 | 148 | public function walkComparisonExpression($compExpr) |
|
| 2179 | |||
| 2180 | /** |
||
| 2181 | * {@inheritdoc} |
||
| 2182 | */ |
||
| 2183 | 86 | public function walkInputParameter($inputParam) |
|
| 2195 | |||
| 2196 | /** |
||
| 2197 | * {@inheritdoc} |
||
| 2198 | */ |
||
| 2199 | 166 | public function walkArithmeticExpression($arithmeticExpr) |
|
| 2205 | |||
| 2206 | /** |
||
| 2207 | * {@inheritdoc} |
||
| 2208 | */ |
||
| 2209 | 201 | public function walkSimpleArithmeticExpression($simpleArithmeticExpr) |
|
| 2217 | |||
| 2218 | /** |
||
| 2219 | * {@inheritdoc} |
||
| 2220 | */ |
||
| 2221 | 205 | public function walkArithmeticTerm($term) |
|
| 2237 | |||
| 2238 | /** |
||
| 2239 | * {@inheritdoc} |
||
| 2240 | */ |
||
| 2241 | 205 | public function walkArithmeticFactor($factor) |
|
| 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) |
|
| 2279 | |||
| 2280 | /** |
||
| 2281 | * {@inheritdoc} |
||
| 2282 | */ |
||
| 2283 | 12 | public function walkStringPrimary($stringPrimary) |
|
| 2289 | |||
| 2290 | /** |
||
| 2291 | * {@inheritdoc} |
||
| 2292 | */ |
||
| 2293 | 25 | public function walkResultVariable($resultVariable) |
|
| 2303 | } |
||
| 2304 |
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.