/vendor/doctrine/lib/Doctrine/ORM/Query/SqlWalker.php
PHP | 1882 lines | 1140 code | 285 blank | 457 comment | 252 complexity | d2b00888246e968c2cd133bd4bebc7f7 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- <?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 LGPL. For more information, see
- * <http://www.doctrine-project.org>.
- */
- namespace Doctrine\ORM\Query;
- use Doctrine\DBAL\LockMode,
- Doctrine\ORM\Mapping\ClassMetadata,
- Doctrine\ORM\Query,
- Doctrine\ORM\Query\QueryException;
- /**
- * The SqlWalker is a TreeWalker that walks over a DQL AST and constructs
- * the corresponding SQL.
- *
- * @author Roman Borschel <roman@code-factory.org>
- * @author Benjamin Eberlei <kontakt@beberlei.de>
- * @since 2.0
- * @todo Rename: SQLWalker
- */
- class SqlWalker implements TreeWalker
- {
- /**
- * @var ResultSetMapping
- */
- private $_rsm;
- /** Counters for generating unique column aliases, table aliases and parameter indexes. */
- private $_aliasCounter = 0;
- private $_tableAliasCounter = 0;
- private $_scalarResultCounter = 1;
- private $_sqlParamIndex = 1;
- /**
- * @var ParserResult
- */
- private $_parserResult;
- /**
- * @var EntityManager
- */
- private $_em;
- /**
- * @var Doctrine\DBAL\Connection
- */
- private $_conn;
- /**
- * @var AbstractQuery
- */
- private $_query;
- private $_tableAliasMap = array();
- /** Map from result variable names to their SQL column alias names. */
- private $_scalarResultAliasMap = array();
- /** Map of all components/classes that appear in the DQL query. */
- private $_queryComponents;
- /** A list of classes that appear in non-scalar SelectExpressions. */
- private $_selectedClasses = array();
- /**
- * The DQL alias of the root class of the currently traversed query.
- */
- 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.
- */
- private $_useSqlTableAliases = true;
- /**
- * The database platform abstraction.
- *
- * @var AbstractPlatform
- */
- private $_platform;
- /**
- * {@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();
- }
- /**
- * 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.
- * @return array
- */
- public function getQueryComponent($dqlAlias)
- {
- return $this->_queryComponents[$dqlAlias];
- }
- /**
- * Gets an executor that can be used to execute the result of this walker.
- *
- * @return AbstractExecutor
- */
- public function getExecutor($AST)
- {
- $isDeleteStatement = $AST instanceof AST\DeleteStatement;
- $isUpdateStatement = $AST instanceof AST\UpdateStatement;
- if ($isDeleteStatement) {
- $primaryClass = $this->_em->getClassMetadata(
- $AST->deleteClause->abstractSchemaName
- );
- if ($primaryClass->isInheritanceTypeJoined()) {
- return new Exec\MultiTableDeleteExecutor($AST, $this);
- } else {
- return new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
- }
- } else if ($isUpdateStatement) {
- $primaryClass = $this->_em->getClassMetadata(
- $AST->updateClause->abstractSchemaName
- );
- if ($primaryClass->isInheritanceTypeJoined()) {
- return new Exec\MultiTableUpdateExecutor($AST, $this);
- } else {
- return new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
- }
- }
- 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;
- 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;
- $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 $columnName . $this->_aliasCounter++;
- }
- /**
- * 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->table['name'], $dqlAlias);
- // INNER JOIN parent class tables
- foreach ($class->parentClasses as $parentClassName) {
- $parentClass = $this->_em->getClassMetadata($parentClassName);
- $tableAlias = $this->getSQLTableAlias($parentClass->table['name'], $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 ' . $parentClass->getQuotedTableName($this->_platform)
- . ' ' . $tableAlias . ' ON ';
- $first = true;
- foreach ($class->identifier as $idField) {
- if ($first) $first = false; else $sql .= ' AND ';
- $columnName = $class->getQuotedColumnName($idField, $this->_platform);
- $sql .= $baseTableAlias . '.' . $columnName
- . ' = '
- . $tableAlias . '.' . $columnName;
- }
- }
- // LEFT JOIN subclass tables, if partial objects disallowed.
- if ( ! $this->_query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
- foreach ($class->subClasses as $subClassName) {
- $subClass = $this->_em->getClassMetadata($subClassName);
- $tableAlias = $this->getSQLTableAlias($subClass->table['name'], $dqlAlias);
- $sql .= ' LEFT JOIN ' . $subClass->getQuotedTableName($this->_platform)
- . ' ' . $tableAlias . ' ON ';
- $first = true;
- foreach ($class->identifier as $idField) {
- if ($first) $first = false; else $sql .= ' AND ';
- $columnName = $class->getQuotedColumnName($idField, $this->_platform);
- $sql .= $baseTableAlias . '.' . $columnName
- . ' = '
- . $tableAlias . '.' . $columnName;
- }
- }
- }
- return $sql;
- }
- private function _generateOrderedCollectionOrderByItems()
- {
- $sql = '';
- foreach ($this->_selectedClasses AS $dqlAlias => $class) {
- $qComp = $this->_queryComponents[$dqlAlias];
- if (isset($qComp['relation']['orderBy'])) {
- foreach ($qComp['relation']['orderBy'] AS $fieldName => $orientation) {
- if ($qComp['metadata']->isInheritanceTypeJoined()) {
- $tableName = $this->_em->getUnitOfWork()->getEntityPersister($class->name)->getOwningTable($fieldName);
- } else {
- $tableName = $qComp['metadata']->table['name'];
- }
- if ($sql != '') {
- $sql .= ', ';
- }
- $sql .= $this->getSqlTableAlias($tableName, $dqlAlias) . '.' .
- $qComp['metadata']->getQuotedColumnName($fieldName, $this->_platform) . " $orientation";
- }
- }
- }
- return $sql;
- }
- /**
- * 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)
- {
- $encapsulate = false;
- $sql = '';
- foreach ($dqlAliases as $dqlAlias) {
- $class = $this->_queryComponents[$dqlAlias]['metadata'];
- if ($class->isInheritanceTypeSingleTable()) {
- $conn = $this->_em->getConnection();
- $values = array();
- if ($class->discriminatorValue !== null) { // discrimnators can be 0
- $values[] = $conn->quote($class->discriminatorValue);
- }
- foreach ($class->subClasses as $subclassName) {
- $values[] = $conn->quote($this->_em->getClassMetadata($subclassName)->discriminatorValue);
- }
- if ($sql != '') {
- $sql .= ' AND ';
- $encapsulate = true;
- }
- $sql .= ($sql != '' ? ' AND ' : '')
- . (($this->_useSqlTableAliases) ? $this->getSqlTableAlias($class->table['name'], $dqlAlias) . '.' : '')
- . $class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')';
- }
- }
- return ($encapsulate) ? '(' . $sql . ')' : $sql;
- }
- /**
- * Walks down a SelectStatement AST node, thereby generating the appropriate SQL.
- *
- * @return string The SQL.
- */
- public function walkSelectStatement(AST\SelectStatement $AST)
- {
- $sql = $this->walkSelectClause($AST->selectClause);
- $sql .= $this->walkFromClause($AST->fromClause);
- if (($whereClause = $AST->whereClause) !== null) {
- $sql .= $this->walkWhereClause($whereClause);
- } else if (($discSql = $this->_generateDiscriminatorColumnConditionSQL($this->_rootAliases)) !== '') {
- $sql .= ' WHERE ' . $discSql;
- }
- $sql .= $AST->groupByClause ? $this->walkGroupByClause($AST->groupByClause) : '';
- $sql .= $AST->havingClause ? $this->walkHavingClause($AST->havingClause) : '';
- if (($orderByClause = $AST->orderByClause) !== null) {
- $sql .= $AST->orderByClause ? $this->walkOrderByClause($AST->orderByClause) : '';
- } else if (($orderBySql = $this->_generateOrderedCollectionOrderByItems()) !== '') {
- $sql .= ' ORDER BY '.$orderBySql;
- }
- $sql = $this->_platform->modifyLimitQuery(
- $sql, $this->_query->getMaxResults(), $this->_query->getFirstResult()
- );
- if (($lockMode = $this->_query->getHint(Query::HINT_LOCK_MODE)) !== false) {
- if ($lockMode == LockMode::PESSIMISTIC_READ) {
- $sql .= " " . $this->_platform->getReadLockSQL();
- } else if ($lockMode == LockMode::PESSIMISTIC_WRITE) {
- $sql .= " " . $this->_platform->getWriteLockSQL();
- } else if ($lockMode == LockMode::OPTIMISTIC) {
- foreach ($this->_selectedClasses AS $class) {
- if ( ! $class->isVersioned) {
- throw \Doctrine\ORM\OptimisticLockException::lockFailed();
- }
- }
- }
- }
- return $sql;
- }
- /**
- * Walks down an UpdateStatement AST node, thereby generating the appropriate SQL.
- *
- * @param UpdateStatement
- * @return string The SQL.
- */
- public function walkUpdateStatement(AST\UpdateStatement $AST)
- {
- $this->_useSqlTableAliases = false;
- $sql = $this->walkUpdateClause($AST->updateClause);
- if (($whereClause = $AST->whereClause) !== null) {
- $sql .= $this->walkWhereClause($whereClause);
- } else if (($discSql = $this->_generateDiscriminatorColumnConditionSQL($this->_rootAliases)) !== '') {
- $sql .= ' WHERE ' . $discSql;
- }
- return $sql;
- }
- /**
- * Walks down a DeleteStatement AST node, thereby generating the appropriate SQL.
- *
- * @param DeleteStatement
- * @return string The SQL.
- */
- public function walkDeleteStatement(AST\DeleteStatement $AST)
- {
- $this->_useSqlTableAliases = false;
- $sql = $this->walkDeleteClause($AST->deleteClause);
- if (($whereClause = $AST->whereClause) !== null) {
- $sql .= $this->walkWhereClause($whereClause);
- } else if (($discSql = $this->_generateDiscriminatorColumnConditionSQL($this->_rootAliases)) !== '') {
- $sql .= ' WHERE ' . $discSql;
- }
- return $sql;
- }
- /**
- * 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->table['name'], $identificationVariable);
- }
- /**
- * Walks down a PathExpression AST node, thereby generating the appropriate SQL.
- *
- * @param mixed
- * @return string The SQL.
- */
- 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 .= $class->getQuotedColumnName($fieldName, $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']) {
- // COMPOSITE KEYS NOT (YET?) SUPPORTED
- if (count($assoc['sourceToTargetKeyColumns']) > 1) {
- throw QueryException::associationPathCompositeKeyNotSupported();
- }
- if ($this->_useSqlTableAliases) {
- $sql .= $this->getSqlTableAlias($class->table['name'], $dqlAlias) . '.';
- }
- $sql .= reset($assoc['targetToSourceKeyColumns']);
- } else {
- throw QueryException::associationPathInverseSideNotSupported();
- }
- break;
-
- default:
- throw QueryException::invalidPathExpression($pathExpr);
- }
- return $sql;
- }
- /**
- * Walks down a SelectClause AST node, thereby generating the appropriate SQL.
- *
- * @param $selectClause
- * @return string The SQL.
- */
- public function walkSelectClause($selectClause)
- {
- $sql = 'SELECT ' . (($selectClause->isDistinct) ? 'DISTINCT ' : '') . implode(
- ', ', array_map(array($this, 'walkSelectExpression'), $selectClause->selectExpressions)
- );
- $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 $dqlAlias => $class) {
- // Register as entity or joined entity result
- if ($this->_queryComponents[$dqlAlias]['relation'] === null) {
- $this->_rsm->addEntityResult($class->name, $dqlAlias);
- } 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->table['name'], $dqlAlias);
- $discrColumn = $rootClass->discriminatorColumn;
- $columnAlias = $this->getSqlColumnAlias($discrColumn['name']);
- $sql .= ", $tblAlias." . $discrColumn['name'] . ' AS ' . $columnAlias;
- $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
- $this->_rsm->setDiscriminatorColumn($dqlAlias, $columnAlias);
- $this->_rsm->addMetaResult($dqlAlias, $this->_platform->getSQLResultCasing($columnAlias), $discrColumn['fieldName']);
- // Add foreign key columns to SQL, if necessary
- if ($addMetaColumns) {
- //FIXME: Include foreign key columns of child classes also!!??
- foreach ($class->associationMappings as $assoc) {
- if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) {
- if (isset($assoc['inherited'])) {
- $owningClass = $this->_em->getClassMetadata($assoc['inherited']);
- $sqlTableAlias = $this->getSqlTableAlias($owningClass->table['name'], $dqlAlias);
- } else {
- $sqlTableAlias = $this->getSqlTableAlias($class->table['name'], $dqlAlias);
- }
-
- foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
- $columnAlias = $this->getSqlColumnAlias($srcColumn);
- $sql .= ", $sqlTableAlias." . $srcColumn . ' AS ' . $columnAlias;
- $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
- $this->_rsm->addMetaResult($dqlAlias, $this->_platform->getSQLResultCasing($columnAlias), $srcColumn);
- }
- }
- }
- }
- } else {
- // Add foreign key columns to SQL, if necessary
- if ($addMetaColumns) {
- $sqlTableAlias = $this->getSqlTableAlias($class->table['name'], $dqlAlias);
- foreach ($class->associationMappings as $assoc) {
- if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) {
- foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
- $columnAlias = $this->getSqlColumnAlias($srcColumn);
- $sql .= ', ' . $sqlTableAlias . '.' . $srcColumn . ' AS ' . $columnAlias;
- $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
- $this->_rsm->addMetaResult($dqlAlias, $this->_platform->getSQLResultCasing($columnAlias), $srcColumn);
- }
- }
- }
- }
- }
- }
- return $sql;
- }
- /**
- * Walks down a FromClause AST node, thereby generating the appropriate SQL.
- *
- * @return string The SQL.
- */
- public function walkFromClause($fromClause)
- {
- $identificationVarDecls = $fromClause->identificationVariableDeclarations;
- $sqlParts = array();
- foreach ($identificationVarDecls as $identificationVariableDecl) {
- $sql = '';
- $rangeDecl = $identificationVariableDecl->rangeVariableDeclaration;
- $dqlAlias = $rangeDecl->aliasIdentificationVariable;
-
- $this->_rootAliases[] = $dqlAlias;
- $class = $this->_em->getClassMetadata($rangeDecl->abstractSchemaName);
- $sql .= $class->getQuotedTableName($this->_platform) . ' '
- . $this->getSqlTableAlias($class->table['name'], $dqlAlias);
- if ($class->isInheritanceTypeJoined()) {
- $sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias);
- }
- foreach ($identificationVariableDecl->joinVariableDeclarations as $joinVarDecl) {
- $sql .= $this->walkJoinVariableDeclaration($joinVarDecl);
- }
- if ($identificationVariableDecl->indexBy) {
- $this->_rsm->addIndexBy(
- $identificationVariableDecl->indexBy->simpleStateFieldPathExpression->identificationVariable,
- $identificationVariableDecl->indexBy->simpleStateFieldPathExpression->field
- );
- }
- $sqlParts[] = $this->_platform->appendLockHint($sql, $this->_query->getHint(Query::HINT_LOCK_MODE));
- }
- return ' FROM ' . implode(', ', $sqlParts);
- }
- /**
- * Walks down a FunctionNode AST node, thereby generating the appropriate SQL.
- *
- * @return string The SQL.
- */
- public function walkFunction($function)
- {
- return $function->getSql($this);
- }
- /**
- * Walks down an OrderByClause AST node, thereby generating the appropriate SQL.
- *
- * @param OrderByClause
- * @return string The SQL.
- */
- public function walkOrderByClause($orderByClause)
- {
- $colSql = $this->_generateOrderedCollectionOrderByItems();
- if ($colSql != '') {
- $colSql = ", ".$colSql;
- }
- // OrderByClause ::= "ORDER" "BY" OrderByItem {"," OrderByItem}*
- return ' ORDER BY ' . implode(
- ', ', array_map(array($this, 'walkOrderByItem'), $orderByClause->orderByItems)
- ) . $colSql;
- }
- /**
- * Walks down an OrderByItem AST node, thereby generating the appropriate SQL.
- *
- * @param OrderByItem
- * @return string The SQL.
- */
- public function walkOrderByItem($orderByItem)
- {
- $sql = '';
- $expr = $orderByItem->expression;
- if ($expr instanceof AST\PathExpression) {
- $sql = $this->walkPathExpression($expr);
- } else {
- $columnName = $this->_queryComponents[$expr]['token']['value'];
- $sql = $this->_scalarResultAliasMap[$columnName];
- }
- return $sql . ' ' . strtoupper($orderByItem->type);
- }
- /**
- * Walks down a HavingClause AST node, thereby generating the appropriate SQL.
- *
- * @param HavingClause
- * @return string The SQL.
- */
- public function walkHavingClause($havingClause)
- {
- return ' HAVING ' . $this->walkConditionalExpression($havingClause->conditionalExpression);
- }
- /**
- * Walks down a JoinVariableDeclaration AST node and creates the corresponding SQL.
- *
- * @param JoinVariableDeclaration $joinVarDecl
- * @return string The SQL.
- */
- public function walkJoinVariableDeclaration($joinVarDecl)
- {
- $join = $joinVarDecl->join;
- $joinType = $join->joinType;
- if ($joinType == AST\Join::JOIN_TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE_LEFTOUTER) {
- $sql = ' LEFT JOIN ';
- } else {
- $sql = ' INNER JOIN ';
- }
- $joinAssocPathExpr = $join->joinAssociationPathExpression;
- $joinedDqlAlias = $join->aliasIdentificationVariable;
- $relation = $this->_queryComponents[$joinedDqlAlias]['relation'];
- $targetClass = $this->_em->getClassMetadata($relation['targetEntity']);
- $sourceClass = $this->_em->getClassMetadata($relation['sourceEntity']);
- $targetTableName = $targetClass->getQuotedTableName($this->_platform);
- $targetTableAlias = $this->getSqlTableAlias($targetClass->table['name'], $joinedDqlAlias);
- $sourceTableAlias = $this->getSqlTableAlias($sourceClass->table['name'], $joinAssocPathExpr->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) {
- if ($relation['type'] == ClassMetadata::ONE_TO_MANY || $relation['type'] == ClassMetadata::MANY_TO_MANY) {
- throw QueryException::iterateWithFetchJoinNotAllowed($assoc);
- }
- }
- // 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.
- if ($assoc['type'] & ClassMetadata::TO_ONE) {
- $sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ';
- $first = true;
- foreach ($assoc['sourceToTargetKeyColumns'] as $sourceColumn => $targetColumn) {
- if ( ! $first) $sql .= ' AND '; else $first = false;
- if ($relation['isOwningSide']) {
- if ($targetClass->containsForeignIdentifier && !isset($targetClass->fieldNames[$targetColumn])) {
- $quotedTargetColumn = $targetColumn; // Join columns cannot be quoted.
- } else {
- $quotedTargetColumn = $targetClass->getQuotedColumnName($targetClass->fieldNames[$targetColumn], $this->_platform);
- }
- $sql .= $sourceTableAlias . '.' . $sourceColumn . ' = ' . $targetTableAlias . '.' . $quotedTargetColumn;
- } else {
- if ($sourceClass->containsForeignIdentifier && !isset($sourceClass->fieldNames[$targetColumn])) {
- $quotedTargetColumn = $targetColumn; // Join columns cannot be quoted.
- } else {
- $quotedTargetColumn = $sourceClass->getQuotedColumnName($sourceClass->fieldNames[$targetColumn], $this->_platform);
- }
- $sql .= $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $targetTableAlias . '.' . $sourceColumn;
- }
- }
- } else if ($assoc['type'] == ClassMetadata::MANY_TO_MANY) {
- // Join relation table
- $joinTable = $assoc['joinTable'];
- $joinTableAlias = $this->getSqlTableAlias($joinTable['name'], $joinedDqlAlias);
- $sql .= $sourceClass->getQuotedJoinTableName($assoc, $this->_platform) . ' ' . $joinTableAlias . ' ON ';
- $first = true;
- if ($relation['isOwningSide']) {
- foreach ($assoc['relationToSourceKeyColumns'] as $relationColumn => $sourceColumn) {
- if ( ! $first) $sql .= ' AND '; else $first = false;
- if ($sourceClass->containsForeignIdentifier && !isset($sourceClass->fieldNames[$sourceColumn])) {
- $quotedTargetColumn = $sourceColumn; // Join columns cannot be quoted.
- } else {
- $quotedTargetColumn = $sourceClass->getQuotedColumnName($sourceClass->fieldNames[$sourceColumn], $this->_platform);
- }
- $sql .= $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $relationColumn;
- }
- } else {
- foreach ($assoc['relationToTargetKeyColumns'] as $relationColumn => $targetColumn) {
- if ( ! $first) $sql .= ' AND '; else $first = false;
- if ($sourceClass->containsForeignIdentifier && !isset($sourceClass->fieldNames[$targetColumn])) {
- $quotedTargetColumn = $targetColumn; // Join columns cannot be quoted.
- } else {
- $quotedTargetColumn = $sourceClass->getQuotedColumnName($sourceClass->fieldNames[$targetColumn], $this->_platform);
- }
- $sql .= $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $relationColumn;
- }
- }
- // Join target table
- $sql .= ($joinType == AST\Join::JOIN_TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE_LEFTOUTER)
- ? ' LEFT JOIN ' : ' INNER JOIN ';
- $sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ';
- $first = true;
- if ($relation['isOwningSide']) {
- foreach ($assoc['relationToTargetKeyColumns'] as $relationColumn => $targetColumn) {
- if ( ! $first) $sql .= ' AND '; else $first = false;
- if ($targetClass->containsForeignIdentifier && !isset($targetClass->fieldNames[$targetColumn])) {
- $quotedTargetColumn = $targetColumn; // Join columns cannot be quoted.
- } else {
- $quotedTargetColumn = $targetClass->getQuotedColumnName($targetClass->fieldNames[$targetColumn], $this->_platform);
- }
- $sql .= $targetTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $relationColumn;
- }
- } else {
- foreach ($assoc['relationToSourceKeyColumns'] as $relationColumn => $sourceColumn) {
- if ( ! $first) $sql .= ' AND '; else $first = false;
- if ($targetClass->containsForeignIdentifier && !isset($targetClass->fieldNames[$sourceColumn])) {
- $quotedTargetColumn = $sourceColumn; // Join columns cannot be quoted.
- } else {
- $quotedTargetColumn = $targetClass->getQuotedColumnName($targetClass->fieldNames[$sourceColumn], $this->_platform);
- }
- $sql .= $targetTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $relationColumn;
- }
- }
- }
- // Handle WITH clause
- if (($condExpr = $join->conditionalExpression) !== null) {
- // Phase 2 AST optimization: Skip processment of ConditionalExpression
- // if only one ConditionalTerm is defined
- $sql .= ' AND (' . $this->walkConditionalExpression($condExpr) . ')';
- }
- $discrSql = $this->_generateDiscriminatorColumnConditionSQL(array($joinedDqlAlias));
- if ($discrSql) {
- $sql .= ' AND ' . $discrSql;
- }
- // FIXME: these should either be nested or all forced to be left joins (DDC-XXX)
- if ($targetClass->isInheritanceTypeJoined()) {
- $sql .= $this->_generateClassTableInheritanceJoins($targetClass, $joinedDqlAlias);
- }
- return $sql;
- }
- /**
- * Walks down a SelectExpression AST node and generates the corresponding SQL.
- *
- * @param SelectExpression $selectExpression
- * @return string The SQL.
- */
- public function walkSelectExpression($selectExpression)
- {
- $sql = '';
- $expr = $selectExpression->expression;
- if ($expr instanceof AST\PathExpression) {
- if ($expr->type == AST\PathExpression::TYPE_STATE_FIELD) {
- $fieldName = $expr->field;
- $dqlAlias = $expr->identificationVariable;
- $qComp = $this->_queryComponents[$dqlAlias];
- $class = $qComp['metadata'];
- if ( ! $selectExpression->fieldIdentificationVariable) {
- $resultAlias = $fieldName;
- } else {
- $resultAlias = $selectExpression->fieldIdentificationVariable;
- }
- if ($class->isInheritanceTypeJoined()) {
- $tableName = $this->_em->getUnitOfWork()->getEntityPersister($class->name)->getOwningTable($fieldName);
- } else {
- $tableName = $class->getTableName();
- }
- $sqlTableAlias = $this->getSqlTableAlias($tableName, $dqlAlias);
- $columnName = $class->getQuotedColumnName($fieldName, $this->_platform);
- $columnAlias = $this->getSqlColumnAlias($columnName);
- $sql .= $sqlTableAlias . '.' . $columnName . ' AS ' . $columnAlias;
- $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
- $this->_rsm->addScalarResult($columnAlias, $resultAlias);
- } else {
- throw QueryException::invalidPathExpression($expr->type);
- }
- }
- else if ($expr instanceof AST\AggregateExpression) {
- if ( ! $selectExpression->fieldIdentificationVariable) {
- $resultAlias = $this->_scalarResultCounter++;
- } else {
- $resultAlias = $selectExpression->fieldIdentificationVariable;
- }
- $columnAlias = 'sclr' . $this->_aliasCounter++;
- $sql .= $this->walkAggregateExpression($expr) . ' AS ' . $columnAlias;
- $this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
- $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
- $this->_rsm->addScalarResult($columnAlias, $resultAlias);
- }
- else if ($expr instanceof AST\Subselect) {
- if ( ! $selectExpression->fieldIdentificationVariable) {
- $resultAlias = $this->_scalarResultCounter++;
- } else {
- $resultAlias = $selectExpression->fieldIdentificationVariable;
- }
- $columnAlias = 'sclr' . $this->_aliasCounter++;
- $sql .= '(' . $this->walkSubselect($expr) . ') AS '.$columnAlias;
- $this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
- $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
- $this->_rsm->addScalarResult($columnAlias, $resultAlias);
- }
- else if ($expr instanceof AST\Functions\FunctionNode) {
- if ( ! $selectExpression->fieldIdentificationVariable) {
- $resultAlias = $this->_scalarResultCounter++;
- } else {
- $resultAlias = $selectExpression->fieldIdentificationVariable;
- }
- $columnAlias = 'sclr' . $this->_aliasCounter++;
- $sql .= $this->walkFunction($expr) . ' AS ' . $columnAlias;
- $this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
- $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
- $this->_rsm->addScalarResult($columnAlias, $resultAlias);
- }
- else if (
- $expr instanceof AST\SimpleArithmeticExpression ||
- $expr instanceof AST\ArithmeticTerm ||
- $expr instanceof AST\ArithmeticFactor ||
- $expr instanceof AST\ArithmeticPrimary
- ) {
- if ( ! $selectExpression->fieldIdentificationVariable) {
- $resultAlias = $this->_scalarResultCounter++;
- } else {
- $resultAlias = $selectExpression->fieldIdentificationVariable;
- }
- $columnAlias = 'sclr' . $this->_aliasCounter++;
- $sql .= $this->walkSimpleArithmeticExpression($expr) . ' AS ' . $columnAlias;
- $this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
- $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
- $this->_rsm->addScalarResult($columnAlias, $resultAlias);
- } else {
- // 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'];
- if ( ! isset($this->_selectedClasses[$dqlAlias])) {
- $this->_selectedClasses[$dqlAlias] = $class;
- }
- $beginning = true;
- // Select all fields from the queried class
- foreach ($class->fieldMappings as $fieldName => $mapping) {
- if ($partialFieldSet && !in_array($fieldName, $partialFieldSet)) {
- continue;
- }
- if (isset($mapping['inherited'])) {
- $tableName = $this->_em->getClassMetadata($mapping['inherited'])->table['name'];
- } else {
- $tableName = $class->table['name'];
- }
- if ($beginning) $beginning = false; else $sql .= ', ';
- $sqlTableAlias = $this->getSqlTableAlias($tableName, $dqlAlias);
- $columnAlias = $this->getSqlColumnAlias($mapping['columnName']);
- $sql .= $sqlTableAlias . '.' . $class->getQuotedColumnName($fieldName, $this->_platform)
- . ' AS ' . $columnAlias;
- $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
- $this->_rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $class->name);
- }
- if ($class->containsForeignIdentifier) {
- // Add double entry for association identifier columns to simplify hydrator code
- foreach ($class->identifier AS $idField) {
- if (isset($class->associationMappings[$idField])) {
- if (isset($mapping['inherited'])) {
- $tableName = $this->_em->getClassMetadata($mapping['inherited'])->table['name'];
- } else {
- $tableName = $class->table['name'];
- }
- if ($beginning) $beginning = false; else $sql .= ', ';
- $joinColumnName = $class->associationMappings[$idField]['joinColumns'][0]['name'];
- $sqlTableAlias = $this->getSqlTableAlias($tableName, $dqlAlias);
- $columnAlias = $this->getSqlColumnAlias($joinColumnName);
- $sql .= $sqlTableAlias . '.' . $joinColumnName . ' AS ' . $columnAlias;
- $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
- $this->_rsm->addMetaResult($dqlAlias, $columnAlias, $idField);
- }
- }
- }
- // 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->table['name'], $dqlAlias);
- foreach ($subClass->fieldMappings as $fieldName => $mapping) {
- if (isset($mapping['inherited']) || $partialFieldSet && !in_array($fieldName, $partialFieldSet)) {
- continue;
- }
- if ($beginning) $beginning = false; else $sql .= ', ';
- $columnAlias = $this->getSqlColumnAlias($mapping['columnName']);
- $sql .= $sqlTableAlias . '.' . $subClass->getQuotedColumnName($fieldName, $this->_platform)
- . ' AS ' . $columnAlias;
- $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
- $this->_rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $subClassName);
- }
- // Add join columns (foreign keys) of the subclass
- //TODO: Probably better do this in walkSelectClause to honor the INCLUDE_META_COLUMNS hint
- foreach ($subClass->associationMappings as $fieldName => $assoc) {
- if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE && ! isset($assoc['inherited'])) {
- foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
- if ($beginning) $beginning = false; else $sql .= ', ';
- $columnAlias = $this->getSqlColumnAlias($srcColumn);
- $sql .= $sqlTableAlias . '.' . $srcColumn . ' AS ' . $columnAlias;
- $this->_rsm->addMetaResult($dqlAlias, $this->_platform->getSQLResultCasing($columnAlias), $srcColumn);
- }
- }
- }
- }
- }
- }
- return $sql;
- }
- /**
- * Walks down a QuantifiedExpression AST node, thereby generating the appropriate SQL.
- *
- * @param QuantifiedExpression
- * @return string The SQL.
- */
- public function walkQuantifiedExpression($qExpr)
- {
- return ' ' . strtoupper($qExpr->type)
- . '(' . $this->walkSubselect($qExpr->subselect) . ')';
- }
- /**
- * Walks down a Subselect AST node, thereby generating the appropriate SQL.
- *
- * @param Subselect
- * @return string The SQL.
- */
- public function walkSubselect($subselect)
- {
- $useAliasesBefore = $this->_useSqlTableAliases;
- $this->_useSqlTableAliases = true;
- $sql = $this->walkSimpleSelectClause($subselect->simpleSelectClause);
- $sql .= $this->walkSubselectFromClause($subselect->subselectFromClause);
- $sql .= $subselect->whereClause ? $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->_useSqlTableAliases = $useAliasesBefore;
- return $sql;
- }
- /**
- * Walks down a SubselectFromClause AST node, thereby generating the appropriate SQL.
- *
- * @param SubselectFromClause
- * @return string The SQL.
- */
- public function walkSubselectFromClause($subselectFromClause)
- {
- $identificationVarDecls = $subselectFromClause->identificationVariableDeclarations;
- $sqlParts = array ();
- foreach ($identificationVarDecls as $subselectIdVarDecl) {
- $sql = '';
- $rangeDecl = $subselectIdVarDecl->rangeVariableDeclaration;
- $dqlAlias = $rangeDecl->aliasIdentificationVariable;
- $class = $this->_em->getClassMetadata($rangeDecl->abstractSchemaName);
- $sql .= $class->getQuotedTableName($this->_platform) . ' '
- . $this->getSqlTableAlias($class->table['name'], $dqlAlias);
- if ($class->isInheritanceTypeJoined()) {
- $sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias);
- }
- foreach ($subselectIdVarDecl->joinVariableDeclarations as $joinVarDecl) {
- $sql .= $this->walkJoinVariableDeclaration($joinVarDecl);
- }
- $sqlParts[] = $this->_platform->appendLockHint($sql, $this->_query->getHint(Query::HINT_LOCK_MODE));
- }
- return ' FROM ' . implode(', ', $sqlParts);
- }
- /**
- * Walks down a SimpleSelectClause AST node, thereby generating the appropriate SQL.
- *
- * @param SimpleSelectClause
- * @return string The SQL.
- */
- public function walkSimpleSelectClause($simpleSelectClause)
- {
- return 'SELECT' . ($simpleSelectClause->isDistinct ? ' DISTINCT' : '')
- . $this->walkSimpleSelectExpression($simpleSelectClause->simpleSelectExpression);
- }
- /**
- * Walks down a SimpleSelectExpression AST node, thereby generating the appropriate SQL.
- *
- * @param SimpleSelectExpression
- * @return string The SQL.
- */
- public function walkSimpleSelectExpression($simpleSelectExpression)
- {
- $sql = '';
- $expr = $simpleSelectExpression->expression;
- if ($expr instanceof AST\PathExpression) {
- $sql .= $this->walkPathExpression($expr);
- } else if ($expr instanceof AST\AggregateExpression) {
- if ( ! $simpleSelectExpression->fieldIdentificationVariable) {
- $alias = $this->_scalarResultCounter++;
- } else {
- $alias = $simpleSelectExpression->fieldIdentificationVariable;
- }
- $sql .= $this->walkAggregateExpression($expr) . ' AS dctrn__' . $alias;
- } else if ($expr instanceof AST\Subselect) {
- if ( ! $simpleSelectExpression->fieldIdentificationVariable) {
- $alias = $this->_scalarResultCounter++;
- } else {
- $alias = $simpleSelectExpression->fieldIdentificationVariable;
- }
- $columnAlias = 'sclr' . $this->_aliasCounter++;
- $sql .= '(' . $this->walkSubselect($expr) . ') AS ' . $columnAlias;
- $this->_scalarResultAliasMap[$alias] = $columnAlias;
- } else if ($expr instanceof AST\Functions\FunctionNode) {
- if ( ! $simpleSelectExpression->fieldIdentificationVariable) {
- $alias = $this->_scalarResultCounter++;
- } else {
- $alias = $simpleSelectExpression->fieldIdentificationVariable;
- }
- $columnAlias = 'sclr' . $this->_aliasCounter++;
- $sql .= $this->walkFunction($expr) . ' AS ' . $columnAlias;
- $this->_scalarResultAliasMap[$alias] = $columnAlias;
- } else if (
- $expr instanceof AST\SimpleArithmeticExpression ||
- $expr instanceof AST\ArithmeticTerm ||
- $expr instanceof AST\ArithmeticFactor ||
- $expr instanceof AST\ArithmeticPrimary
- ) {
- if ( ! $simpleSelectExpression->fieldIdentificationVariable) {
- $alias = $this->_scalarResultCounter++;
- } else {
- $alias = $simpleSelectExpression->fieldIdentificationVariable;
- }
- $columnAlias = 'sclr' . $this->_aliasCounter++;
- $sql .= $this->walkSimpleArithmeticExpression($expr) . ' AS ' . $columnAlias;
- $this->_scalarResultAliasMap[$alias] = $columnAlias;
- } else {
- // IdentificationVariable
- $class…
Large files files are truncated, but you can click here to view the full file