123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367 |
- <?php
- /*
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software consists of voluntary contributions made by many individuals
- * and is licensed under the MIT license. For more information, see
- * <http://www.doctrine-project.org>.
- */
- namespace Doctrine\ORM\Query;
- use BadMethodCallException;
- use Doctrine\DBAL\Connection;
- use Doctrine\DBAL\LockMode;
- use Doctrine\DBAL\Platforms\AbstractPlatform;
- use Doctrine\DBAL\Types\Type;
- use Doctrine\ORM\EntityManager;
- use Doctrine\ORM\Mapping\ClassMetadata;
- use Doctrine\ORM\Mapping\ClassMetadataInfo;
- use Doctrine\ORM\Mapping\QuoteStrategy;
- use Doctrine\ORM\OptimisticLockException;
- use Doctrine\ORM\Query;
- use Doctrine\ORM\Utility\HierarchyDiscriminatorResolver;
- use Doctrine\ORM\Utility\PersisterHelper;
- use function array_diff;
- use function array_filter;
- use function array_keys;
- use function array_map;
- use function array_merge;
- use function count;
- use function implode;
- use function in_array;
- use function is_array;
- use function is_float;
- use function is_numeric;
- use function is_string;
- use function preg_match;
- use function reset;
- use function sprintf;
- use function strtolower;
- use function strtoupper;
- use function trim;
- /**
- * The SqlWalker is a TreeWalker that walks over a DQL AST and constructs
- * the corresponding SQL.
- */
- class SqlWalker implements TreeWalker
- {
- public const HINT_DISTINCT = 'doctrine.distinct';
- /**
- * Used to mark a query as containing a PARTIAL expression, which needs to be known by SLC.
- */
- public const HINT_PARTIAL = 'doctrine.partial';
- /** @var ResultSetMapping */
- private $rsm;
- /**
- * Counter for generating unique column aliases.
- *
- * @var int
- */
- private $aliasCounter = 0;
- /**
- * Counter for generating unique table aliases.
- *
- * @var int
- */
- private $tableAliasCounter = 0;
- /**
- * Counter for generating unique scalar result.
- *
- * @var int
- */
- private $scalarResultCounter = 1;
- /**
- * Counter for generating unique parameter indexes.
- *
- * @var int
- */
- private $sqlParamIndex = 0;
- /**
- * Counter for generating indexes.
- *
- * @var int
- */
- private $newObjectCounter = 0;
- /** @var ParserResult */
- private $parserResult;
- /** @var EntityManager */
- private $em;
- /** @var Connection */
- private $conn;
- /** @var Query */
- private $query;
- /** @var mixed[] */
- private $tableAliasMap = [];
- /**
- * Map from result variable names to their SQL column alias names.
- *
- * @psalm-var array<string, string|list<string>>
- */
- private $scalarResultAliasMap = [];
- /**
- * Map from Table-Alias + Column-Name to OrderBy-Direction.
- *
- * @var array<string, string>
- */
- private $orderedColumnsMap = [];
- /**
- * Map from DQL-Alias + Field-Name to SQL Column Alias.
- *
- * @var array<string, array<string, string>>
- */
- private $scalarFields = [];
- /**
- * Map of all components/classes that appear in the DQL query.
- *
- * @psalm-var array<string, array{
- * metadata: ClassMetadata,
- * parent: string,
- * relation: mixed[],
- * map: mixed,
- * nestingLevel: int,
- * token: array
- * }>
- */
- private $queryComponents;
- /**
- * A list of classes that appear in non-scalar SelectExpressions.
- *
- * @psalm-var list<array{class: ClassMetadata, dqlAlias: string, resultAlias: string}>
- */
- private $selectedClasses = [];
- /**
- * The DQL alias of the root class of the currently traversed query.
- *
- * @psalm-var list<string>
- */
- private $rootAliases = [];
- /**
- * Flag that indicates whether to generate SQL table aliases in the SQL.
- * These should only be generated for SELECT queries, not for UPDATE/DELETE.
- *
- * @var bool
- */
- private $useSqlTableAliases = true;
- /**
- * The database platform abstraction.
- *
- * @var AbstractPlatform
- */
- private $platform;
- /**
- * The quote strategy.
- *
- * @var QuoteStrategy
- */
- private $quoteStrategy;
- /**
- * {@inheritDoc}
- */
- public function __construct($query, $parserResult, array $queryComponents)
- {
- $this->query = $query;
- $this->parserResult = $parserResult;
- $this->queryComponents = $queryComponents;
- $this->rsm = $parserResult->getResultSetMapping();
- $this->em = $query->getEntityManager();
- $this->conn = $this->em->getConnection();
- $this->platform = $this->conn->getDatabasePlatform();
- $this->quoteStrategy = $this->em->getConfiguration()->getQuoteStrategy();
- }
- /**
- * Gets the Query instance used by the walker.
- *
- * @return Query
- */
- public function getQuery()
- {
- return $this->query;
- }
- /**
- * Gets the Connection used by the walker.
- *
- * @return Connection
- */
- public function getConnection()
- {
- return $this->conn;
- }
- /**
- * Gets the EntityManager used by the walker.
- *
- * @return EntityManager
- */
- public function getEntityManager()
- {
- return $this->em;
- }
- /**
- * Gets the information about a single query component.
- *
- * @param string $dqlAlias The DQL alias.
- *
- * @psalm-return array{
- * metadata: ClassMetadata,
- * parent: string,
- * relation: mixed[],
- * map: mixed,
- * nestingLevel: int,
- * token: array
- * }
- */
- public function getQueryComponent($dqlAlias)
- {
- return $this->queryComponents[$dqlAlias];
- }
- /**
- * {@inheritdoc}
- */
- public function getQueryComponents()
- {
- return $this->queryComponents;
- }
- /**
- * {@inheritdoc}
- */
- public function setQueryComponent($dqlAlias, array $queryComponent)
- {
- $requiredKeys = ['metadata', 'parent', 'relation', 'map', 'nestingLevel', 'token'];
- if (array_diff($requiredKeys, array_keys($queryComponent))) {
- throw QueryException::invalidQueryComponent($dqlAlias);
- }
- $this->queryComponents[$dqlAlias] = $queryComponent;
- }
- /**
- * {@inheritdoc}
- */
- public function getExecutor($AST)
- {
- switch (true) {
- case $AST instanceof AST\DeleteStatement:
- $primaryClass = $this->em->getClassMetadata($AST->deleteClause->abstractSchemaName);
- return $primaryClass->isInheritanceTypeJoined()
- ? new Exec\MultiTableDeleteExecutor($AST, $this)
- : new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
- case $AST instanceof AST\UpdateStatement:
- $primaryClass = $this->em->getClassMetadata($AST->updateClause->abstractSchemaName);
- return $primaryClass->isInheritanceTypeJoined()
- ? new Exec\MultiTableUpdateExecutor($AST, $this)
- : new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
- default:
- return new Exec\SingleSelectExecutor($AST, $this);
- }
- }
- /**
- * Generates a unique, short SQL table alias.
- *
- * @param string $tableName Table name
- * @param string $dqlAlias The DQL alias.
- *
- * @return string Generated table alias.
- */
- public function getSQLTableAlias($tableName, $dqlAlias = '')
- {
- $tableName .= $dqlAlias ? '@[' . $dqlAlias . ']' : '';
- if (! isset($this->tableAliasMap[$tableName])) {
- $this->tableAliasMap[$tableName] = (preg_match('/[a-z]/i', $tableName[0]) ? strtolower($tableName[0]) : 't')
- . $this->tableAliasCounter++ . '_';
- }
- return $this->tableAliasMap[$tableName];
- }
- /**
- * Forces the SqlWalker to use a specific alias for a table name, rather than
- * generating an alias on its own.
- *
- * @param string $tableName
- * @param string $alias
- * @param string $dqlAlias
- *
- * @return string
- */
- public function setSQLTableAlias($tableName, $alias, $dqlAlias = '')
- {
- $tableName .= $dqlAlias ? '@[' . $dqlAlias . ']' : '';
- $this->tableAliasMap[$tableName] = $alias;
- return $alias;
- }
- /**
- * Gets an SQL column alias for a column name.
- *
- * @param string $columnName
- *
- * @return string
- */
- public function getSQLColumnAlias($columnName)
- {
- return $this->quoteStrategy->getColumnAlias($columnName, $this->aliasCounter++, $this->platform);
- }
- /**
- * Generates the SQL JOINs that are necessary for Class Table Inheritance
- * for the given class.
- *
- * @param ClassMetadata $class The class for which to generate the joins.
- * @param string $dqlAlias The DQL alias of the class.
- *
- * @return string The SQL.
- */
- private function generateClassTableInheritanceJoins(
- ClassMetadata $class,
- string $dqlAlias
- ): string {
- $sql = '';
- $baseTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
- // INNER JOIN parent class tables
- foreach ($class->parentClasses as $parentClassName) {
- $parentClass = $this->em->getClassMetadata($parentClassName);
- $tableAlias = $this->getSQLTableAlias($parentClass->getTableName(), $dqlAlias);
- // If this is a joined association we must use left joins to preserve the correct result.
- $sql .= isset($this->queryComponents[$dqlAlias]['relation']) ? ' LEFT ' : ' INNER ';
- $sql .= 'JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON ';
- $sqlParts = [];
- foreach ($this->quoteStrategy->getIdentifierColumnNames($class, $this->platform) as $columnName) {
- $sqlParts[] = $baseTableAlias . '.' . $columnName . ' = ' . $tableAlias . '.' . $columnName;
- }
- // Add filters on the root class
- $sqlParts[] = $this->generateFilterConditionSQL($parentClass, $tableAlias);
- $sql .= implode(' AND ', array_filter($sqlParts));
- }
- // Ignore subclassing inclusion if partial objects is disallowed
- if ($this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
- return $sql;
- }
- // LEFT JOIN child class tables
- foreach ($class->subClasses as $subClassName) {
- $subClass = $this->em->getClassMetadata($subClassName);
- $tableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
- $sql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->platform) . ' ' . $tableAlias . ' ON ';
- $sqlParts = [];
- foreach ($this->quoteStrategy->getIdentifierColumnNames($subClass, $this->platform) as $columnName) {
- $sqlParts[] = $baseTableAlias . '.' . $columnName . ' = ' . $tableAlias . '.' . $columnName;
- }
- $sql .= implode(' AND ', $sqlParts);
- }
- return $sql;
- }
- private function generateOrderedCollectionOrderByItems(): string
- {
- $orderedColumns = [];
- foreach ($this->selectedClasses as $selectedClass) {
- $dqlAlias = $selectedClass['dqlAlias'];
- $qComp = $this->queryComponents[$dqlAlias];
- if (! isset($qComp['relation']['orderBy'])) {
- continue;
- }
- $persister = $this->em->getUnitOfWork()->getEntityPersister($qComp['metadata']->name);
- foreach ($qComp['relation']['orderBy'] as $fieldName => $orientation) {
- $columnName = $this->quoteStrategy->getColumnName($fieldName, $qComp['metadata'], $this->platform);
- $tableName = $qComp['metadata']->isInheritanceTypeJoined()
- ? $persister->getOwningTable($fieldName)
- : $qComp['metadata']->getTableName();
- $orderedColumn = $this->getSQLTableAlias($tableName, $dqlAlias) . '.' . $columnName;
- // OrderByClause should replace an ordered relation. see - DDC-2475
- if (isset($this->orderedColumnsMap[$orderedColumn])) {
- continue;
- }
- $this->orderedColumnsMap[$orderedColumn] = $orientation;
- $orderedColumns[] = $orderedColumn . ' ' . $orientation;
- }
- }
- return implode(', ', $orderedColumns);
- }
- /**
- * Generates a discriminator column SQL condition for the class with the given DQL alias.
- *
- * @psalm-param list<string> $dqlAliases List of root DQL aliases to inspect for discriminator restrictions.
- */
- private function generateDiscriminatorColumnConditionSQL(array $dqlAliases): string
- {
- $sqlParts = [];
- foreach ($dqlAliases as $dqlAlias) {
- $class = $this->queryComponents[$dqlAlias]['metadata'];
- if (! $class->isInheritanceTypeSingleTable()) {
- continue;
- }
- $conn = $this->em->getConnection();
- $values = [];
- if ($class->discriminatorValue !== null) { // discriminators can be 0
- $values[] = $conn->quote($class->discriminatorValue);
- }
- foreach ($class->subClasses as $subclassName) {
- $values[] = $conn->quote($this->em->getClassMetadata($subclassName)->discriminatorValue);
- }
- $sqlTableAlias = $this->useSqlTableAliases
- ? $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.'
- : '';
- $sqlParts[] = $sqlTableAlias . $class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')';
- }
- $sql = implode(' AND ', $sqlParts);
- return count($sqlParts) > 1 ? '(' . $sql . ')' : $sql;
- }
- /**
- * Generates the filter SQL for a given entity and table alias.
- *
- * @param ClassMetadata $targetEntity Metadata of the target entity.
- * @param string $targetTableAlias The table alias of the joined/selected table.
- *
- * @return string The SQL query part to add to a query.
- */
- private function generateFilterConditionSQL(
- ClassMetadata $targetEntity,
- string $targetTableAlias
- ): string {
- if (! $this->em->hasFilters()) {
- return '';
- }
- switch ($targetEntity->inheritanceType) {
- case ClassMetadata::INHERITANCE_TYPE_NONE:
- break;
- case ClassMetadata::INHERITANCE_TYPE_JOINED:
- // The classes in the inheritance will be added to the query one by one,
- // but only the root node is getting filtered
- if ($targetEntity->name !== $targetEntity->rootEntityName) {
- return '';
- }
- break;
- case ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE:
- // With STI the table will only be queried once, make sure that the filters
- // are added to the root entity
- $targetEntity = $this->em->getClassMetadata($targetEntity->rootEntityName);
- break;
- default:
- //@todo: throw exception?
- return '';
- }
- $filterClauses = [];
- foreach ($this->em->getFilters()->getEnabledFilters() as $filter) {
- $filterExpr = $filter->addFilterConstraint($targetEntity, $targetTableAlias);
- if ($filterExpr !== '') {
- $filterClauses[] = '(' . $filterExpr . ')';
- }
- }
- return implode(' AND ', $filterClauses);
- }
- /**
- * {@inheritdoc}
- */
- public function walkSelectStatement(AST\SelectStatement $AST)
- {
- $limit = $this->query->getMaxResults();
- $offset = $this->query->getFirstResult();
- $lockMode = $this->query->getHint(Query::HINT_LOCK_MODE);
- $sql = $this->walkSelectClause($AST->selectClause)
- . $this->walkFromClause($AST->fromClause)
- . $this->walkWhereClause($AST->whereClause);
- if ($AST->groupByClause) {
- $sql .= $this->walkGroupByClause($AST->groupByClause);
- }
- if ($AST->havingClause) {
- $sql .= $this->walkHavingClause($AST->havingClause);
- }
- if ($AST->orderByClause) {
- $sql .= $this->walkOrderByClause($AST->orderByClause);
- }
- $orderBySql = $this->generateOrderedCollectionOrderByItems();
- if (! $AST->orderByClause && $orderBySql) {
- $sql .= ' ORDER BY ' . $orderBySql;
- }
- if ($limit !== null || $offset !== null) {
- $sql = $this->platform->modifyLimitQuery($sql, $limit, $offset);
- }
- if ($lockMode === null || $lockMode === false || $lockMode === LockMode::NONE) {
- return $sql;
- }
- if ($lockMode === LockMode::PESSIMISTIC_READ) {
- return $sql . ' ' . $this->platform->getReadLockSQL();
- }
- if ($lockMode === LockMode::PESSIMISTIC_WRITE) {
- return $sql . ' ' . $this->platform->getWriteLockSQL();
- }
- if ($lockMode !== LockMode::OPTIMISTIC) {
- throw QueryException::invalidLockMode();
- }
- foreach ($this->selectedClasses as $selectedClass) {
- if (! $selectedClass['class']->isVersioned) {
- throw OptimisticLockException::lockFailed($selectedClass['class']->name);
- }
- }
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkUpdateStatement(AST\UpdateStatement $AST)
- {
- $this->useSqlTableAliases = false;
- $this->rsm->isSelect = false;
- return $this->walkUpdateClause($AST->updateClause)
- . $this->walkWhereClause($AST->whereClause);
- }
- /**
- * {@inheritdoc}
- */
- public function walkDeleteStatement(AST\DeleteStatement $AST)
- {
- $this->useSqlTableAliases = false;
- $this->rsm->isSelect = false;
- return $this->walkDeleteClause($AST->deleteClause)
- . $this->walkWhereClause($AST->whereClause);
- }
- /**
- * Walks down an IdentificationVariable AST node, thereby generating the appropriate SQL.
- * This one differs of ->walkIdentificationVariable() because it generates the entity identifiers.
- *
- * @param string $identVariable
- *
- * @return string
- */
- public function walkEntityIdentificationVariable($identVariable)
- {
- $class = $this->queryComponents[$identVariable]['metadata'];
- $tableAlias = $this->getSQLTableAlias($class->getTableName(), $identVariable);
- $sqlParts = [];
- foreach ($this->quoteStrategy->getIdentifierColumnNames($class, $this->platform) as $columnName) {
- $sqlParts[] = $tableAlias . '.' . $columnName;
- }
- return implode(', ', $sqlParts);
- }
- /**
- * Walks down an IdentificationVariable (no AST node associated), thereby generating the SQL.
- *
- * @param string $identificationVariable
- * @param string $fieldName
- *
- * @return string The SQL.
- */
- public function walkIdentificationVariable($identificationVariable, $fieldName = null)
- {
- $class = $this->queryComponents[$identificationVariable]['metadata'];
- if (
- $fieldName !== null && $class->isInheritanceTypeJoined() &&
- isset($class->fieldMappings[$fieldName]['inherited'])
- ) {
- $class = $this->em->getClassMetadata($class->fieldMappings[$fieldName]['inherited']);
- }
- return $this->getSQLTableAlias($class->getTableName(), $identificationVariable);
- }
- /**
- * {@inheritdoc}
- */
- public function walkPathExpression($pathExpr)
- {
- $sql = '';
- switch ($pathExpr->type) {
- case AST\PathExpression::TYPE_STATE_FIELD:
- $fieldName = $pathExpr->field;
- $dqlAlias = $pathExpr->identificationVariable;
- $class = $this->queryComponents[$dqlAlias]['metadata'];
- if ($this->useSqlTableAliases) {
- $sql .= $this->walkIdentificationVariable($dqlAlias, $fieldName) . '.';
- }
- $sql .= $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform);
- break;
- case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION:
- // 1- the owning side:
- // Just use the foreign key, i.e. u.group_id
- $fieldName = $pathExpr->field;
- $dqlAlias = $pathExpr->identificationVariable;
- $class = $this->queryComponents[$dqlAlias]['metadata'];
- if (isset($class->associationMappings[$fieldName]['inherited'])) {
- $class = $this->em->getClassMetadata($class->associationMappings[$fieldName]['inherited']);
- }
- $assoc = $class->associationMappings[$fieldName];
- if (! $assoc['isOwningSide']) {
- throw QueryException::associationPathInverseSideNotSupported($pathExpr);
- }
- // COMPOSITE KEYS NOT (YET?) SUPPORTED
- if (count($assoc['sourceToTargetKeyColumns']) > 1) {
- throw QueryException::associationPathCompositeKeyNotSupported();
- }
- if ($this->useSqlTableAliases) {
- $sql .= $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.';
- }
- $sql .= reset($assoc['targetToSourceKeyColumns']);
- break;
- default:
- throw QueryException::invalidPathExpression($pathExpr);
- }
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkSelectClause($selectClause)
- {
- $sql = 'SELECT ' . ($selectClause->isDistinct ? 'DISTINCT ' : '');
- $sqlSelectExpressions = array_filter(array_map([$this, 'walkSelectExpression'], $selectClause->selectExpressions));
- if ($this->query->getHint(Query::HINT_INTERNAL_ITERATION) === true && $selectClause->isDistinct) {
- $this->query->setHint(self::HINT_DISTINCT, true);
- }
- $addMetaColumns = ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD) &&
- $this->query->getHydrationMode() === Query::HYDRATE_OBJECT
- ||
- $this->query->getHydrationMode() !== Query::HYDRATE_OBJECT &&
- $this->query->getHint(Query::HINT_INCLUDE_META_COLUMNS);
- foreach ($this->selectedClasses as $selectedClass) {
- $class = $selectedClass['class'];
- $dqlAlias = $selectedClass['dqlAlias'];
- $resultAlias = $selectedClass['resultAlias'];
- // Register as entity or joined entity result
- if ($this->queryComponents[$dqlAlias]['relation'] === null) {
- $this->rsm->addEntityResult($class->name, $dqlAlias, $resultAlias);
- } else {
- $this->rsm->addJoinedEntityResult(
- $class->name,
- $dqlAlias,
- $this->queryComponents[$dqlAlias]['parent'],
- $this->queryComponents[$dqlAlias]['relation']['fieldName']
- );
- }
- if ($class->isInheritanceTypeSingleTable() || $class->isInheritanceTypeJoined()) {
- // Add discriminator columns to SQL
- $rootClass = $this->em->getClassMetadata($class->rootEntityName);
- $tblAlias = $this->getSQLTableAlias($rootClass->getTableName(), $dqlAlias);
- $discrColumn = $rootClass->discriminatorColumn;
- $columnAlias = $this->getSQLColumnAlias($discrColumn['name']);
- $sqlSelectExpressions[] = $tblAlias . '.' . $discrColumn['name'] . ' AS ' . $columnAlias;
- $this->rsm->setDiscriminatorColumn($dqlAlias, $columnAlias);
- $this->rsm->addMetaResult($dqlAlias, $columnAlias, $discrColumn['fieldName'], false, $discrColumn['type']);
- }
- // Add foreign key columns to SQL, if necessary
- if (! $addMetaColumns && ! $class->containsForeignIdentifier) {
- continue;
- }
- // Add foreign key columns of class and also parent classes
- foreach ($class->associationMappings as $assoc) {
- if (
- ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE)
- || ( ! $addMetaColumns && ! isset($assoc['id']))
- ) {
- continue;
- }
- $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
- $isIdentifier = (isset($assoc['id']) && $assoc['id'] === true);
- $owningClass = isset($assoc['inherited']) ? $this->em->getClassMetadata($assoc['inherited']) : $class;
- $sqlTableAlias = $this->getSQLTableAlias($owningClass->getTableName(), $dqlAlias);
- foreach ($assoc['joinColumns'] as $joinColumn) {
- $columnName = $joinColumn['name'];
- $columnAlias = $this->getSQLColumnAlias($columnName);
- $columnType = PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em);
- $quotedColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
- $sqlSelectExpressions[] = $sqlTableAlias . '.' . $quotedColumnName . ' AS ' . $columnAlias;
- $this->rsm->addMetaResult($dqlAlias, $columnAlias, $columnName, $isIdentifier, $columnType);
- }
- }
- // Add foreign key columns to SQL, if necessary
- if (! $addMetaColumns) {
- continue;
- }
- // Add foreign key columns of subclasses
- foreach ($class->subClasses as $subClassName) {
- $subClass = $this->em->getClassMetadata($subClassName);
- $sqlTableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
- foreach ($subClass->associationMappings as $assoc) {
- // Skip if association is inherited
- if (isset($assoc['inherited'])) {
- continue;
- }
- if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) {
- $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
- foreach ($assoc['joinColumns'] as $joinColumn) {
- $columnName = $joinColumn['name'];
- $columnAlias = $this->getSQLColumnAlias($columnName);
- $columnType = PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em);
- $quotedColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $subClass, $this->platform);
- $sqlSelectExpressions[] = $sqlTableAlias . '.' . $quotedColumnName . ' AS ' . $columnAlias;
- $this->rsm->addMetaResult($dqlAlias, $columnAlias, $columnName, $subClass->isIdentifier($columnName), $columnType);
- }
- }
- }
- }
- }
- return $sql . implode(', ', $sqlSelectExpressions);
- }
- /**
- * {@inheritdoc}
- */
- public function walkFromClause($fromClause)
- {
- $identificationVarDecls = $fromClause->identificationVariableDeclarations;
- $sqlParts = [];
- foreach ($identificationVarDecls as $identificationVariableDecl) {
- $sqlParts[] = $this->walkIdentificationVariableDeclaration($identificationVariableDecl);
- }
- return ' FROM ' . implode(', ', $sqlParts);
- }
- /**
- * Walks down a IdentificationVariableDeclaration AST node, thereby generating the appropriate SQL.
- *
- * @param AST\IdentificationVariableDeclaration $identificationVariableDecl
- *
- * @return string
- */
- public function walkIdentificationVariableDeclaration($identificationVariableDecl)
- {
- $sql = $this->walkRangeVariableDeclaration($identificationVariableDecl->rangeVariableDeclaration);
- if ($identificationVariableDecl->indexBy) {
- $this->walkIndexBy($identificationVariableDecl->indexBy);
- }
- foreach ($identificationVariableDecl->joins as $join) {
- $sql .= $this->walkJoin($join);
- }
- return $sql;
- }
- /**
- * Walks down a IndexBy AST node.
- *
- * @param AST\IndexBy $indexBy
- *
- * @return void
- */
- public function walkIndexBy($indexBy)
- {
- $pathExpression = $indexBy->simpleStateFieldPathExpression;
- $alias = $pathExpression->identificationVariable;
- $field = $pathExpression->field;
- if (isset($this->scalarFields[$alias][$field])) {
- $this->rsm->addIndexByScalar($this->scalarFields[$alias][$field]);
- return;
- }
- $this->rsm->addIndexBy($alias, $field);
- }
- /**
- * Walks down a RangeVariableDeclaration AST node, thereby generating the appropriate SQL.
- *
- * @param AST\RangeVariableDeclaration $rangeVariableDeclaration
- *
- * @return string
- */
- public function walkRangeVariableDeclaration($rangeVariableDeclaration)
- {
- return $this->generateRangeVariableDeclarationSQL($rangeVariableDeclaration, false);
- }
- /**
- * Generate appropriate SQL for RangeVariableDeclaration AST node
- *
- * @param AST\RangeVariableDeclaration $rangeVariableDeclaration
- */
- private function generateRangeVariableDeclarationSQL($rangeVariableDeclaration, bool $buildNestedJoins): string
- {
- $class = $this->em->getClassMetadata($rangeVariableDeclaration->abstractSchemaName);
- $dqlAlias = $rangeVariableDeclaration->aliasIdentificationVariable;
- if ($rangeVariableDeclaration->isRoot) {
- $this->rootAliases[] = $dqlAlias;
- }
- $sql = $this->platform->appendLockHint(
- $this->quoteStrategy->getTableName($class, $this->platform) . ' ' .
- $this->getSQLTableAlias($class->getTableName(), $dqlAlias),
- $this->query->getHint(Query::HINT_LOCK_MODE)
- );
- if (! $class->isInheritanceTypeJoined()) {
- return $sql;
- }
- $classTableInheritanceJoins = $this->generateClassTableInheritanceJoins($class, $dqlAlias);
- if (! $buildNestedJoins) {
- return $sql . $classTableInheritanceJoins;
- }
- return $classTableInheritanceJoins === '' ? $sql : '(' . $sql . $classTableInheritanceJoins . ')';
- }
- /**
- * Walks down a JoinAssociationDeclaration AST node, thereby generating the appropriate SQL.
- *
- * @param AST\JoinAssociationDeclaration $joinAssociationDeclaration
- * @param int $joinType
- * @param AST\ConditionalExpression $condExpr
- *
- * @return string
- *
- * @throws QueryException
- */
- public function walkJoinAssociationDeclaration($joinAssociationDeclaration, $joinType = AST\Join::JOIN_TYPE_INNER, $condExpr = null)
- {
- $sql = '';
- $associationPathExpression = $joinAssociationDeclaration->joinAssociationPathExpression;
- $joinedDqlAlias = $joinAssociationDeclaration->aliasIdentificationVariable;
- $indexBy = $joinAssociationDeclaration->indexBy;
- $relation = $this->queryComponents[$joinedDqlAlias]['relation'];
- $targetClass = $this->em->getClassMetadata($relation['targetEntity']);
- $sourceClass = $this->em->getClassMetadata($relation['sourceEntity']);
- $targetTableName = $this->quoteStrategy->getTableName($targetClass, $this->platform);
- $targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName(), $joinedDqlAlias);
- $sourceTableAlias = $this->getSQLTableAlias($sourceClass->getTableName(), $associationPathExpression->identificationVariable);
- // Ensure we got the owning side, since it has all mapping info
- $assoc = ! $relation['isOwningSide'] ? $targetClass->associationMappings[$relation['mappedBy']] : $relation;
- if ($this->query->getHint(Query::HINT_INTERNAL_ITERATION) === true && (! $this->query->getHint(self::HINT_DISTINCT) || isset($this->selectedClasses[$joinedDqlAlias]))) {
- if ($relation['type'] === ClassMetadata::ONE_TO_MANY || $relation['type'] === ClassMetadata::MANY_TO_MANY) {
- throw QueryException::iterateWithFetchJoinNotAllowed($assoc);
- }
- }
- $targetTableJoin = null;
- // This condition is not checking ClassMetadata::MANY_TO_ONE, because by definition it cannot
- // be the owning side and previously we ensured that $assoc is always the owning side of the associations.
- // The owning side is necessary at this point because only it contains the JoinColumn information.
- switch (true) {
- case $assoc['type'] & ClassMetadata::TO_ONE:
- $conditions = [];
- foreach ($assoc['joinColumns'] as $joinColumn) {
- $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
- $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
- if ($relation['isOwningSide']) {
- $conditions[] = $sourceTableAlias . '.' . $quotedSourceColumn . ' = ' . $targetTableAlias . '.' . $quotedTargetColumn;
- continue;
- }
- $conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $targetTableAlias . '.' . $quotedSourceColumn;
- }
- // Apply remaining inheritance restrictions
- $discrSql = $this->generateDiscriminatorColumnConditionSQL([$joinedDqlAlias]);
- if ($discrSql) {
- $conditions[] = $discrSql;
- }
- // Apply the filters
- $filterExpr = $this->generateFilterConditionSQL($targetClass, $targetTableAlias);
- if ($filterExpr) {
- $conditions[] = $filterExpr;
- }
- $targetTableJoin = [
- 'table' => $targetTableName . ' ' . $targetTableAlias,
- 'condition' => implode(' AND ', $conditions),
- ];
- break;
- case $assoc['type'] === ClassMetadata::MANY_TO_MANY:
- // Join relation table
- $joinTable = $assoc['joinTable'];
- $joinTableAlias = $this->getSQLTableAlias($joinTable['name'], $joinedDqlAlias);
- $joinTableName = $this->quoteStrategy->getJoinTableName($assoc, $sourceClass, $this->platform);
- $conditions = [];
- $relationColumns = $relation['isOwningSide']
- ? $assoc['joinTable']['joinColumns']
- : $assoc['joinTable']['inverseJoinColumns'];
- foreach ($relationColumns as $joinColumn) {
- $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
- $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
- $conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $quotedSourceColumn;
- }
- $sql .= $joinTableName . ' ' . $joinTableAlias . ' ON ' . implode(' AND ', $conditions);
- // Join target table
- $sql .= $joinType === AST\Join::JOIN_TYPE_LEFT || $joinType === AST\Join::JOIN_TYPE_LEFTOUTER ? ' LEFT JOIN ' : ' INNER JOIN ';
- $conditions = [];
- $relationColumns = $relation['isOwningSide']
- ? $assoc['joinTable']['inverseJoinColumns']
- : $assoc['joinTable']['joinColumns'];
- foreach ($relationColumns as $joinColumn) {
- $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
- $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
- $conditions[] = $targetTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $quotedSourceColumn;
- }
- // Apply remaining inheritance restrictions
- $discrSql = $this->generateDiscriminatorColumnConditionSQL([$joinedDqlAlias]);
- if ($discrSql) {
- $conditions[] = $discrSql;
- }
- // Apply the filters
- $filterExpr = $this->generateFilterConditionSQL($targetClass, $targetTableAlias);
- if ($filterExpr) {
- $conditions[] = $filterExpr;
- }
- $targetTableJoin = [
- 'table' => $targetTableName . ' ' . $targetTableAlias,
- 'condition' => implode(' AND ', $conditions),
- ];
- break;
- default:
- throw new BadMethodCallException('Type of association must be one of *_TO_ONE or MANY_TO_MANY');
- }
- // Handle WITH clause
- $withCondition = $condExpr === null ? '' : ('(' . $this->walkConditionalExpression($condExpr) . ')');
- if ($targetClass->isInheritanceTypeJoined()) {
- $ctiJoins = $this->generateClassTableInheritanceJoins($targetClass, $joinedDqlAlias);
- // If we have WITH condition, we need to build nested joins for target class table and cti joins
- if ($withCondition) {
- $sql .= '(' . $targetTableJoin['table'] . $ctiJoins . ') ON ' . $targetTableJoin['condition'];
- } else {
- $sql .= $targetTableJoin['table'] . ' ON ' . $targetTableJoin['condition'] . $ctiJoins;
- }
- } else {
- $sql .= $targetTableJoin['table'] . ' ON ' . $targetTableJoin['condition'];
- }
- if ($withCondition) {
- $sql .= ' AND ' . $withCondition;
- }
- // Apply the indexes
- if ($indexBy) {
- // For Many-To-One or One-To-One associations this obviously makes no sense, but is ignored silently.
- $this->walkIndexBy($indexBy);
- } elseif (isset($relation['indexBy'])) {
- $this->rsm->addIndexBy($joinedDqlAlias, $relation['indexBy']);
- }
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkFunction($function)
- {
- return $function->getSql($this);
- }
- /**
- * {@inheritdoc}
- */
- public function walkOrderByClause($orderByClause)
- {
- $orderByItems = array_map([$this, 'walkOrderByItem'], $orderByClause->orderByItems);
- $collectionOrderByItems = $this->generateOrderedCollectionOrderByItems();
- if ($collectionOrderByItems !== '') {
- $orderByItems = array_merge($orderByItems, (array) $collectionOrderByItems);
- }
- return ' ORDER BY ' . implode(', ', $orderByItems);
- }
- /**
- * {@inheritdoc}
- */
- public function walkOrderByItem($orderByItem)
- {
- $type = strtoupper($orderByItem->type);
- $expr = $orderByItem->expression;
- $sql = $expr instanceof AST\Node
- ? $expr->dispatch($this)
- : $this->walkResultVariable($this->queryComponents[$expr]['token']['value']);
- $this->orderedColumnsMap[$sql] = $type;
- if ($expr instanceof AST\Subselect) {
- return '(' . $sql . ') ' . $type;
- }
- return $sql . ' ' . $type;
- }
- /**
- * {@inheritdoc}
- */
- public function walkHavingClause($havingClause)
- {
- return ' HAVING ' . $this->walkConditionalExpression($havingClause->conditionalExpression);
- }
- /**
- * {@inheritdoc}
- */
- public function walkJoin($join)
- {
- $joinType = $join->joinType;
- $joinDeclaration = $join->joinAssociationDeclaration;
- $sql = $joinType === AST\Join::JOIN_TYPE_LEFT || $joinType === AST\Join::JOIN_TYPE_LEFTOUTER
- ? ' LEFT JOIN '
- : ' INNER JOIN ';
- switch (true) {
- case $joinDeclaration instanceof AST\RangeVariableDeclaration:
- $class = $this->em->getClassMetadata($joinDeclaration->abstractSchemaName);
- $dqlAlias = $joinDeclaration->aliasIdentificationVariable;
- $tableAlias = $this->getSQLTableAlias($class->table['name'], $dqlAlias);
- $conditions = [];
- if ($join->conditionalExpression) {
- $conditions[] = '(' . $this->walkConditionalExpression($join->conditionalExpression) . ')';
- }
- $isUnconditionalJoin = empty($conditions);
- $condExprConjunction = $class->isInheritanceTypeJoined() && $joinType !== AST\Join::JOIN_TYPE_LEFT && $joinType !== AST\Join::JOIN_TYPE_LEFTOUTER && $isUnconditionalJoin
- ? ' AND '
- : ' ON ';
- $sql .= $this->generateRangeVariableDeclarationSQL($joinDeclaration, ! $isUnconditionalJoin);
- // Apply remaining inheritance restrictions
- $discrSql = $this->generateDiscriminatorColumnConditionSQL([$dqlAlias]);
- if ($discrSql) {
- $conditions[] = $discrSql;
- }
- // Apply the filters
- $filterExpr = $this->generateFilterConditionSQL($class, $tableAlias);
- if ($filterExpr) {
- $conditions[] = $filterExpr;
- }
- if ($conditions) {
- $sql .= $condExprConjunction . implode(' AND ', $conditions);
- }
- break;
- case $joinDeclaration instanceof AST\JoinAssociationDeclaration:
- $sql .= $this->walkJoinAssociationDeclaration($joinDeclaration, $joinType, $join->conditionalExpression);
- break;
- }
- return $sql;
- }
- /**
- * Walks down a CoalesceExpression AST node and generates the corresponding SQL.
- *
- * @param AST\CoalesceExpression $coalesceExpression
- *
- * @return string The SQL.
- */
- public function walkCoalesceExpression($coalesceExpression)
- {
- $sql = 'COALESCE(';
- $scalarExpressions = [];
- foreach ($coalesceExpression->scalarExpressions as $scalarExpression) {
- $scalarExpressions[] = $this->walkSimpleArithmeticExpression($scalarExpression);
- }
- return $sql . implode(', ', $scalarExpressions) . ')';
- }
- /**
- * Walks down a NullIfExpression AST node and generates the corresponding SQL.
- *
- * @param AST\NullIfExpression $nullIfExpression
- *
- * @return string The SQL.
- */
- public function walkNullIfExpression($nullIfExpression)
- {
- $firstExpression = is_string($nullIfExpression->firstExpression)
- ? $this->conn->quote($nullIfExpression->firstExpression)
- : $this->walkSimpleArithmeticExpression($nullIfExpression->firstExpression);
- $secondExpression = is_string($nullIfExpression->secondExpression)
- ? $this->conn->quote($nullIfExpression->secondExpression)
- : $this->walkSimpleArithmeticExpression($nullIfExpression->secondExpression);
- return 'NULLIF(' . $firstExpression . ', ' . $secondExpression . ')';
- }
- /**
- * Walks down a GeneralCaseExpression AST node and generates the corresponding SQL.
- *
- * @return string The SQL.
- */
- public function walkGeneralCaseExpression(AST\GeneralCaseExpression $generalCaseExpression)
- {
- $sql = 'CASE';
- foreach ($generalCaseExpression->whenClauses as $whenClause) {
- $sql .= ' WHEN ' . $this->walkConditionalExpression($whenClause->caseConditionExpression);
- $sql .= ' THEN ' . $this->walkSimpleArithmeticExpression($whenClause->thenScalarExpression);
- }
- $sql .= ' ELSE ' . $this->walkSimpleArithmeticExpression($generalCaseExpression->elseScalarExpression) . ' END';
- return $sql;
- }
- /**
- * Walks down a SimpleCaseExpression AST node and generates the corresponding SQL.
- *
- * @param AST\SimpleCaseExpression $simpleCaseExpression
- *
- * @return string The SQL.
- */
- public function walkSimpleCaseExpression($simpleCaseExpression)
- {
- $sql = 'CASE ' . $this->walkStateFieldPathExpression($simpleCaseExpression->caseOperand);
- foreach ($simpleCaseExpression->simpleWhenClauses as $simpleWhenClause) {
- $sql .= ' WHEN ' . $this->walkSimpleArithmeticExpression($simpleWhenClause->caseScalarExpression);
- $sql .= ' THEN ' . $this->walkSimpleArithmeticExpression($simpleWhenClause->thenScalarExpression);
- }
- $sql .= ' ELSE ' . $this->walkSimpleArithmeticExpression($simpleCaseExpression->elseScalarExpression) . ' END';
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkSelectExpression($selectExpression)
- {
- $sql = '';
- $expr = $selectExpression->expression;
- $hidden = $selectExpression->hiddenAliasResultVariable;
- switch (true) {
- case $expr instanceof AST\PathExpression:
- if ($expr->type !== AST\PathExpression::TYPE_STATE_FIELD) {
- throw QueryException::invalidPathExpression($expr);
- }
- $fieldName = $expr->field;
- $dqlAlias = $expr->identificationVariable;
- $qComp = $this->queryComponents[$dqlAlias];
- $class = $qComp['metadata'];
- $resultAlias = $selectExpression->fieldIdentificationVariable ?: $fieldName;
- $tableName = $class->isInheritanceTypeJoined()
- ? $this->em->getUnitOfWork()->getEntityPersister($class->name)->getOwningTable($fieldName)
- : $class->getTableName();
- $sqlTableAlias = $this->getSQLTableAlias($tableName, $dqlAlias);
- $fieldMapping = $class->fieldMappings[$fieldName];
- $columnName = $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform);
- $columnAlias = $this->getSQLColumnAlias($fieldMapping['columnName']);
- $col = $sqlTableAlias . '.' . $columnName;
- if (isset($fieldMapping['requireSQLConversion'])) {
- $type = Type::getType($fieldMapping['type']);
- $col = $type->convertToPHPValueSQL($col, $this->conn->getDatabasePlatform());
- }
- $sql .= $col . ' AS ' . $columnAlias;
- $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
- if (! $hidden) {
- $this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldMapping['type']);
- $this->scalarFields[$dqlAlias][$fieldName] = $columnAlias;
- }
- break;
- case $expr instanceof AST\AggregateExpression:
- case $expr instanceof AST\Functions\FunctionNode:
- case $expr instanceof AST\SimpleArithmeticExpression:
- case $expr instanceof AST\ArithmeticTerm:
- case $expr instanceof AST\ArithmeticFactor:
- case $expr instanceof AST\ParenthesisExpression:
- case $expr instanceof AST\Literal:
- case $expr instanceof AST\NullIfExpression:
- case $expr instanceof AST\CoalesceExpression:
- case $expr instanceof AST\GeneralCaseExpression:
- case $expr instanceof AST\SimpleCaseExpression:
- $columnAlias = $this->getSQLColumnAlias('sclr');
- $resultAlias = $selectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
- $sql .= $expr->dispatch($this) . ' AS ' . $columnAlias;
- $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
- if ($hidden) {
- break;
- }
- if (! $expr instanceof Query\AST\TypedExpression) {
- // Conceptually we could resolve field type here by traverse through AST to retrieve field type,
- // but this is not a feasible solution; assume 'string'.
- $this->rsm->addScalarResult($columnAlias, $resultAlias, 'string');
- break;
- }
- $this->rsm->addScalarResult($columnAlias, $resultAlias, $expr->getReturnType()->getName());
- break;
- case $expr instanceof AST\Subselect:
- $columnAlias = $this->getSQLColumnAlias('sclr');
- $resultAlias = $selectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
- $sql .= '(' . $this->walkSubselect($expr) . ') AS ' . $columnAlias;
- $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
- if (! $hidden) {
- // We cannot resolve field type here; assume 'string'.
- $this->rsm->addScalarResult($columnAlias, $resultAlias, 'string');
- }
- break;
- case $expr instanceof AST\NewObjectExpression:
- $sql .= $this->walkNewObject($expr, $selectExpression->fieldIdentificationVariable);
- break;
- default:
- // IdentificationVariable or PartialObjectExpression
- if ($expr instanceof AST\PartialObjectExpression) {
- $this->query->setHint(self::HINT_PARTIAL, true);
- $dqlAlias = $expr->identificationVariable;
- $partialFieldSet = $expr->partialFieldSet;
- } else {
- $dqlAlias = $expr;
- $partialFieldSet = [];
- }
- $queryComp = $this->queryComponents[$dqlAlias];
- $class = $queryComp['metadata'];
- $resultAlias = $selectExpression->fieldIdentificationVariable ?: null;
- if (! isset($this->selectedClasses[$dqlAlias])) {
- $this->selectedClasses[$dqlAlias] = [
- 'class' => $class,
- 'dqlAlias' => $dqlAlias,
- 'resultAlias' => $resultAlias,
- ];
- }
- $sqlParts = [];
- // Select all fields from the queried class
- foreach ($class->fieldMappings as $fieldName => $mapping) {
- if ($partialFieldSet && ! in_array($fieldName, $partialFieldSet)) {
- continue;
- }
- $tableName = isset($mapping['inherited'])
- ? $this->em->getClassMetadata($mapping['inherited'])->getTableName()
- : $class->getTableName();
- $sqlTableAlias = $this->getSQLTableAlias($tableName, $dqlAlias);
- $columnAlias = $this->getSQLColumnAlias($mapping['columnName']);
- $quotedColumnName = $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform);
- $col = $sqlTableAlias . '.' . $quotedColumnName;
- if (isset($mapping['requireSQLConversion'])) {
- $type = Type::getType($mapping['type']);
- $col = $type->convertToPHPValueSQL($col, $this->platform);
- }
- $sqlParts[] = $col . ' AS ' . $columnAlias;
- $this->scalarResultAliasMap[$resultAlias][] = $columnAlias;
- $this->rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $class->name);
- }
- // Add any additional fields of subclasses (excluding inherited fields)
- // 1) on Single Table Inheritance: always, since its marginal overhead
- // 2) on Class Table Inheritance only if partial objects are disallowed,
- // since it requires outer joining subtables.
- if ($class->isInheritanceTypeSingleTable() || ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
- foreach ($class->subClasses as $subClassName) {
- $subClass = $this->em->getClassMetadata($subClassName);
- $sqlTableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
- foreach ($subClass->fieldMappings as $fieldName => $mapping) {
- if (isset($mapping['inherited']) || ($partialFieldSet && ! in_array($fieldName, $partialFieldSet))) {
- continue;
- }
- $columnAlias = $this->getSQLColumnAlias($mapping['columnName']);
- $quotedColumnName = $this->quoteStrategy->getColumnName($fieldName, $subClass, $this->platform);
- $col = $sqlTableAlias . '.' . $quotedColumnName;
- if (isset($mapping['requireSQLConversion'])) {
- $type = Type::getType($mapping['type']);
- $col = $type->convertToPHPValueSQL($col, $this->platform);
- }
- $sqlParts[] = $col . ' AS ' . $columnAlias;
- $this->scalarResultAliasMap[$resultAlias][] = $columnAlias;
- $this->rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $subClassName);
- }
- }
- }
- $sql .= implode(', ', $sqlParts);
- }
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkQuantifiedExpression($qExpr)
- {
- return ' ' . strtoupper($qExpr->type) . '(' . $this->walkSubselect($qExpr->subselect) . ')';
- }
- /**
- * {@inheritdoc}
- */
- public function walkSubselect($subselect)
- {
- $useAliasesBefore = $this->useSqlTableAliases;
- $rootAliasesBefore = $this->rootAliases;
- $this->rootAliases = []; // reset the rootAliases for the subselect
- $this->useSqlTableAliases = true;
- $sql = $this->walkSimpleSelectClause($subselect->simpleSelectClause);
- $sql .= $this->walkSubselectFromClause($subselect->subselectFromClause);
- $sql .= $this->walkWhereClause($subselect->whereClause);
- $sql .= $subselect->groupByClause ? $this->walkGroupByClause($subselect->groupByClause) : '';
- $sql .= $subselect->havingClause ? $this->walkHavingClause($subselect->havingClause) : '';
- $sql .= $subselect->orderByClause ? $this->walkOrderByClause($subselect->orderByClause) : '';
- $this->rootAliases = $rootAliasesBefore; // put the main aliases back
- $this->useSqlTableAliases = $useAliasesBefore;
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkSubselectFromClause($subselectFromClause)
- {
- $identificationVarDecls = $subselectFromClause->identificationVariableDeclarations;
- $sqlParts = [];
- foreach ($identificationVarDecls as $subselectIdVarDecl) {
- $sqlParts[] = $this->walkIdentificationVariableDeclaration($subselectIdVarDecl);
- }
- return ' FROM ' . implode(', ', $sqlParts);
- }
- /**
- * {@inheritdoc}
- */
- public function walkSimpleSelectClause($simpleSelectClause)
- {
- return 'SELECT' . ($simpleSelectClause->isDistinct ? ' DISTINCT' : '')
- . $this->walkSimpleSelectExpression($simpleSelectClause->simpleSelectExpression);
- }
- /**
- * @return string
- */
- public function walkParenthesisExpression(AST\ParenthesisExpression $parenthesisExpression)
- {
- return sprintf('(%s)', $parenthesisExpression->expression->dispatch($this));
- }
- /**
- * @param AST\NewObjectExpression $newObjectExpression
- * @param string|null $newObjectResultAlias
- *
- * @return string The SQL.
- */
- public function walkNewObject($newObjectExpression, $newObjectResultAlias = null)
- {
- $sqlSelectExpressions = [];
- $objIndex = $newObjectResultAlias ?: $this->newObjectCounter++;
- foreach ($newObjectExpression->args as $argIndex => $e) {
- $resultAlias = $this->scalarResultCounter++;
- $columnAlias = $this->getSQLColumnAlias('sclr');
- $fieldType = 'string';
- switch (true) {
- case $e instanceof AST\NewObjectExpression:
- $sqlSelectExpressions[] = $e->dispatch($this);
- break;
- case $e instanceof AST\Subselect:
- $sqlSelectExpressions[] = '(' . $e->dispatch($this) . ') AS ' . $columnAlias;
- break;
- case $e instanceof AST\PathExpression:
- $dqlAlias = $e->identificationVariable;
- $qComp = $this->queryComponents[$dqlAlias];
- $class = $qComp['metadata'];
- $fieldType = $class->fieldMappings[$e->field]['type'];
- $fieldName = $e->field;
- $fieldMapping = $class->fieldMappings[$fieldName];
- $col = trim($e->dispatch($this));
- if (isset($fieldMapping['requireSQLConversion'])) {
- $type = Type::getType($fieldType);
- $col = $type->convertToPHPValueSQL($col, $this->platform);
- }
- $sqlSelectExpressions[] = $col . ' AS ' . $columnAlias;
- break;
- case $e instanceof AST\Literal:
- switch ($e->type) {
- case AST\Literal::BOOLEAN:
- $fieldType = 'boolean';
- break;
- case AST\Literal::NUMERIC:
- $fieldType = is_float($e->value) ? 'float' : 'integer';
- break;
- }
- $sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' . $columnAlias;
- break;
- default:
- $sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' . $columnAlias;
- break;
- }
- $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
- $this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldType);
- $this->rsm->newObjectMappings[$columnAlias] = [
- 'className' => $newObjectExpression->className,
- 'objIndex' => $objIndex,
- 'argIndex' => $argIndex,
- ];
- }
- return implode(', ', $sqlSelectExpressions);
- }
- /**
- * {@inheritdoc}
- */
- public function walkSimpleSelectExpression($simpleSelectExpression)
- {
- $expr = $simpleSelectExpression->expression;
- $sql = ' ';
- switch (true) {
- case $expr instanceof AST\PathExpression:
- $sql .= $this->walkPathExpression($expr);
- break;
- case $expr instanceof AST\Subselect:
- $alias = $simpleSelectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
- $columnAlias = 'sclr' . $this->aliasCounter++;
- $this->scalarResultAliasMap[$alias] = $columnAlias;
- $sql .= '(' . $this->walkSubselect($expr) . ') AS ' . $columnAlias;
- break;
- case $expr instanceof AST\Functions\FunctionNode:
- case $expr instanceof AST\SimpleArithmeticExpression:
- case $expr instanceof AST\ArithmeticTerm:
- case $expr instanceof AST\ArithmeticFactor:
- case $expr instanceof AST\Literal:
- case $expr instanceof AST\NullIfExpression:
- case $expr instanceof AST\CoalesceExpression:
- case $expr instanceof AST\GeneralCaseExpression:
- case $expr instanceof AST\SimpleCaseExpression:
- $alias = $simpleSelectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
- $columnAlias = $this->getSQLColumnAlias('sclr');
- $this->scalarResultAliasMap[$alias] = $columnAlias;
- $sql .= $expr->dispatch($this) . ' AS ' . $columnAlias;
- break;
- case $expr instanceof AST\ParenthesisExpression:
- $sql .= $this->walkParenthesisExpression($expr);
- break;
- default: // IdentificationVariable
- $sql .= $this->walkEntityIdentificationVariable($expr);
- break;
- }
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkAggregateExpression($aggExpression)
- {
- return $aggExpression->functionName . '(' . ($aggExpression->isDistinct ? 'DISTINCT ' : '')
- . $this->walkSimpleArithmeticExpression($aggExpression->pathExpression) . ')';
- }
- /**
- * {@inheritdoc}
- */
- public function walkGroupByClause($groupByClause)
- {
- $sqlParts = [];
- foreach ($groupByClause->groupByItems as $groupByItem) {
- $sqlParts[] = $this->walkGroupByItem($groupByItem);
- }
- return ' GROUP BY ' . implode(', ', $sqlParts);
- }
- /**
- * {@inheritdoc}
- */
- public function walkGroupByItem($groupByItem)
- {
- // StateFieldPathExpression
- if (! is_string($groupByItem)) {
- return $this->walkPathExpression($groupByItem);
- }
- // ResultVariable
- if (isset($this->queryComponents[$groupByItem]['resultVariable'])) {
- $resultVariable = $this->queryComponents[$groupByItem]['resultVariable'];
- if ($resultVariable instanceof AST\PathExpression) {
- return $this->walkPathExpression($resultVariable);
- }
- if (isset($resultVariable->pathExpression)) {
- return $this->walkPathExpression($resultVariable->pathExpression);
- }
- return $this->walkResultVariable($groupByItem);
- }
- // IdentificationVariable
- $sqlParts = [];
- foreach ($this->queryComponents[$groupByItem]['metadata']->fieldNames as $field) {
- $item = new AST\PathExpression(AST\PathExpression::TYPE_STATE_FIELD, $groupByItem, $field);
- $item->type = AST\PathExpression::TYPE_STATE_FIELD;
- $sqlParts[] = $this->walkPathExpression($item);
- }
- foreach ($this->queryComponents[$groupByItem]['metadata']->associationMappings as $mapping) {
- if ($mapping['isOwningSide'] && $mapping['type'] & ClassMetadataInfo::TO_ONE) {
- $item = new AST\PathExpression(AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $groupByItem, $mapping['fieldName']);
- $item->type = AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
- $sqlParts[] = $this->walkPathExpression($item);
- }
- }
- return implode(', ', $sqlParts);
- }
- /**
- * {@inheritdoc}
- */
- public function walkDeleteClause(AST\DeleteClause $deleteClause)
- {
- $class = $this->em->getClassMetadata($deleteClause->abstractSchemaName);
- $tableName = $class->getTableName();
- $sql = 'DELETE FROM ' . $this->quoteStrategy->getTableName($class, $this->platform);
- $this->setSQLTableAlias($tableName, $tableName, $deleteClause->aliasIdentificationVariable);
- $this->rootAliases[] = $deleteClause->aliasIdentificationVariable;
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkUpdateClause($updateClause)
- {
- $class = $this->em->getClassMetadata($updateClause->abstractSchemaName);
- $tableName = $class->getTableName();
- $sql = 'UPDATE ' . $this->quoteStrategy->getTableName($class, $this->platform);
- $this->setSQLTableAlias($tableName, $tableName, $updateClause->aliasIdentificationVariable);
- $this->rootAliases[] = $updateClause->aliasIdentificationVariable;
- return $sql . ' SET ' . implode(', ', array_map([$this, 'walkUpdateItem'], $updateClause->updateItems));
- }
- /**
- * {@inheritdoc}
- */
- public function walkUpdateItem($updateItem)
- {
- $useTableAliasesBefore = $this->useSqlTableAliases;
- $this->useSqlTableAliases = false;
- $sql = $this->walkPathExpression($updateItem->pathExpression) . ' = ';
- $newValue = $updateItem->newValue;
- switch (true) {
- case $newValue instanceof AST\Node:
- $sql .= $newValue->dispatch($this);
- break;
- case $newValue === null:
- $sql .= 'NULL';
- break;
- default:
- $sql .= $this->conn->quote($newValue);
- break;
- }
- $this->useSqlTableAliases = $useTableAliasesBefore;
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkWhereClause($whereClause)
- {
- $condSql = $whereClause !== null ? $this->walkConditionalExpression($whereClause->conditionalExpression) : '';
- $discrSql = $this->generateDiscriminatorColumnConditionSQL($this->rootAliases);
- if ($this->em->hasFilters()) {
- $filterClauses = [];
- foreach ($this->rootAliases as $dqlAlias) {
- $class = $this->queryComponents[$dqlAlias]['metadata'];
- $tableAlias = $this->getSQLTableAlias($class->table['name'], $dqlAlias);
- $filterExpr = $this->generateFilterConditionSQL($class, $tableAlias);
- if ($filterExpr) {
- $filterClauses[] = $filterExpr;
- }
- }
- if (count($filterClauses)) {
- if ($condSql) {
- $condSql = '(' . $condSql . ') AND ';
- }
- $condSql .= implode(' AND ', $filterClauses);
- }
- }
- if ($condSql) {
- return ' WHERE ' . (! $discrSql ? $condSql : '(' . $condSql . ') AND ' . $discrSql);
- }
- if ($discrSql) {
- return ' WHERE ' . $discrSql;
- }
- return '';
- }
- /**
- * {@inheritdoc}
- */
- public function walkConditionalExpression($condExpr)
- {
- // Phase 2 AST optimization: Skip processing of ConditionalExpression
- // if only one ConditionalTerm is defined
- if (! ($condExpr instanceof AST\ConditionalExpression)) {
- return $this->walkConditionalTerm($condExpr);
- }
- return implode(' OR ', array_map([$this, 'walkConditionalTerm'], $condExpr->conditionalTerms));
- }
- /**
- * {@inheritdoc}
- */
- public function walkConditionalTerm($condTerm)
- {
- // Phase 2 AST optimization: Skip processing of ConditionalTerm
- // if only one ConditionalFactor is defined
- if (! ($condTerm instanceof AST\ConditionalTerm)) {
- return $this->walkConditionalFactor($condTerm);
- }
- return implode(' AND ', array_map([$this, 'walkConditionalFactor'], $condTerm->conditionalFactors));
- }
- /**
- * {@inheritdoc}
- */
- public function walkConditionalFactor($factor)
- {
- // Phase 2 AST optimization: Skip processing of ConditionalFactor
- // if only one ConditionalPrimary is defined
- return ! ($factor instanceof AST\ConditionalFactor)
- ? $this->walkConditionalPrimary($factor)
- : ($factor->not ? 'NOT ' : '') . $this->walkConditionalPrimary($factor->conditionalPrimary);
- }
- /**
- * {@inheritdoc}
- */
- public function walkConditionalPrimary($primary)
- {
- if ($primary->isSimpleConditionalExpression()) {
- return $primary->simpleConditionalExpression->dispatch($this);
- }
- if ($primary->isConditionalExpression()) {
- $condExpr = $primary->conditionalExpression;
- return '(' . $this->walkConditionalExpression($condExpr) . ')';
- }
- }
- /**
- * {@inheritdoc}
- */
- public function walkExistsExpression($existsExpr)
- {
- $sql = $existsExpr->not ? 'NOT ' : '';
- $sql .= 'EXISTS (' . $this->walkSubselect($existsExpr->subselect) . ')';
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkCollectionMemberExpression($collMemberExpr)
- {
- $sql = $collMemberExpr->not ? 'NOT ' : '';
- $sql .= 'EXISTS (SELECT 1 FROM ';
- $entityExpr = $collMemberExpr->entityExpression;
- $collPathExpr = $collMemberExpr->collectionValuedPathExpression;
- $fieldName = $collPathExpr->field;
- $dqlAlias = $collPathExpr->identificationVariable;
- $class = $this->queryComponents[$dqlAlias]['metadata'];
- switch (true) {
- // InputParameter
- case $entityExpr instanceof AST\InputParameter:
- $dqlParamKey = $entityExpr->name;
- $entitySql = '?';
- break;
- // SingleValuedAssociationPathExpression | IdentificationVariable
- case $entityExpr instanceof AST\PathExpression:
- $entitySql = $this->walkPathExpression($entityExpr);
- break;
- default:
- throw new BadMethodCallException('Not implemented');
- }
- $assoc = $class->associationMappings[$fieldName];
- if ($assoc['type'] === ClassMetadata::ONE_TO_MANY) {
- $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
- $targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName());
- $sourceTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
- $sql .= $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' ' . $targetTableAlias . ' WHERE ';
- $owningAssoc = $targetClass->associationMappings[$assoc['mappedBy']];
- $sqlParts = [];
- foreach ($owningAssoc['targetToSourceKeyColumns'] as $targetColumn => $sourceColumn) {
- $targetColumn = $this->quoteStrategy->getColumnName($class->fieldNames[$targetColumn], $class, $this->platform);
- $sqlParts[] = $sourceTableAlias . '.' . $targetColumn . ' = ' . $targetTableAlias . '.' . $sourceColumn;
- }
- foreach ($this->quoteStrategy->getIdentifierColumnNames($targetClass, $this->platform) as $targetColumnName) {
- if (isset($dqlParamKey)) {
- $this->parserResult->addParameterMapping($dqlParamKey, $this->sqlParamIndex++);
- }
- $sqlParts[] = $targetTableAlias . '.' . $targetColumnName . ' = ' . $entitySql;
- }
- $sql .= implode(' AND ', $sqlParts);
- } else { // many-to-many
- $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
- $owningAssoc = $assoc['isOwningSide'] ? $assoc : $targetClass->associationMappings[$assoc['mappedBy']];
- $joinTable = $owningAssoc['joinTable'];
- // SQL table aliases
- $joinTableAlias = $this->getSQLTableAlias($joinTable['name']);
- $targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName());
- $sourceTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
- // join to target table
- $sql .= $this->quoteStrategy->getJoinTableName($owningAssoc, $targetClass, $this->platform) . ' ' . $joinTableAlias
- . ' INNER JOIN ' . $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' ' . $targetTableAlias . ' ON ';
- // join conditions
- $joinColumns = $assoc['isOwningSide'] ? $joinTable['inverseJoinColumns'] : $joinTable['joinColumns'];
- $joinSqlParts = [];
- foreach ($joinColumns as $joinColumn) {
- $targetColumn = $this->quoteStrategy->getColumnName($targetClass->fieldNames[$joinColumn['referencedColumnName']], $targetClass, $this->platform);
- $joinSqlParts[] = $joinTableAlias . '.' . $joinColumn['name'] . ' = ' . $targetTableAlias . '.' . $targetColumn;
- }
- $sql .= implode(' AND ', $joinSqlParts);
- $sql .= ' WHERE ';
- $joinColumns = $assoc['isOwningSide'] ? $joinTable['joinColumns'] : $joinTable['inverseJoinColumns'];
- $sqlParts = [];
- foreach ($joinColumns as $joinColumn) {
- $targetColumn = $this->quoteStrategy->getColumnName($class->fieldNames[$joinColumn['referencedColumnName']], $class, $this->platform);
- $sqlParts[] = $joinTableAlias . '.' . $joinColumn['name'] . ' = ' . $sourceTableAlias . '.' . $targetColumn;
- }
- foreach ($this->quoteStrategy->getIdentifierColumnNames($targetClass, $this->platform) as $targetColumnName) {
- if (isset($dqlParamKey)) {
- $this->parserResult->addParameterMapping($dqlParamKey, $this->sqlParamIndex++);
- }
- $sqlParts[] = $targetTableAlias . '.' . $targetColumnName . ' IN (' . $entitySql . ')';
- }
- $sql .= implode(' AND ', $sqlParts);
- }
- return $sql . ')';
- }
- /**
- * {@inheritdoc}
- */
- public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr)
- {
- $sizeFunc = new AST\Functions\SizeFunction('size');
- $sizeFunc->collectionPathExpression = $emptyCollCompExpr->expression;
- return $sizeFunc->getSql($this) . ($emptyCollCompExpr->not ? ' > 0' : ' = 0');
- }
- /**
- * {@inheritdoc}
- */
- public function walkNullComparisonExpression($nullCompExpr)
- {
- $expression = $nullCompExpr->expression;
- $comparison = ' IS' . ($nullCompExpr->not ? ' NOT' : '') . ' NULL';
- // Handle ResultVariable
- if (is_string($expression) && isset($this->queryComponents[$expression]['resultVariable'])) {
- return $this->walkResultVariable($expression) . $comparison;
- }
- // Handle InputParameter mapping inclusion to ParserResult
- if ($expression instanceof AST\InputParameter) {
- return $this->walkInputParameter($expression) . $comparison;
- }
- return $expression->dispatch($this) . $comparison;
- }
- /**
- * {@inheritdoc}
- */
- public function walkInExpression($inExpr)
- {
- $sql = $this->walkArithmeticExpression($inExpr->expression) . ($inExpr->not ? ' NOT' : '') . ' IN (';
- $sql .= $inExpr->subselect
- ? $this->walkSubselect($inExpr->subselect)
- : implode(', ', array_map([$this, 'walkInParameter'], $inExpr->literals));
- $sql .= ')';
- return $sql;
- }
- /**
- * {@inheritdoc}
- *
- * @throws QueryException
- */
- public function walkInstanceOfExpression($instanceOfExpr)
- {
- $sql = '';
- $dqlAlias = $instanceOfExpr->identificationVariable;
- $discrClass = $class = $this->queryComponents[$dqlAlias]['metadata'];
- if ($class->discriminatorColumn) {
- $discrClass = $this->em->getClassMetadata($class->rootEntityName);
- }
- if ($this->useSqlTableAliases) {
- $sql .= $this->getSQLTableAlias($discrClass->getTableName(), $dqlAlias) . '.';
- }
- $sql .= $class->discriminatorColumn['name'] . ($instanceOfExpr->not ? ' NOT IN ' : ' IN ');
- $sql .= $this->getChildDiscriminatorsFromClassMetadata($discrClass, $instanceOfExpr);
- return $sql;
- }
- /**
- * @param mixed $inParam
- *
- * @return string
- */
- public function walkInParameter($inParam)
- {
- return $inParam instanceof AST\InputParameter
- ? $this->walkInputParameter($inParam)
- : $this->walkLiteral($inParam);
- }
- /**
- * {@inheritdoc}
- */
- public function walkLiteral($literal)
- {
- switch ($literal->type) {
- case AST\Literal::STRING:
- return $this->conn->quote($literal->value);
- case AST\Literal::BOOLEAN:
- return $this->conn->getDatabasePlatform()->convertBooleans(strtolower($literal->value) === 'true');
- case AST\Literal::NUMERIC:
- return $literal->value;
- default:
- throw QueryException::invalidLiteral($literal);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function walkBetweenExpression($betweenExpr)
- {
- $sql = $this->walkArithmeticExpression($betweenExpr->expression);
- if ($betweenExpr->not) {
- $sql .= ' NOT';
- }
- $sql .= ' BETWEEN ' . $this->walkArithmeticExpression($betweenExpr->leftBetweenExpression)
- . ' AND ' . $this->walkArithmeticExpression($betweenExpr->rightBetweenExpression);
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkLikeExpression($likeExpr)
- {
- $stringExpr = $likeExpr->stringExpression;
- $leftExpr = is_string($stringExpr) && isset($this->queryComponents[$stringExpr]['resultVariable'])
- ? $this->walkResultVariable($stringExpr)
- : $stringExpr->dispatch($this);
- $sql = $leftExpr . ($likeExpr->not ? ' NOT' : '') . ' LIKE ';
- if ($likeExpr->stringPattern instanceof AST\InputParameter) {
- $sql .= $this->walkInputParameter($likeExpr->stringPattern);
- } elseif ($likeExpr->stringPattern instanceof AST\Functions\FunctionNode) {
- $sql .= $this->walkFunction($likeExpr->stringPattern);
- } elseif ($likeExpr->stringPattern instanceof AST\PathExpression) {
- $sql .= $this->walkPathExpression($likeExpr->stringPattern);
- } else {
- $sql .= $this->walkLiteral($likeExpr->stringPattern);
- }
- if ($likeExpr->escapeChar) {
- $sql .= ' ESCAPE ' . $this->walkLiteral($likeExpr->escapeChar);
- }
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkStateFieldPathExpression($stateFieldPathExpression)
- {
- return $this->walkPathExpression($stateFieldPathExpression);
- }
- /**
- * {@inheritdoc}
- */
- public function walkComparisonExpression($compExpr)
- {
- $leftExpr = $compExpr->leftExpression;
- $rightExpr = $compExpr->rightExpression;
- $sql = '';
- $sql .= $leftExpr instanceof AST\Node
- ? $leftExpr->dispatch($this)
- : (is_numeric($leftExpr) ? $leftExpr : $this->conn->quote($leftExpr));
- $sql .= ' ' . $compExpr->operator . ' ';
- $sql .= $rightExpr instanceof AST\Node
- ? $rightExpr->dispatch($this)
- : (is_numeric($rightExpr) ? $rightExpr : $this->conn->quote($rightExpr));
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkInputParameter($inputParam)
- {
- $this->parserResult->addParameterMapping($inputParam->name, $this->sqlParamIndex++);
- $parameter = $this->query->getParameter($inputParam->name);
- if ($parameter) {
- $type = $parameter->getType();
- if (Type::hasType($type)) {
- return Type::getType($type)->convertToDatabaseValueSQL('?', $this->platform);
- }
- }
- return '?';
- }
- /**
- * {@inheritdoc}
- */
- public function walkArithmeticExpression($arithmeticExpr)
- {
- return $arithmeticExpr->isSimpleArithmeticExpression()
- ? $this->walkSimpleArithmeticExpression($arithmeticExpr->simpleArithmeticExpression)
- : '(' . $this->walkSubselect($arithmeticExpr->subselect) . ')';
- }
- /**
- * {@inheritdoc}
- */
- public function walkSimpleArithmeticExpression($simpleArithmeticExpr)
- {
- if (! ($simpleArithmeticExpr instanceof AST\SimpleArithmeticExpression)) {
- return $this->walkArithmeticTerm($simpleArithmeticExpr);
- }
- return implode(' ', array_map([$this, 'walkArithmeticTerm'], $simpleArithmeticExpr->arithmeticTerms));
- }
- /**
- * {@inheritdoc}
- */
- public function walkArithmeticTerm($term)
- {
- if (is_string($term)) {
- return isset($this->queryComponents[$term])
- ? $this->walkResultVariable($this->queryComponents[$term]['token']['value'])
- : $term;
- }
- // Phase 2 AST optimization: Skip processing of ArithmeticTerm
- // if only one ArithmeticFactor is defined
- if (! ($term instanceof AST\ArithmeticTerm)) {
- return $this->walkArithmeticFactor($term);
- }
- return implode(' ', array_map([$this, 'walkArithmeticFactor'], $term->arithmeticFactors));
- }
- /**
- * {@inheritdoc}
- */
- public function walkArithmeticFactor($factor)
- {
- if (is_string($factor)) {
- return isset($this->queryComponents[$factor])
- ? $this->walkResultVariable($this->queryComponents[$factor]['token']['value'])
- : $factor;
- }
- // Phase 2 AST optimization: Skip processing of ArithmeticFactor
- // if only one ArithmeticPrimary is defined
- if (! ($factor instanceof AST\ArithmeticFactor)) {
- return $this->walkArithmeticPrimary($factor);
- }
- $sign = $factor->isNegativeSigned() ? '-' : ($factor->isPositiveSigned() ? '+' : '');
- return $sign . $this->walkArithmeticPrimary($factor->arithmeticPrimary);
- }
- /**
- * Walks down an ArithmeticPrimary that represents an AST node, thereby generating the appropriate SQL.
- *
- * @param mixed $primary
- *
- * @return string The SQL.
- */
- public function walkArithmeticPrimary($primary)
- {
- if ($primary instanceof AST\SimpleArithmeticExpression) {
- return '(' . $this->walkSimpleArithmeticExpression($primary) . ')';
- }
- if ($primary instanceof AST\Node) {
- return $primary->dispatch($this);
- }
- return $this->walkEntityIdentificationVariable($primary);
- }
- /**
- * {@inheritdoc}
- */
- public function walkStringPrimary($stringPrimary)
- {
- return is_string($stringPrimary)
- ? $this->conn->quote($stringPrimary)
- : $stringPrimary->dispatch($this);
- }
- /**
- * {@inheritdoc}
- */
- public function walkResultVariable($resultVariable)
- {
- $resultAlias = $this->scalarResultAliasMap[$resultVariable];
- if (is_array($resultAlias)) {
- return implode(', ', $resultAlias);
- }
- return $resultAlias;
- }
- /**
- * @return string The list in parentheses of valid child discriminators from the given class
- *
- * @throws QueryException
- */
- private function getChildDiscriminatorsFromClassMetadata(ClassMetadataInfo $rootClass, AST\InstanceOfExpression $instanceOfExpr): string
- {
- $sqlParameterList = [];
- $discriminators = [];
- foreach ($instanceOfExpr->value as $parameter) {
- if ($parameter instanceof AST\InputParameter) {
- $this->rsm->discriminatorParameters[$parameter->name] = $parameter->name;
- $sqlParameterList[] = $this->walkInParameter($parameter);
- continue;
- }
- $metadata = $this->em->getClassMetadata($parameter);
- if ($metadata->getName() !== $rootClass->name && ! $metadata->getReflectionClass()->isSubclassOf($rootClass->name)) {
- throw QueryException::instanceOfUnrelatedClass($parameter, $rootClass->name);
- }
- $discriminators += HierarchyDiscriminatorResolver::resolveDiscriminatorsForClass($metadata, $this->em);
- }
- foreach (array_keys($discriminators) as $dis) {
- $sqlParameterList[] = $this->conn->quote($dis);
- }
- return '(' . implode(', ', $sqlParameterList) . ')';
- }
- }
|