1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260 |
- <?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 Doctrine\DBAL\LockMode;
- use Doctrine\DBAL\Types\Type;
- use Doctrine\ORM\Mapping\ClassMetadata;
- use Doctrine\ORM\Query;
- use Doctrine\ORM\Query\QueryException;
- use Doctrine\ORM\OptimisticLockException;
- use Doctrine\ORM\Mapping\ClassMetadataInfo;
- /**
- * The SqlWalker is a TreeWalker that walks over a DQL AST and constructs
- * the corresponding SQL.
- *
- * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
- * @author Roman Borschel <roman@code-factory.org>
- * @author Benjamin Eberlei <kontakt@beberlei.de>
- * @author Alexander <iam.asm89@gmail.com>
- * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
- * @since 2.0
- * @todo Rename: SQLWalker
- */
- class SqlWalker implements TreeWalker
- {
- /**
- * @var string
- */
- const HINT_DISTINCT = 'doctrine.distinct';
- /**
- * @var ResultSetMapping
- */
- private $rsm;
- /**
- * Counter for generating unique column aliases.
- *
- * @var integer
- */
- private $aliasCounter = 0;
- /**
- * Counter for generating unique table aliases.
- *
- * @var integer
- */
- private $tableAliasCounter = 0;
- /**
- * Counter for generating unique scalar result.
- *
- * @var integer
- */
- private $scalarResultCounter = 1;
- /**
- * Counter for generating unique parameter indexes.
- *
- * @var integer
- */
- private $sqlParamIndex = 0;
- /**
- * Counter for generating indexes.
- *
- * @var integer
- */
- private $newObjectCounter = 0;
- /**
- * @var ParserResult
- */
- private $parserResult;
- /**
- * @var \Doctrine\ORM\EntityManager
- */
- private $em;
- /**
- * @var \Doctrine\DBAL\Connection
- */
- private $conn;
- /**
- * @var \Doctrine\ORM\AbstractQuery
- */
- private $query;
- /**
- * @var array
- */
- private $tableAliasMap = array();
- /**
- * Map from result variable names to their SQL column alias names.
- *
- * @var array
- */
- private $scalarResultAliasMap = array();
- /**
- * Map from Table-Alias + Column-Name to OrderBy-Direction.
- *
- * @var array
- */
- private $orderedColumnsMap = array();
- /**
- * Map from DQL-Alias + Field-Name to SQL Column Alias.
- *
- * @var array
- */
- private $scalarFields = array();
- /**
- * Map of all components/classes that appear in the DQL query.
- *
- * @var array
- */
- private $queryComponents;
- /**
- * A list of classes that appear in non-scalar SelectExpressions.
- *
- * @var array
- */
- private $selectedClasses = array();
- /**
- * The DQL alias of the root class of the currently traversed query.
- *
- * @var array
- */
- private $rootAliases = array();
- /**
- * 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 boolean
- */
- private $useSqlTableAliases = true;
- /**
- * The database platform abstraction.
- *
- * @var \Doctrine\DBAL\Platforms\AbstractPlatform
- */
- private $platform;
- /**
- * The quote strategy.
- *
- * @var \Doctrine\ORM\Mapping\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 \Doctrine\DBAL\Connection
- */
- public function getConnection()
- {
- return $this->conn;
- }
- /**
- * Gets the EntityManager used by the walker.
- *
- * @return \Doctrine\ORM\EntityManager
- */
- public function getEntityManager()
- {
- return $this->em;
- }
- /**
- * Gets the information about a single query component.
- *
- * @param string $dqlAlias The DQL alias.
- *
- * @return array
- */
- public function getQueryComponent($dqlAlias)
- {
- return $this->queryComponents[$dqlAlias];
- }
- /**
- * {@inheritdoc}
- */
- public function getQueryComponents()
- {
- return $this->queryComponents;
- }
- /**
- * {@inheritdoc}
- */
- public function setQueryComponent($dqlAlias, array $queryComponent)
- {
- $requiredKeys = array('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] = strtolower(substr($tableName, 0, 1)) . $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($class, $dqlAlias)
- {
- $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 = array();
- foreach ($this->quoteStrategy->getIdentifierColumnNames($class, $this->platform) as $columnName) {
- $sqlParts[] = $baseTableAlias . '.' . $columnName . ' = ' . $tableAlias . '.' . $columnName;
- }
- // Add filters on the root class
- if ($filterSql = $this->generateFilterConditionSQL($parentClass, $tableAlias)) {
- $sqlParts[] = $filterSql;
- }
- $sql .= implode(' AND ', $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 = array();
- foreach ($this->quoteStrategy->getIdentifierColumnNames($subClass, $this->platform) as $columnName) {
- $sqlParts[] = $baseTableAlias . '.' . $columnName . ' = ' . $tableAlias . '.' . $columnName;
- }
- $sql .= implode(' AND ', $sqlParts);
- }
- return $sql;
- }
- /**
- * @return string
- */
- private function _generateOrderedCollectionOrderByItems()
- {
- $orderedColumns = array();
- foreach ($this->selectedClasses as $selectedClass) {
- $dqlAlias = $selectedClass['dqlAlias'];
- $qComp = $this->queryComponents[$dqlAlias];
- $persister = $this->em->getUnitOfWork()->getEntityPersister($qComp['metadata']->name);
- if ( ! isset($qComp['relation']['orderBy'])) {
- continue;
- }
- 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.
- *
- * @param array $dqlAliases List of root DQL aliases to inspect for discriminator restrictions.
- *
- * @return string
- */
- private function _generateDiscriminatorColumnConditionSQL(array $dqlAliases)
- {
- $sqlParts = array();
- foreach ($dqlAliases as $dqlAlias) {
- $class = $this->queryComponents[$dqlAlias]['metadata'];
- if ( ! $class->isInheritanceTypeSingleTable()) continue;
- $conn = $this->em->getConnection();
- $values = array();
- 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);
- }
- $sqlParts[] = (($this->useSqlTableAliases) ? $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.' : '')
- . $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, $targetTableAlias)
- {
- 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 '';
- break;
- }
- $filterClauses = array();
- foreach ($this->em->getFilters()->getEnabledFilters() as $filter) {
- if ('' !== $filterExpr = $filter->addFilterConstraint($targetEntity, $targetTableAlias)) {
- $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);
- }
- if ( ! $AST->orderByClause && ($orderBySql = $this->_generateOrderedCollectionOrderByItems())) {
- $sql .= ' ORDER BY ' . $orderBySql;
- }
- if ($limit !== null || $offset !== null) {
- $sql = $this->platform->modifyLimitQuery($sql, $limit, $offset);
- }
- if ($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;
- return $this->walkUpdateClause($AST->updateClause)
- . $this->walkWhereClause($AST->whereClause);
- }
- /**
- * {@inheritdoc}
- */
- public function walkDeleteStatement(AST\DeleteStatement $AST)
- {
- $this->useSqlTableAliases = 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 = array();
- 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();
- }
- // 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(array($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']);
- }
- // 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)) {
- continue;
- } else if ( !$addMetaColumns && !isset($assoc['id'])) {
- continue;
- }
- $owningClass = (isset($assoc['inherited'])) ? $this->em->getClassMetadata($assoc['inherited']) : $class;
- $sqlTableAlias = $this->getSQLTableAlias($owningClass->getTableName(), $dqlAlias);
- foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
- $columnAlias = $this->getSQLColumnAlias($srcColumn);
- $sqlSelectExpressions[] = $sqlTableAlias . '.' . $srcColumn . ' AS ' . $columnAlias;
- $this->rsm->addMetaResult($dqlAlias, $columnAlias, $srcColumn, (isset($assoc['id']) && $assoc['id'] === true));
- }
- }
- // 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)) continue;
- foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
- $columnAlias = $this->getSQLColumnAlias($srcColumn);
- $sqlSelectExpressions[] = $sqlTableAlias . '.' . $srcColumn . ' AS ' . $columnAlias;
- $this->rsm->addMetaResult($dqlAlias, $columnAlias, $srcColumn);
- }
- }
- }
- }
- $sql .= implode(', ', $sqlSelectExpressions);
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- public function walkFromClause($fromClause)
- {
- $identificationVarDecls = $fromClause->identificationVariableDeclarations;
- $sqlParts = array();
- foreach ($identificationVarDecls as $identificationVariableDecl) {
- $sql = $this->walkRangeVariableDeclaration($identificationVariableDecl->rangeVariableDeclaration);
- foreach ($identificationVariableDecl->joins as $join) {
- $sql .= $this->walkJoin($join);
- }
- if ($identificationVariableDecl->indexBy) {
- $alias = $identificationVariableDecl->indexBy->simpleStateFieldPathExpression->identificationVariable;
- $field = $identificationVariableDecl->indexBy->simpleStateFieldPathExpression->field;
- if (isset($this->scalarFields[$alias][$field])) {
- $this->rsm->addIndexByScalar($this->scalarFields[$alias][$field]);
- } else {
- $this->rsm->addIndexBy(
- $identificationVariableDecl->indexBy->simpleStateFieldPathExpression->identificationVariable,
- $identificationVariableDecl->indexBy->simpleStateFieldPathExpression->field
- );
- }
- }
- $sqlParts[] = $sql;
- }
- return ' FROM ' . implode(', ', $sqlParts);
- }
- /**
- * Walks down a RangeVariableDeclaration AST node, thereby generating the appropriate SQL.
- *
- * @param AST\RangeVariableDeclaration $rangeVariableDeclaration
- *
- * @return string
- */
- public function walkRangeVariableDeclaration($rangeVariableDeclaration)
- {
- $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()) {
- $sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias);
- }
- return $sql;
- }
- /**
- * 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 = array();
- 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(array($joinedDqlAlias));
- if ($discrSql) {
- $conditions[] = $discrSql;
- }
- // Apply the filters
- $filterExpr = $this->generateFilterConditionSQL($targetClass, $targetTableAlias);
- if ($filterExpr) {
- $conditions[] = $filterExpr;
- }
- $targetTableJoin = array(
- '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 = array();
- $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 = array();
- $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(array($joinedDqlAlias));
- if ($discrSql) {
- $conditions[] = $discrSql;
- }
- // Apply the filters
- $filterExpr = $this->generateFilterConditionSQL($targetClass, $targetTableAlias);
- if ($filterExpr) {
- $conditions[] = $filterExpr;
- }
- $targetTableJoin = array(
- 'table' => $targetTableName . ' ' . $targetTableAlias,
- 'condition' => implode(' AND ', $conditions),
- );
- break;
- }
- // Handle WITH clause
- $withCondition = (null === $condExpr) ? '' : ('(' . $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->rsm->addIndexBy(
- $indexBy->simpleStateFieldPathExpression->identificationVariable,
- $indexBy->simpleStateFieldPathExpression->field
- );
- } else if (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(array($this, 'walkOrderByItem'), $orderByClause->orderByItems);
- if (($collectionOrderByItems = $this->_generateOrderedCollectionOrderByItems()) !== '') {
- $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;
- 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 \Doctrine\ORM\Query\AST\RangeVariableDeclaration):
- $class = $this->em->getClassMetadata($joinDeclaration->abstractSchemaName);
- $dqlAlias = $joinDeclaration->aliasIdentificationVariable;
- $tableAlias = $this->getSQLTableAlias($class->table['name'], $dqlAlias);
- $condition = '(' . $this->walkConditionalExpression($join->conditionalExpression) . ')';
- $condExprConjunction = ($class->isInheritanceTypeJoined() && $joinType != AST\Join::JOIN_TYPE_LEFT && $joinType != AST\Join::JOIN_TYPE_LEFTOUTER)
- ? ' AND '
- : ' ON ';
- $sql .= $this->walkRangeVariableDeclaration($joinDeclaration);
- $conditions = array($condition);
- // Apply remaining inheritance restrictions
- $discrSql = $this->_generateDiscriminatorColumnConditionSQL(array($dqlAlias));
- if ($discrSql) {
- $conditions[] = $discrSql;
- }
- // Apply the filters
- $filterExpr = $this->generateFilterConditionSQL($class, $tableAlias);
- if ($filterExpr) {
- $conditions[] = $filterExpr;
- }
- $sql .= $condExprConjunction . implode(' AND ', $conditions);
- break;
- case ($joinDeclaration instanceof \Doctrine\ORM\Query\AST\JoinAssociationDeclaration):
- $sql .= $this->walkJoinAssociationDeclaration($joinDeclaration, $joinType, $join->conditionalExpression);
- break;
- }
- return $sql;
- }
- /**
- * Walks down a CaseExpression AST node and generates the corresponding SQL.
- *
- * @param AST\CoalesceExpression|AST\NullIfExpression|AST\GeneralCaseExpression|AST\SimpleCaseExpression $expression
- *
- * @return string The SQL.
- */
- public function walkCaseExpression($expression)
- {
- switch (true) {
- case ($expression instanceof AST\CoalesceExpression):
- return $this->walkCoalesceExpression($expression);
- case ($expression instanceof AST\NullIfExpression):
- return $this->walkNullIfExpression($expression);
- case ($expression instanceof AST\GeneralCaseExpression):
- return $this->walkGeneralCaseExpression($expression);
- case ($expression instanceof AST\SimpleCaseExpression):
- return $this->walkSimpleCaseExpression($expression);
- default:
- return '';
- }
- }
- /**
- * 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 = array();
- foreach ($coalesceExpression->scalarExpressions as $scalarExpression) {
- $scalarExpressions[] = $this->walkSimpleArithmeticExpression($scalarExpression);
- }
- $sql .= implode(', ', $scalarExpressions) . ')';
- return $sql;
- }
- /**
- * 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.
- *
- * @param AST\GeneralCaseExpression $generalCaseExpression
- *
- * @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);
- $columnName = $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform);
- $columnAlias = $this->getSQLColumnAlias($class->fieldMappings[$fieldName]['columnName']);
- $col = $sqlTableAlias . '.' . $columnName;
- $fieldType = $class->getTypeOfField($fieldName);
- if (isset($class->fieldMappings[$fieldName]['requireSQLConversion'])) {
- $type = Type::getType($fieldType);
- $col = $type->convertToPHPValueSQL($col, $this->conn->getDatabasePlatform());
- }
- $sql .= $col . ' AS ' . $columnAlias;
- $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
- if ( ! $hidden) {
- $this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldType);
- $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) {
- // We cannot resolve field type here; assume 'string'.
- $this->rsm->addScalarResult($columnAlias, $resultAlias, 'string');
- }
- 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);
- break;
- default:
- // IdentificationVariable or PartialObjectExpression
- if ($expr instanceof AST\PartialObjectExpression) {
- $dqlAlias = $expr->identificationVariable;
- $partialFieldSet = $expr->partialFieldSet;
- } else {
- $dqlAlias = $expr;
- $partialFieldSet = array();
- }
- $queryComp = $this->queryComponents[$dqlAlias];
- $class = $queryComp['metadata'];
- $resultAlias = $selectExpression->fieldIdentificationVariable ?: null;
- if ( ! isset($this->selectedClasses[$dqlAlias])) {
- $this->selectedClasses[$dqlAlias] = array(
- 'class' => $class,
- 'dqlAlias' => $dqlAlias,
- 'resultAlias' => $resultAlias
- );
- }
- $sqlParts = array();
- // 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($class->fieldMappings[$fieldName]['requireSQLConversion'])) {
- $type = Type::getType($class->getTypeOfField($fieldName));
- $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($subClass->fieldMappings[$fieldName]['requireSQLConversion'])) {
- $type = Type::getType($subClass->getTypeOfField($fieldName));
- $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 = array(); // 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 = array ();
- foreach ($identificationVarDecls as $subselectIdVarDecl) {
- $sql = $this->walkRangeVariableDeclaration($subselectIdVarDecl->rangeVariableDeclaration);
- foreach ($subselectIdVarDecl->joins as $join) {
- $sql .= $this->walkJoin($join);
- }
- $sqlParts[] = $sql;
- }
- return ' FROM ' . implode(', ', $sqlParts);
- }
- /**
- * {@inheritdoc}
- */
- public function walkSimpleSelectClause($simpleSelectClause)
- {
- return 'SELECT' . ($simpleSelectClause->isDistinct ? ' DISTINCT' : '')
- . $this->walkSimpleSelectExpression($simpleSelectClause->simpleSelectExpression);
- }
- /**
- * @param \Doctrine\ORM\Query\AST\ParenthesisExpression $parenthesisExpression
- *
- * @return string.
- */
- public function walkParenthesisExpression(AST\ParenthesisExpression $parenthesisExpression)
- {
- return sprintf('(%s)', $parenthesisExpression->expression->dispatch($this));
- }
- /**
- * @param AST\NewObjectExpression $newObjectExpression
- *
- * @return string The SQL.
- */
- public function walkNewObject($newObjectExpression)
- {
- $sqlSelectExpressions = array();
- $objIndex = $this->newObjectCounter++;
- foreach ($newObjectExpression->args as $argIndex => $e) {
- $resultAlias = $this->scalarResultCounter++;
- $columnAlias = $this->getSQLColumnAlias('sclr');
- switch (true) {
- case ($e instanceof AST\NewObjectExpression):
- $sqlSelectExpressions[] = $e->dispatch($this);
- break;
- default:
- $sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' . $columnAlias;
- break;
- }
- $fieldType = 'string';
- switch (true) {
- case ($e instanceof AST\PathExpression):
- $fieldName = $e->field;
- $dqlAlias = $e->identificationVariable;
- $qComp = $this->queryComponents[$dqlAlias];
- $class = $qComp['metadata'];
- $fieldType = $class->getTypeOfField($fieldName);
- 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;
- }
- break;
- }
- $this->scalarResultAliasMap[$resultAlias] = $columnAlias;
- $this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldType);
- $this->rsm->newObjectMappings[$columnAlias] = array(
- '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\AggregateExpression):
- $alias = $simpleSelectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
- $sql .= $this->walkAggregateExpression($expr) . ' AS dctrn__' . $alias;
- 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;
- 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 = array();
- 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'])) {
- return $this->walkResultVariable($groupByItem);
- }
- // IdentificationVariable
- $sqlParts = array();
- 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;
- $sql .= ' SET ' . implode(', ', array_map(array($this, 'walkUpdateItem'), $updateClause->updateItems));
- return $sql;
- }
- /**
- * {@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 = null !== $whereClause ? $this->walkConditionalExpression($whereClause->conditionalExpression) : '';
- $discrSql = $this->_generateDiscriminatorColumnConditionSql($this->rootAliases);
- if ($this->em->hasFilters()) {
- $filterClauses = array();
- foreach ($this->rootAliases as $dqlAlias) {
- $class = $this->queryComponents[$dqlAlias]['metadata'];
- $tableAlias = $this->getSQLTableAlias($class->table['name'], $dqlAlias);
- if ($filterExpr = $this->generateFilterConditionSQL($class, $tableAlias)) {
- $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(array($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(array($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 = array();
- 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 = array();
- 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 = array();
- 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 . ' = ' . $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';
- if ($expression instanceof AST\InputParameter) {
- $this->parserResult->addParameterMapping($expression->name, $this->sqlParamIndex++);
- return '?' . $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(array($this, 'walkInParameter'), $inExpr->literals));
- $sql .= ')';
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- 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 ');
- $sqlParameterList = array();
- foreach ($instanceOfExpr->value as $parameter) {
- if ($parameter instanceof AST\InputParameter) {
- $this->rsm->addMetadataParameterMapping($parameter->name, 'discriminatorValue');
- $sqlParameterList[] = $this->walkInputParameter($parameter);
- continue;
- }
- // Get name from ClassMetadata to resolve aliases.
- $entityClassName = $this->em->getClassMetadata($parameter)->name;
- $discriminatorValue = $class->discriminatorValue;
- if ($entityClassName !== $class->name) {
- $discrMap = array_flip($class->discriminatorMap);
- if ( ! isset($discrMap[$entityClassName])) {
- throw QueryException::instanceOfUnrelatedClass($entityClassName, $class->rootEntityName);
- }
- $discriminatorValue = $discrMap[$entityClassName];
- }
- $sqlParameterList[] = $this->conn->quote($discriminatorValue);
- }
- $sql .= '(' . implode(', ', $sqlParameterList) . ')';
- return $sql;
- }
- /**
- * {@inheritdoc}
- */
- 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:
- $bool = strtolower($literal->value) == 'true' ? true : false;
- $boolVal = $this->conn->getDatabasePlatform()->convertBooleans($bool);
- return $boolVal;
- 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;
- $sql = $stringExpr->dispatch($this) . ($likeExpr->not ? ' NOT' : '') . ' LIKE ';
- if ($likeExpr->stringPattern instanceof AST\InputParameter) {
- $inputParam = $likeExpr->stringPattern;
- $dqlParamKey = $inputParam->name;
- $this->parserResult->addParameterMapping($dqlParamKey, $this->sqlParamIndex++);
- $sql .= '?';
- } 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++);
- 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(array($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(array($this, 'walkArithmeticFactor'), $term->arithmeticFactors));
- }
- /**
- * {@inheritdoc}
- */
- public function walkArithmeticFactor($factor)
- {
- if (is_string($factor)) {
- return $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;
- }
- }
|