vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php line 284

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ORM\Query;
  4. use BadMethodCallException;
  5. use Doctrine\DBAL\Connection;
  6. use Doctrine\DBAL\LockMode;
  7. use Doctrine\DBAL\Platforms\AbstractPlatform;
  8. use Doctrine\DBAL\Types\Type;
  9. use Doctrine\Deprecations\Deprecation;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Doctrine\ORM\Mapping\ClassMetadata;
  12. use Doctrine\ORM\Mapping\QuoteStrategy;
  13. use Doctrine\ORM\OptimisticLockException;
  14. use Doctrine\ORM\Query;
  15. use Doctrine\ORM\Utility\HierarchyDiscriminatorResolver;
  16. use Doctrine\ORM\Utility\PersisterHelper;
  17. use InvalidArgumentException;
  18. use LogicException;
  19. use function array_diff;
  20. use function array_filter;
  21. use function array_keys;
  22. use function array_map;
  23. use function array_merge;
  24. use function assert;
  25. use function count;
  26. use function implode;
  27. use function in_array;
  28. use function is_array;
  29. use function is_float;
  30. use function is_numeric;
  31. use function is_string;
  32. use function preg_match;
  33. use function reset;
  34. use function sprintf;
  35. use function strtolower;
  36. use function strtoupper;
  37. use function trim;
  38. /**
  39.  * The SqlWalker walks over a DQL AST and constructs the corresponding SQL.
  40.  *
  41.  * @psalm-import-type QueryComponent from Parser
  42.  * @psalm-consistent-constructor
  43.  */
  44. class SqlWalker implements TreeWalker
  45. {
  46.     public const HINT_DISTINCT 'doctrine.distinct';
  47.     /**
  48.      * Used to mark a query as containing a PARTIAL expression, which needs to be known by SLC.
  49.      */
  50.     public const HINT_PARTIAL 'doctrine.partial';
  51.     /** @var ResultSetMapping */
  52.     private $rsm;
  53.     /**
  54.      * Counter for generating unique column aliases.
  55.      *
  56.      * @var int
  57.      */
  58.     private $aliasCounter 0;
  59.     /**
  60.      * Counter for generating unique table aliases.
  61.      *
  62.      * @var int
  63.      */
  64.     private $tableAliasCounter 0;
  65.     /**
  66.      * Counter for generating unique scalar result.
  67.      *
  68.      * @var int
  69.      */
  70.     private $scalarResultCounter 1;
  71.     /**
  72.      * Counter for generating unique parameter indexes.
  73.      *
  74.      * @var int
  75.      */
  76.     private $sqlParamIndex 0;
  77.     /**
  78.      * Counter for generating indexes.
  79.      *
  80.      * @var int
  81.      */
  82.     private $newObjectCounter 0;
  83.     /** @var ParserResult */
  84.     private $parserResult;
  85.     /** @var EntityManagerInterface */
  86.     private $em;
  87.     /** @var Connection */
  88.     private $conn;
  89.     /** @var Query */
  90.     private $query;
  91.     /** @var mixed[] */
  92.     private $tableAliasMap = [];
  93.     /**
  94.      * Map from result variable names to their SQL column alias names.
  95.      *
  96.      * @psalm-var array<string|int, string|list<string>>
  97.      */
  98.     private $scalarResultAliasMap = [];
  99.     /**
  100.      * Map from Table-Alias + Column-Name to OrderBy-Direction.
  101.      *
  102.      * @var array<string, string>
  103.      */
  104.     private $orderedColumnsMap = [];
  105.     /**
  106.      * Map from DQL-Alias + Field-Name to SQL Column Alias.
  107.      *
  108.      * @var array<string, array<string, string>>
  109.      */
  110.     private $scalarFields = [];
  111.     /**
  112.      * Map of all components/classes that appear in the DQL query.
  113.      *
  114.      * @psalm-var array<string, QueryComponent>
  115.      */
  116.     private $queryComponents;
  117.     /**
  118.      * A list of classes that appear in non-scalar SelectExpressions.
  119.      *
  120.      * @psalm-var array<string, array{class: ClassMetadata, dqlAlias: string, resultAlias: string|null}>
  121.      */
  122.     private $selectedClasses = [];
  123.     /**
  124.      * The DQL alias of the root class of the currently traversed query.
  125.      *
  126.      * @psalm-var list<string>
  127.      */
  128.     private $rootAliases = [];
  129.     /**
  130.      * Flag that indicates whether to generate SQL table aliases in the SQL.
  131.      * These should only be generated for SELECT queries, not for UPDATE/DELETE.
  132.      *
  133.      * @var bool
  134.      */
  135.     private $useSqlTableAliases true;
  136.     /**
  137.      * The database platform abstraction.
  138.      *
  139.      * @var AbstractPlatform
  140.      */
  141.     private $platform;
  142.     /**
  143.      * The quote strategy.
  144.      *
  145.      * @var QuoteStrategy
  146.      */
  147.     private $quoteStrategy;
  148.     /**
  149.      * @param Query        $query        The parsed Query.
  150.      * @param ParserResult $parserResult The result of the parsing process.
  151.      * @psalm-param array<string, QueryComponent> $queryComponents The query components (symbol table).
  152.      */
  153.     public function __construct($query$parserResult, array $queryComponents)
  154.     {
  155.         $this->query           $query;
  156.         $this->parserResult    $parserResult;
  157.         $this->queryComponents $queryComponents;
  158.         $this->rsm             $parserResult->getResultSetMapping();
  159.         $this->em              $query->getEntityManager();
  160.         $this->conn            $this->em->getConnection();
  161.         $this->platform        $this->conn->getDatabasePlatform();
  162.         $this->quoteStrategy   $this->em->getConfiguration()->getQuoteStrategy();
  163.     }
  164.     /**
  165.      * Gets the Query instance used by the walker.
  166.      *
  167.      * @return Query
  168.      */
  169.     public function getQuery()
  170.     {
  171.         return $this->query;
  172.     }
  173.     /**
  174.      * Gets the Connection used by the walker.
  175.      *
  176.      * @return Connection
  177.      */
  178.     public function getConnection()
  179.     {
  180.         return $this->conn;
  181.     }
  182.     /**
  183.      * Gets the EntityManager used by the walker.
  184.      *
  185.      * @return EntityManagerInterface
  186.      */
  187.     public function getEntityManager()
  188.     {
  189.         return $this->em;
  190.     }
  191.     /**
  192.      * Gets the information about a single query component.
  193.      *
  194.      * @param string $dqlAlias The DQL alias.
  195.      *
  196.      * @return mixed[]
  197.      * @psalm-return QueryComponent
  198.      */
  199.     public function getQueryComponent($dqlAlias)
  200.     {
  201.         return $this->queryComponents[$dqlAlias];
  202.     }
  203.     public function getMetadataForDqlAlias(string $dqlAlias): ClassMetadata
  204.     {
  205.         if (! isset($this->queryComponents[$dqlAlias]['metadata'])) {
  206.             throw new LogicException(sprintf('No metadata for DQL alias: %s'$dqlAlias));
  207.         }
  208.         return $this->queryComponents[$dqlAlias]['metadata'];
  209.     }
  210.     /**
  211.      * Returns internal queryComponents array.
  212.      *
  213.      * @return array<string, QueryComponent>
  214.      */
  215.     public function getQueryComponents()
  216.     {
  217.         return $this->queryComponents;
  218.     }
  219.     /**
  220.      * Sets or overrides a query component for a given dql alias.
  221.      *
  222.      * @param string $dqlAlias The DQL alias.
  223.      * @psalm-param QueryComponent $queryComponent
  224.      *
  225.      * @return void
  226.      *
  227.      * @not-deprecated
  228.      */
  229.     public function setQueryComponent($dqlAlias, array $queryComponent)
  230.     {
  231.         $requiredKeys = ['metadata''parent''relation''map''nestingLevel''token'];
  232.         if (array_diff($requiredKeysarray_keys($queryComponent))) {
  233.             throw QueryException::invalidQueryComponent($dqlAlias);
  234.         }
  235.         $this->queryComponents[$dqlAlias] = $queryComponent;
  236.     }
  237.     /**
  238.      * Gets an executor that can be used to execute the result of this walker.
  239.      *
  240.      * @param AST\DeleteStatement|AST\UpdateStatement|AST\SelectStatement $AST
  241.      *
  242.      * @return Exec\AbstractSqlExecutor
  243.      *
  244.      * @not-deprecated
  245.      */
  246.     public function getExecutor($AST)
  247.     {
  248.         switch (true) {
  249.             case $AST instanceof AST\DeleteStatement:
  250.                 $primaryClass $this->em->getClassMetadata($AST->deleteClause->abstractSchemaName);
  251.                 return $primaryClass->isInheritanceTypeJoined()
  252.                     ? new Exec\MultiTableDeleteExecutor($AST$this)
  253.                     : new Exec\SingleTableDeleteUpdateExecutor($AST$this);
  254.             case $AST instanceof AST\UpdateStatement:
  255.                 $primaryClass $this->em->getClassMetadata($AST->updateClause->abstractSchemaName);
  256.                 return $primaryClass->isInheritanceTypeJoined()
  257.                     ? new Exec\MultiTableUpdateExecutor($AST$this)
  258.                     : new Exec\SingleTableDeleteUpdateExecutor($AST$this);
  259.             default:
  260.                 return new Exec\SingleSelectExecutor($AST$this);
  261.         }
  262.     }
  263.     /**
  264.      * Generates a unique, short SQL table alias.
  265.      *
  266.      * @param string $tableName Table name
  267.      * @param string $dqlAlias  The DQL alias.
  268.      *
  269.      * @return string Generated table alias.
  270.      */
  271.     public function getSQLTableAlias($tableName$dqlAlias '')
  272.     {
  273.         $tableName .= $dqlAlias '@[' $dqlAlias ']' '';
  274.         if (! isset($this->tableAliasMap[$tableName])) {
  275.             $this->tableAliasMap[$tableName] = (preg_match('/[a-z]/i'$tableName[0]) ? strtolower($tableName[0]) : 't')
  276.                 . $this->tableAliasCounter++ . '_';
  277.         }
  278.         return $this->tableAliasMap[$tableName];
  279.     }
  280.     /**
  281.      * Forces the SqlWalker to use a specific alias for a table name, rather than
  282.      * generating an alias on its own.
  283.      *
  284.      * @param string $tableName
  285.      * @param string $alias
  286.      * @param string $dqlAlias
  287.      *
  288.      * @return string
  289.      */
  290.     public function setSQLTableAlias($tableName$alias$dqlAlias '')
  291.     {
  292.         $tableName .= $dqlAlias '@[' $dqlAlias ']' '';
  293.         $this->tableAliasMap[$tableName] = $alias;
  294.         return $alias;
  295.     }
  296.     /**
  297.      * Gets an SQL column alias for a column name.
  298.      *
  299.      * @param string $columnName
  300.      *
  301.      * @return string
  302.      */
  303.     public function getSQLColumnAlias($columnName)
  304.     {
  305.         return $this->quoteStrategy->getColumnAlias($columnName$this->aliasCounter++, $this->platform);
  306.     }
  307.     /**
  308.      * Generates the SQL JOINs that are necessary for Class Table Inheritance
  309.      * for the given class.
  310.      *
  311.      * @param ClassMetadata $class    The class for which to generate the joins.
  312.      * @param string        $dqlAlias The DQL alias of the class.
  313.      *
  314.      * @return string The SQL.
  315.      */
  316.     private function generateClassTableInheritanceJoins(
  317.         ClassMetadata $class,
  318.         string $dqlAlias
  319.     ): string {
  320.         $sql '';
  321.         $baseTableAlias $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
  322.         // INNER JOIN parent class tables
  323.         foreach ($class->parentClasses as $parentClassName) {
  324.             $parentClass $this->em->getClassMetadata($parentClassName);
  325.             $tableAlias  $this->getSQLTableAlias($parentClass->getTableName(), $dqlAlias);
  326.             // If this is a joined association we must use left joins to preserve the correct result.
  327.             $sql .= isset($this->queryComponents[$dqlAlias]['relation']) ? ' LEFT ' ' INNER ';
  328.             $sql .= 'JOIN ' $this->quoteStrategy->getTableName($parentClass$this->platform) . ' ' $tableAlias ' ON ';
  329.             $sqlParts = [];
  330.             foreach ($this->quoteStrategy->getIdentifierColumnNames($class$this->platform) as $columnName) {
  331.                 $sqlParts[] = $baseTableAlias '.' $columnName ' = ' $tableAlias '.' $columnName;
  332.             }
  333.             // Add filters on the root class
  334.             $sqlParts[] = $this->generateFilterConditionSQL($parentClass$tableAlias);
  335.             $sql .= implode(' AND 'array_filter($sqlParts));
  336.         }
  337.         // Ignore subclassing inclusion if partial objects is disallowed
  338.         if ($this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
  339.             return $sql;
  340.         }
  341.         // LEFT JOIN child class tables
  342.         foreach ($class->subClasses as $subClassName) {
  343.             $subClass   $this->em->getClassMetadata($subClassName);
  344.             $tableAlias $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
  345.             $sql .= ' LEFT JOIN ' $this->quoteStrategy->getTableName($subClass$this->platform) . ' ' $tableAlias ' ON ';
  346.             $sqlParts = [];
  347.             foreach ($this->quoteStrategy->getIdentifierColumnNames($subClass$this->platform) as $columnName) {
  348.                 $sqlParts[] = $baseTableAlias '.' $columnName ' = ' $tableAlias '.' $columnName;
  349.             }
  350.             $sql .= implode(' AND '$sqlParts);
  351.         }
  352.         return $sql;
  353.     }
  354.     private function generateOrderedCollectionOrderByItems(): string
  355.     {
  356.         $orderedColumns = [];
  357.         foreach ($this->selectedClasses as $selectedClass) {
  358.             $dqlAlias $selectedClass['dqlAlias'];
  359.             $qComp    $this->queryComponents[$dqlAlias];
  360.             if (! isset($qComp['relation']['orderBy'])) {
  361.                 continue;
  362.             }
  363.             assert(isset($qComp['metadata']));
  364.             $persister $this->em->getUnitOfWork()->getEntityPersister($qComp['metadata']->name);
  365.             foreach ($qComp['relation']['orderBy'] as $fieldName => $orientation) {
  366.                 $columnName $this->quoteStrategy->getColumnName($fieldName$qComp['metadata'], $this->platform);
  367.                 $tableName  $qComp['metadata']->isInheritanceTypeJoined()
  368.                     ? $persister->getOwningTable($fieldName)
  369.                     : $qComp['metadata']->getTableName();
  370.                 $orderedColumn $this->getSQLTableAlias($tableName$dqlAlias) . '.' $columnName;
  371.                 // OrderByClause should replace an ordered relation. see - DDC-2475
  372.                 if (isset($this->orderedColumnsMap[$orderedColumn])) {
  373.                     continue;
  374.                 }
  375.                 $this->orderedColumnsMap[$orderedColumn] = $orientation;
  376.                 $orderedColumns[]                        = $orderedColumn ' ' $orientation;
  377.             }
  378.         }
  379.         return implode(', '$orderedColumns);
  380.     }
  381.     /**
  382.      * Generates a discriminator column SQL condition for the class with the given DQL alias.
  383.      *
  384.      * @psalm-param list<string> $dqlAliases List of root DQL aliases to inspect for discriminator restrictions.
  385.      */
  386.     private function generateDiscriminatorColumnConditionSQL(array $dqlAliases): string
  387.     {
  388.         $sqlParts = [];
  389.         foreach ($dqlAliases as $dqlAlias) {
  390.             $class $this->getMetadataForDqlAlias($dqlAlias);
  391.             if (! $class->isInheritanceTypeSingleTable()) {
  392.                 continue;
  393.             }
  394.             $conn   $this->em->getConnection();
  395.             $values = [];
  396.             if ($class->discriminatorValue !== null) { // discriminators can be 0
  397.                 $values[] = $conn->quote($class->discriminatorValue);
  398.             }
  399.             foreach ($class->subClasses as $subclassName) {
  400.                 $values[] = $conn->quote($this->em->getClassMetadata($subclassName)->discriminatorValue);
  401.             }
  402.             $sqlTableAlias $this->useSqlTableAliases
  403.                 $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.'
  404.                 '';
  405.             $sqlParts[] = $sqlTableAlias $class->getDiscriminatorColumn()['name'] . ' IN (' implode(', '$values) . ')';
  406.         }
  407.         $sql implode(' AND '$sqlParts);
  408.         return count($sqlParts) > '(' $sql ')' $sql;
  409.     }
  410.     /**
  411.      * Generates the filter SQL for a given entity and table alias.
  412.      *
  413.      * @param ClassMetadata $targetEntity     Metadata of the target entity.
  414.      * @param string        $targetTableAlias The table alias of the joined/selected table.
  415.      *
  416.      * @return string The SQL query part to add to a query.
  417.      */
  418.     private function generateFilterConditionSQL(
  419.         ClassMetadata $targetEntity,
  420.         string $targetTableAlias
  421.     ): string {
  422.         if (! $this->em->hasFilters()) {
  423.             return '';
  424.         }
  425.         switch ($targetEntity->inheritanceType) {
  426.             case ClassMetadata::INHERITANCE_TYPE_NONE:
  427.                 break;
  428.             case ClassMetadata::INHERITANCE_TYPE_JOINED:
  429.                 // The classes in the inheritance will be added to the query one by one,
  430.                 // but only the root node is getting filtered
  431.                 if ($targetEntity->name !== $targetEntity->rootEntityName) {
  432.                     return '';
  433.                 }
  434.                 break;
  435.             case ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE:
  436.                 // With STI the table will only be queried once, make sure that the filters
  437.                 // are added to the root entity
  438.                 $targetEntity $this->em->getClassMetadata($targetEntity->rootEntityName);
  439.                 break;
  440.             default:
  441.                 //@todo: throw exception?
  442.                 return '';
  443.         }
  444.         $filterClauses = [];
  445.         foreach ($this->em->getFilters()->getEnabledFilters() as $filter) {
  446.             $filterExpr $filter->addFilterConstraint($targetEntity$targetTableAlias);
  447.             if ($filterExpr !== '') {
  448.                 $filterClauses[] = '(' $filterExpr ')';
  449.             }
  450.         }
  451.         return implode(' AND '$filterClauses);
  452.     }
  453.     /**
  454.      * Walks down a SelectStatement AST node, thereby generating the appropriate SQL.
  455.      *
  456.      * @return string
  457.      */
  458.     public function walkSelectStatement(AST\SelectStatement $AST)
  459.     {
  460.         $limit    $this->query->getMaxResults();
  461.         $offset   $this->query->getFirstResult();
  462.         $lockMode $this->query->getHint(Query::HINT_LOCK_MODE) ?: LockMode::NONE;
  463.         $sql      $this->walkSelectClause($AST->selectClause)
  464.             . $this->walkFromClause($AST->fromClause)
  465.             . $this->walkWhereClause($AST->whereClause);
  466.         if ($AST->groupByClause) {
  467.             $sql .= $this->walkGroupByClause($AST->groupByClause);
  468.         }
  469.         if ($AST->havingClause) {
  470.             $sql .= $this->walkHavingClause($AST->havingClause);
  471.         }
  472.         if ($AST->orderByClause) {
  473.             $sql .= $this->walkOrderByClause($AST->orderByClause);
  474.         }
  475.         $orderBySql $this->generateOrderedCollectionOrderByItems();
  476.         if (! $AST->orderByClause && $orderBySql) {
  477.             $sql .= ' ORDER BY ' $orderBySql;
  478.         }
  479.         $sql $this->platform->modifyLimitQuery($sql$limit$offset);
  480.         if ($lockMode === LockMode::NONE) {
  481.             return $sql;
  482.         }
  483.         if ($lockMode === LockMode::PESSIMISTIC_READ) {
  484.             return $sql ' ' $this->platform->getReadLockSQL();
  485.         }
  486.         if ($lockMode === LockMode::PESSIMISTIC_WRITE) {
  487.             return $sql ' ' $this->platform->getWriteLockSQL();
  488.         }
  489.         if ($lockMode !== LockMode::OPTIMISTIC) {
  490.             throw QueryException::invalidLockMode();
  491.         }
  492.         foreach ($this->selectedClasses as $selectedClass) {
  493.             if (! $selectedClass['class']->isVersioned) {
  494.                 throw OptimisticLockException::lockFailed($selectedClass['class']->name);
  495.             }
  496.         }
  497.         return $sql;
  498.     }
  499.     /**
  500.      * Walks down an UpdateStatement AST node, thereby generating the appropriate SQL.
  501.      *
  502.      * @return string
  503.      */
  504.     public function walkUpdateStatement(AST\UpdateStatement $AST)
  505.     {
  506.         $this->useSqlTableAliases false;
  507.         $this->rsm->isSelect      false;
  508.         return $this->walkUpdateClause($AST->updateClause)
  509.             . $this->walkWhereClause($AST->whereClause);
  510.     }
  511.     /**
  512.      * Walks down a DeleteStatement AST node, thereby generating the appropriate SQL.
  513.      *
  514.      * @return string
  515.      */
  516.     public function walkDeleteStatement(AST\DeleteStatement $AST)
  517.     {
  518.         $this->useSqlTableAliases false;
  519.         $this->rsm->isSelect      false;
  520.         return $this->walkDeleteClause($AST->deleteClause)
  521.             . $this->walkWhereClause($AST->whereClause);
  522.     }
  523.     /**
  524.      * Walks down an IdentificationVariable AST node, thereby generating the appropriate SQL.
  525.      * This one differs of ->walkIdentificationVariable() because it generates the entity identifiers.
  526.      *
  527.      * @param string $identVariable
  528.      *
  529.      * @return string
  530.      *
  531.      * @not-deprecated
  532.      */
  533.     public function walkEntityIdentificationVariable($identVariable)
  534.     {
  535.         $class      $this->getMetadataForDqlAlias($identVariable);
  536.         $tableAlias $this->getSQLTableAlias($class->getTableName(), $identVariable);
  537.         $sqlParts   = [];
  538.         foreach ($this->quoteStrategy->getIdentifierColumnNames($class$this->platform) as $columnName) {
  539.             $sqlParts[] = $tableAlias '.' $columnName;
  540.         }
  541.         return implode(', '$sqlParts);
  542.     }
  543.     /**
  544.      * Walks down an IdentificationVariable (no AST node associated), thereby generating the SQL.
  545.      *
  546.      * @param string $identificationVariable
  547.      * @param string $fieldName
  548.      *
  549.      * @return string The SQL.
  550.      *
  551.      * @not-deprecated
  552.      */
  553.     public function walkIdentificationVariable($identificationVariable$fieldName null)
  554.     {
  555.         $class $this->getMetadataForDqlAlias($identificationVariable);
  556.         if (
  557.             $fieldName !== null && $class->isInheritanceTypeJoined() &&
  558.             isset($class->fieldMappings[$fieldName]['inherited'])
  559.         ) {
  560.             $class $this->em->getClassMetadata($class->fieldMappings[$fieldName]['inherited']);
  561.         }
  562.         return $this->getSQLTableAlias($class->getTableName(), $identificationVariable);
  563.     }
  564.     /**
  565.      * Walks down a PathExpression AST node, thereby generating the appropriate SQL.
  566.      *
  567.      * @param AST\PathExpression $pathExpr
  568.      *
  569.      * @return string
  570.      *
  571.      * @not-deprecated
  572.      */
  573.     public function walkPathExpression($pathExpr)
  574.     {
  575.         $sql '';
  576.         assert($pathExpr->field !== null);
  577.         switch ($pathExpr->type) {
  578.             case AST\PathExpression::TYPE_STATE_FIELD:
  579.                 $fieldName $pathExpr->field;
  580.                 $dqlAlias  $pathExpr->identificationVariable;
  581.                 $class     $this->getMetadataForDqlAlias($dqlAlias);
  582.                 if ($this->useSqlTableAliases) {
  583.                     $sql .= $this->walkIdentificationVariable($dqlAlias$fieldName) . '.';
  584.                 }
  585.                 $sql .= $this->quoteStrategy->getColumnName($fieldName$class$this->platform);
  586.                 break;
  587.             case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION:
  588.                 // 1- the owning side:
  589.                 //    Just use the foreign key, i.e. u.group_id
  590.                 $fieldName $pathExpr->field;
  591.                 $dqlAlias  $pathExpr->identificationVariable;
  592.                 $class     $this->getMetadataForDqlAlias($dqlAlias);
  593.                 if (isset($class->associationMappings[$fieldName]['inherited'])) {
  594.                     $class $this->em->getClassMetadata($class->associationMappings[$fieldName]['inherited']);
  595.                 }
  596.                 $assoc $class->associationMappings[$fieldName];
  597.                 if (! $assoc['isOwningSide']) {
  598.                     throw QueryException::associationPathInverseSideNotSupported($pathExpr);
  599.                 }
  600.                 // COMPOSITE KEYS NOT (YET?) SUPPORTED
  601.                 if (count($assoc['sourceToTargetKeyColumns']) > 1) {
  602.                     throw QueryException::associationPathCompositeKeyNotSupported();
  603.                 }
  604.                 if ($this->useSqlTableAliases) {
  605.                     $sql .= $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.';
  606.                 }
  607.                 $sql .= reset($assoc['targetToSourceKeyColumns']);
  608.                 break;
  609.             default:
  610.                 throw QueryException::invalidPathExpression($pathExpr);
  611.         }
  612.         return $sql;
  613.     }
  614.     /**
  615.      * Walks down a SelectClause AST node, thereby generating the appropriate SQL.
  616.      *
  617.      * @param AST\SelectClause $selectClause
  618.      *
  619.      * @return string
  620.      *
  621.      * @not-deprecated
  622.      */
  623.     public function walkSelectClause($selectClause)
  624.     {
  625.         $sql                  'SELECT ' . ($selectClause->isDistinct 'DISTINCT ' '');
  626.         $sqlSelectExpressions array_filter(array_map([$this'walkSelectExpression'], $selectClause->selectExpressions));
  627.         if ($this->query->getHint(Query::HINT_INTERNAL_ITERATION) === true && $selectClause->isDistinct) {
  628.             $this->query->setHint(self::HINT_DISTINCTtrue);
  629.         }
  630.         $addMetaColumns = ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD) &&
  631.             $this->query->getHydrationMode() === Query::HYDRATE_OBJECT
  632.             || $this->query->getHint(Query::HINT_INCLUDE_META_COLUMNS);
  633.         foreach ($this->selectedClasses as $selectedClass) {
  634.             $class       $selectedClass['class'];
  635.             $dqlAlias    $selectedClass['dqlAlias'];
  636.             $resultAlias $selectedClass['resultAlias'];
  637.             // Register as entity or joined entity result
  638.             if (! isset($this->queryComponents[$dqlAlias]['relation'])) {
  639.                 $this->rsm->addEntityResult($class->name$dqlAlias$resultAlias);
  640.             } else {
  641.                 assert(isset($this->queryComponents[$dqlAlias]['parent']));
  642.                 $this->rsm->addJoinedEntityResult(
  643.                     $class->name,
  644.                     $dqlAlias,
  645.                     $this->queryComponents[$dqlAlias]['parent'],
  646.                     $this->queryComponents[$dqlAlias]['relation']['fieldName']
  647.                 );
  648.             }
  649.             if ($class->isInheritanceTypeSingleTable() || $class->isInheritanceTypeJoined()) {
  650.                 // Add discriminator columns to SQL
  651.                 $rootClass   $this->em->getClassMetadata($class->rootEntityName);
  652.                 $tblAlias    $this->getSQLTableAlias($rootClass->getTableName(), $dqlAlias);
  653.                 $discrColumn $rootClass->getDiscriminatorColumn();
  654.                 $columnAlias $this->getSQLColumnAlias($discrColumn['name']);
  655.                 $sqlSelectExpressions[] = $tblAlias '.' $discrColumn['name'] . ' AS ' $columnAlias;
  656.                 $this->rsm->setDiscriminatorColumn($dqlAlias$columnAlias);
  657.                 $this->rsm->addMetaResult($dqlAlias$columnAlias$discrColumn['fieldName'], false$discrColumn['type']);
  658.                 if (! empty($discrColumn['enumType'])) {
  659.                     $this->rsm->addEnumResult($columnAlias$discrColumn['enumType']);
  660.                 }
  661.             }
  662.             // Add foreign key columns to SQL, if necessary
  663.             if (! $addMetaColumns && ! $class->containsForeignIdentifier) {
  664.                 continue;
  665.             }
  666.             // Add foreign key columns of class and also parent classes
  667.             foreach ($class->associationMappings as $assoc) {
  668.                 if (
  669.                     ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE)
  670.                     || ( ! $addMetaColumns && ! isset($assoc['id']))
  671.                 ) {
  672.                     continue;
  673.                 }
  674.                 $targetClass   $this->em->getClassMetadata($assoc['targetEntity']);
  675.                 $isIdentifier  = (isset($assoc['id']) && $assoc['id'] === true);
  676.                 $owningClass   = isset($assoc['inherited']) ? $this->em->getClassMetadata($assoc['inherited']) : $class;
  677.                 $sqlTableAlias $this->getSQLTableAlias($owningClass->getTableName(), $dqlAlias);
  678.                 foreach ($assoc['joinColumns'] as $joinColumn) {
  679.                     $columnName  $joinColumn['name'];
  680.                     $columnAlias $this->getSQLColumnAlias($columnName);
  681.                     $columnType  PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass$this->em);
  682.                     $quotedColumnName       $this->quoteStrategy->getJoinColumnName($joinColumn$class$this->platform);
  683.                     $sqlSelectExpressions[] = $sqlTableAlias '.' $quotedColumnName ' AS ' $columnAlias;
  684.                     $this->rsm->addMetaResult($dqlAlias$columnAlias$columnName$isIdentifier$columnType);
  685.                 }
  686.             }
  687.             // Add foreign key columns to SQL, if necessary
  688.             if (! $addMetaColumns) {
  689.                 continue;
  690.             }
  691.             // Add foreign key columns of subclasses
  692.             foreach ($class->subClasses as $subClassName) {
  693.                 $subClass      $this->em->getClassMetadata($subClassName);
  694.                 $sqlTableAlias $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
  695.                 foreach ($subClass->associationMappings as $assoc) {
  696.                     // Skip if association is inherited
  697.                     if (isset($assoc['inherited'])) {
  698.                         continue;
  699.                     }
  700.                     if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) {
  701.                         $targetClass $this->em->getClassMetadata($assoc['targetEntity']);
  702.                         foreach ($assoc['joinColumns'] as $joinColumn) {
  703.                             $columnName  $joinColumn['name'];
  704.                             $columnAlias $this->getSQLColumnAlias($columnName);
  705.                             $columnType  PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass$this->em);
  706.                             $quotedColumnName       $this->quoteStrategy->getJoinColumnName($joinColumn$subClass$this->platform);
  707.                             $sqlSelectExpressions[] = $sqlTableAlias '.' $quotedColumnName ' AS ' $columnAlias;
  708.                             $this->rsm->addMetaResult($dqlAlias$columnAlias$columnName$subClass->isIdentifier($columnName), $columnType);
  709.                         }
  710.                     }
  711.                 }
  712.             }
  713.         }
  714.         return $sql implode(', '$sqlSelectExpressions);
  715.     }
  716.     /**
  717.      * Walks down a FromClause AST node, thereby generating the appropriate SQL.
  718.      *
  719.      * @param AST\FromClause $fromClause
  720.      *
  721.      * @return string
  722.      *
  723.      * @not-deprecated
  724.      */
  725.     public function walkFromClause($fromClause)
  726.     {
  727.         $identificationVarDecls $fromClause->identificationVariableDeclarations;
  728.         $sqlParts               = [];
  729.         foreach ($identificationVarDecls as $identificationVariableDecl) {
  730.             $sqlParts[] = $this->walkIdentificationVariableDeclaration($identificationVariableDecl);
  731.         }
  732.         return ' FROM ' implode(', '$sqlParts);
  733.     }
  734.     /**
  735.      * Walks down a IdentificationVariableDeclaration AST node, thereby generating the appropriate SQL.
  736.      *
  737.      * @param AST\IdentificationVariableDeclaration $identificationVariableDecl
  738.      *
  739.      * @return string
  740.      *
  741.      * @not-deprecated
  742.      */
  743.     public function walkIdentificationVariableDeclaration($identificationVariableDecl)
  744.     {
  745.         $sql $this->walkRangeVariableDeclaration($identificationVariableDecl->rangeVariableDeclaration);
  746.         if ($identificationVariableDecl->indexBy) {
  747.             $this->walkIndexBy($identificationVariableDecl->indexBy);
  748.         }
  749.         foreach ($identificationVariableDecl->joins as $join) {
  750.             $sql .= $this->walkJoin($join);
  751.         }
  752.         return $sql;
  753.     }
  754.     /**
  755.      * Walks down a IndexBy AST node.
  756.      *
  757.      * @param AST\IndexBy $indexBy
  758.      *
  759.      * @return void
  760.      *
  761.      * @not-deprecated
  762.      */
  763.     public function walkIndexBy($indexBy)
  764.     {
  765.         $pathExpression $indexBy->singleValuedPathExpression;
  766.         $alias          $pathExpression->identificationVariable;
  767.         assert($pathExpression->field !== null);
  768.         switch ($pathExpression->type) {
  769.             case AST\PathExpression::TYPE_STATE_FIELD:
  770.                 $field $pathExpression->field;
  771.                 break;
  772.             case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION:
  773.                 // Just use the foreign key, i.e. u.group_id
  774.                 $fieldName $pathExpression->field;
  775.                 $class     $this->getMetadataForDqlAlias($alias);
  776.                 if (isset($class->associationMappings[$fieldName]['inherited'])) {
  777.                     $class $this->em->getClassMetadata($class->associationMappings[$fieldName]['inherited']);
  778.                 }
  779.                 $association $class->associationMappings[$fieldName];
  780.                 if (! $association['isOwningSide']) {
  781.                     throw QueryException::associationPathInverseSideNotSupported($pathExpression);
  782.                 }
  783.                 if (count($association['sourceToTargetKeyColumns']) > 1) {
  784.                     throw QueryException::associationPathCompositeKeyNotSupported();
  785.                 }
  786.                 $field reset($association['targetToSourceKeyColumns']);
  787.                 break;
  788.             default:
  789.                 throw QueryException::invalidPathExpression($pathExpression);
  790.         }
  791.         if (isset($this->scalarFields[$alias][$field])) {
  792.             $this->rsm->addIndexByScalar($this->scalarFields[$alias][$field]);
  793.             return;
  794.         }
  795.         $this->rsm->addIndexBy($alias$field);
  796.     }
  797.     /**
  798.      * Walks down a RangeVariableDeclaration AST node, thereby generating the appropriate SQL.
  799.      *
  800.      * @param AST\RangeVariableDeclaration $rangeVariableDeclaration
  801.      *
  802.      * @return string
  803.      *
  804.      * @not-deprecated
  805.      */
  806.     public function walkRangeVariableDeclaration($rangeVariableDeclaration)
  807.     {
  808.         return $this->generateRangeVariableDeclarationSQL($rangeVariableDeclarationfalse);
  809.     }
  810.     /**
  811.      * Generate appropriate SQL for RangeVariableDeclaration AST node
  812.      */
  813.     private function generateRangeVariableDeclarationSQL(
  814.         AST\RangeVariableDeclaration $rangeVariableDeclaration,
  815.         bool $buildNestedJoins
  816.     ): string {
  817.         $class    $this->em->getClassMetadata($rangeVariableDeclaration->abstractSchemaName);
  818.         $dqlAlias $rangeVariableDeclaration->aliasIdentificationVariable;
  819.         if ($rangeVariableDeclaration->isRoot) {
  820.             $this->rootAliases[] = $dqlAlias;
  821.         }
  822.         $sql $this->platform->appendLockHint(
  823.             $this->quoteStrategy->getTableName($class$this->platform) . ' ' .
  824.             $this->getSQLTableAlias($class->getTableName(), $dqlAlias),
  825.             $this->query->getHint(Query::HINT_LOCK_MODE) ?: LockMode::NONE
  826.         );
  827.         if (! $class->isInheritanceTypeJoined()) {
  828.             return $sql;
  829.         }
  830.         $classTableInheritanceJoins $this->generateClassTableInheritanceJoins($class$dqlAlias);
  831.         if (! $buildNestedJoins) {
  832.             return $sql $classTableInheritanceJoins;
  833.         }
  834.         return $classTableInheritanceJoins === '' $sql '(' $sql $classTableInheritanceJoins ')';
  835.     }
  836.     /**
  837.      * Walks down a JoinAssociationDeclaration AST node, thereby generating the appropriate SQL.
  838.      *
  839.      * @param AST\JoinAssociationDeclaration $joinAssociationDeclaration
  840.      * @param int                            $joinType
  841.      * @param AST\ConditionalExpression      $condExpr
  842.      * @psalm-param AST\Join::JOIN_TYPE_* $joinType
  843.      *
  844.      * @return string
  845.      *
  846.      * @throws QueryException
  847.      *
  848.      * @not-deprecated
  849.      */
  850.     public function walkJoinAssociationDeclaration($joinAssociationDeclaration$joinType AST\Join::JOIN_TYPE_INNER$condExpr null)
  851.     {
  852.         $sql '';
  853.         $associationPathExpression $joinAssociationDeclaration->joinAssociationPathExpression;
  854.         $joinedDqlAlias            $joinAssociationDeclaration->aliasIdentificationVariable;
  855.         $indexBy                   $joinAssociationDeclaration->indexBy;
  856.         $relation $this->queryComponents[$joinedDqlAlias]['relation'] ?? null;
  857.         assert($relation !== null);
  858.         $targetClass     $this->em->getClassMetadata($relation['targetEntity']);
  859.         $sourceClass     $this->em->getClassMetadata($relation['sourceEntity']);
  860.         $targetTableName $this->quoteStrategy->getTableName($targetClass$this->platform);
  861.         $targetTableAlias $this->getSQLTableAlias($targetClass->getTableName(), $joinedDqlAlias);
  862.         $sourceTableAlias $this->getSQLTableAlias($sourceClass->getTableName(), $associationPathExpression->identificationVariable);
  863.         // Ensure we got the owning side, since it has all mapping info
  864.         $assoc = ! $relation['isOwningSide'] ? $targetClass->associationMappings[$relation['mappedBy']] : $relation;
  865.         if ($this->query->getHint(Query::HINT_INTERNAL_ITERATION) === true && (! $this->query->getHint(self::HINT_DISTINCT) || isset($this->selectedClasses[$joinedDqlAlias]))) {
  866.             if ($relation['type'] === ClassMetadata::ONE_TO_MANY || $relation['type'] === ClassMetadata::MANY_TO_MANY) {
  867.                 throw QueryException::iterateWithFetchJoinNotAllowed($assoc);
  868.             }
  869.         }
  870.         $targetTableJoin null;
  871.         // This condition is not checking ClassMetadata::MANY_TO_ONE, because by definition it cannot
  872.         // be the owning side and previously we ensured that $assoc is always the owning side of the associations.
  873.         // The owning side is necessary at this point because only it contains the JoinColumn information.
  874.         switch (true) {
  875.             case $assoc['type'] & ClassMetadata::TO_ONE:
  876.                 $conditions = [];
  877.                 foreach ($assoc['joinColumns'] as $joinColumn) {
  878.                     $quotedSourceColumn $this->quoteStrategy->getJoinColumnName($joinColumn$targetClass$this->platform);
  879.                     $quotedTargetColumn $this->quoteStrategy->getReferencedJoinColumnName($joinColumn$targetClass$this->platform);
  880.                     if ($relation['isOwningSide']) {
  881.                         $conditions[] = $sourceTableAlias '.' $quotedSourceColumn ' = ' $targetTableAlias '.' $quotedTargetColumn;
  882.                         continue;
  883.                     }
  884.                     $conditions[] = $sourceTableAlias '.' $quotedTargetColumn ' = ' $targetTableAlias '.' $quotedSourceColumn;
  885.                 }
  886.                 // Apply remaining inheritance restrictions
  887.                 $discrSql $this->generateDiscriminatorColumnConditionSQL([$joinedDqlAlias]);
  888.                 if ($discrSql) {
  889.                     $conditions[] = $discrSql;
  890.                 }
  891.                 // Apply the filters
  892.                 $filterExpr $this->generateFilterConditionSQL($targetClass$targetTableAlias);
  893.                 if ($filterExpr) {
  894.                     $conditions[] = $filterExpr;
  895.                 }
  896.                 $targetTableJoin = [
  897.                     'table' => $targetTableName ' ' $targetTableAlias,
  898.                     'condition' => implode(' AND '$conditions),
  899.                 ];
  900.                 break;
  901.             case $assoc['type'] === ClassMetadata::MANY_TO_MANY:
  902.                 // Join relation table
  903.                 $joinTable      $assoc['joinTable'];
  904.                 $joinTableAlias $this->getSQLTableAlias($joinTable['name'], $joinedDqlAlias);
  905.                 $joinTableName  $this->quoteStrategy->getJoinTableName($assoc$sourceClass$this->platform);
  906.                 $conditions      = [];
  907.                 $relationColumns $relation['isOwningSide']
  908.                     ? $assoc['joinTable']['joinColumns']
  909.                     : $assoc['joinTable']['inverseJoinColumns'];
  910.                 foreach ($relationColumns as $joinColumn) {
  911.                     $quotedSourceColumn $this->quoteStrategy->getJoinColumnName($joinColumn$targetClass$this->platform);
  912.                     $quotedTargetColumn $this->quoteStrategy->getReferencedJoinColumnName($joinColumn$targetClass$this->platform);
  913.                     $conditions[] = $sourceTableAlias '.' $quotedTargetColumn ' = ' $joinTableAlias '.' $quotedSourceColumn;
  914.                 }
  915.                 $sql .= $joinTableName ' ' $joinTableAlias ' ON ' implode(' AND '$conditions);
  916.                 // Join target table
  917.                 $sql .= $joinType === AST\Join::JOIN_TYPE_LEFT || $joinType === AST\Join::JOIN_TYPE_LEFTOUTER ' LEFT JOIN ' ' INNER JOIN ';
  918.                 $conditions      = [];
  919.                 $relationColumns $relation['isOwningSide']
  920.                     ? $assoc['joinTable']['inverseJoinColumns']
  921.                     : $assoc['joinTable']['joinColumns'];
  922.                 foreach ($relationColumns as $joinColumn) {
  923.                     $quotedSourceColumn $this->quoteStrategy->getJoinColumnName($joinColumn$targetClass$this->platform);
  924.                     $quotedTargetColumn $this->quoteStrategy->getReferencedJoinColumnName($joinColumn$targetClass$this->platform);
  925.                     $conditions[] = $targetTableAlias '.' $quotedTargetColumn ' = ' $joinTableAlias '.' $quotedSourceColumn;
  926.                 }
  927.                 // Apply remaining inheritance restrictions
  928.                 $discrSql $this->generateDiscriminatorColumnConditionSQL([$joinedDqlAlias]);
  929.                 if ($discrSql) {
  930.                     $conditions[] = $discrSql;
  931.                 }
  932.                 // Apply the filters
  933.                 $filterExpr $this->generateFilterConditionSQL($targetClass$targetTableAlias);
  934.                 if ($filterExpr) {
  935.                     $conditions[] = $filterExpr;
  936.                 }
  937.                 $targetTableJoin = [
  938.                     'table' => $targetTableName ' ' $targetTableAlias,
  939.                     'condition' => implode(' AND '$conditions),
  940.                 ];
  941.                 break;
  942.             default:
  943.                 throw new BadMethodCallException('Type of association must be one of *_TO_ONE or MANY_TO_MANY');
  944.         }
  945.         // Handle WITH clause
  946.         $withCondition $condExpr === null '' : ('(' $this->walkConditionalExpression($condExpr) . ')');
  947.         if ($targetClass->isInheritanceTypeJoined()) {
  948.             $ctiJoins $this->generateClassTableInheritanceJoins($targetClass$joinedDqlAlias);
  949.             // If we have WITH condition, we need to build nested joins for target class table and cti joins
  950.             if ($withCondition && $ctiJoins) {
  951.                 $sql .= '(' $targetTableJoin['table'] . $ctiJoins ') ON ' $targetTableJoin['condition'];
  952.             } else {
  953.                 $sql .= $targetTableJoin['table'] . ' ON ' $targetTableJoin['condition'] . $ctiJoins;
  954.             }
  955.         } else {
  956.             $sql .= $targetTableJoin['table'] . ' ON ' $targetTableJoin['condition'];
  957.         }
  958.         if ($withCondition) {
  959.             $sql .= ' AND ' $withCondition;
  960.         }
  961.         // Apply the indexes
  962.         if ($indexBy) {
  963.             // For Many-To-One or One-To-One associations this obviously makes no sense, but is ignored silently.
  964.             $this->walkIndexBy($indexBy);
  965.         } elseif (isset($relation['indexBy'])) {
  966.             $this->rsm->addIndexBy($joinedDqlAlias$relation['indexBy']);
  967.         }
  968.         return $sql;
  969.     }
  970.     /**
  971.      * Walks down a FunctionNode AST node, thereby generating the appropriate SQL.
  972.      *
  973.      * @param AST\Functions\FunctionNode $function
  974.      *
  975.      * @return string
  976.      *
  977.      * @not-deprecated
  978.      */
  979.     public function walkFunction($function)
  980.     {
  981.         return $function->getSql($this);
  982.     }
  983.     /**
  984.      * Walks down an OrderByClause AST node, thereby generating the appropriate SQL.
  985.      *
  986.      * @param AST\OrderByClause $orderByClause
  987.      *
  988.      * @return string
  989.      *
  990.      * @not-deprecated
  991.      */
  992.     public function walkOrderByClause($orderByClause)
  993.     {
  994.         $orderByItems array_map([$this'walkOrderByItem'], $orderByClause->orderByItems);
  995.         $collectionOrderByItems $this->generateOrderedCollectionOrderByItems();
  996.         if ($collectionOrderByItems !== '') {
  997.             $orderByItems array_merge($orderByItems, (array) $collectionOrderByItems);
  998.         }
  999.         return ' ORDER BY ' implode(', '$orderByItems);
  1000.     }
  1001.     /**
  1002.      * Walks down an OrderByItem AST node, thereby generating the appropriate SQL.
  1003.      *
  1004.      * @param AST\OrderByItem $orderByItem
  1005.      *
  1006.      * @return string
  1007.      *
  1008.      * @not-deprecated
  1009.      */
  1010.     public function walkOrderByItem($orderByItem)
  1011.     {
  1012.         $type strtoupper($orderByItem->type);
  1013.         $expr $orderByItem->expression;
  1014.         $sql  $expr instanceof AST\Node
  1015.             $expr->dispatch($this)
  1016.             : $this->walkResultVariable($this->queryComponents[$expr]['token']->value);
  1017.         $this->orderedColumnsMap[$sql] = $type;
  1018.         if ($expr instanceof AST\Subselect) {
  1019.             return '(' $sql ') ' $type;
  1020.         }
  1021.         return $sql ' ' $type;
  1022.     }
  1023.     /**
  1024.      * Walks down a HavingClause AST node, thereby generating the appropriate SQL.
  1025.      *
  1026.      * @param AST\HavingClause $havingClause
  1027.      *
  1028.      * @return string The SQL.
  1029.      *
  1030.      * @not-deprecated
  1031.      */
  1032.     public function walkHavingClause($havingClause)
  1033.     {
  1034.         return ' HAVING ' $this->walkConditionalExpression($havingClause->conditionalExpression);
  1035.     }
  1036.     /**
  1037.      * Walks down a Join AST node and creates the corresponding SQL.
  1038.      *
  1039.      * @param AST\Join $join
  1040.      *
  1041.      * @return string
  1042.      *
  1043.      * @not-deprecated
  1044.      */
  1045.     public function walkJoin($join)
  1046.     {
  1047.         $joinType        $join->joinType;
  1048.         $joinDeclaration $join->joinAssociationDeclaration;
  1049.         $sql $joinType === AST\Join::JOIN_TYPE_LEFT || $joinType === AST\Join::JOIN_TYPE_LEFTOUTER
  1050.             ' LEFT JOIN '
  1051.             ' INNER JOIN ';
  1052.         switch (true) {
  1053.             case $joinDeclaration instanceof AST\RangeVariableDeclaration:
  1054.                 $class      $this->em->getClassMetadata($joinDeclaration->abstractSchemaName);
  1055.                 $dqlAlias   $joinDeclaration->aliasIdentificationVariable;
  1056.                 $tableAlias $this->getSQLTableAlias($class->table['name'], $dqlAlias);
  1057.                 $conditions = [];
  1058.                 if ($join->conditionalExpression) {
  1059.                     $conditions[] = '(' $this->walkConditionalExpression($join->conditionalExpression) . ')';
  1060.                 }
  1061.                 $isUnconditionalJoin $conditions === [];
  1062.                 $condExprConjunction $class->isInheritanceTypeJoined() && $joinType !== AST\Join::JOIN_TYPE_LEFT && $joinType !== AST\Join::JOIN_TYPE_LEFTOUTER && $isUnconditionalJoin
  1063.                     ' AND '
  1064.                     ' ON ';
  1065.                 $sql .= $this->generateRangeVariableDeclarationSQL($joinDeclaration, ! $isUnconditionalJoin);
  1066.                 // Apply remaining inheritance restrictions
  1067.                 $discrSql $this->generateDiscriminatorColumnConditionSQL([$dqlAlias]);
  1068.                 if ($discrSql) {
  1069.                     $conditions[] = $discrSql;
  1070.                 }
  1071.                 // Apply the filters
  1072.                 $filterExpr $this->generateFilterConditionSQL($class$tableAlias);
  1073.                 if ($filterExpr) {
  1074.                     $conditions[] = $filterExpr;
  1075.                 }
  1076.                 if ($conditions) {
  1077.                     $sql .= $condExprConjunction implode(' AND '$conditions);
  1078.                 }
  1079.                 break;
  1080.             case $joinDeclaration instanceof AST\JoinAssociationDeclaration:
  1081.                 $sql .= $this->walkJoinAssociationDeclaration($joinDeclaration$joinType$join->conditionalExpression);
  1082.                 break;
  1083.         }
  1084.         return $sql;
  1085.     }
  1086.     /**
  1087.      * Walks down a CoalesceExpression AST node and generates the corresponding SQL.
  1088.      *
  1089.      * @param AST\CoalesceExpression $coalesceExpression
  1090.      *
  1091.      * @return string The SQL.
  1092.      *
  1093.      * @not-deprecated
  1094.      */
  1095.     public function walkCoalesceExpression($coalesceExpression)
  1096.     {
  1097.         $sql 'COALESCE(';
  1098.         $scalarExpressions = [];
  1099.         foreach ($coalesceExpression->scalarExpressions as $scalarExpression) {
  1100.             $scalarExpressions[] = $this->walkSimpleArithmeticExpression($scalarExpression);
  1101.         }
  1102.         return $sql implode(', '$scalarExpressions) . ')';
  1103.     }
  1104.     /**
  1105.      * Walks down a NullIfExpression AST node and generates the corresponding SQL.
  1106.      *
  1107.      * @param AST\NullIfExpression $nullIfExpression
  1108.      *
  1109.      * @return string The SQL.
  1110.      *
  1111.      * @not-deprecated
  1112.      */
  1113.     public function walkNullIfExpression($nullIfExpression)
  1114.     {
  1115.         $firstExpression is_string($nullIfExpression->firstExpression)
  1116.             ? $this->conn->quote($nullIfExpression->firstExpression)
  1117.             : $this->walkSimpleArithmeticExpression($nullIfExpression->firstExpression);
  1118.         $secondExpression is_string($nullIfExpression->secondExpression)
  1119.             ? $this->conn->quote($nullIfExpression->secondExpression)
  1120.             : $this->walkSimpleArithmeticExpression($nullIfExpression->secondExpression);
  1121.         return 'NULLIF(' $firstExpression ', ' $secondExpression ')';
  1122.     }
  1123.     /**
  1124.      * Walks down a GeneralCaseExpression AST node and generates the corresponding SQL.
  1125.      *
  1126.      * @return string The SQL.
  1127.      *
  1128.      * @not-deprecated
  1129.      */
  1130.     public function walkGeneralCaseExpression(AST\GeneralCaseExpression $generalCaseExpression)
  1131.     {
  1132.         $sql 'CASE';
  1133.         foreach ($generalCaseExpression->whenClauses as $whenClause) {
  1134.             $sql .= ' WHEN ' $this->walkConditionalExpression($whenClause->caseConditionExpression);
  1135.             $sql .= ' THEN ' $this->walkSimpleArithmeticExpression($whenClause->thenScalarExpression);
  1136.         }
  1137.         $sql .= ' ELSE ' $this->walkSimpleArithmeticExpression($generalCaseExpression->elseScalarExpression) . ' END';
  1138.         return $sql;
  1139.     }
  1140.     /**
  1141.      * Walks down a SimpleCaseExpression AST node and generates the corresponding SQL.
  1142.      *
  1143.      * @param AST\SimpleCaseExpression $simpleCaseExpression
  1144.      *
  1145.      * @return string The SQL.
  1146.      *
  1147.      * @not-deprecated
  1148.      */
  1149.     public function walkSimpleCaseExpression($simpleCaseExpression)
  1150.     {
  1151.         $sql 'CASE ' $this->walkStateFieldPathExpression($simpleCaseExpression->caseOperand);
  1152.         foreach ($simpleCaseExpression->simpleWhenClauses as $simpleWhenClause) {
  1153.             $sql .= ' WHEN ' $this->walkSimpleArithmeticExpression($simpleWhenClause->caseScalarExpression);
  1154.             $sql .= ' THEN ' $this->walkSimpleArithmeticExpression($simpleWhenClause->thenScalarExpression);
  1155.         }
  1156.         $sql .= ' ELSE ' $this->walkSimpleArithmeticExpression($simpleCaseExpression->elseScalarExpression) . ' END';
  1157.         return $sql;
  1158.     }
  1159.     /**
  1160.      * Walks down a SelectExpression AST node and generates the corresponding SQL.
  1161.      *
  1162.      * @param AST\SelectExpression $selectExpression
  1163.      *
  1164.      * @return string
  1165.      *
  1166.      * @not-deprecated
  1167.      */
  1168.     public function walkSelectExpression($selectExpression)
  1169.     {
  1170.         $sql    '';
  1171.         $expr   $selectExpression->expression;
  1172.         $hidden $selectExpression->hiddenAliasResultVariable;
  1173.         switch (true) {
  1174.             case $expr instanceof AST\PathExpression:
  1175.                 if ($expr->type !== AST\PathExpression::TYPE_STATE_FIELD) {
  1176.                     throw QueryException::invalidPathExpression($expr);
  1177.                 }
  1178.                 assert($expr->field !== null);
  1179.                 $fieldName $expr->field;
  1180.                 $dqlAlias  $expr->identificationVariable;
  1181.                 $class     $this->getMetadataForDqlAlias($dqlAlias);
  1182.                 $resultAlias $selectExpression->fieldIdentificationVariable ?: $fieldName;
  1183.                 $tableName   $class->isInheritanceTypeJoined()
  1184.                     ? $this->em->getUnitOfWork()->getEntityPersister($class->name)->getOwningTable($fieldName)
  1185.                     : $class->getTableName();
  1186.                 $sqlTableAlias $this->getSQLTableAlias($tableName$dqlAlias);
  1187.                 $fieldMapping  $class->fieldMappings[$fieldName];
  1188.                 $columnName    $this->quoteStrategy->getColumnName($fieldName$class$this->platform);
  1189.                 $columnAlias   $this->getSQLColumnAlias($fieldMapping['columnName']);
  1190.                 $col           $sqlTableAlias '.' $columnName;
  1191.                 if (isset($fieldMapping['requireSQLConversion'])) {
  1192.                     $type Type::getType($fieldMapping['type']);
  1193.                     $col  $type->convertToPHPValueSQL($col$this->conn->getDatabasePlatform());
  1194.                 }
  1195.                 $sql .= $col ' AS ' $columnAlias;
  1196.                 $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
  1197.                 if (! $hidden) {
  1198.                     $this->rsm->addScalarResult($columnAlias$resultAlias$fieldMapping['type']);
  1199.                     $this->scalarFields[$dqlAlias][$fieldName] = $columnAlias;
  1200.                     if (! empty($fieldMapping['enumType'])) {
  1201.                         $this->rsm->addEnumResult($columnAlias$fieldMapping['enumType']);
  1202.                     }
  1203.                 }
  1204.                 break;
  1205.             case $expr instanceof AST\AggregateExpression:
  1206.             case $expr instanceof AST\Functions\FunctionNode:
  1207.             case $expr instanceof AST\SimpleArithmeticExpression:
  1208.             case $expr instanceof AST\ArithmeticTerm:
  1209.             case $expr instanceof AST\ArithmeticFactor:
  1210.             case $expr instanceof AST\ParenthesisExpression:
  1211.             case $expr instanceof AST\Literal:
  1212.             case $expr instanceof AST\NullIfExpression:
  1213.             case $expr instanceof AST\CoalesceExpression:
  1214.             case $expr instanceof AST\GeneralCaseExpression:
  1215.             case $expr instanceof AST\SimpleCaseExpression:
  1216.                 $columnAlias $this->getSQLColumnAlias('sclr');
  1217.                 $resultAlias $selectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
  1218.                 $sql .= $expr->dispatch($this) . ' AS ' $columnAlias;
  1219.                 $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
  1220.                 if ($hidden) {
  1221.                     break;
  1222.                 }
  1223.                 if (! $expr instanceof Query\AST\TypedExpression) {
  1224.                     // Conceptually we could resolve field type here by traverse through AST to retrieve field type,
  1225.                     // but this is not a feasible solution; assume 'string'.
  1226.                     $this->rsm->addScalarResult($columnAlias$resultAlias'string');
  1227.                     break;
  1228.                 }
  1229.                 $this->rsm->addScalarResult($columnAlias$resultAliasType::getTypeRegistry()->lookupName($expr->getReturnType()));
  1230.                 break;
  1231.             case $expr instanceof AST\Subselect:
  1232.                 $columnAlias $this->getSQLColumnAlias('sclr');
  1233.                 $resultAlias $selectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
  1234.                 $sql .= '(' $this->walkSubselect($expr) . ') AS ' $columnAlias;
  1235.                 $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
  1236.                 if (! $hidden) {
  1237.                     // We cannot resolve field type here; assume 'string'.
  1238.                     $this->rsm->addScalarResult($columnAlias$resultAlias'string');
  1239.                 }
  1240.                 break;
  1241.             case $expr instanceof AST\NewObjectExpression:
  1242.                 $sql .= $this->walkNewObject($expr$selectExpression->fieldIdentificationVariable);
  1243.                 break;
  1244.             default:
  1245.                 // IdentificationVariable or PartialObjectExpression
  1246.                 if ($expr instanceof AST\PartialObjectExpression) {
  1247.                     $this->query->setHint(self::HINT_PARTIALtrue);
  1248.                     $dqlAlias        $expr->identificationVariable;
  1249.                     $partialFieldSet $expr->partialFieldSet;
  1250.                 } else {
  1251.                     $dqlAlias        $expr;
  1252.                     $partialFieldSet = [];
  1253.                 }
  1254.                 $class       $this->getMetadataForDqlAlias($dqlAlias);
  1255.                 $resultAlias $selectExpression->fieldIdentificationVariable ?: null;
  1256.                 if (! isset($this->selectedClasses[$dqlAlias])) {
  1257.                     $this->selectedClasses[$dqlAlias] = [
  1258.                         'class'       => $class,
  1259.                         'dqlAlias'    => $dqlAlias,
  1260.                         'resultAlias' => $resultAlias,
  1261.                     ];
  1262.                 }
  1263.                 $sqlParts = [];
  1264.                 // Select all fields from the queried class
  1265.                 foreach ($class->fieldMappings as $fieldName => $mapping) {
  1266.                     if ($partialFieldSet && ! in_array($fieldName$partialFieldSettrue)) {
  1267.                         continue;
  1268.                     }
  1269.                     $tableName = isset($mapping['inherited'])
  1270.                         ? $this->em->getClassMetadata($mapping['inherited'])->getTableName()
  1271.                         : $class->getTableName();
  1272.                     $sqlTableAlias    $this->getSQLTableAlias($tableName$dqlAlias);
  1273.                     $columnAlias      $this->getSQLColumnAlias($mapping['columnName']);
  1274.                     $quotedColumnName $this->quoteStrategy->getColumnName($fieldName$class$this->platform);
  1275.                     $col $sqlTableAlias '.' $quotedColumnName;
  1276.                     if (isset($mapping['requireSQLConversion'])) {
  1277.                         $type Type::getType($mapping['type']);
  1278.                         $col  $type->convertToPHPValueSQL($col$this->platform);
  1279.                     }
  1280.                     $sqlParts[] = $col ' AS ' $columnAlias;
  1281.                     $this->scalarResultAliasMap[$resultAlias][] = $columnAlias;
  1282.                     $this->rsm->addFieldResult($dqlAlias$columnAlias$fieldName$class->name);
  1283.                     if (! empty($mapping['enumType'])) {
  1284.                         $this->rsm->addEnumResult($columnAlias$mapping['enumType']);
  1285.                     }
  1286.                 }
  1287.                 // Add any additional fields of subclasses (excluding inherited fields)
  1288.                 // 1) on Single Table Inheritance: always, since its marginal overhead
  1289.                 // 2) on Class Table Inheritance only if partial objects are disallowed,
  1290.                 //    since it requires outer joining subtables.
  1291.                 if ($class->isInheritanceTypeSingleTable() || ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
  1292.                     foreach ($class->subClasses as $subClassName) {
  1293.                         $subClass      $this->em->getClassMetadata($subClassName);
  1294.                         $sqlTableAlias $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
  1295.                         foreach ($subClass->fieldMappings as $fieldName => $mapping) {
  1296.                             if (isset($mapping['inherited']) || ($partialFieldSet && ! in_array($fieldName$partialFieldSettrue))) {
  1297.                                 continue;
  1298.                             }
  1299.                             $columnAlias      $this->getSQLColumnAlias($mapping['columnName']);
  1300.                             $quotedColumnName $this->quoteStrategy->getColumnName($fieldName$subClass$this->platform);
  1301.                             $col $sqlTableAlias '.' $quotedColumnName;
  1302.                             if (isset($mapping['requireSQLConversion'])) {
  1303.                                 $type Type::getType($mapping['type']);
  1304.                                 $col  $type->convertToPHPValueSQL($col$this->platform);
  1305.                             }
  1306.                             $sqlParts[] = $col ' AS ' $columnAlias;
  1307.                             $this->scalarResultAliasMap[$resultAlias][] = $columnAlias;
  1308.                             $this->rsm->addFieldResult($dqlAlias$columnAlias$fieldName$subClassName);
  1309.                         }
  1310.                     }
  1311.                 }
  1312.                 $sql .= implode(', '$sqlParts);
  1313.         }
  1314.         return $sql;
  1315.     }
  1316.     /**
  1317.      * Walks down a QuantifiedExpression AST node, thereby generating the appropriate SQL.
  1318.      *
  1319.      * @param AST\QuantifiedExpression $qExpr
  1320.      *
  1321.      * @return string
  1322.      *
  1323.      * @not-deprecated
  1324.      */
  1325.     public function walkQuantifiedExpression($qExpr)
  1326.     {
  1327.         return ' ' strtoupper($qExpr->type) . '(' $this->walkSubselect($qExpr->subselect) . ')';
  1328.     }
  1329.     /**
  1330.      * Walks down a Subselect AST node, thereby generating the appropriate SQL.
  1331.      *
  1332.      * @param AST\Subselect $subselect
  1333.      *
  1334.      * @return string
  1335.      *
  1336.      * @not-deprecated
  1337.      */
  1338.     public function walkSubselect($subselect)
  1339.     {
  1340.         $useAliasesBefore  $this->useSqlTableAliases;
  1341.         $rootAliasesBefore $this->rootAliases;
  1342.         $this->rootAliases        = []; // reset the rootAliases for the subselect
  1343.         $this->useSqlTableAliases true;
  1344.         $sql  $this->walkSimpleSelectClause($subselect->simpleSelectClause);
  1345.         $sql .= $this->walkSubselectFromClause($subselect->subselectFromClause);
  1346.         $sql .= $this->walkWhereClause($subselect->whereClause);
  1347.         $sql .= $subselect->groupByClause $this->walkGroupByClause($subselect->groupByClause) : '';
  1348.         $sql .= $subselect->havingClause $this->walkHavingClause($subselect->havingClause) : '';
  1349.         $sql .= $subselect->orderByClause $this->walkOrderByClause($subselect->orderByClause) : '';
  1350.         $this->rootAliases        $rootAliasesBefore// put the main aliases back
  1351.         $this->useSqlTableAliases $useAliasesBefore;
  1352.         return $sql;
  1353.     }
  1354.     /**
  1355.      * Walks down a SubselectFromClause AST node, thereby generating the appropriate SQL.
  1356.      *
  1357.      * @param AST\SubselectFromClause $subselectFromClause
  1358.      *
  1359.      * @return string
  1360.      *
  1361.      * @not-deprecated
  1362.      */
  1363.     public function walkSubselectFromClause($subselectFromClause)
  1364.     {
  1365.         $identificationVarDecls $subselectFromClause->identificationVariableDeclarations;
  1366.         $sqlParts               = [];
  1367.         foreach ($identificationVarDecls as $subselectIdVarDecl) {
  1368.             $sqlParts[] = $this->walkIdentificationVariableDeclaration($subselectIdVarDecl);
  1369.         }
  1370.         return ' FROM ' implode(', '$sqlParts);
  1371.     }
  1372.     /**
  1373.      * Walks down a SimpleSelectClause AST node, thereby generating the appropriate SQL.
  1374.      *
  1375.      * @param AST\SimpleSelectClause $simpleSelectClause
  1376.      *
  1377.      * @return string
  1378.      *
  1379.      * @not-deprecated
  1380.      */
  1381.     public function walkSimpleSelectClause($simpleSelectClause)
  1382.     {
  1383.         return 'SELECT' . ($simpleSelectClause->isDistinct ' DISTINCT' '')
  1384.             . $this->walkSimpleSelectExpression($simpleSelectClause->simpleSelectExpression);
  1385.     }
  1386.     /** @return string */
  1387.     public function walkParenthesisExpression(AST\ParenthesisExpression $parenthesisExpression)
  1388.     {
  1389.         return sprintf('(%s)'$parenthesisExpression->expression->dispatch($this));
  1390.     }
  1391.     /**
  1392.      * @param AST\NewObjectExpression $newObjectExpression
  1393.      * @param string|null             $newObjectResultAlias
  1394.      *
  1395.      * @return string The SQL.
  1396.      */
  1397.     public function walkNewObject($newObjectExpression$newObjectResultAlias null)
  1398.     {
  1399.         $sqlSelectExpressions = [];
  1400.         $objIndex             $newObjectResultAlias ?: $this->newObjectCounter++;
  1401.         foreach ($newObjectExpression->args as $argIndex => $e) {
  1402.             $resultAlias $this->scalarResultCounter++;
  1403.             $columnAlias $this->getSQLColumnAlias('sclr');
  1404.             $fieldType   'string';
  1405.             switch (true) {
  1406.                 case $e instanceof AST\NewObjectExpression:
  1407.                     $sqlSelectExpressions[] = $e->dispatch($this);
  1408.                     break;
  1409.                 case $e instanceof AST\Subselect:
  1410.                     $sqlSelectExpressions[] = '(' $e->dispatch($this) . ') AS ' $columnAlias;
  1411.                     break;
  1412.                 case $e instanceof AST\PathExpression:
  1413.                     assert($e->field !== null);
  1414.                     $dqlAlias     $e->identificationVariable;
  1415.                     $class        $this->getMetadataForDqlAlias($dqlAlias);
  1416.                     $fieldName    $e->field;
  1417.                     $fieldMapping $class->fieldMappings[$fieldName];
  1418.                     $fieldType    $fieldMapping['type'];
  1419.                     $col          trim($e->dispatch($this));
  1420.                     if (isset($fieldMapping['requireSQLConversion'])) {
  1421.                         $type Type::getType($fieldType);
  1422.                         $col  $type->convertToPHPValueSQL($col$this->platform);
  1423.                     }
  1424.                     $sqlSelectExpressions[] = $col ' AS ' $columnAlias;
  1425.                     if (! empty($fieldMapping['enumType'])) {
  1426.                         $this->rsm->addEnumResult($columnAlias$fieldMapping['enumType']);
  1427.                     }
  1428.                     break;
  1429.                 case $e instanceof AST\Literal:
  1430.                     switch ($e->type) {
  1431.                         case AST\Literal::BOOLEAN:
  1432.                             $fieldType 'boolean';
  1433.                             break;
  1434.                         case AST\Literal::NUMERIC:
  1435.                             $fieldType is_float($e->value) ? 'float' 'integer';
  1436.                             break;
  1437.                     }
  1438.                     $sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' $columnAlias;
  1439.                     break;
  1440.                 default:
  1441.                     $sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' $columnAlias;
  1442.                     break;
  1443.             }
  1444.             $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
  1445.             $this->rsm->addScalarResult($columnAlias$resultAlias$fieldType);
  1446.             $this->rsm->newObjectMappings[$columnAlias] = [
  1447.                 'className' => $newObjectExpression->className,
  1448.                 'objIndex'  => $objIndex,
  1449.                 'argIndex'  => $argIndex,
  1450.             ];
  1451.         }
  1452.         return implode(', '$sqlSelectExpressions);
  1453.     }
  1454.     /**
  1455.      * Walks down a SimpleSelectExpression AST node, thereby generating the appropriate SQL.
  1456.      *
  1457.      * @param AST\SimpleSelectExpression $simpleSelectExpression
  1458.      *
  1459.      * @return string
  1460.      *
  1461.      * @not-deprecated
  1462.      */
  1463.     public function walkSimpleSelectExpression($simpleSelectExpression)
  1464.     {
  1465.         $expr $simpleSelectExpression->expression;
  1466.         $sql  ' ';
  1467.         switch (true) {
  1468.             case $expr instanceof AST\PathExpression:
  1469.                 $sql .= $this->walkPathExpression($expr);
  1470.                 break;
  1471.             case $expr instanceof AST\Subselect:
  1472.                 $alias $simpleSelectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
  1473.                 $columnAlias                        'sclr' $this->aliasCounter++;
  1474.                 $this->scalarResultAliasMap[$alias] = $columnAlias;
  1475.                 $sql .= '(' $this->walkSubselect($expr) . ') AS ' $columnAlias;
  1476.                 break;
  1477.             case $expr instanceof AST\Functions\FunctionNode:
  1478.             case $expr instanceof AST\SimpleArithmeticExpression:
  1479.             case $expr instanceof AST\ArithmeticTerm:
  1480.             case $expr instanceof AST\ArithmeticFactor:
  1481.             case $expr instanceof AST\Literal:
  1482.             case $expr instanceof AST\NullIfExpression:
  1483.             case $expr instanceof AST\CoalesceExpression:
  1484.             case $expr instanceof AST\GeneralCaseExpression:
  1485.             case $expr instanceof AST\SimpleCaseExpression:
  1486.                 $alias $simpleSelectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
  1487.                 $columnAlias                        $this->getSQLColumnAlias('sclr');
  1488.                 $this->scalarResultAliasMap[$alias] = $columnAlias;
  1489.                 $sql .= $expr->dispatch($this) . ' AS ' $columnAlias;
  1490.                 break;
  1491.             case $expr instanceof AST\ParenthesisExpression:
  1492.                 $sql .= $this->walkParenthesisExpression($expr);
  1493.                 break;
  1494.             default: // IdentificationVariable
  1495.                 $sql .= $this->walkEntityIdentificationVariable($expr);
  1496.                 break;
  1497.         }
  1498.         return $sql;
  1499.     }
  1500.     /**
  1501.      * Walks down an AggregateExpression AST node, thereby generating the appropriate SQL.
  1502.      *
  1503.      * @param AST\AggregateExpression $aggExpression
  1504.      *
  1505.      * @return string
  1506.      *
  1507.      * @not-deprecated
  1508.      */
  1509.     public function walkAggregateExpression($aggExpression)
  1510.     {
  1511.         return $aggExpression->functionName '(' . ($aggExpression->isDistinct 'DISTINCT ' '')
  1512.             . $this->walkSimpleArithmeticExpression($aggExpression->pathExpression) . ')';
  1513.     }
  1514.     /**
  1515.      * Walks down a GroupByClause AST node, thereby generating the appropriate SQL.
  1516.      *
  1517.      * @param AST\GroupByClause $groupByClause
  1518.      *
  1519.      * @return string
  1520.      *
  1521.      * @not-deprecated
  1522.      */
  1523.     public function walkGroupByClause($groupByClause)
  1524.     {
  1525.         $sqlParts = [];
  1526.         foreach ($groupByClause->groupByItems as $groupByItem) {
  1527.             $sqlParts[] = $this->walkGroupByItem($groupByItem);
  1528.         }
  1529.         return ' GROUP BY ' implode(', '$sqlParts);
  1530.     }
  1531.     /**
  1532.      * Walks down a GroupByItem AST node, thereby generating the appropriate SQL.
  1533.      *
  1534.      * @param AST\PathExpression|string $groupByItem
  1535.      *
  1536.      * @return string
  1537.      *
  1538.      * @not-deprecated
  1539.      */
  1540.     public function walkGroupByItem($groupByItem)
  1541.     {
  1542.         // StateFieldPathExpression
  1543.         if (! is_string($groupByItem)) {
  1544.             return $this->walkPathExpression($groupByItem);
  1545.         }
  1546.         // ResultVariable
  1547.         if (isset($this->queryComponents[$groupByItem]['resultVariable'])) {
  1548.             $resultVariable $this->queryComponents[$groupByItem]['resultVariable'];
  1549.             if ($resultVariable instanceof AST\PathExpression) {
  1550.                 return $this->walkPathExpression($resultVariable);
  1551.             }
  1552.             if ($resultVariable instanceof AST\Node && isset($resultVariable->pathExpression)) {
  1553.                 return $this->walkPathExpression($resultVariable->pathExpression);
  1554.             }
  1555.             return $this->walkResultVariable($groupByItem);
  1556.         }
  1557.         // IdentificationVariable
  1558.         $sqlParts = [];
  1559.         foreach ($this->getMetadataForDqlAlias($groupByItem)->fieldNames as $field) {
  1560.             $item       = new AST\PathExpression(AST\PathExpression::TYPE_STATE_FIELD$groupByItem$field);
  1561.             $item->type AST\PathExpression::TYPE_STATE_FIELD;
  1562.             $sqlParts[] = $this->walkPathExpression($item);
  1563.         }
  1564.         foreach ($this->getMetadataForDqlAlias($groupByItem)->associationMappings as $mapping) {
  1565.             if ($mapping['isOwningSide'] && $mapping['type'] & ClassMetadata::TO_ONE) {
  1566.                 $item       = new AST\PathExpression(AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION$groupByItem$mapping['fieldName']);
  1567.                 $item->type AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
  1568.                 $sqlParts[] = $this->walkPathExpression($item);
  1569.             }
  1570.         }
  1571.         return implode(', '$sqlParts);
  1572.     }
  1573.     /**
  1574.      * Walks down a DeleteClause AST node, thereby generating the appropriate SQL.
  1575.      *
  1576.      * @return string
  1577.      *
  1578.      * @not-deprecated
  1579.      */
  1580.     public function walkDeleteClause(AST\DeleteClause $deleteClause)
  1581.     {
  1582.         $class     $this->em->getClassMetadata($deleteClause->abstractSchemaName);
  1583.         $tableName $class->getTableName();
  1584.         $sql       'DELETE FROM ' $this->quoteStrategy->getTableName($class$this->platform);
  1585.         $this->setSQLTableAlias($tableName$tableName$deleteClause->aliasIdentificationVariable);
  1586.         $this->rootAliases[] = $deleteClause->aliasIdentificationVariable;
  1587.         return $sql;
  1588.     }
  1589.     /**
  1590.      * Walks down an UpdateClause AST node, thereby generating the appropriate SQL.
  1591.      *
  1592.      * @param AST\UpdateClause $updateClause
  1593.      *
  1594.      * @return string
  1595.      *
  1596.      * @not-deprecated
  1597.      */
  1598.     public function walkUpdateClause($updateClause)
  1599.     {
  1600.         $class     $this->em->getClassMetadata($updateClause->abstractSchemaName);
  1601.         $tableName $class->getTableName();
  1602.         $sql       'UPDATE ' $this->quoteStrategy->getTableName($class$this->platform);
  1603.         $this->setSQLTableAlias($tableName$tableName$updateClause->aliasIdentificationVariable);
  1604.         $this->rootAliases[] = $updateClause->aliasIdentificationVariable;
  1605.         return $sql ' SET ' implode(', 'array_map([$this'walkUpdateItem'], $updateClause->updateItems));
  1606.     }
  1607.     /**
  1608.      * Walks down an UpdateItem AST node, thereby generating the appropriate SQL.
  1609.      *
  1610.      * @param AST\UpdateItem $updateItem
  1611.      *
  1612.      * @return string
  1613.      *
  1614.      * @not-deprecated
  1615.      */
  1616.     public function walkUpdateItem($updateItem)
  1617.     {
  1618.         $useTableAliasesBefore    $this->useSqlTableAliases;
  1619.         $this->useSqlTableAliases false;
  1620.         $sql      $this->walkPathExpression($updateItem->pathExpression) . ' = ';
  1621.         $newValue $updateItem->newValue;
  1622.         switch (true) {
  1623.             case $newValue instanceof AST\Node:
  1624.                 $sql .= $newValue->dispatch($this);
  1625.                 break;
  1626.             case $newValue === null:
  1627.                 $sql .= 'NULL';
  1628.                 break;
  1629.             default:
  1630.                 $sql .= $this->conn->quote($newValue);
  1631.                 break;
  1632.         }
  1633.         $this->useSqlTableAliases $useTableAliasesBefore;
  1634.         return $sql;
  1635.     }
  1636.     /**
  1637.      * Walks down a WhereClause AST node, thereby generating the appropriate SQL.
  1638.      * WhereClause or not, the appropriate discriminator sql is added.
  1639.      *
  1640.      * @param AST\WhereClause $whereClause
  1641.      *
  1642.      * @return string
  1643.      *
  1644.      * @not-deprecated
  1645.      */
  1646.     public function walkWhereClause($whereClause)
  1647.     {
  1648.         $condSql  $whereClause !== null $this->walkConditionalExpression($whereClause->conditionalExpression) : '';
  1649.         $discrSql $this->generateDiscriminatorColumnConditionSQL($this->rootAliases);
  1650.         if ($this->em->hasFilters()) {
  1651.             $filterClauses = [];
  1652.             foreach ($this->rootAliases as $dqlAlias) {
  1653.                 $class      $this->getMetadataForDqlAlias($dqlAlias);
  1654.                 $tableAlias $this->getSQLTableAlias($class->table['name'], $dqlAlias);
  1655.                 $filterExpr $this->generateFilterConditionSQL($class$tableAlias);
  1656.                 if ($filterExpr) {
  1657.                     $filterClauses[] = $filterExpr;
  1658.                 }
  1659.             }
  1660.             if (count($filterClauses)) {
  1661.                 if ($condSql) {
  1662.                     $condSql '(' $condSql ') AND ';
  1663.                 }
  1664.                 $condSql .= implode(' AND '$filterClauses);
  1665.             }
  1666.         }
  1667.         if ($condSql) {
  1668.             return ' WHERE ' . (! $discrSql $condSql '(' $condSql ') AND ' $discrSql);
  1669.         }
  1670.         if ($discrSql) {
  1671.             return ' WHERE ' $discrSql;
  1672.         }
  1673.         return '';
  1674.     }
  1675.     /**
  1676.      * Walk down a ConditionalExpression AST node, thereby generating the appropriate SQL.
  1677.      *
  1678.      * @param AST\ConditionalExpression $condExpr
  1679.      *
  1680.      * @return string
  1681.      *
  1682.      * @not-deprecated
  1683.      */
  1684.     public function walkConditionalExpression($condExpr)
  1685.     {
  1686.         // Phase 2 AST optimization: Skip processing of ConditionalExpression
  1687.         // if only one ConditionalTerm is defined
  1688.         if (! ($condExpr instanceof AST\ConditionalExpression)) {
  1689.             return $this->walkConditionalTerm($condExpr);
  1690.         }
  1691.         return implode(' OR 'array_map([$this'walkConditionalTerm'], $condExpr->conditionalTerms));
  1692.     }
  1693.     /**
  1694.      * Walks down a ConditionalTerm AST node, thereby generating the appropriate SQL.
  1695.      *
  1696.      * @param AST\ConditionalTerm $condTerm
  1697.      *
  1698.      * @return string
  1699.      *
  1700.      * @not-deprecated
  1701.      */
  1702.     public function walkConditionalTerm($condTerm)
  1703.     {
  1704.         // Phase 2 AST optimization: Skip processing of ConditionalTerm
  1705.         // if only one ConditionalFactor is defined
  1706.         if (! ($condTerm instanceof AST\ConditionalTerm)) {
  1707.             return $this->walkConditionalFactor($condTerm);
  1708.         }
  1709.         return implode(' AND 'array_map([$this'walkConditionalFactor'], $condTerm->conditionalFactors));
  1710.     }
  1711.     /**
  1712.      * Walks down a ConditionalFactor AST node, thereby generating the appropriate SQL.
  1713.      *
  1714.      * @param AST\ConditionalFactor $factor
  1715.      *
  1716.      * @return string The SQL.
  1717.      *
  1718.      * @not-deprecated
  1719.      */
  1720.     public function walkConditionalFactor($factor)
  1721.     {
  1722.         // Phase 2 AST optimization: Skip processing of ConditionalFactor
  1723.         // if only one ConditionalPrimary is defined
  1724.         return ! ($factor instanceof AST\ConditionalFactor)
  1725.             ? $this->walkConditionalPrimary($factor)
  1726.             : ($factor->not 'NOT ' '') . $this->walkConditionalPrimary($factor->conditionalPrimary);
  1727.     }
  1728.     /**
  1729.      * Walks down a ConditionalPrimary AST node, thereby generating the appropriate SQL.
  1730.      *
  1731.      * @param AST\ConditionalPrimary $primary
  1732.      *
  1733.      * @return string
  1734.      *
  1735.      * @not-deprecated
  1736.      */
  1737.     public function walkConditionalPrimary($primary)
  1738.     {
  1739.         if ($primary->isSimpleConditionalExpression()) {
  1740.             return $primary->simpleConditionalExpression->dispatch($this);
  1741.         }
  1742.         if ($primary->isConditionalExpression()) {
  1743.             $condExpr $primary->conditionalExpression;
  1744.             return '(' $this->walkConditionalExpression($condExpr) . ')';
  1745.         }
  1746.     }
  1747.     /**
  1748.      * Walks down an ExistsExpression AST node, thereby generating the appropriate SQL.
  1749.      *
  1750.      * @param AST\ExistsExpression $existsExpr
  1751.      *
  1752.      * @return string
  1753.      *
  1754.      * @not-deprecated
  1755.      */
  1756.     public function walkExistsExpression($existsExpr)
  1757.     {
  1758.         $sql $existsExpr->not 'NOT ' '';
  1759.         $sql .= 'EXISTS (' $this->walkSubselect($existsExpr->subselect) . ')';
  1760.         return $sql;
  1761.     }
  1762.     /**
  1763.      * Walks down a CollectionMemberExpression AST node, thereby generating the appropriate SQL.
  1764.      *
  1765.      * @param AST\CollectionMemberExpression $collMemberExpr
  1766.      *
  1767.      * @return string
  1768.      *
  1769.      * @not-deprecated
  1770.      */
  1771.     public function walkCollectionMemberExpression($collMemberExpr)
  1772.     {
  1773.         $sql  $collMemberExpr->not 'NOT ' '';
  1774.         $sql .= 'EXISTS (SELECT 1 FROM ';
  1775.         $entityExpr   $collMemberExpr->entityExpression;
  1776.         $collPathExpr $collMemberExpr->collectionValuedPathExpression;
  1777.         assert($collPathExpr->field !== null);
  1778.         $fieldName $collPathExpr->field;
  1779.         $dqlAlias  $collPathExpr->identificationVariable;
  1780.         $class $this->getMetadataForDqlAlias($dqlAlias);
  1781.         switch (true) {
  1782.             // InputParameter
  1783.             case $entityExpr instanceof AST\InputParameter:
  1784.                 $dqlParamKey $entityExpr->name;
  1785.                 $entitySql   '?';
  1786.                 break;
  1787.             // SingleValuedAssociationPathExpression | IdentificationVariable
  1788.             case $entityExpr instanceof AST\PathExpression:
  1789.                 $entitySql $this->walkPathExpression($entityExpr);
  1790.                 break;
  1791.             default:
  1792.                 throw new BadMethodCallException('Not implemented');
  1793.         }
  1794.         $assoc $class->associationMappings[$fieldName];
  1795.         if ($assoc['type'] === ClassMetadata::ONE_TO_MANY) {
  1796.             $targetClass      $this->em->getClassMetadata($assoc['targetEntity']);
  1797.             $targetTableAlias $this->getSQLTableAlias($targetClass->getTableName());
  1798.             $sourceTableAlias $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
  1799.             $sql .= $this->quoteStrategy->getTableName($targetClass$this->platform) . ' ' $targetTableAlias ' WHERE ';
  1800.             $owningAssoc $targetClass->associationMappings[$assoc['mappedBy']];
  1801.             $sqlParts    = [];
  1802.             foreach ($owningAssoc['targetToSourceKeyColumns'] as $targetColumn => $sourceColumn) {
  1803.                 $targetColumn $this->quoteStrategy->getColumnName($class->fieldNames[$targetColumn], $class$this->platform);
  1804.                 $sqlParts[] = $sourceTableAlias '.' $targetColumn ' = ' $targetTableAlias '.' $sourceColumn;
  1805.             }
  1806.             foreach ($this->quoteStrategy->getIdentifierColumnNames($targetClass$this->platform) as $targetColumnName) {
  1807.                 if (isset($dqlParamKey)) {
  1808.                     $this->parserResult->addParameterMapping($dqlParamKey$this->sqlParamIndex++);
  1809.                 }
  1810.                 $sqlParts[] = $targetTableAlias '.' $targetColumnName ' = ' $entitySql;
  1811.             }
  1812.             $sql .= implode(' AND '$sqlParts);
  1813.         } else { // many-to-many
  1814.             $targetClass $this->em->getClassMetadata($assoc['targetEntity']);
  1815.             $owningAssoc $assoc['isOwningSide'] ? $assoc $targetClass->associationMappings[$assoc['mappedBy']];
  1816.             $joinTable   $owningAssoc['joinTable'];
  1817.             // SQL table aliases
  1818.             $joinTableAlias   $this->getSQLTableAlias($joinTable['name']);
  1819.             $sourceTableAlias $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
  1820.             $sql .= $this->quoteStrategy->getJoinTableName($owningAssoc$targetClass$this->platform) . ' ' $joinTableAlias ' WHERE ';
  1821.             $joinColumns $assoc['isOwningSide'] ? $joinTable['joinColumns'] : $joinTable['inverseJoinColumns'];
  1822.             $sqlParts    = [];
  1823.             foreach ($joinColumns as $joinColumn) {
  1824.                 $targetColumn $this->quoteStrategy->getColumnName($class->fieldNames[$joinColumn['referencedColumnName']], $class$this->platform);
  1825.                 $sqlParts[] = $joinTableAlias '.' $joinColumn['name'] . ' = ' $sourceTableAlias '.' $targetColumn;
  1826.             }
  1827.             $joinColumns $assoc['isOwningSide'] ? $joinTable['inverseJoinColumns'] : $joinTable['joinColumns'];
  1828.             foreach ($joinColumns as $joinColumn) {
  1829.                 if (isset($dqlParamKey)) {
  1830.                     $this->parserResult->addParameterMapping($dqlParamKey$this->sqlParamIndex++);
  1831.                 }
  1832.                 $sqlParts[] = $joinTableAlias '.' $joinColumn['name'] . ' IN (' $entitySql ')';
  1833.             }
  1834.             $sql .= implode(' AND '$sqlParts);
  1835.         }
  1836.         return $sql ')';
  1837.     }
  1838.     /**
  1839.      * Walks down an EmptyCollectionComparisonExpression AST node, thereby generating the appropriate SQL.
  1840.      *
  1841.      * @param AST\EmptyCollectionComparisonExpression $emptyCollCompExpr
  1842.      *
  1843.      * @return string
  1844.      *
  1845.      * @not-deprecated
  1846.      */
  1847.     public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr)
  1848.     {
  1849.         $sizeFunc                           = new AST\Functions\SizeFunction('size');
  1850.         $sizeFunc->collectionPathExpression $emptyCollCompExpr->expression;
  1851.         return $sizeFunc->getSql($this) . ($emptyCollCompExpr->not ' > 0' ' = 0');
  1852.     }
  1853.     /**
  1854.      * Walks down a NullComparisonExpression AST node, thereby generating the appropriate SQL.
  1855.      *
  1856.      * @param AST\NullComparisonExpression $nullCompExpr
  1857.      *
  1858.      * @return string
  1859.      *
  1860.      * @not-deprecated
  1861.      */
  1862.     public function walkNullComparisonExpression($nullCompExpr)
  1863.     {
  1864.         $expression $nullCompExpr->expression;
  1865.         $comparison ' IS' . ($nullCompExpr->not ' NOT' '') . ' NULL';
  1866.         // Handle ResultVariable
  1867.         if (is_string($expression) && isset($this->queryComponents[$expression]['resultVariable'])) {
  1868.             return $this->walkResultVariable($expression) . $comparison;
  1869.         }
  1870.         // Handle InputParameter mapping inclusion to ParserResult
  1871.         if ($expression instanceof AST\InputParameter) {
  1872.             return $this->walkInputParameter($expression) . $comparison;
  1873.         }
  1874.         return $expression->dispatch($this) . $comparison;
  1875.     }
  1876.     /**
  1877.      * Walks down an InExpression AST node, thereby generating the appropriate SQL.
  1878.      *
  1879.      * @deprecated Use {@see walkInListExpression()} or {@see walkInSubselectExpression()} instead.
  1880.      *
  1881.      * @param AST\InExpression $inExpr
  1882.      *
  1883.      * @return string
  1884.      */
  1885.     public function walkInExpression($inExpr)
  1886.     {
  1887.         Deprecation::triggerIfCalledFromOutside(
  1888.             'doctrine/orm',
  1889.             'https://github.com/doctrine/orm/pull/10267',
  1890.             '%s() is deprecated, call walkInListExpression() or walkInSubselectExpression() instead.',
  1891.             __METHOD__
  1892.         );
  1893.         if ($inExpr instanceof AST\InListExpression) {
  1894.             return $this->walkInListExpression($inExpr);
  1895.         }
  1896.         if ($inExpr instanceof AST\InSubselectExpression) {
  1897.             return $this->walkInSubselectExpression($inExpr);
  1898.         }
  1899.         $sql $this->walkArithmeticExpression($inExpr->expression) . ($inExpr->not ' NOT' '') . ' IN (';
  1900.         $sql .= $inExpr->subselect
  1901.             $this->walkSubselect($inExpr->subselect)
  1902.             : implode(', 'array_map([$this'walkInParameter'], $inExpr->literals));
  1903.         $sql .= ')';
  1904.         return $sql;
  1905.     }
  1906.     /**
  1907.      * Walks down an InExpression AST node, thereby generating the appropriate SQL.
  1908.      */
  1909.     public function walkInListExpression(AST\InListExpression $inExpr): string
  1910.     {
  1911.         return $this->walkArithmeticExpression($inExpr->expression)
  1912.             . ($inExpr->not ' NOT' '') . ' IN ('
  1913.             implode(', 'array_map([$this'walkInParameter'], $inExpr->literals))
  1914.             . ')';
  1915.     }
  1916.     /**
  1917.      * Walks down an InExpression AST node, thereby generating the appropriate SQL.
  1918.      */
  1919.     public function walkInSubselectExpression(AST\InSubselectExpression $inExpr): string
  1920.     {
  1921.         return $this->walkArithmeticExpression($inExpr->expression)
  1922.             . ($inExpr->not ' NOT' '') . ' IN ('
  1923.             $this->walkSubselect($inExpr->subselect)
  1924.             . ')';
  1925.     }
  1926.     /**
  1927.      * Walks down an InstanceOfExpression AST node, thereby generating the appropriate SQL.
  1928.      *
  1929.      * @param AST\InstanceOfExpression $instanceOfExpr
  1930.      *
  1931.      * @return string
  1932.      *
  1933.      * @throws QueryException
  1934.      *
  1935.      * @not-deprecated
  1936.      */
  1937.     public function walkInstanceOfExpression($instanceOfExpr)
  1938.     {
  1939.         $sql '';
  1940.         $dqlAlias   $instanceOfExpr->identificationVariable;
  1941.         $discrClass $class $this->getMetadataForDqlAlias($dqlAlias);
  1942.         if ($class->discriminatorColumn) {
  1943.             $discrClass $this->em->getClassMetadata($class->rootEntityName);
  1944.         }
  1945.         if ($this->useSqlTableAliases) {
  1946.             $sql .= $this->getSQLTableAlias($discrClass->getTableName(), $dqlAlias) . '.';
  1947.         }
  1948.         $sql .= $class->getDiscriminatorColumn()['name'] . ($instanceOfExpr->not ' NOT IN ' ' IN ');
  1949.         $sql .= $this->getChildDiscriminatorsFromClassMetadata($discrClass$instanceOfExpr);
  1950.         return $sql;
  1951.     }
  1952.     /**
  1953.      * @param mixed $inParam
  1954.      *
  1955.      * @return string
  1956.      *
  1957.      * @not-deprecated
  1958.      */
  1959.     public function walkInParameter($inParam)
  1960.     {
  1961.         return $inParam instanceof AST\InputParameter
  1962.             $this->walkInputParameter($inParam)
  1963.             : $this->walkArithmeticExpression($inParam);
  1964.     }
  1965.     /**
  1966.      * Walks down a literal that represents an AST node, thereby generating the appropriate SQL.
  1967.      *
  1968.      * @param AST\Literal $literal
  1969.      *
  1970.      * @return string
  1971.      *
  1972.      * @not-deprecated
  1973.      */
  1974.     public function walkLiteral($literal)
  1975.     {
  1976.         switch ($literal->type) {
  1977.             case AST\Literal::STRING:
  1978.                 return $this->conn->quote($literal->value);
  1979.             case AST\Literal::BOOLEAN:
  1980.                 return (string) $this->conn->getDatabasePlatform()->convertBooleans(strtolower($literal->value) === 'true');
  1981.             case AST\Literal::NUMERIC:
  1982.                 return (string) $literal->value;
  1983.             default:
  1984.                 throw QueryException::invalidLiteral($literal);
  1985.         }
  1986.     }
  1987.     /**
  1988.      * Walks down a BetweenExpression AST node, thereby generating the appropriate SQL.
  1989.      *
  1990.      * @param AST\BetweenExpression $betweenExpr
  1991.      *
  1992.      * @return string
  1993.      *
  1994.      * @not-deprecated
  1995.      */
  1996.     public function walkBetweenExpression($betweenExpr)
  1997.     {
  1998.         $sql $this->walkArithmeticExpression($betweenExpr->expression);
  1999.         if ($betweenExpr->not) {
  2000.             $sql .= ' NOT';
  2001.         }
  2002.         $sql .= ' BETWEEN ' $this->walkArithmeticExpression($betweenExpr->leftBetweenExpression)
  2003.             . ' AND ' $this->walkArithmeticExpression($betweenExpr->rightBetweenExpression);
  2004.         return $sql;
  2005.     }
  2006.     /**
  2007.      * Walks down a LikeExpression AST node, thereby generating the appropriate SQL.
  2008.      *
  2009.      * @param AST\LikeExpression $likeExpr
  2010.      *
  2011.      * @return string
  2012.      *
  2013.      * @not-deprecated
  2014.      */
  2015.     public function walkLikeExpression($likeExpr)
  2016.     {
  2017.         $stringExpr $likeExpr->stringExpression;
  2018.         if (is_string($stringExpr)) {
  2019.             if (! isset($this->queryComponents[$stringExpr]['resultVariable'])) {
  2020.                 throw new LogicException(sprintf('No result variable found for string expression "%s".'$stringExpr));
  2021.             }
  2022.             $leftExpr $this->walkResultVariable($stringExpr);
  2023.         } else {
  2024.             $leftExpr $stringExpr->dispatch($this);
  2025.         }
  2026.         $sql $leftExpr . ($likeExpr->not ' NOT' '') . ' LIKE ';
  2027.         if ($likeExpr->stringPattern instanceof AST\InputParameter) {
  2028.             $sql .= $this->walkInputParameter($likeExpr->stringPattern);
  2029.         } elseif ($likeExpr->stringPattern instanceof AST\Functions\FunctionNode) {
  2030.             $sql .= $this->walkFunction($likeExpr->stringPattern);
  2031.         } elseif ($likeExpr->stringPattern instanceof AST\PathExpression) {
  2032.             $sql .= $this->walkPathExpression($likeExpr->stringPattern);
  2033.         } else {
  2034.             $sql .= $this->walkLiteral($likeExpr->stringPattern);
  2035.         }
  2036.         if ($likeExpr->escapeChar) {
  2037.             $sql .= ' ESCAPE ' $this->walkLiteral($likeExpr->escapeChar);
  2038.         }
  2039.         return $sql;
  2040.     }
  2041.     /**
  2042.      * Walks down a StateFieldPathExpression AST node, thereby generating the appropriate SQL.
  2043.      *
  2044.      * @param AST\PathExpression $stateFieldPathExpression
  2045.      *
  2046.      * @return string
  2047.      *
  2048.      * @not-deprecated
  2049.      */
  2050.     public function walkStateFieldPathExpression($stateFieldPathExpression)
  2051.     {
  2052.         return $this->walkPathExpression($stateFieldPathExpression);
  2053.     }
  2054.     /**
  2055.      * Walks down a ComparisonExpression AST node, thereby generating the appropriate SQL.
  2056.      *
  2057.      * @param AST\ComparisonExpression $compExpr
  2058.      *
  2059.      * @return string
  2060.      *
  2061.      * @not-deprecated
  2062.      */
  2063.     public function walkComparisonExpression($compExpr)
  2064.     {
  2065.         $leftExpr  $compExpr->leftExpression;
  2066.         $rightExpr $compExpr->rightExpression;
  2067.         $sql       '';
  2068.         $sql .= $leftExpr instanceof AST\Node
  2069.             $leftExpr->dispatch($this)
  2070.             : (is_numeric($leftExpr) ? $leftExpr $this->conn->quote($leftExpr));
  2071.         $sql .= ' ' $compExpr->operator ' ';
  2072.         $sql .= $rightExpr instanceof AST\Node
  2073.             $rightExpr->dispatch($this)
  2074.             : (is_numeric($rightExpr) ? $rightExpr $this->conn->quote($rightExpr));
  2075.         return $sql;
  2076.     }
  2077.     /**
  2078.      * Walks down an InputParameter AST node, thereby generating the appropriate SQL.
  2079.      *
  2080.      * @param AST\InputParameter $inputParam
  2081.      *
  2082.      * @return string
  2083.      *
  2084.      * @not-deprecated
  2085.      */
  2086.     public function walkInputParameter($inputParam)
  2087.     {
  2088.         $this->parserResult->addParameterMapping($inputParam->name$this->sqlParamIndex++);
  2089.         $parameter $this->query->getParameter($inputParam->name);
  2090.         if ($parameter) {
  2091.             $type $parameter->getType();
  2092.             if (Type::hasType($type)) {
  2093.                 return Type::getType($type)->convertToDatabaseValueSQL('?'$this->platform);
  2094.             }
  2095.         }
  2096.         return '?';
  2097.     }
  2098.     /**
  2099.      * Walks down an ArithmeticExpression AST node, thereby generating the appropriate SQL.
  2100.      *
  2101.      * @param AST\ArithmeticExpression $arithmeticExpr
  2102.      *
  2103.      * @return string
  2104.      *
  2105.      * @not-deprecated
  2106.      */
  2107.     public function walkArithmeticExpression($arithmeticExpr)
  2108.     {
  2109.         return $arithmeticExpr->isSimpleArithmeticExpression()
  2110.             ? $this->walkSimpleArithmeticExpression($arithmeticExpr->simpleArithmeticExpression)
  2111.             : '(' $this->walkSubselect($arithmeticExpr->subselect) . ')';
  2112.     }
  2113.     /**
  2114.      * Walks down an SimpleArithmeticExpression AST node, thereby generating the appropriate SQL.
  2115.      *
  2116.      * @param AST\SimpleArithmeticExpression $simpleArithmeticExpr
  2117.      *
  2118.      * @return string
  2119.      *
  2120.      * @not-deprecated
  2121.      */
  2122.     public function walkSimpleArithmeticExpression($simpleArithmeticExpr)
  2123.     {
  2124.         if (! ($simpleArithmeticExpr instanceof AST\SimpleArithmeticExpression)) {
  2125.             return $this->walkArithmeticTerm($simpleArithmeticExpr);
  2126.         }
  2127.         return implode(' 'array_map([$this'walkArithmeticTerm'], $simpleArithmeticExpr->arithmeticTerms));
  2128.     }
  2129.     /**
  2130.      * Walks down an ArithmeticTerm AST node, thereby generating the appropriate SQL.
  2131.      *
  2132.      * @param mixed $term
  2133.      *
  2134.      * @return string
  2135.      *
  2136.      * @not-deprecated
  2137.      */
  2138.     public function walkArithmeticTerm($term)
  2139.     {
  2140.         if (is_string($term)) {
  2141.             return isset($this->queryComponents[$term])
  2142.                 ? $this->walkResultVariable($this->queryComponents[$term]['token']->value)
  2143.                 : $term;
  2144.         }
  2145.         // Phase 2 AST optimization: Skip processing of ArithmeticTerm
  2146.         // if only one ArithmeticFactor is defined
  2147.         if (! ($term instanceof AST\ArithmeticTerm)) {
  2148.             return $this->walkArithmeticFactor($term);
  2149.         }
  2150.         return implode(' 'array_map([$this'walkArithmeticFactor'], $term->arithmeticFactors));
  2151.     }
  2152.     /**
  2153.      * Walks down an ArithmeticFactor that represents an AST node, thereby generating the appropriate SQL.
  2154.      *
  2155.      * @param mixed $factor
  2156.      *
  2157.      * @return string
  2158.      *
  2159.      * @not-deprecated
  2160.      */
  2161.     public function walkArithmeticFactor($factor)
  2162.     {
  2163.         if (is_string($factor)) {
  2164.             return isset($this->queryComponents[$factor])
  2165.                 ? $this->walkResultVariable($this->queryComponents[$factor]['token']->value)
  2166.                 : $factor;
  2167.         }
  2168.         // Phase 2 AST optimization: Skip processing of ArithmeticFactor
  2169.         // if only one ArithmeticPrimary is defined
  2170.         if (! ($factor instanceof AST\ArithmeticFactor)) {
  2171.             return $this->walkArithmeticPrimary($factor);
  2172.         }
  2173.         $sign $factor->isNegativeSigned() ? '-' : ($factor->isPositiveSigned() ? '+' '');
  2174.         return $sign $this->walkArithmeticPrimary($factor->arithmeticPrimary);
  2175.     }
  2176.     /**
  2177.      * Walks down an ArithmeticPrimary that represents an AST node, thereby generating the appropriate SQL.
  2178.      *
  2179.      * @param mixed $primary
  2180.      *
  2181.      * @return string The SQL.
  2182.      *
  2183.      * @not-deprecated
  2184.      */
  2185.     public function walkArithmeticPrimary($primary)
  2186.     {
  2187.         if ($primary instanceof AST\SimpleArithmeticExpression) {
  2188.             return '(' $this->walkSimpleArithmeticExpression($primary) . ')';
  2189.         }
  2190.         if ($primary instanceof AST\Node) {
  2191.             return $primary->dispatch($this);
  2192.         }
  2193.         return $this->walkEntityIdentificationVariable($primary);
  2194.     }
  2195.     /**
  2196.      * Walks down a StringPrimary that represents an AST node, thereby generating the appropriate SQL.
  2197.      *
  2198.      * @param mixed $stringPrimary
  2199.      *
  2200.      * @return string
  2201.      *
  2202.      * @not-deprecated
  2203.      */
  2204.     public function walkStringPrimary($stringPrimary)
  2205.     {
  2206.         return is_string($stringPrimary)
  2207.             ? $this->conn->quote($stringPrimary)
  2208.             : $stringPrimary->dispatch($this);
  2209.     }
  2210.     /**
  2211.      * Walks down a ResultVariable that represents an AST node, thereby generating the appropriate SQL.
  2212.      *
  2213.      * @param string $resultVariable
  2214.      *
  2215.      * @return string
  2216.      *
  2217.      * @not-deprecated
  2218.      */
  2219.     public function walkResultVariable($resultVariable)
  2220.     {
  2221.         if (! isset($this->scalarResultAliasMap[$resultVariable])) {
  2222.             throw new InvalidArgumentException(sprintf('Unknown result variable: %s'$resultVariable));
  2223.         }
  2224.         $resultAlias $this->scalarResultAliasMap[$resultVariable];
  2225.         if (is_array($resultAlias)) {
  2226.             return implode(', '$resultAlias);
  2227.         }
  2228.         return $resultAlias;
  2229.     }
  2230.     /**
  2231.      * @return string The list in parentheses of valid child discriminators from the given class
  2232.      *
  2233.      * @throws QueryException
  2234.      */
  2235.     private function getChildDiscriminatorsFromClassMetadata(
  2236.         ClassMetadata $rootClass,
  2237.         AST\InstanceOfExpression $instanceOfExpr
  2238.     ): string {
  2239.         $sqlParameterList = [];
  2240.         $discriminators   = [];
  2241.         foreach ($instanceOfExpr->value as $parameter) {
  2242.             if ($parameter instanceof AST\InputParameter) {
  2243.                 $this->rsm->discriminatorParameters[$parameter->name] = $parameter->name;
  2244.                 $sqlParameterList[]                                   = $this->walkInParameter($parameter);
  2245.                 continue;
  2246.             }
  2247.             $metadata $this->em->getClassMetadata($parameter);
  2248.             if ($metadata->getName() !== $rootClass->name && ! $metadata->getReflectionClass()->isSubclassOf($rootClass->name)) {
  2249.                 throw QueryException::instanceOfUnrelatedClass($parameter$rootClass->name);
  2250.             }
  2251.             $discriminators += HierarchyDiscriminatorResolver::resolveDiscriminatorsForClass($metadata$this->em);
  2252.         }
  2253.         foreach (array_keys($discriminators) as $dis) {
  2254.             $sqlParameterList[] = $this->conn->quote($dis);
  2255.         }
  2256.         return '(' implode(', '$sqlParameterList) . ')';
  2257.     }
  2258. }