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 |
||
| 41 | class SqlWalker implements TreeWalker |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | const HINT_DISTINCT = 'doctrine.distinct'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var ResultSetMapping |
||
| 50 | */ |
||
| 51 | private $rsm; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Counter for generating unique column aliases. |
||
| 55 | * |
||
| 56 | * @var integer |
||
| 57 | */ |
||
| 58 | private $aliasCounter = 0; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Counter for generating unique table aliases. |
||
| 62 | * |
||
| 63 | * @var integer |
||
| 64 | */ |
||
| 65 | private $tableAliasCounter = 0; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Counter for generating unique scalar result. |
||
| 69 | * |
||
| 70 | * @var integer |
||
| 71 | */ |
||
| 72 | private $scalarResultCounter = 1; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Counter for generating unique parameter indexes. |
||
| 76 | * |
||
| 77 | * @var integer |
||
| 78 | */ |
||
| 79 | private $sqlParamIndex = 0; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Counter for generating indexes. |
||
| 83 | * |
||
| 84 | * @var integer |
||
| 85 | */ |
||
| 86 | private $newObjectCounter = 0; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var ParserResult |
||
| 90 | */ |
||
| 91 | private $parserResult; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var \Doctrine\ORM\EntityManager |
||
| 95 | */ |
||
| 96 | private $em; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var \Doctrine\DBAL\Connection |
||
| 100 | */ |
||
| 101 | private $conn; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var \Doctrine\ORM\AbstractQuery |
||
| 105 | */ |
||
| 106 | private $query; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | private $tableAliasMap = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Map from result variable names to their SQL column alias names. |
||
| 115 | * |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | private $scalarResultAliasMap = []; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Map from Table-Alias + Column-Name to OrderBy-Direction. |
||
| 122 | * |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | private $orderedColumnsMap = []; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Map from DQL-Alias + Field-Name to SQL Column Alias. |
||
| 129 | * |
||
| 130 | * @var array |
||
| 131 | */ |
||
| 132 | private $scalarFields = []; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Map of all components/classes that appear in the DQL query. |
||
| 136 | * |
||
| 137 | * @var array |
||
| 138 | */ |
||
| 139 | private $queryComponents; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * A list of classes that appear in non-scalar SelectExpressions. |
||
| 143 | * |
||
| 144 | * @var array |
||
| 145 | */ |
||
| 146 | private $selectedClasses = []; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * The DQL alias of the root class of the currently traversed query. |
||
| 150 | * |
||
| 151 | * @var array |
||
| 152 | */ |
||
| 153 | private $rootAliases = []; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Flag that indicates whether to generate SQL table aliases in the SQL. |
||
| 157 | * These should only be generated for SELECT queries, not for UPDATE/DELETE. |
||
| 158 | * |
||
| 159 | * @var boolean |
||
| 160 | */ |
||
| 161 | private $useSqlTableAliases = true; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * The database platform abstraction. |
||
| 165 | * |
||
| 166 | * @var \Doctrine\DBAL\Platforms\AbstractPlatform |
||
| 167 | */ |
||
| 168 | private $platform; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * The quote strategy. |
||
| 172 | * |
||
| 173 | * @var \Doctrine\ORM\Mapping\QuoteStrategy |
||
| 174 | */ |
||
| 175 | private $quoteStrategy; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritDoc} |
||
| 179 | */ |
||
| 180 | 690 | public function __construct($query, $parserResult, array $queryComponents) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Gets the Query instance used by the walker. |
||
| 194 | * |
||
| 195 | * @return Query. |
||
|
|
|||
| 196 | */ |
||
| 197 | public function getQuery() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Gets the Connection used by the walker. |
||
| 204 | * |
||
| 205 | * @return \Doctrine\DBAL\Connection |
||
| 206 | */ |
||
| 207 | 35 | public function getConnection() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Gets the EntityManager used by the walker. |
||
| 214 | * |
||
| 215 | * @return \Doctrine\ORM\EntityManager |
||
| 216 | */ |
||
| 217 | 22 | public function getEntityManager() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Gets the information about a single query component. |
||
| 224 | * |
||
| 225 | * @param string $dqlAlias The DQL alias. |
||
| 226 | * |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | 17 | public function getQueryComponent($dqlAlias) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * {@inheritdoc} |
||
| 236 | */ |
||
| 237 | public function getQueryComponents() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * {@inheritdoc} |
||
| 244 | */ |
||
| 245 | 1 | View Code Duplication | public function setQueryComponent($dqlAlias, array $queryComponent) |
| 246 | { |
||
| 247 | 1 | $requiredKeys = ['metadata', 'parent', 'relation', 'map', 'nestingLevel', 'token']; |
|
| 248 | |||
| 249 | 1 | if (array_diff($requiredKeys, array_keys($queryComponent))) { |
|
| 250 | 1 | throw QueryException::invalidQueryComponent($dqlAlias); |
|
| 251 | } |
||
| 252 | |||
| 253 | $this->queryComponents[$dqlAlias] = $queryComponent; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritdoc} |
||
| 258 | */ |
||
| 259 | 684 | public function getExecutor($AST) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Generates a unique, short SQL table alias. |
||
| 283 | * |
||
| 284 | * @param string $tableName Table name |
||
| 285 | * @param string $dqlAlias The DQL alias. |
||
| 286 | * |
||
| 287 | * @return string Generated table alias. |
||
| 288 | */ |
||
| 289 | 636 | public function getSQLTableAlias($tableName, $dqlAlias = '') |
|
| 290 | { |
||
| 291 | 636 | $tableName .= ($dqlAlias) ? '@[' . $dqlAlias . ']' : ''; |
|
| 292 | |||
| 293 | 636 | if ( ! isset($this->tableAliasMap[$tableName])) { |
|
| 294 | 636 | $this->tableAliasMap[$tableName] = (preg_match('/[a-z]/i', $tableName[0]) ? strtolower($tableName[0]) : 't') |
|
| 295 | 636 | . $this->tableAliasCounter++ . '_'; |
|
| 296 | } |
||
| 297 | |||
| 298 | 636 | return $this->tableAliasMap[$tableName]; |
|
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Forces the SqlWalker to use a specific alias for a table name, rather than |
||
| 303 | * generating an alias on its own. |
||
| 304 | * |
||
| 305 | * @param string $tableName |
||
| 306 | * @param string $alias |
||
| 307 | * @param string $dqlAlias |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | 65 | public function setSQLTableAlias($tableName, $alias, $dqlAlias = '') |
|
| 312 | { |
||
| 313 | 65 | $tableName .= ($dqlAlias) ? '@[' . $dqlAlias . ']' : ''; |
|
| 314 | |||
| 315 | 65 | $this->tableAliasMap[$tableName] = $alias; |
|
| 316 | |||
| 317 | 65 | return $alias; |
|
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Gets an SQL column alias for a column name. |
||
| 322 | * |
||
| 323 | * @param string $columnName |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | 625 | public function getSQLColumnAlias($columnName) |
|
| 328 | { |
||
| 329 | 625 | return $this->quoteStrategy->getColumnAlias($columnName, $this->aliasCounter++, $this->platform); |
|
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Generates the SQL JOINs that are necessary for Class Table Inheritance |
||
| 334 | * for the given class. |
||
| 335 | * |
||
| 336 | * @param ClassMetadata $class The class for which to generate the joins. |
||
| 337 | * @param string $dqlAlias The DQL alias of the class. |
||
| 338 | * |
||
| 339 | * @return string The SQL. |
||
| 340 | */ |
||
| 341 | 94 | private function _generateClassTableInheritanceJoins($class, $dqlAlias) |
|
| 342 | { |
||
| 343 | 94 | $sql = ''; |
|
| 344 | |||
| 345 | 94 | $baseTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias); |
|
| 346 | |||
| 347 | // INNER JOIN parent class tables |
||
| 348 | 94 | foreach ($class->parentClasses as $parentClassName) { |
|
| 349 | 66 | $parentClass = $this->em->getClassMetadata($parentClassName); |
|
| 350 | 66 | $tableAlias = $this->getSQLTableAlias($parentClass->getTableName(), $dqlAlias); |
|
| 351 | |||
| 352 | // If this is a joined association we must use left joins to preserve the correct result. |
||
| 353 | 66 | $sql .= isset($this->queryComponents[$dqlAlias]['relation']) ? ' LEFT ' : ' INNER '; |
|
| 354 | 66 | $sql .= 'JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON '; |
|
| 355 | |||
| 356 | 66 | $sqlParts = []; |
|
| 357 | |||
| 358 | 66 | View Code Duplication | foreach ($this->quoteStrategy->getIdentifierColumnNames($class, $this->platform) as $columnName) { |
| 359 | 66 | $sqlParts[] = $baseTableAlias . '.' . $columnName . ' = ' . $tableAlias . '.' . $columnName; |
|
| 360 | } |
||
| 361 | |||
| 362 | // Add filters on the root class |
||
| 363 | 66 | if ($filterSql = $this->generateFilterConditionSQL($parentClass, $tableAlias)) { |
|
| 364 | 1 | $sqlParts[] = $filterSql; |
|
| 365 | } |
||
| 366 | |||
| 367 | 66 | $sql .= implode(' AND ', $sqlParts); |
|
| 368 | } |
||
| 369 | |||
| 370 | // Ignore subclassing inclusion if partial objects is disallowed |
||
| 371 | 94 | if ($this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) { |
|
| 372 | 21 | return $sql; |
|
| 373 | } |
||
| 374 | |||
| 375 | // LEFT JOIN child class tables |
||
| 376 | 73 | foreach ($class->subClasses as $subClassName) { |
|
| 377 | 34 | $subClass = $this->em->getClassMetadata($subClassName); |
|
| 378 | 34 | $tableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias); |
|
| 379 | |||
| 380 | 34 | $sql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->platform) . ' ' . $tableAlias . ' ON '; |
|
| 381 | |||
| 382 | 34 | $sqlParts = []; |
|
| 383 | |||
| 384 | 34 | View Code Duplication | foreach ($this->quoteStrategy->getIdentifierColumnNames($subClass, $this->platform) as $columnName) { |
| 385 | 34 | $sqlParts[] = $baseTableAlias . '.' . $columnName . ' = ' . $tableAlias . '.' . $columnName; |
|
| 386 | } |
||
| 387 | |||
| 388 | 34 | $sql .= implode(' AND ', $sqlParts); |
|
| 389 | } |
||
| 390 | |||
| 391 | 73 | return $sql; |
|
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | 615 | private function _generateOrderedCollectionOrderByItems() |
|
| 398 | { |
||
| 399 | 615 | $orderedColumns = []; |
|
| 400 | |||
| 401 | 615 | foreach ($this->selectedClasses as $selectedClass) { |
|
| 402 | 478 | $dqlAlias = $selectedClass['dqlAlias']; |
|
| 403 | 478 | $qComp = $this->queryComponents[$dqlAlias]; |
|
| 404 | |||
| 405 | 478 | if ( ! isset($qComp['relation']['orderBy'])) { |
|
| 406 | 478 | continue; |
|
| 407 | } |
||
| 408 | |||
| 409 | 6 | $persister = $this->em->getUnitOfWork()->getEntityPersister($qComp['metadata']->name); |
|
| 410 | |||
| 411 | 6 | foreach ($qComp['relation']['orderBy'] as $fieldName => $orientation) { |
|
| 412 | 6 | $columnName = $this->quoteStrategy->getColumnName($fieldName, $qComp['metadata'], $this->platform); |
|
| 413 | 6 | $tableName = ($qComp['metadata']->isInheritanceTypeJoined()) |
|
| 414 | 1 | ? $persister->getOwningTable($fieldName) |
|
| 415 | 6 | : $qComp['metadata']->getTableName(); |
|
| 416 | |||
| 417 | 6 | $orderedColumn = $this->getSQLTableAlias($tableName, $dqlAlias) . '.' . $columnName; |
|
| 418 | |||
| 419 | // OrderByClause should replace an ordered relation. see - DDC-2475 |
||
| 420 | 6 | if (isset($this->orderedColumnsMap[$orderedColumn])) { |
|
| 421 | 1 | continue; |
|
| 422 | } |
||
| 423 | |||
| 424 | 6 | $this->orderedColumnsMap[$orderedColumn] = $orientation; |
|
| 425 | 6 | $orderedColumns[] = $orderedColumn . ' ' . $orientation; |
|
| 426 | } |
||
| 427 | } |
||
| 428 | |||
| 429 | 615 | return implode(', ', $orderedColumns); |
|
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Generates a discriminator column SQL condition for the class with the given DQL alias. |
||
| 434 | * |
||
| 435 | * @param array $dqlAliases List of root DQL aliases to inspect for discriminator restrictions. |
||
| 436 | * |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | 675 | private function _generateDiscriminatorColumnConditionSQL(array $dqlAliases) |
|
| 440 | { |
||
| 441 | 675 | $sqlParts = []; |
|
| 442 | |||
| 443 | 675 | foreach ($dqlAliases as $dqlAlias) { |
|
| 444 | 675 | $class = $this->queryComponents[$dqlAlias]['metadata']; |
|
| 445 | |||
| 446 | 675 | if ( ! $class->isInheritanceTypeSingleTable()) continue; |
|
| 447 | |||
| 448 | 40 | $conn = $this->em->getConnection(); |
|
| 449 | 40 | $values = []; |
|
| 450 | |||
| 451 | 40 | if ($class->discriminatorValue !== null) { // discriminators can be 0 |
|
| 452 | 21 | $values[] = $conn->quote($class->discriminatorValue); |
|
| 453 | } |
||
| 454 | |||
| 455 | 40 | foreach ($class->subClasses as $subclassName) { |
|
| 456 | 29 | $values[] = $conn->quote($this->em->getClassMetadata($subclassName)->discriminatorValue); |
|
| 457 | } |
||
| 458 | |||
| 459 | 40 | $sqlTableAlias = ($this->useSqlTableAliases) |
|
| 460 | 35 | ? $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.' |
|
| 461 | 40 | : ''; |
|
| 462 | |||
| 463 | 40 | $sqlParts[] = $sqlTableAlias . $class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')'; |
|
| 464 | } |
||
| 465 | |||
| 466 | 675 | $sql = implode(' AND ', $sqlParts); |
|
| 467 | |||
| 468 | 675 | return (count($sqlParts) > 1) ? '(' . $sql . ')' : $sql; |
|
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Generates the filter SQL for a given entity and table alias. |
||
| 473 | * |
||
| 474 | * @param ClassMetadata $targetEntity Metadata of the target entity. |
||
| 475 | * @param string $targetTableAlias The table alias of the joined/selected table. |
||
| 476 | * |
||
| 477 | * @return string The SQL query part to add to a query. |
||
| 478 | */ |
||
| 479 | 319 | private function generateFilterConditionSQL(ClassMetadata $targetEntity, $targetTableAlias) |
|
| 480 | { |
||
| 481 | 319 | if (!$this->em->hasFilters()) { |
|
| 482 | 281 | return ''; |
|
| 483 | } |
||
| 484 | |||
| 485 | 43 | switch($targetEntity->inheritanceType) { |
|
| 486 | 43 | case ClassMetadata::INHERITANCE_TYPE_NONE: |
|
| 487 | 33 | break; |
|
| 488 | 10 | case ClassMetadata::INHERITANCE_TYPE_JOINED: |
|
| 489 | // The classes in the inheritance will be added to the query one by one, |
||
| 490 | // but only the root node is getting filtered |
||
| 491 | 6 | if ($targetEntity->name !== $targetEntity->rootEntityName) { |
|
| 492 | 4 | return ''; |
|
| 493 | } |
||
| 494 | 6 | break; |
|
| 495 | 4 | case ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE: |
|
| 496 | // With STI the table will only be queried once, make sure that the filters |
||
| 497 | // are added to the root entity |
||
| 498 | 4 | $targetEntity = $this->em->getClassMetadata($targetEntity->rootEntityName); |
|
| 499 | 4 | break; |
|
| 500 | default: |
||
| 501 | //@todo: throw exception? |
||
| 502 | return ''; |
||
| 503 | } |
||
| 504 | |||
| 505 | 43 | $filterClauses = []; |
|
| 506 | 43 | foreach ($this->em->getFilters()->getEnabledFilters() as $filter) { |
|
| 507 | 10 | if ('' !== $filterExpr = $filter->addFilterConstraint($targetEntity, $targetTableAlias)) { |
|
| 508 | 9 | $filterClauses[] = '(' . $filterExpr . ')'; |
|
| 509 | } |
||
| 510 | } |
||
| 511 | |||
| 512 | 43 | return implode(' AND ', $filterClauses); |
|
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * {@inheritdoc} |
||
| 517 | */ |
||
| 518 | 625 | public function walkSelectStatement(AST\SelectStatement $AST) |
|
| 519 | { |
||
| 520 | 625 | $limit = $this->query->getMaxResults(); |
|
| 521 | 625 | $offset = $this->query->getFirstResult(); |
|
| 522 | 625 | $lockMode = $this->query->getHint(Query::HINT_LOCK_MODE); |
|
| 523 | 625 | $sql = $this->walkSelectClause($AST->selectClause) |
|
| 524 | 625 | . $this->walkFromClause($AST->fromClause) |
|
| 525 | 623 | . $this->walkWhereClause($AST->whereClause); |
|
| 526 | |||
| 527 | 616 | if ($AST->groupByClause) { |
|
| 528 | 23 | $sql .= $this->walkGroupByClause($AST->groupByClause); |
|
| 529 | } |
||
| 530 | |||
| 531 | 616 | if ($AST->havingClause) { |
|
| 532 | 14 | $sql .= $this->walkHavingClause($AST->havingClause); |
|
| 533 | } |
||
| 534 | |||
| 535 | 616 | if ($AST->orderByClause) { |
|
| 536 | 142 | $sql .= $this->walkOrderByClause($AST->orderByClause); |
|
| 537 | } |
||
| 538 | |||
| 539 | 615 | if ( ! $AST->orderByClause && ($orderBySql = $this->_generateOrderedCollectionOrderByItems())) { |
|
| 540 | 6 | $sql .= ' ORDER BY ' . $orderBySql; |
|
| 541 | } |
||
| 542 | |||
| 543 | 615 | View Code Duplication | if ($limit !== null || $offset !== null) { |
| 544 | 39 | $sql = $this->platform->modifyLimitQuery($sql, $limit, $offset); |
|
| 545 | } |
||
| 546 | |||
| 547 | 615 | if ($lockMode === null || $lockMode === false || $lockMode === LockMode::NONE) { |
|
| 548 | 610 | return $sql; |
|
| 549 | } |
||
| 550 | |||
| 551 | 5 | if ($lockMode === LockMode::PESSIMISTIC_READ) { |
|
| 552 | 3 | return $sql . ' ' . $this->platform->getReadLockSQL(); |
|
| 553 | } |
||
| 554 | |||
| 555 | 2 | if ($lockMode === LockMode::PESSIMISTIC_WRITE) { |
|
| 556 | 1 | return $sql . ' ' . $this->platform->getWriteLockSQL(); |
|
| 557 | } |
||
| 558 | |||
| 559 | 1 | if ($lockMode !== LockMode::OPTIMISTIC) { |
|
| 560 | throw QueryException::invalidLockMode(); |
||
| 561 | } |
||
| 562 | |||
| 563 | 1 | foreach ($this->selectedClasses as $selectedClass) { |
|
| 564 | 1 | if ( ! $selectedClass['class']->isVersioned) { |
|
| 565 | 1 | throw OptimisticLockException::lockFailed($selectedClass['class']->name); |
|
| 566 | } |
||
| 567 | } |
||
| 568 | |||
| 569 | return $sql; |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * {@inheritdoc} |
||
| 574 | */ |
||
| 575 | 25 | public function walkUpdateStatement(AST\UpdateStatement $AST) |
|
| 576 | { |
||
| 577 | 25 | $this->useSqlTableAliases = false; |
|
| 578 | 25 | $this->rsm->isSelect = false; |
|
| 579 | |||
| 580 | 25 | return $this->walkUpdateClause($AST->updateClause) |
|
| 581 | 25 | . $this->walkWhereClause($AST->whereClause); |
|
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * {@inheritdoc} |
||
| 586 | */ |
||
| 587 | 36 | public function walkDeleteStatement(AST\DeleteStatement $AST) |
|
| 588 | { |
||
| 589 | 36 | $this->useSqlTableAliases = false; |
|
| 590 | 36 | $this->rsm->isSelect = false; |
|
| 591 | |||
| 592 | 36 | return $this->walkDeleteClause($AST->deleteClause) |
|
| 593 | 36 | . $this->walkWhereClause($AST->whereClause); |
|
| 594 | } |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Walks down an IdentificationVariable AST node, thereby generating the appropriate SQL. |
||
| 598 | * This one differs of ->walkIdentificationVariable() because it generates the entity identifiers. |
||
| 599 | * |
||
| 600 | * @param string $identVariable |
||
| 601 | * |
||
| 602 | * @return string |
||
| 603 | */ |
||
| 604 | 2 | public function walkEntityIdentificationVariable($identVariable) |
|
| 605 | { |
||
| 606 | 2 | $class = $this->queryComponents[$identVariable]['metadata']; |
|
| 607 | 2 | $tableAlias = $this->getSQLTableAlias($class->getTableName(), $identVariable); |
|
| 608 | 2 | $sqlParts = []; |
|
| 609 | |||
| 610 | 2 | foreach ($this->quoteStrategy->getIdentifierColumnNames($class, $this->platform) as $columnName) { |
|
| 611 | 2 | $sqlParts[] = $tableAlias . '.' . $columnName; |
|
| 612 | } |
||
| 613 | |||
| 614 | 2 | return implode(', ', $sqlParts); |
|
| 615 | } |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Walks down an IdentificationVariable (no AST node associated), thereby generating the SQL. |
||
| 619 | * |
||
| 620 | * @param string $identificationVariable |
||
| 621 | * @param string $fieldName |
||
| 622 | * |
||
| 623 | * @return string The SQL. |
||
| 624 | */ |
||
| 625 | 423 | public function walkIdentificationVariable($identificationVariable, $fieldName = null) |
|
| 626 | { |
||
| 627 | 423 | $class = $this->queryComponents[$identificationVariable]['metadata']; |
|
| 628 | |||
| 629 | if ( |
||
| 630 | 423 | $fieldName !== null && $class->isInheritanceTypeJoined() && |
|
| 631 | 54 | isset($class->fieldMappings[$fieldName]['inherited']) |
|
| 632 | ) { |
||
| 633 | 37 | $class = $this->em->getClassMetadata($class->fieldMappings[$fieldName]['inherited']); |
|
| 634 | } |
||
| 635 | |||
| 636 | 423 | return $this->getSQLTableAlias($class->getTableName(), $identificationVariable); |
|
| 637 | } |
||
| 638 | |||
| 639 | /** |
||
| 640 | * {@inheritdoc} |
||
| 641 | */ |
||
| 642 | 492 | public function walkPathExpression($pathExpr) |
|
| 643 | { |
||
| 644 | 492 | $sql = ''; |
|
| 645 | |||
| 646 | /* @var $pathExpr Query\AST\PathExpression */ |
||
| 647 | 492 | switch ($pathExpr->type) { |
|
| 648 | 492 | case AST\PathExpression::TYPE_STATE_FIELD: |
|
| 649 | 471 | $fieldName = $pathExpr->field; |
|
| 650 | 471 | $dqlAlias = $pathExpr->identificationVariable; |
|
| 651 | 471 | $class = $this->queryComponents[$dqlAlias]['metadata']; |
|
| 652 | |||
| 653 | 471 | if ($this->useSqlTableAliases) { |
|
| 654 | 423 | $sql .= $this->walkIdentificationVariable($dqlAlias, $fieldName) . '.'; |
|
| 655 | } |
||
| 656 | |||
| 657 | 471 | $sql .= $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform); |
|
| 658 | 471 | break; |
|
| 659 | |||
| 660 | 62 | case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION: |
|
| 661 | // 1- the owning side: |
||
| 662 | // Just use the foreign key, i.e. u.group_id |
||
| 663 | 62 | $fieldName = $pathExpr->field; |
|
| 664 | 62 | $dqlAlias = $pathExpr->identificationVariable; |
|
| 665 | 62 | $class = $this->queryComponents[$dqlAlias]['metadata']; |
|
| 666 | |||
| 667 | 62 | if (isset($class->associationMappings[$fieldName]['inherited'])) { |
|
| 668 | 2 | $class = $this->em->getClassMetadata($class->associationMappings[$fieldName]['inherited']); |
|
| 669 | } |
||
| 670 | |||
| 671 | 62 | $assoc = $class->associationMappings[$fieldName]; |
|
| 672 | |||
| 673 | 62 | if ( ! $assoc['isOwningSide']) { |
|
| 674 | 2 | throw QueryException::associationPathInverseSideNotSupported($pathExpr); |
|
| 675 | } |
||
| 676 | |||
| 677 | // COMPOSITE KEYS NOT (YET?) SUPPORTED |
||
| 678 | 60 | if (count($assoc['sourceToTargetKeyColumns']) > 1) { |
|
| 679 | 1 | throw QueryException::associationPathCompositeKeyNotSupported(); |
|
| 680 | } |
||
| 681 | |||
| 682 | 59 | if ($this->useSqlTableAliases) { |
|
| 683 | 56 | $sql .= $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.'; |
|
| 684 | } |
||
| 685 | |||
| 686 | 59 | $sql .= reset($assoc['targetToSourceKeyColumns']); |
|
| 687 | 59 | break; |
|
| 688 | |||
| 689 | default: |
||
| 690 | throw QueryException::invalidPathExpression($pathExpr); |
||
| 691 | } |
||
| 692 | |||
| 693 | 489 | return $sql; |
|
| 694 | } |
||
| 695 | |||
| 696 | /** |
||
| 697 | * {@inheritdoc} |
||
| 698 | */ |
||
| 699 | 625 | public function walkSelectClause($selectClause) |
|
| 700 | { |
||
| 701 | 625 | $sql = 'SELECT ' . (($selectClause->isDistinct) ? 'DISTINCT ' : ''); |
|
| 702 | 625 | $sqlSelectExpressions = array_filter(array_map([$this, 'walkSelectExpression'], $selectClause->selectExpressions)); |
|
| 703 | |||
| 704 | 625 | if ($this->query->getHint(Query::HINT_INTERNAL_ITERATION) == true && $selectClause->isDistinct) { |
|
| 705 | 1 | $this->query->setHint(self::HINT_DISTINCT, true); |
|
| 706 | } |
||
| 707 | |||
| 708 | 625 | $addMetaColumns = ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD) && |
|
| 709 | 462 | $this->query->getHydrationMode() == Query::HYDRATE_OBJECT |
|
| 710 | || |
||
| 711 | 290 | $this->query->getHydrationMode() != Query::HYDRATE_OBJECT && |
|
| 712 | 625 | $this->query->getHint(Query::HINT_INCLUDE_META_COLUMNS); |
|
| 713 | |||
| 714 | 625 | foreach ($this->selectedClasses as $selectedClass) { |
|
| 715 | 488 | $class = $selectedClass['class']; |
|
| 716 | 488 | $dqlAlias = $selectedClass['dqlAlias']; |
|
| 717 | 488 | $resultAlias = $selectedClass['resultAlias']; |
|
| 718 | |||
| 719 | // Register as entity or joined entity result |
||
| 720 | 488 | if ($this->queryComponents[$dqlAlias]['relation'] === null) { |
|
| 721 | 488 | $this->rsm->addEntityResult($class->name, $dqlAlias, $resultAlias); |
|
| 722 | } else { |
||
| 723 | 157 | $this->rsm->addJoinedEntityResult( |
|
| 724 | 157 | $class->name, |
|
| 725 | $dqlAlias, |
||
| 726 | 157 | $this->queryComponents[$dqlAlias]['parent'], |
|
| 727 | 157 | $this->queryComponents[$dqlAlias]['relation']['fieldName'] |
|
| 728 | ); |
||
| 729 | } |
||
| 730 | |||
| 731 | 488 | if ($class->isInheritanceTypeSingleTable() || $class->isInheritanceTypeJoined()) { |
|
| 732 | // Add discriminator columns to SQL |
||
| 733 | 92 | $rootClass = $this->em->getClassMetadata($class->rootEntityName); |
|
| 734 | 92 | $tblAlias = $this->getSQLTableAlias($rootClass->getTableName(), $dqlAlias); |
|
| 735 | 92 | $discrColumn = $rootClass->discriminatorColumn; |
|
| 736 | 92 | $columnAlias = $this->getSQLColumnAlias($discrColumn['name']); |
|
| 737 | |||
| 738 | 92 | $sqlSelectExpressions[] = $tblAlias . '.' . $discrColumn['name'] . ' AS ' . $columnAlias; |
|
| 739 | |||
| 740 | 92 | $this->rsm->setDiscriminatorColumn($dqlAlias, $columnAlias); |
|
| 741 | 92 | $this->rsm->addMetaResult($dqlAlias, $columnAlias, $discrColumn['fieldName'], false, $discrColumn['type']); |
|
| 742 | } |
||
| 743 | |||
| 744 | // Add foreign key columns to SQL, if necessary |
||
| 745 | 488 | if ( ! $addMetaColumns && ! $class->containsForeignIdentifier) { |
|
| 746 | 181 | continue; |
|
| 747 | } |
||
| 748 | |||
| 749 | // Add foreign key columns of class and also parent classes |
||
| 750 | 357 | foreach ($class->associationMappings as $assoc) { |
|
| 751 | 315 | if ( ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE)) { |
|
| 752 | 264 | continue; |
|
| 753 | 282 | } else if ( !$addMetaColumns && !isset($assoc['id'])) { |
|
| 754 | continue; |
||
| 755 | } |
||
| 756 | |||
| 757 | 282 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
|
| 758 | 282 | $isIdentifier = (isset($assoc['id']) && $assoc['id'] === true); |
|
| 759 | 282 | $owningClass = (isset($assoc['inherited'])) ? $this->em->getClassMetadata($assoc['inherited']) : $class; |
|
| 760 | 282 | $sqlTableAlias = $this->getSQLTableAlias($owningClass->getTableName(), $dqlAlias); |
|
| 761 | |||
| 762 | 282 | View Code Duplication | foreach ($assoc['joinColumns'] as $joinColumn) { |
| 763 | 282 | $columnName = $joinColumn['name']; |
|
| 764 | 282 | $columnAlias = $this->getSQLColumnAlias($columnName); |
|
| 765 | 282 | $columnType = PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em); |
|
| 766 | |||
| 767 | 282 | $quotedColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); |
|
| 768 | 282 | $sqlSelectExpressions[] = $sqlTableAlias . '.' . $quotedColumnName . ' AS ' . $columnAlias; |
|
| 769 | |||
| 770 | 282 | $this->rsm->addMetaResult($dqlAlias, $columnAlias, $columnName, $isIdentifier, $columnType); |
|
| 771 | } |
||
| 772 | } |
||
| 773 | |||
| 774 | // Add foreign key columns to SQL, if necessary |
||
| 775 | 357 | if ( ! $addMetaColumns) { |
|
| 776 | 8 | continue; |
|
| 777 | } |
||
| 778 | |||
| 779 | // Add foreign key columns of subclasses |
||
| 780 | 352 | foreach ($class->subClasses as $subClassName) { |
|
| 781 | 32 | $subClass = $this->em->getClassMetadata($subClassName); |
|
| 782 | 32 | $sqlTableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias); |
|
| 783 | |||
| 784 | 32 | foreach ($subClass->associationMappings as $assoc) { |
|
| 785 | // Skip if association is inherited |
||
| 786 | 25 | if (isset($assoc['inherited'])) continue; |
|
| 787 | |||
| 788 | 14 | if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) { |
|
| 789 | 12 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
|
| 790 | |||
| 791 | 12 | View Code Duplication | foreach ($assoc['joinColumns'] as $joinColumn) { |
| 792 | 12 | $columnName = $joinColumn['name']; |
|
| 793 | 12 | $columnAlias = $this->getSQLColumnAlias($columnName); |
|
| 794 | 12 | $columnType = PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em); |
|
| 795 | |||
| 796 | 12 | $quotedColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $subClass, $this->platform); |
|
| 797 | 12 | $sqlSelectExpressions[] = $sqlTableAlias . '.' . $quotedColumnName . ' AS ' . $columnAlias; |
|
| 798 | |||
| 799 | 352 | $this->rsm->addMetaResult($dqlAlias, $columnAlias, $columnName, $subClass->isIdentifier($columnName), $columnType); |
|
| 800 | } |
||
| 801 | } |
||
| 802 | } |
||
| 803 | } |
||
| 804 | } |
||
| 805 | |||
| 806 | 625 | $sql .= implode(', ', $sqlSelectExpressions); |
|
| 807 | |||
| 808 | 625 | return $sql; |
|
| 809 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * {@inheritdoc} |
||
| 813 | */ |
||
| 814 | 627 | View Code Duplication | public function walkFromClause($fromClause) |
| 815 | { |
||
| 816 | 627 | $identificationVarDecls = $fromClause->identificationVariableDeclarations; |
|
| 817 | 627 | $sqlParts = []; |
|
| 818 | |||
| 819 | 627 | foreach ($identificationVarDecls as $identificationVariableDecl) { |
|
| 820 | 627 | $sqlParts[] = $this->walkIdentificationVariableDeclaration($identificationVariableDecl); |
|
| 821 | } |
||
| 822 | |||
| 823 | 625 | 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 | 628 | 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 | 8 | 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 | 628 | public function walkRangeVariableDeclaration($rangeVariableDeclaration) |
|
| 898 | |||
| 899 | /** |
||
| 900 | * Walks down a JoinAssociationDeclaration AST node, thereby generating the appropriate SQL. |
||
| 901 | * |
||
| 902 | * @param AST\JoinAssociationDeclaration $joinAssociationDeclaration |
||
| 903 | * @param int $joinType |
||
| 904 | * @param AST\ConditionalExpression $condExpr |
||
| 905 | * |
||
| 906 | * @return string |
||
| 907 | * |
||
| 908 | * @throws QueryException |
||
| 909 | */ |
||
| 910 | 227 | public function walkJoinAssociationDeclaration($joinAssociationDeclaration, $joinType = AST\Join::JOIN_TYPE_INNER, $condExpr = null) |
|
| 911 | { |
||
| 912 | 227 | $sql = ''; |
|
| 913 | |||
| 914 | 227 | $associationPathExpression = $joinAssociationDeclaration->joinAssociationPathExpression; |
|
| 915 | 227 | $joinedDqlAlias = $joinAssociationDeclaration->aliasIdentificationVariable; |
|
| 916 | 227 | $indexBy = $joinAssociationDeclaration->indexBy; |
|
| 917 | |||
| 918 | 227 | $relation = $this->queryComponents[$joinedDqlAlias]['relation']; |
|
| 919 | 227 | $targetClass = $this->em->getClassMetadata($relation['targetEntity']); |
|
| 920 | 227 | $sourceClass = $this->em->getClassMetadata($relation['sourceEntity']); |
|
| 921 | 227 | $targetTableName = $this->quoteStrategy->getTableName($targetClass, $this->platform); |
|
| 922 | |||
| 923 | 227 | $targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName(), $joinedDqlAlias); |
|
| 924 | 227 | $sourceTableAlias = $this->getSQLTableAlias($sourceClass->getTableName(), $associationPathExpression->identificationVariable); |
|
| 925 | |||
| 926 | // Ensure we got the owning side, since it has all mapping info |
||
| 927 | 227 | $assoc = ( ! $relation['isOwningSide']) ? $targetClass->associationMappings[$relation['mappedBy']] : $relation; |
|
| 928 | |||
| 929 | 227 | if ($this->query->getHint(Query::HINT_INTERNAL_ITERATION) == true && (!$this->query->getHint(self::HINT_DISTINCT) || isset($this->selectedClasses[$joinedDqlAlias]))) { |
|
| 930 | 3 | if ($relation['type'] == ClassMetadata::ONE_TO_MANY || $relation['type'] == ClassMetadata::MANY_TO_MANY) { |
|
| 931 | 2 | throw QueryException::iterateWithFetchJoinNotAllowed($assoc); |
|
| 932 | } |
||
| 933 | } |
||
| 934 | |||
| 935 | 225 | $targetTableJoin = null; |
|
| 936 | |||
| 937 | // This condition is not checking ClassMetadata::MANY_TO_ONE, because by definition it cannot |
||
| 938 | // be the owning side and previously we ensured that $assoc is always the owning side of the associations. |
||
| 939 | // The owning side is necessary at this point because only it contains the JoinColumn information. |
||
| 940 | switch (true) { |
||
| 941 | 225 | case ($assoc['type'] & ClassMetadata::TO_ONE): |
|
| 942 | 178 | $conditions = []; |
|
| 943 | |||
| 944 | 178 | foreach ($assoc['joinColumns'] as $joinColumn) { |
|
| 945 | 178 | $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform); |
|
| 946 | 178 | $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform); |
|
| 947 | |||
| 948 | 178 | if ($relation['isOwningSide']) { |
|
| 949 | 103 | $conditions[] = $sourceTableAlias . '.' . $quotedSourceColumn . ' = ' . $targetTableAlias . '.' . $quotedTargetColumn; |
|
| 950 | |||
| 951 | 103 | continue; |
|
| 952 | } |
||
| 953 | |||
| 954 | 108 | $conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $targetTableAlias . '.' . $quotedSourceColumn; |
|
| 955 | } |
||
| 956 | |||
| 957 | // Apply remaining inheritance restrictions |
||
| 958 | 178 | $discrSql = $this->_generateDiscriminatorColumnConditionSQL([$joinedDqlAlias]); |
|
| 959 | |||
| 960 | 178 | if ($discrSql) { |
|
| 961 | 3 | $conditions[] = $discrSql; |
|
| 962 | } |
||
| 963 | |||
| 964 | // Apply the filters |
||
| 965 | 178 | $filterExpr = $this->generateFilterConditionSQL($targetClass, $targetTableAlias); |
|
| 966 | |||
| 967 | 178 | if ($filterExpr) { |
|
| 968 | 1 | $conditions[] = $filterExpr; |
|
| 969 | } |
||
| 970 | |||
| 971 | $targetTableJoin = [ |
||
| 972 | 178 | 'table' => $targetTableName . ' ' . $targetTableAlias, |
|
| 973 | 178 | 'condition' => implode(' AND ', $conditions), |
|
| 974 | ]; |
||
| 975 | 178 | break; |
|
| 976 | |||
| 977 | 57 | case ($assoc['type'] == ClassMetadata::MANY_TO_MANY): |
|
| 978 | // Join relation table |
||
| 979 | 57 | $joinTable = $assoc['joinTable']; |
|
| 980 | 57 | $joinTableAlias = $this->getSQLTableAlias($joinTable['name'], $joinedDqlAlias); |
|
| 981 | 57 | $joinTableName = $this->quoteStrategy->getJoinTableName($assoc, $sourceClass, $this->platform); |
|
| 982 | |||
| 983 | 57 | $conditions = []; |
|
| 984 | 57 | $relationColumns = ($relation['isOwningSide']) |
|
| 985 | 48 | ? $assoc['joinTable']['joinColumns'] |
|
| 986 | 57 | : $assoc['joinTable']['inverseJoinColumns']; |
|
| 987 | |||
| 988 | 57 | View Code Duplication | foreach ($relationColumns as $joinColumn) { |
| 989 | 57 | $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform); |
|
| 990 | 57 | $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform); |
|
| 991 | |||
| 992 | 57 | $conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $quotedSourceColumn; |
|
| 993 | } |
||
| 994 | |||
| 995 | 57 | $sql .= $joinTableName . ' ' . $joinTableAlias . ' ON ' . implode(' AND ', $conditions); |
|
| 996 | |||
| 997 | // Join target table |
||
| 998 | 57 | $sql .= ($joinType == AST\Join::JOIN_TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE_LEFTOUTER) ? ' LEFT JOIN ' : ' INNER JOIN '; |
|
| 999 | |||
| 1000 | 57 | $conditions = []; |
|
| 1001 | 57 | $relationColumns = ($relation['isOwningSide']) |
|
| 1002 | 48 | ? $assoc['joinTable']['inverseJoinColumns'] |
|
| 1003 | 57 | : $assoc['joinTable']['joinColumns']; |
|
| 1004 | |||
| 1005 | 57 | View Code Duplication | foreach ($relationColumns as $joinColumn) { |
| 1006 | 57 | $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform); |
|
| 1007 | 57 | $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform); |
|
| 1008 | |||
| 1009 | 57 | $conditions[] = $targetTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $quotedSourceColumn; |
|
| 1010 | } |
||
| 1011 | |||
| 1012 | // Apply remaining inheritance restrictions |
||
| 1013 | 57 | $discrSql = $this->_generateDiscriminatorColumnConditionSQL([$joinedDqlAlias]); |
|
| 1014 | |||
| 1015 | 57 | if ($discrSql) { |
|
| 1016 | 1 | $conditions[] = $discrSql; |
|
| 1017 | } |
||
| 1018 | |||
| 1019 | // Apply the filters |
||
| 1020 | 57 | $filterExpr = $this->generateFilterConditionSQL($targetClass, $targetTableAlias); |
|
| 1021 | |||
| 1022 | 57 | if ($filterExpr) { |
|
| 1023 | 1 | $conditions[] = $filterExpr; |
|
| 1024 | } |
||
| 1025 | |||
| 1026 | $targetTableJoin = [ |
||
| 1027 | 57 | 'table' => $targetTableName . ' ' . $targetTableAlias, |
|
| 1028 | 57 | 'condition' => implode(' AND ', $conditions), |
|
| 1029 | ]; |
||
| 1030 | 57 | break; |
|
| 1031 | |||
| 1032 | default: |
||
| 1033 | throw new \BadMethodCallException('Type of association must be one of *_TO_ONE or MANY_TO_MANY'); |
||
| 1034 | } |
||
| 1035 | |||
| 1036 | // Handle WITH clause |
||
| 1037 | 225 | $withCondition = (null === $condExpr) ? '' : ('(' . $this->walkConditionalExpression($condExpr) . ')'); |
|
| 1038 | |||
| 1039 | 225 | if ($targetClass->isInheritanceTypeJoined()) { |
|
| 1040 | 9 | $ctiJoins = $this->_generateClassTableInheritanceJoins($targetClass, $joinedDqlAlias); |
|
| 1041 | // If we have WITH condition, we need to build nested joins for target class table and cti joins |
||
| 1042 | 9 | if ($withCondition) { |
|
| 1043 | 1 | $sql .= '(' . $targetTableJoin['table'] . $ctiJoins . ') ON ' . $targetTableJoin['condition']; |
|
| 1044 | } else { |
||
| 1045 | 8 | $sql .= $targetTableJoin['table'] . ' ON ' . $targetTableJoin['condition'] . $ctiJoins; |
|
| 1046 | } |
||
| 1047 | } else { |
||
| 1048 | 216 | $sql .= $targetTableJoin['table'] . ' ON ' . $targetTableJoin['condition']; |
|
| 1049 | } |
||
| 1050 | |||
| 1051 | 225 | if ($withCondition) { |
|
| 1052 | 5 | $sql .= ' AND ' . $withCondition; |
|
| 1053 | } |
||
| 1054 | |||
| 1055 | // Apply the indexes |
||
| 1056 | 225 | if ($indexBy) { |
|
| 1057 | // For Many-To-One or One-To-One associations this obviously makes no sense, but is ignored silently. |
||
| 1058 | 5 | $this->walkIndexBy($indexBy); |
|
| 1059 | 220 | } else if (isset($relation['indexBy'])) { |
|
| 1060 | 3 | $this->rsm->addIndexBy($joinedDqlAlias, $relation['indexBy']); |
|
| 1061 | } |
||
| 1062 | |||
| 1063 | 225 | return $sql; |
|
| 1064 | } |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * {@inheritdoc} |
||
| 1068 | */ |
||
| 1069 | 118 | public function walkFunction($function) |
|
| 1073 | |||
| 1074 | /** |
||
| 1075 | * {@inheritdoc} |
||
| 1076 | */ |
||
| 1077 | 153 | public function walkOrderByClause($orderByClause) |
|
| 1087 | |||
| 1088 | /** |
||
| 1089 | * {@inheritdoc} |
||
| 1090 | */ |
||
| 1091 | 171 | public function walkOrderByItem($orderByItem) |
|
| 1107 | |||
| 1108 | /** |
||
| 1109 | * {@inheritdoc} |
||
| 1110 | */ |
||
| 1111 | 14 | public function walkHavingClause($havingClause) |
|
| 1115 | |||
| 1116 | /** |
||
| 1117 | * {@inheritdoc} |
||
| 1118 | */ |
||
| 1119 | 244 | public function walkJoin($join) |
|
| 1120 | { |
||
| 1121 | 244 | $joinType = $join->joinType; |
|
| 1122 | 244 | $joinDeclaration = $join->joinAssociationDeclaration; |
|
| 1123 | |||
| 1124 | 244 | $sql = ($joinType == AST\Join::JOIN_TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE_LEFTOUTER) |
|
| 1125 | 56 | ? ' LEFT JOIN ' |
|
| 1126 | 244 | : ' INNER JOIN '; |
|
| 1127 | |||
| 1128 | switch (true) { |
||
| 1129 | 244 | case ($joinDeclaration instanceof \Doctrine\ORM\Query\AST\RangeVariableDeclaration): |
|
| 1130 | 17 | $class = $this->em->getClassMetadata($joinDeclaration->abstractSchemaName); |
|
| 1131 | 17 | $dqlAlias = $joinDeclaration->aliasIdentificationVariable; |
|
| 1132 | 17 | $tableAlias = $this->getSQLTableAlias($class->table['name'], $dqlAlias); |
|
| 1133 | 17 | $conditions = []; |
|
| 1134 | |||
| 1135 | 17 | if ($join->conditionalExpression) { |
|
| 1136 | 15 | $conditions[] = '(' . $this->walkConditionalExpression($join->conditionalExpression) . ')'; |
|
| 1137 | } |
||
| 1138 | |||
| 1139 | 17 | $condExprConjunction = ($class->isInheritanceTypeJoined() && $joinType != AST\Join::JOIN_TYPE_LEFT && $joinType != AST\Join::JOIN_TYPE_LEFTOUTER) |
|
| 1140 | 3 | ? ' AND ' |
|
| 1141 | 17 | : ' ON '; |
|
| 1142 | |||
| 1143 | 17 | $sql .= $this->walkRangeVariableDeclaration($joinDeclaration); |
|
| 1144 | |||
| 1145 | // Apply remaining inheritance restrictions |
||
| 1146 | 17 | $discrSql = $this->_generateDiscriminatorColumnConditionSQL([$dqlAlias]); |
|
| 1147 | |||
| 1148 | 17 | if ($discrSql) { |
|
| 1149 | 3 | $conditions[] = $discrSql; |
|
| 1150 | } |
||
| 1151 | |||
| 1152 | // Apply the filters |
||
| 1153 | 17 | $filterExpr = $this->generateFilterConditionSQL($class, $tableAlias); |
|
| 1154 | |||
| 1155 | 17 | if ($filterExpr) { |
|
| 1156 | $conditions[] = $filterExpr; |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | 17 | if ($conditions) { |
|
| 1160 | 15 | $sql .= $condExprConjunction . implode(' AND ', $conditions); |
|
| 1161 | } |
||
| 1162 | |||
| 1163 | 17 | break; |
|
| 1164 | |||
| 1165 | 227 | case ($joinDeclaration instanceof \Doctrine\ORM\Query\AST\JoinAssociationDeclaration): |
|
| 1166 | 227 | $sql .= $this->walkJoinAssociationDeclaration($joinDeclaration, $joinType, $join->conditionalExpression); |
|
| 1167 | 225 | break; |
|
| 1168 | } |
||
| 1169 | |||
| 1170 | 242 | return $sql; |
|
| 1171 | } |
||
| 1172 | |||
| 1173 | /** |
||
| 1174 | * Walks down a CoalesceExpression AST node and generates the corresponding SQL. |
||
| 1175 | * |
||
| 1176 | * @param AST\CoalesceExpression $coalesceExpression |
||
| 1177 | * |
||
| 1178 | * @return string The SQL. |
||
| 1179 | */ |
||
| 1180 | 2 | public function walkCoalesceExpression($coalesceExpression) |
|
| 1194 | |||
| 1195 | /** |
||
| 1196 | * Walks down a NullIfExpression AST node and generates the corresponding SQL. |
||
| 1197 | * |
||
| 1198 | * @param AST\NullIfExpression $nullIfExpression |
||
| 1199 | * |
||
| 1200 | * @return string The SQL. |
||
| 1201 | */ |
||
| 1202 | 3 | public function walkNullIfExpression($nullIfExpression) |
|
| 1214 | |||
| 1215 | /** |
||
| 1216 | * Walks down a GeneralCaseExpression AST node and generates the corresponding SQL. |
||
| 1217 | * |
||
| 1218 | * @param AST\GeneralCaseExpression $generalCaseExpression |
||
| 1219 | * |
||
| 1220 | * @return string The SQL. |
||
| 1221 | */ |
||
| 1222 | 9 | View Code Duplication | public function walkGeneralCaseExpression(AST\GeneralCaseExpression $generalCaseExpression) |
| 1223 | { |
||
| 1224 | 9 | $sql = 'CASE'; |
|
| 1225 | |||
| 1226 | 9 | foreach ($generalCaseExpression->whenClauses as $whenClause) { |
|
| 1227 | 9 | $sql .= ' WHEN ' . $this->walkConditionalExpression($whenClause->caseConditionExpression); |
|
| 1228 | 9 | $sql .= ' THEN ' . $this->walkSimpleArithmeticExpression($whenClause->thenScalarExpression); |
|
| 1229 | } |
||
| 1230 | |||
| 1231 | 9 | $sql .= ' ELSE ' . $this->walkSimpleArithmeticExpression($generalCaseExpression->elseScalarExpression) . ' END'; |
|
| 1232 | |||
| 1233 | 9 | return $sql; |
|
| 1234 | } |
||
| 1235 | |||
| 1236 | /** |
||
| 1237 | * Walks down a SimpleCaseExpression AST node and generates the corresponding SQL. |
||
| 1238 | * |
||
| 1239 | * @param AST\SimpleCaseExpression $simpleCaseExpression |
||
| 1240 | * |
||
| 1241 | * @return string The SQL. |
||
| 1242 | */ |
||
| 1243 | 5 | View Code Duplication | public function walkSimpleCaseExpression($simpleCaseExpression) |
| 1244 | { |
||
| 1245 | 5 | $sql = 'CASE ' . $this->walkStateFieldPathExpression($simpleCaseExpression->caseOperand); |
|
| 1246 | |||
| 1247 | 5 | foreach ($simpleCaseExpression->simpleWhenClauses as $simpleWhenClause) { |
|
| 1248 | 5 | $sql .= ' WHEN ' . $this->walkSimpleArithmeticExpression($simpleWhenClause->caseScalarExpression); |
|
| 1249 | 5 | $sql .= ' THEN ' . $this->walkSimpleArithmeticExpression($simpleWhenClause->thenScalarExpression); |
|
| 1250 | } |
||
| 1251 | |||
| 1252 | 5 | $sql .= ' ELSE ' . $this->walkSimpleArithmeticExpression($simpleCaseExpression->elseScalarExpression) . ' END'; |
|
| 1253 | |||
| 1254 | 5 | return $sql; |
|
| 1255 | } |
||
| 1256 | |||
| 1257 | /** |
||
| 1258 | * {@inheritdoc} |
||
| 1259 | */ |
||
| 1260 | 625 | public function walkSelectExpression($selectExpression) |
|
| 1261 | { |
||
| 1262 | 625 | $sql = ''; |
|
| 1263 | 625 | $expr = $selectExpression->expression; |
|
| 1264 | 625 | $hidden = $selectExpression->hiddenAliasResultVariable; |
|
| 1265 | |||
| 1266 | switch (true) { |
||
| 1267 | 625 | case ($expr instanceof AST\PathExpression): |
|
| 1268 | 101 | if ($expr->type !== AST\PathExpression::TYPE_STATE_FIELD) { |
|
| 1269 | throw QueryException::invalidPathExpression($expr); |
||
| 1270 | } |
||
| 1271 | |||
| 1272 | 101 | $fieldName = $expr->field; |
|
| 1273 | 101 | $dqlAlias = $expr->identificationVariable; |
|
| 1274 | 101 | $qComp = $this->queryComponents[$dqlAlias]; |
|
| 1275 | 101 | $class = $qComp['metadata']; |
|
| 1276 | |||
| 1277 | 101 | $resultAlias = $selectExpression->fieldIdentificationVariable ?: $fieldName; |
|
| 1278 | 101 | $tableName = ($class->isInheritanceTypeJoined()) |
|
| 1279 | 11 | ? $this->em->getUnitOfWork()->getEntityPersister($class->name)->getOwningTable($fieldName) |
|
| 1280 | 101 | : $class->getTableName(); |
|
| 1281 | |||
| 1282 | 101 | $sqlTableAlias = $this->getSQLTableAlias($tableName, $dqlAlias); |
|
| 1283 | 101 | $fieldMapping = $class->fieldMappings[$fieldName]; |
|
| 1284 | 101 | $columnName = $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform); |
|
| 1285 | 101 | $columnAlias = $this->getSQLColumnAlias($fieldMapping['columnName']); |
|
| 1286 | 101 | $col = $sqlTableAlias . '.' . $columnName; |
|
| 1287 | |||
| 1288 | 101 | if (isset($fieldMapping['requireSQLConversion'])) { |
|
| 1289 | 2 | $type = Type::getType($fieldMapping['type']); |
|
| 1290 | 2 | $col = $type->convertToPHPValueSQL($col, $this->conn->getDatabasePlatform()); |
|
| 1291 | } |
||
| 1292 | |||
| 1293 | 101 | $sql .= $col . ' AS ' . $columnAlias; |
|
| 1294 | |||
| 1295 | 101 | $this->scalarResultAliasMap[$resultAlias] = $columnAlias; |
|
| 1296 | |||
| 1297 | 101 | if ( ! $hidden) { |
|
| 1298 | 101 | $this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldMapping['type']); |
|
| 1299 | 101 | $this->scalarFields[$dqlAlias][$fieldName] = $columnAlias; |
|
| 1300 | } |
||
| 1301 | |||
| 1302 | 101 | break; |
|
| 1303 | |||
| 1304 | 575 | case ($expr instanceof AST\AggregateExpression): |
|
| 1305 | 566 | case ($expr instanceof AST\Functions\FunctionNode): |
|
| 1306 | 526 | case ($expr instanceof AST\SimpleArithmeticExpression): |
|
| 1307 | 526 | case ($expr instanceof AST\ArithmeticTerm): |
|
| 1308 | 524 | case ($expr instanceof AST\ArithmeticFactor): |
|
| 1309 | 523 | case ($expr instanceof AST\ParenthesisExpression): |
|
| 1310 | 522 | case ($expr instanceof AST\Literal): |
|
| 1311 | 521 | case ($expr instanceof AST\NullIfExpression): |
|
| 1312 | 520 | case ($expr instanceof AST\CoalesceExpression): |
|
| 1313 | 519 | case ($expr instanceof AST\GeneralCaseExpression): |
|
| 1314 | 515 | View Code Duplication | case ($expr instanceof AST\SimpleCaseExpression): |
| 1315 | 107 | $columnAlias = $this->getSQLColumnAlias('sclr'); |
|
| 1316 | 107 | $resultAlias = $selectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++; |
|
| 1317 | |||
| 1318 | 107 | $sql .= $expr->dispatch($this) . ' AS ' . $columnAlias; |
|
| 1319 | |||
| 1320 | 107 | $this->scalarResultAliasMap[$resultAlias] = $columnAlias; |
|
| 1321 | |||
| 1322 | 107 | if ( ! $hidden) { |
|
| 1323 | // We cannot resolve field type here; assume 'string'. |
||
| 1324 | 107 | $this->rsm->addScalarResult($columnAlias, $resultAlias, 'string'); |
|
| 1325 | } |
||
| 1326 | 107 | break; |
|
| 1327 | |||
| 1328 | 514 | View Code Duplication | case ($expr instanceof AST\Subselect): |
| 1329 | 15 | $columnAlias = $this->getSQLColumnAlias('sclr'); |
|
| 1330 | 15 | $resultAlias = $selectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++; |
|
| 1331 | |||
| 1332 | 15 | $sql .= '(' . $this->walkSubselect($expr) . ') AS ' . $columnAlias; |
|
| 1333 | |||
| 1334 | 15 | $this->scalarResultAliasMap[$resultAlias] = $columnAlias; |
|
| 1335 | |||
| 1336 | 15 | if ( ! $hidden) { |
|
| 1337 | // We cannot resolve field type here; assume 'string'. |
||
| 1338 | 13 | $this->rsm->addScalarResult($columnAlias, $resultAlias, 'string'); |
|
| 1339 | } |
||
| 1340 | 15 | break; |
|
| 1341 | |||
| 1342 | 510 | case ($expr instanceof AST\NewObjectExpression): |
|
| 1343 | 22 | $sql .= $this->walkNewObject($expr,$selectExpression->fieldIdentificationVariable); |
|
| 1344 | 22 | break; |
|
| 1345 | |||
| 1346 | default: |
||
| 1347 | // IdentificationVariable or PartialObjectExpression |
||
| 1348 | 488 | if ($expr instanceof AST\PartialObjectExpression) { |
|
| 1349 | 16 | $dqlAlias = $expr->identificationVariable; |
|
| 1350 | 16 | $partialFieldSet = $expr->partialFieldSet; |
|
| 1351 | } else { |
||
| 1352 | 483 | $dqlAlias = $expr; |
|
| 1353 | 483 | $partialFieldSet = []; |
|
| 1354 | } |
||
| 1355 | |||
| 1356 | 488 | $queryComp = $this->queryComponents[$dqlAlias]; |
|
| 1357 | 488 | $class = $queryComp['metadata']; |
|
| 1358 | 488 | $resultAlias = $selectExpression->fieldIdentificationVariable ?: null; |
|
| 1359 | |||
| 1360 | 488 | if ( ! isset($this->selectedClasses[$dqlAlias])) { |
|
| 1361 | 488 | $this->selectedClasses[$dqlAlias] = [ |
|
| 1362 | 488 | 'class' => $class, |
|
| 1363 | 488 | 'dqlAlias' => $dqlAlias, |
|
| 1364 | 488 | 'resultAlias' => $resultAlias |
|
| 1365 | ]; |
||
| 1366 | } |
||
| 1367 | |||
| 1368 | 488 | $sqlParts = []; |
|
| 1369 | |||
| 1370 | // Select all fields from the queried class |
||
| 1371 | 488 | View Code Duplication | foreach ($class->fieldMappings as $fieldName => $mapping) { |
| 1372 | 487 | if ($partialFieldSet && ! in_array($fieldName, $partialFieldSet)) { |
|
| 1373 | 14 | continue; |
|
| 1374 | } |
||
| 1375 | |||
| 1376 | 486 | $tableName = (isset($mapping['inherited'])) |
|
| 1377 | 52 | ? $this->em->getClassMetadata($mapping['inherited'])->getTableName() |
|
| 1378 | 486 | : $class->getTableName(); |
|
| 1379 | |||
| 1380 | 486 | $sqlTableAlias = $this->getSQLTableAlias($tableName, $dqlAlias); |
|
| 1381 | 486 | $columnAlias = $this->getSQLColumnAlias($mapping['columnName']); |
|
| 1382 | 486 | $quotedColumnName = $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform); |
|
| 1383 | |||
| 1384 | 486 | $col = $sqlTableAlias . '.' . $quotedColumnName; |
|
| 1385 | |||
| 1386 | 486 | if (isset($mapping['requireSQLConversion'])) { |
|
| 1387 | 5 | $type = Type::getType($mapping['type']); |
|
| 1388 | 5 | $col = $type->convertToPHPValueSQL($col, $this->platform); |
|
| 1389 | } |
||
| 1390 | |||
| 1391 | 486 | $sqlParts[] = $col . ' AS '. $columnAlias; |
|
| 1392 | |||
| 1393 | 486 | $this->scalarResultAliasMap[$resultAlias][] = $columnAlias; |
|
| 1394 | |||
| 1395 | 486 | $this->rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $class->name); |
|
| 1396 | } |
||
| 1397 | |||
| 1398 | // Add any additional fields of subclasses (excluding inherited fields) |
||
| 1399 | // 1) on Single Table Inheritance: always, since its marginal overhead |
||
| 1400 | // 2) on Class Table Inheritance only if partial objects are disallowed, |
||
| 1401 | // since it requires outer joining subtables. |
||
| 1402 | 488 | if ($class->isInheritanceTypeSingleTable() || ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) { |
|
| 1403 | 395 | View Code Duplication | foreach ($class->subClasses as $subClassName) { |
| 1404 | 43 | $subClass = $this->em->getClassMetadata($subClassName); |
|
| 1405 | 43 | $sqlTableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias); |
|
| 1406 | |||
| 1407 | 43 | foreach ($subClass->fieldMappings as $fieldName => $mapping) { |
|
| 1408 | 43 | if (isset($mapping['inherited']) || ($partialFieldSet && !in_array($fieldName, $partialFieldSet))) { |
|
| 1409 | 43 | continue; |
|
| 1410 | } |
||
| 1411 | |||
| 1412 | 35 | $columnAlias = $this->getSQLColumnAlias($mapping['columnName']); |
|
| 1413 | 35 | $quotedColumnName = $this->quoteStrategy->getColumnName($fieldName, $subClass, $this->platform); |
|
| 1414 | |||
| 1415 | 35 | $col = $sqlTableAlias . '.' . $quotedColumnName; |
|
| 1416 | |||
| 1417 | 35 | if (isset($mapping['requireSQLConversion'])) { |
|
| 1418 | $type = Type::getType($mapping['type']); |
||
| 1419 | $col = $type->convertToPHPValueSQL($col, $this->platform); |
||
| 1420 | } |
||
| 1421 | |||
| 1422 | 35 | $sqlParts[] = $col . ' AS ' . $columnAlias; |
|
| 1423 | |||
| 1424 | 35 | $this->scalarResultAliasMap[$resultAlias][] = $columnAlias; |
|
| 1425 | |||
| 1426 | 43 | $this->rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $subClassName); |
|
| 1427 | } |
||
| 1428 | } |
||
| 1429 | } |
||
| 1430 | |||
| 1431 | 488 | $sql .= implode(', ', $sqlParts); |
|
| 1432 | } |
||
| 1433 | |||
| 1434 | 625 | return $sql; |
|
| 1435 | } |
||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * {@inheritdoc} |
||
| 1439 | */ |
||
| 1440 | public function walkQuantifiedExpression($qExpr) |
||
| 1444 | |||
| 1445 | /** |
||
| 1446 | * {@inheritdoc} |
||
| 1447 | */ |
||
| 1448 | 33 | public function walkSubselect($subselect) |
|
| 1469 | |||
| 1470 | /** |
||
| 1471 | * {@inheritdoc} |
||
| 1472 | */ |
||
| 1473 | 33 | View Code Duplication | public function walkSubselectFromClause($subselectFromClause) |
| 1474 | { |
||
| 1475 | 33 | $identificationVarDecls = $subselectFromClause->identificationVariableDeclarations; |
|
| 1476 | 33 | $sqlParts = []; |
|
| 1477 | |||
| 1478 | 33 | foreach ($identificationVarDecls as $subselectIdVarDecl) { |
|
| 1479 | 33 | $sqlParts[] = $this->walkIdentificationVariableDeclaration($subselectIdVarDecl); |
|
| 1480 | } |
||
| 1481 | |||
| 1482 | 33 | return ' FROM ' . implode(', ', $sqlParts); |
|
| 1483 | } |
||
| 1484 | |||
| 1485 | /** |
||
| 1486 | * {@inheritdoc} |
||
| 1487 | */ |
||
| 1488 | 33 | public function walkSimpleSelectClause($simpleSelectClause) |
|
| 1493 | |||
| 1494 | /** |
||
| 1495 | * @param \Doctrine\ORM\Query\AST\ParenthesisExpression $parenthesisExpression |
||
| 1496 | * |
||
| 1497 | * @return string. |
||
| 1498 | */ |
||
| 1499 | 22 | public function walkParenthesisExpression(AST\ParenthesisExpression $parenthesisExpression) |
|
| 1503 | |||
| 1504 | /** |
||
| 1505 | * @param AST\NewObjectExpression $newObjectExpression |
||
| 1506 | * @param null|string $newObjectResultAlias |
||
| 1507 | * @return string The SQL. |
||
| 1508 | */ |
||
| 1509 | 22 | public function walkNewObject($newObjectExpression, $newObjectResultAlias=null) |
|
| 1510 | { |
||
| 1511 | 22 | $sqlSelectExpressions = []; |
|
| 1512 | 22 | $objIndex = $newObjectResultAlias?:$this->newObjectCounter++; |
|
| 1513 | |||
| 1514 | 22 | foreach ($newObjectExpression->args as $argIndex => $e) { |
|
| 1568 | |||
| 1569 | /** |
||
| 1570 | * {@inheritdoc} |
||
| 1571 | */ |
||
| 1572 | 33 | public function walkSimpleSelectExpression($simpleSelectExpression) |
|
| 1573 | { |
||
| 1574 | 33 | $expr = $simpleSelectExpression->expression; |
|
| 1575 | 33 | $sql = ' '; |
|
| 1576 | |||
| 1577 | switch (true) { |
||
| 1578 | 33 | case ($expr instanceof AST\PathExpression): |
|
| 1579 | 9 | $sql .= $this->walkPathExpression($expr); |
|
| 1580 | 9 | break; |
|
| 1581 | |||
| 1582 | 24 | View Code Duplication | case ($expr instanceof AST\Subselect): |
| 1583 | $alias = $simpleSelectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++; |
||
| 1584 | |||
| 1585 | $columnAlias = 'sclr' . $this->aliasCounter++; |
||
| 1586 | $this->scalarResultAliasMap[$alias] = $columnAlias; |
||
| 1587 | |||
| 1588 | $sql .= '(' . $this->walkSubselect($expr) . ') AS ' . $columnAlias; |
||
| 1589 | break; |
||
| 1590 | |||
| 1591 | 24 | case ($expr instanceof AST\Functions\FunctionNode): |
|
| 1592 | 11 | case ($expr instanceof AST\SimpleArithmeticExpression): |
|
| 1593 | 10 | case ($expr instanceof AST\ArithmeticTerm): |
|
| 1594 | 9 | case ($expr instanceof AST\ArithmeticFactor): |
|
| 1595 | 9 | case ($expr instanceof AST\Literal): |
|
| 1596 | 7 | case ($expr instanceof AST\NullIfExpression): |
|
| 1597 | 7 | case ($expr instanceof AST\CoalesceExpression): |
|
| 1598 | 7 | case ($expr instanceof AST\GeneralCaseExpression): |
|
| 1599 | 5 | View Code Duplication | case ($expr instanceof AST\SimpleCaseExpression): |
| 1600 | 21 | $alias = $simpleSelectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++; |
|
| 1601 | |||
| 1602 | 21 | $columnAlias = $this->getSQLColumnAlias('sclr'); |
|
| 1603 | 21 | $this->scalarResultAliasMap[$alias] = $columnAlias; |
|
| 1604 | |||
| 1605 | 21 | $sql .= $expr->dispatch($this) . ' AS ' . $columnAlias; |
|
| 1606 | 21 | break; |
|
| 1607 | |||
| 1608 | 3 | case ($expr instanceof AST\ParenthesisExpression): |
|
| 1609 | 1 | $sql .= $this->walkParenthesisExpression($expr); |
|
| 1610 | 1 | break; |
|
| 1611 | |||
| 1612 | default: // IdentificationVariable |
||
| 1613 | 2 | $sql .= $this->walkEntityIdentificationVariable($expr); |
|
| 1614 | 2 | break; |
|
| 1615 | } |
||
| 1616 | |||
| 1617 | 33 | return $sql; |
|
| 1618 | } |
||
| 1619 | |||
| 1620 | /** |
||
| 1621 | * {@inheritdoc} |
||
| 1622 | */ |
||
| 1623 | 76 | public function walkAggregateExpression($aggExpression) |
|
| 1628 | |||
| 1629 | /** |
||
| 1630 | * {@inheritdoc} |
||
| 1631 | */ |
||
| 1632 | 23 | public function walkGroupByClause($groupByClause) |
|
| 1642 | |||
| 1643 | /** |
||
| 1644 | * {@inheritdoc} |
||
| 1645 | */ |
||
| 1646 | 23 | public function walkGroupByItem($groupByItem) |
|
| 1689 | |||
| 1690 | /** |
||
| 1691 | * {@inheritdoc} |
||
| 1692 | */ |
||
| 1693 | 36 | public function walkDeleteClause(AST\DeleteClause $deleteClause) |
|
| 1704 | |||
| 1705 | /** |
||
| 1706 | * {@inheritdoc} |
||
| 1707 | */ |
||
| 1708 | 25 | public function walkUpdateClause($updateClause) |
|
| 1721 | |||
| 1722 | /** |
||
| 1723 | * {@inheritdoc} |
||
| 1724 | */ |
||
| 1725 | 29 | public function walkUpdateItem($updateItem) |
|
| 1751 | |||
| 1752 | /** |
||
| 1753 | * {@inheritdoc} |
||
| 1754 | */ |
||
| 1755 | 682 | public function walkWhereClause($whereClause) |
|
| 1790 | |||
| 1791 | /** |
||
| 1792 | * {@inheritdoc} |
||
| 1793 | */ |
||
| 1794 | 365 | public function walkConditionalExpression($condExpr) |
|
| 1804 | |||
| 1805 | /** |
||
| 1806 | * {@inheritdoc} |
||
| 1807 | */ |
||
| 1808 | 365 | public function walkConditionalTerm($condTerm) |
|
| 1818 | |||
| 1819 | /** |
||
| 1820 | * {@inheritdoc} |
||
| 1821 | */ |
||
| 1822 | 365 | public function walkConditionalFactor($factor) |
|
| 1830 | |||
| 1831 | /** |
||
| 1832 | * {@inheritdoc} |
||
| 1833 | */ |
||
| 1834 | 365 | public function walkConditionalPrimary($primary) |
|
| 1846 | |||
| 1847 | /** |
||
| 1848 | * {@inheritdoc} |
||
| 1849 | */ |
||
| 1850 | 5 | public function walkExistsExpression($existsExpr) |
|
| 1858 | |||
| 1859 | /** |
||
| 1860 | * {@inheritdoc} |
||
| 1861 | */ |
||
| 1862 | 6 | public function walkCollectionMemberExpression($collMemberExpr) |
|
| 1863 | { |
||
| 1864 | 6 | $sql = $collMemberExpr->not ? 'NOT ' : ''; |
|
| 1865 | 6 | $sql .= 'EXISTS (SELECT 1 FROM '; |
|
| 1866 | |||
| 1867 | 6 | $entityExpr = $collMemberExpr->entityExpression; |
|
| 1868 | 6 | $collPathExpr = $collMemberExpr->collectionValuedPathExpression; |
|
| 1869 | |||
| 1870 | 6 | $fieldName = $collPathExpr->field; |
|
| 1871 | 6 | $dqlAlias = $collPathExpr->identificationVariable; |
|
| 1872 | |||
| 1873 | 6 | $class = $this->queryComponents[$dqlAlias]['metadata']; |
|
| 1874 | |||
| 1875 | switch (true) { |
||
| 1876 | // InputParameter |
||
| 1877 | 6 | case ($entityExpr instanceof AST\InputParameter): |
|
| 1878 | 4 | $dqlParamKey = $entityExpr->name; |
|
| 1879 | 4 | $entitySql = '?'; |
|
| 1880 | 4 | break; |
|
| 1881 | |||
| 1882 | // SingleValuedAssociationPathExpression | IdentificationVariable |
||
| 1883 | 2 | case ($entityExpr instanceof AST\PathExpression): |
|
| 1884 | 2 | $entitySql = $this->walkPathExpression($entityExpr); |
|
| 1885 | 2 | break; |
|
| 1886 | |||
| 1887 | default: |
||
| 1888 | throw new \BadMethodCallException("Not implemented"); |
||
| 1889 | } |
||
| 1890 | |||
| 1891 | 6 | $assoc = $class->associationMappings[$fieldName]; |
|
| 1892 | |||
| 1893 | 6 | if ($assoc['type'] == ClassMetadata::ONE_TO_MANY) { |
|
| 1894 | 1 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
|
| 1895 | 1 | $targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName()); |
|
| 1896 | 1 | $sourceTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias); |
|
| 1897 | |||
| 1898 | 1 | $sql .= $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' ' . $targetTableAlias . ' WHERE '; |
|
| 1899 | |||
| 1900 | 1 | $owningAssoc = $targetClass->associationMappings[$assoc['mappedBy']]; |
|
| 1901 | 1 | $sqlParts = []; |
|
| 1902 | |||
| 1903 | 1 | View Code Duplication | foreach ($owningAssoc['targetToSourceKeyColumns'] as $targetColumn => $sourceColumn) { |
| 1904 | 1 | $targetColumn = $this->quoteStrategy->getColumnName($class->fieldNames[$targetColumn], $class, $this->platform); |
|
| 1905 | |||
| 1906 | 1 | $sqlParts[] = $sourceTableAlias . '.' . $targetColumn . ' = ' . $targetTableAlias . '.' . $sourceColumn; |
|
| 1907 | } |
||
| 1908 | |||
| 1909 | 1 | View Code Duplication | foreach ($this->quoteStrategy->getIdentifierColumnNames($targetClass, $this->platform) as $targetColumnName) { |
| 1910 | 1 | if (isset($dqlParamKey)) { |
|
| 1911 | 1 | $this->parserResult->addParameterMapping($dqlParamKey, $this->sqlParamIndex++); |
|
| 1912 | } |
||
| 1913 | |||
| 1914 | 1 | $sqlParts[] = $targetTableAlias . '.' . $targetColumnName . ' = ' . $entitySql; |
|
| 1915 | } |
||
| 1916 | |||
| 1917 | 1 | $sql .= implode(' AND ', $sqlParts); |
|
| 1918 | } else { // many-to-many |
||
| 1919 | 5 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
|
| 1920 | |||
| 1921 | 5 | $owningAssoc = $assoc['isOwningSide'] ? $assoc : $targetClass->associationMappings[$assoc['mappedBy']]; |
|
| 1922 | 5 | $joinTable = $owningAssoc['joinTable']; |
|
| 1923 | |||
| 1924 | // SQL table aliases |
||
| 1925 | 5 | $joinTableAlias = $this->getSQLTableAlias($joinTable['name']); |
|
| 1926 | 5 | $targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName()); |
|
| 1927 | 5 | $sourceTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias); |
|
| 1928 | |||
| 1929 | // join to target table |
||
| 1930 | 5 | $sql .= $this->quoteStrategy->getJoinTableName($owningAssoc, $targetClass, $this->platform) . ' ' . $joinTableAlias |
|
| 1931 | 5 | . ' INNER JOIN ' . $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' ' . $targetTableAlias . ' ON '; |
|
| 1932 | |||
| 1933 | // join conditions |
||
| 1934 | 5 | $joinColumns = $assoc['isOwningSide'] ? $joinTable['inverseJoinColumns'] : $joinTable['joinColumns']; |
|
| 1935 | 5 | $joinSqlParts = []; |
|
| 1936 | |||
| 1937 | 5 | View Code Duplication | foreach ($joinColumns as $joinColumn) { |
| 1938 | 5 | $targetColumn = $this->quoteStrategy->getColumnName($targetClass->fieldNames[$joinColumn['referencedColumnName']], $targetClass, $this->platform); |
|
| 1939 | |||
| 1940 | 5 | $joinSqlParts[] = $joinTableAlias . '.' . $joinColumn['name'] . ' = ' . $targetTableAlias . '.' . $targetColumn; |
|
| 1941 | } |
||
| 1942 | |||
| 1943 | 5 | $sql .= implode(' AND ', $joinSqlParts); |
|
| 1944 | 5 | $sql .= ' WHERE '; |
|
| 1945 | |||
| 1946 | 5 | $joinColumns = $assoc['isOwningSide'] ? $joinTable['joinColumns'] : $joinTable['inverseJoinColumns']; |
|
| 1947 | 5 | $sqlParts = []; |
|
| 1948 | |||
| 1949 | 5 | View Code Duplication | foreach ($joinColumns as $joinColumn) { |
| 1950 | 5 | $targetColumn = $this->quoteStrategy->getColumnName($class->fieldNames[$joinColumn['referencedColumnName']], $class, $this->platform); |
|
| 1951 | |||
| 1952 | 5 | $sqlParts[] = $joinTableAlias . '.' . $joinColumn['name'] . ' = ' . $sourceTableAlias . '.' . $targetColumn; |
|
| 1953 | } |
||
| 1954 | |||
| 1955 | 5 | View Code Duplication | foreach ($this->quoteStrategy->getIdentifierColumnNames($targetClass, $this->platform) as $targetColumnName) { |
| 1956 | 5 | if (isset($dqlParamKey)) { |
|
| 1957 | 3 | $this->parserResult->addParameterMapping($dqlParamKey, $this->sqlParamIndex++); |
|
| 1958 | } |
||
| 1959 | |||
| 1960 | 5 | $sqlParts[] = $targetTableAlias . '.' . $targetColumnName . ' IN (' . $entitySql . ')'; |
|
| 1961 | } |
||
| 1962 | |||
| 1963 | 5 | $sql .= implode(' AND ', $sqlParts); |
|
| 1964 | } |
||
| 1965 | |||
| 1966 | 6 | return $sql . ')'; |
|
| 1967 | } |
||
| 1968 | |||
| 1969 | /** |
||
| 1970 | * {@inheritdoc} |
||
| 1971 | */ |
||
| 1972 | 3 | public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr) |
|
| 1979 | |||
| 1980 | /** |
||
| 1981 | * {@inheritdoc} |
||
| 1982 | */ |
||
| 1983 | 11 | public function walkNullComparisonExpression($nullCompExpr) |
|
| 1984 | { |
||
| 1985 | 11 | $expression = $nullCompExpr->expression; |
|
| 1986 | 11 | $comparison = ' IS' . ($nullCompExpr->not ? ' NOT' : '') . ' NULL'; |
|
| 1987 | |||
| 1988 | // Handle ResultVariable |
||
| 1989 | 11 | if (is_string($expression) && isset($this->queryComponents[$expression]['resultVariable'])) { |
|
| 1990 | 2 | return $this->walkResultVariable($expression) . $comparison; |
|
| 1991 | } |
||
| 1992 | |||
| 1993 | // Handle InputParameter mapping inclusion to ParserResult |
||
| 1994 | 9 | if ($expression instanceof AST\InputParameter) { |
|
| 1995 | return $this->walkInputParameter($expression) . $comparison; |
||
| 1996 | } |
||
| 1997 | |||
| 1998 | 9 | return $expression->dispatch($this) . $comparison; |
|
| 1999 | } |
||
| 2000 | |||
| 2001 | /** |
||
| 2002 | * {@inheritdoc} |
||
| 2003 | */ |
||
| 2004 | 85 | public function walkInExpression($inExpr) |
|
| 2016 | |||
| 2017 | /** |
||
| 2018 | * {@inheritdoc} |
||
| 2019 | * @throws \Doctrine\ORM\Query\QueryException |
||
| 2020 | */ |
||
| 2021 | 10 | public function walkInstanceOfExpression($instanceOfExpr) |
|
| 2022 | { |
||
| 2023 | 10 | $sql = ''; |
|
| 2024 | |||
| 2025 | 10 | $dqlAlias = $instanceOfExpr->identificationVariable; |
|
| 2026 | 10 | $discrClass = $class = $this->queryComponents[$dqlAlias]['metadata']; |
|
| 2027 | |||
| 2028 | 10 | if ($class->discriminatorColumn) { |
|
| 2029 | 10 | $discrClass = $this->em->getClassMetadata($class->rootEntityName); |
|
| 2030 | } |
||
| 2041 | |||
| 2042 | /** |
||
| 2043 | * {@inheritdoc} |
||
| 2044 | */ |
||
| 2045 | 77 | public function walkInParameter($inParam) |
|
| 2051 | |||
| 2052 | /** |
||
| 2053 | * {@inheritdoc} |
||
| 2054 | */ |
||
| 2055 | 148 | public function walkLiteral($literal) |
|
| 2071 | |||
| 2072 | /** |
||
| 2073 | * {@inheritdoc} |
||
| 2074 | */ |
||
| 2075 | 6 | public function walkBetweenExpression($betweenExpr) |
|
| 2088 | |||
| 2089 | /** |
||
| 2090 | * {@inheritdoc} |
||
| 2091 | */ |
||
| 2092 | 9 | public function walkLikeExpression($likeExpr) |
|
| 2117 | |||
| 2118 | /** |
||
| 2119 | * {@inheritdoc} |
||
| 2120 | */ |
||
| 2121 | 5 | public function walkStateFieldPathExpression($stateFieldPathExpression) |
|
| 2125 | |||
| 2126 | /** |
||
| 2127 | * {@inheritdoc} |
||
| 2128 | */ |
||
| 2129 | 261 | public function walkComparisonExpression($compExpr) |
|
| 2147 | |||
| 2148 | /** |
||
| 2149 | * {@inheritdoc} |
||
| 2150 | */ |
||
| 2151 | 214 | public function walkInputParameter($inputParam) |
|
| 2163 | |||
| 2164 | /** |
||
| 2165 | * {@inheritdoc} |
||
| 2166 | */ |
||
| 2167 | 327 | public function walkArithmeticExpression($arithmeticExpr) |
|
| 2173 | |||
| 2174 | /** |
||
| 2175 | * {@inheritdoc} |
||
| 2176 | */ |
||
| 2177 | 390 | public function walkSimpleArithmeticExpression($simpleArithmeticExpr) |
|
| 2185 | |||
| 2186 | /** |
||
| 2187 | * {@inheritdoc} |
||
| 2188 | */ |
||
| 2189 | 411 | public function walkArithmeticTerm($term) |
|
| 2205 | |||
| 2206 | /** |
||
| 2207 | * {@inheritdoc} |
||
| 2208 | */ |
||
| 2209 | 411 | public function walkArithmeticFactor($factor) |
|
| 2227 | |||
| 2228 | /** |
||
| 2229 | * Walks down an ArithmeticPrimary that represents an AST node, thereby generating the appropriate SQL. |
||
| 2230 | * |
||
| 2231 | * @param mixed $primary |
||
| 2232 | * |
||
| 2233 | * @return string The SQL. |
||
| 2234 | */ |
||
| 2235 | 411 | public function walkArithmeticPrimary($primary) |
|
| 2247 | |||
| 2248 | /** |
||
| 2249 | * {@inheritdoc} |
||
| 2250 | */ |
||
| 2251 | 18 | public function walkStringPrimary($stringPrimary) |
|
| 2257 | |||
| 2258 | /** |
||
| 2259 | * {@inheritdoc} |
||
| 2260 | */ |
||
| 2261 | 30 | public function walkResultVariable($resultVariable) |
|
| 2271 | |||
| 2272 | /** |
||
| 2273 | * @param ClassMetadataInfo $discrClass |
||
| 2274 | * @param AST\InstanceOfExpression $instanceOfExpr |
||
| 2275 | * @return string The list in parentheses of valid child discriminators from the given class |
||
| 2276 | * @throws QueryException |
||
| 2277 | */ |
||
| 2278 | 10 | private function getChildDiscriminatorsFromClassMetadata(ClassMetadataInfo $discrClass, AST\InstanceOfExpression $instanceOfExpr) |
|
| 2315 | } |
||
| 2316 |
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.