2009-01-19 21:40:12 +03:00
|
|
|
<?php
|
2009-02-17 13:54:18 +03:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* 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>.
|
2009-01-19 21:40:12 +03:00
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\ORM\Query;
|
|
|
|
|
2009-07-21 13:25:14 +04:00
|
|
|
use Doctrine\ORM\Query;
|
2009-03-21 22:58:52 +03:00
|
|
|
use Doctrine\ORM\Query\Parser;
|
2009-01-22 22:38:10 +03:00
|
|
|
use Doctrine\ORM\Query\AST;
|
2009-02-18 13:39:51 +03:00
|
|
|
use Doctrine\Common\DoctrineException;
|
2009-01-22 22:38:10 +03:00
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
/**
|
2009-06-14 21:34:28 +04:00
|
|
|
* The SqlWalker is a TreeWalker that walks over a DQL AST and constructs
|
|
|
|
* the corresponding SQL.
|
2009-01-19 21:40:12 +03:00
|
|
|
*
|
2009-05-21 12:53:40 +04:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-02-05 20:34:44 +03:00
|
|
|
* @since 2.0
|
2009-06-14 21:34:28 +04:00
|
|
|
* @todo Code review for identifier quoting.
|
2009-06-30 20:00:28 +04:00
|
|
|
* @todo Code review for schema usage with table names.
|
2009-07-21 13:25:14 +04:00
|
|
|
* (Prepend schema name to tables IF schema is defined AND platform supports them)
|
2009-01-19 21:40:12 +03:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
class SqlWalker implements TreeWalker
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-06-14 21:34:28 +04:00
|
|
|
/** The ResultSetMapping. */
|
|
|
|
private $_rsm;
|
|
|
|
/** Counter for generating unique column aliases. */
|
2009-04-09 22:12:48 +04:00
|
|
|
private $_aliasCounter = 0;
|
2009-06-14 21:34:28 +04:00
|
|
|
/** Counter for generating unique table aliases. */
|
2009-02-17 13:54:18 +03:00
|
|
|
private $_tableAliasCounter = 0;
|
2009-06-15 22:25:47 +04:00
|
|
|
private $_scalarResultCounter = 1;
|
2009-06-14 21:34:28 +04:00
|
|
|
/** Counter for SQL parameter positions. */
|
|
|
|
private $_sqlParamIndex = 1;
|
|
|
|
/** The ParserResult. */
|
2009-01-19 21:40:12 +03:00
|
|
|
private $_parserResult;
|
2009-06-14 21:34:28 +04:00
|
|
|
/** The EntityManager. */
|
2009-01-19 21:40:12 +03:00
|
|
|
private $_em;
|
2009-06-14 21:34:28 +04:00
|
|
|
/** The Connection of the EntityManager. */
|
2009-05-29 14:23:13 +04:00
|
|
|
private $_conn;
|
2009-07-21 13:25:14 +04:00
|
|
|
/** The Query instance. */
|
2009-04-09 22:12:48 +04:00
|
|
|
private $_query;
|
2009-01-19 21:40:12 +03:00
|
|
|
private $_dqlToSqlAliasMap = array();
|
2009-04-09 22:12:48 +04:00
|
|
|
/** Map of all components/classes that appear in the DQL query. */
|
2009-05-03 14:58:16 +04:00
|
|
|
private $_queryComponents;
|
2009-04-09 22:12:48 +04:00
|
|
|
/** A list of classes that appear in non-scalar SelectExpressions. */
|
|
|
|
private $_selectedClasses = array();
|
2009-05-03 14:58:16 +04:00
|
|
|
/**
|
|
|
|
* The DQL alias of the root class of the currently traversed query.
|
|
|
|
* TODO: May need to be turned into a stack for usage in subqueries
|
|
|
|
*/
|
|
|
|
private $_currentRootAlias;
|
2009-06-14 21:34:28 +04:00
|
|
|
/**
|
|
|
|
* Flag that indicates whether to generate SQL table aliases in the SQL.
|
|
|
|
* These should only be generated for SELECT queries.
|
|
|
|
*/
|
2009-05-03 14:58:16 +04:00
|
|
|
private $_useSqlTableAliases = true;
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-02-05 20:34:44 +03:00
|
|
|
/**
|
2009-04-09 22:12:48 +04:00
|
|
|
* Initializes a new SqlWalker instance with the given Query and ParserResult.
|
|
|
|
*
|
|
|
|
* @param Query $query The parsed Query.
|
|
|
|
* @param ParserResult $parserResult The result of the parsing process.
|
2009-02-05 20:34:44 +03:00
|
|
|
*/
|
2009-04-09 22:12:48 +04:00
|
|
|
public function __construct($query, $parserResult, array $queryComponents)
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm = $parserResult->getResultSetMapping();
|
2009-04-09 22:12:48 +04:00
|
|
|
$this->_query = $query;
|
|
|
|
$this->_em = $query->getEntityManager();
|
2009-05-29 14:23:13 +04:00
|
|
|
$this->_conn = $this->_em->getConnection();
|
2009-01-19 21:40:12 +03:00
|
|
|
$this->_parserResult = $parserResult;
|
2009-04-09 22:12:48 +04:00
|
|
|
$this->_queryComponents = $queryComponents;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-06-23 21:50:13 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the Query instance used by the walker.
|
|
|
|
*
|
|
|
|
* @return Query.
|
|
|
|
*/
|
|
|
|
public function getQuery()
|
|
|
|
{
|
|
|
|
return $this->_query;
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
/**
|
2009-06-22 22:48:42 +04:00
|
|
|
* Gets the Connection used by the walker.
|
|
|
|
*
|
2009-05-03 14:58:16 +04:00
|
|
|
* @return Connection
|
|
|
|
*/
|
2009-03-28 20:10:41 +03:00
|
|
|
public function getConnection()
|
|
|
|
{
|
|
|
|
return $this->_em->getConnection();
|
|
|
|
}
|
2009-06-22 22:48:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the EntityManager used by the walker.
|
|
|
|
*
|
|
|
|
* @return EntityManager
|
|
|
|
*/
|
|
|
|
public function getEntityManager()
|
|
|
|
{
|
|
|
|
return $this->_em;
|
|
|
|
}
|
2009-03-28 20:10:41 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Walks down a SelectStatement AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
public function walkSelectStatement(AST\SelectStatement $AST)
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
$sql = $this->walkSelectClause($AST->getSelectClause());
|
|
|
|
$sql .= $this->walkFromClause($AST->getFromClause());
|
2009-05-03 14:58:16 +04:00
|
|
|
if ($whereClause = $AST->getWhereClause()) {
|
|
|
|
$sql .= $this->walkWhereClause($whereClause);
|
|
|
|
} else if ($discSql = $this->_generateDiscriminatorColumnConditionSql($this->_currentRootAlias)) {
|
|
|
|
$sql .= ' WHERE ' . $discSql;
|
|
|
|
}
|
2009-05-07 17:54:01 +04:00
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
$sql .= $AST->getGroupByClause() ? $this->walkGroupByClause($AST->getGroupByClause()) : '';
|
2009-03-28 20:10:41 +03:00
|
|
|
$sql .= $AST->getHavingClause() ? $this->walkHavingClause($AST->getHavingClause()) : '';
|
|
|
|
$sql .= $AST->getOrderByClause() ? $this->walkOrderByClause($AST->getOrderByClause()) : '';
|
2009-01-20 20:07:07 +03:00
|
|
|
|
2009-07-10 21:53:48 +04:00
|
|
|
$q = $this->getQuery();
|
2009-07-21 13:25:14 +04:00
|
|
|
$sql = $this->getConnection()->getDatabasePlatform()->modifyLimitQuery(
|
|
|
|
$sql, $q->getMaxResults(), $q->getFirstResult()
|
|
|
|
);
|
2009-07-10 21:53:48 +04:00
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a SelectClause AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-19 21:40:12 +03:00
|
|
|
public function walkSelectClause($selectClause)
|
|
|
|
{
|
2009-04-09 22:12:48 +04:00
|
|
|
$sql = 'SELECT ' . (($selectClause->isDistinct()) ? 'DISTINCT ' : '')
|
2009-03-23 20:39:33 +03:00
|
|
|
. implode(', ', array_map(array($this, 'walkSelectExpression'),
|
2009-01-19 21:40:12 +03:00
|
|
|
$selectClause->getSelectExpressions()));
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2009-05-26 15:30:07 +04:00
|
|
|
foreach ($this->_selectedClasses as $dqlAlias => $class) {
|
2009-04-09 22:12:48 +04:00
|
|
|
if ($this->_queryComponents[$dqlAlias]['relation'] === null) {
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm->addEntityResult($class->name, $dqlAlias);
|
2009-04-09 22:12:48 +04:00
|
|
|
} else {
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm->addJoinedEntityResult(
|
2009-05-21 23:18:14 +04:00
|
|
|
$class->name, $dqlAlias,
|
2009-04-09 22:12:48 +04:00
|
|
|
$this->_queryComponents[$dqlAlias]['parent'],
|
|
|
|
$this->_queryComponents[$dqlAlias]['relation']
|
|
|
|
);
|
|
|
|
}
|
2009-05-26 15:30:07 +04:00
|
|
|
|
2009-04-12 23:02:12 +04:00
|
|
|
if ($class->isInheritanceTypeSingleTable() || $class->isInheritanceTypeJoined()) {
|
2009-05-21 12:53:40 +04:00
|
|
|
$rootClass = $this->_em->getClassMetadata($class->rootEntityName);
|
2009-07-21 13:25:14 +04:00
|
|
|
$tblAlias = $this->getSqlTableAlias($rootClass->getTableName(), $dqlAlias);
|
2009-05-21 12:53:40 +04:00
|
|
|
$discrColumn = $rootClass->discriminatorColumn;
|
2009-04-12 23:02:12 +04:00
|
|
|
$columnAlias = $this->getSqlColumnAlias($discrColumn['name']);
|
|
|
|
$sql .= ", $tblAlias." . $discrColumn['name'] . ' AS ' . $columnAlias;
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm->setDiscriminatorColumn($dqlAlias, $columnAlias);
|
2009-07-20 19:30:54 +04:00
|
|
|
$this->_rsm->addMetaResult($dqlAlias, $columnAlias, $discrColumn['fieldName']);
|
2009-04-12 23:02:12 +04:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a FromClause AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-19 21:40:12 +03:00
|
|
|
public function walkFromClause($fromClause)
|
|
|
|
{
|
|
|
|
$sql = ' FROM ';
|
|
|
|
$identificationVarDecls = $fromClause->getIdentificationVariableDeclarations();
|
|
|
|
$firstIdentificationVarDecl = $identificationVarDecls[0];
|
|
|
|
$rangeDecl = $firstIdentificationVarDecl->getRangeVariableDeclaration();
|
2009-04-09 22:12:48 +04:00
|
|
|
$dqlAlias = $rangeDecl->getAliasIdentificationVariable();
|
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
$this->_currentRootAlias = $dqlAlias;
|
2009-05-21 12:53:40 +04:00
|
|
|
$class = $rangeDecl->getClassMetadata();
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2009-07-21 13:25:14 +04:00
|
|
|
$sql .= $class->getTableName() . ' ' . $this->getSqlTableAlias($class->getTableName(), $dqlAlias);
|
2009-05-21 12:53:40 +04:00
|
|
|
|
|
|
|
if ($class->isInheritanceTypeJoined()) {
|
|
|
|
$sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias);
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
|
|
|
foreach ($firstIdentificationVarDecl->getJoinVariableDeclarations() as $joinVarDecl) {
|
|
|
|
$sql .= $this->walkJoinVariableDeclaration($joinVarDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a FunctionNode AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-23 20:39:33 +03:00
|
|
|
public function walkFunction($function)
|
|
|
|
{
|
|
|
|
return $function->getSql($this);
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down an OrderByClause AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param OrderByClause
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkOrderByClause($orderByClause)
|
|
|
|
{
|
|
|
|
// OrderByClause ::= "ORDER" "BY" OrderByItem {"," OrderByItem}*
|
|
|
|
return ' ORDER BY '
|
|
|
|
. implode(', ', array_map(array($this, 'walkOrderByItem'),
|
|
|
|
$orderByClause->getOrderByItems()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Walks down an OrderByItem AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param OrderByItem
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkOrderByItem($orderByItem)
|
|
|
|
{
|
|
|
|
//TODO: support general SingleValuedPathExpression, not just state field
|
2009-07-19 20:18:51 +04:00
|
|
|
$expr = $orderByItem->getExpression();
|
|
|
|
$parts = $expr->getParts();
|
|
|
|
$dqlAlias = $expr->getIdentificationVariable();
|
2009-06-14 21:34:28 +04:00
|
|
|
$qComp = $this->_queryComponents[$dqlAlias];
|
|
|
|
$columnName = $qComp['metadata']->getColumnName($parts[0]);
|
2009-07-21 13:25:14 +04:00
|
|
|
$sql = $this->getSqlTableAlias($qComp['metadata']->getTableName(), $dqlAlias) . '.' . $columnName;
|
2009-07-28 15:43:42 +04:00
|
|
|
if ($orderByItem->isAsc()) {
|
|
|
|
$sql .= ' ASC ';
|
|
|
|
} else if ($orderByItem->isDesc()) {
|
|
|
|
$sql .= ' DESC ';
|
|
|
|
}
|
2009-03-28 20:10:41 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Walks down a HavingClause AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param HavingClause
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkHavingClause($havingClause)
|
|
|
|
{
|
|
|
|
return ' HAVING ' . implode(' OR ', array_map(array($this, 'walkConditionalTerm'),
|
|
|
|
$havingClause->getConditionalExpression()->getConditionalTerms()));
|
|
|
|
}
|
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
/**
|
|
|
|
* Walks down a JoinVariableDeclaration AST node and creates the corresponding SQL.
|
|
|
|
*
|
|
|
|
* @param JoinVariableDeclaration $joinVarDecl
|
2009-06-14 21:34:28 +04:00
|
|
|
* @return string The SQL.
|
2009-01-19 21:40:12 +03:00
|
|
|
*/
|
|
|
|
public function walkJoinVariableDeclaration($joinVarDecl)
|
|
|
|
{
|
|
|
|
$join = $joinVarDecl->getJoin();
|
|
|
|
$joinType = $join->getJoinType();
|
2009-03-23 20:39:33 +03:00
|
|
|
if ($joinType == AST\Join::JOIN_TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE_LEFTOUTER) {
|
2009-01-19 21:40:12 +03:00
|
|
|
$sql = ' LEFT JOIN ';
|
|
|
|
} else {
|
|
|
|
$sql = ' INNER JOIN ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$joinAssocPathExpr = $join->getJoinAssociationPathExpression();
|
2009-04-09 22:12:48 +04:00
|
|
|
$joinedDqlAlias = $join->getAliasIdentificationVariable();
|
|
|
|
$sourceQComp = $this->_queryComponents[$joinAssocPathExpr->getIdentificationVariable()];
|
|
|
|
$targetQComp = $this->_queryComponents[$joinedDqlAlias];
|
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
$targetTableName = $targetQComp['metadata']->getTableName();
|
2009-07-21 13:25:14 +04:00
|
|
|
$targetTableAlias = $this->getSqlTableAlias($targetTableName, $joinedDqlAlias);
|
|
|
|
$sourceTableAlias = $this->getSqlTableAlias($sourceQComp['metadata']->getTableName(), $joinAssocPathExpr->getIdentificationVariable());
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-05-07 17:54:01 +04:00
|
|
|
// Ensure we got the owning side, since it has all mapping info
|
2009-01-19 21:40:12 +03:00
|
|
|
if ( ! $targetQComp['relation']->isOwningSide()) {
|
|
|
|
$assoc = $targetQComp['metadata']->getAssociationMapping($targetQComp['relation']->getMappedByFieldName());
|
|
|
|
} else {
|
|
|
|
$assoc = $targetQComp['relation'];
|
|
|
|
}
|
|
|
|
|
2009-05-26 15:30:07 +04:00
|
|
|
if ($assoc->isOneToOne()) {
|
2009-05-07 17:54:01 +04:00
|
|
|
$sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ';
|
2009-01-19 21:40:12 +03:00
|
|
|
$joinColumns = $assoc->getSourceToTargetKeyColumns();
|
|
|
|
$first = true;
|
|
|
|
foreach ($joinColumns as $sourceColumn => $targetColumn) {
|
2009-05-07 17:54:01 +04:00
|
|
|
if ( ! $first) {
|
|
|
|
$sql .= ' AND ';
|
|
|
|
} else {
|
|
|
|
$first = false;
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
if ($targetQComp['relation']->isOwningSide()) {
|
|
|
|
$sql .= "$sourceTableAlias.$sourceColumn = $targetTableAlias.$targetColumn";
|
|
|
|
} else {
|
|
|
|
$sql .= "$sourceTableAlias.$targetColumn = $targetTableAlias.$sourceColumn";
|
|
|
|
}
|
|
|
|
}
|
2009-05-07 17:54:01 +04:00
|
|
|
} else if ($assoc->isManyToMany()) {
|
|
|
|
// Join relation table
|
|
|
|
$joinTable = $assoc->getJoinTable();
|
|
|
|
$joinTableAlias = $this->getSqlTableAlias($joinTable['name']);
|
|
|
|
$sql .= $joinTable['name'] . ' ' . $joinTableAlias . ' ON ';
|
2009-05-21 12:53:40 +04:00
|
|
|
if ($targetQComp['relation']->isOwningSide) {
|
2009-05-07 17:54:01 +04:00
|
|
|
$sourceToRelationJoinColumns = $assoc->getSourceToRelationKeyColumns();
|
|
|
|
foreach ($sourceToRelationJoinColumns as $sourceColumn => $relationColumn) {
|
|
|
|
$sql .= "$sourceTableAlias.$sourceColumn = $joinTableAlias.$relationColumn";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$targetToRelationJoinColumns = $assoc->getTargetToRelationKeyColumns();
|
|
|
|
foreach ($targetToRelationJoinColumns as $targetColumn => $relationColumn) {
|
|
|
|
$sql .= "$sourceTableAlias.$targetColumn = $joinTableAlias.$relationColumn";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join target table
|
|
|
|
if ($joinType == AST\Join::JOIN_TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE_LEFTOUTER) {
|
|
|
|
$sql .= ' LEFT JOIN ';
|
|
|
|
} else {
|
|
|
|
$sql .= ' INNER JOIN ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ';
|
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
if ($targetQComp['relation']->isOwningSide) {
|
2009-05-07 17:54:01 +04:00
|
|
|
$targetToRelationJoinColumns = $assoc->getTargetToRelationKeyColumns();
|
|
|
|
foreach ($targetToRelationJoinColumns as $targetColumn => $relationColumn) {
|
|
|
|
$sql .= "$targetTableAlias.$targetColumn = $joinTableAlias.$relationColumn";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$sourceToRelationJoinColumns = $assoc->getSourceToRelationKeyColumns();
|
|
|
|
foreach ($sourceToRelationJoinColumns as $sourceColumn => $relationColumn) {
|
|
|
|
$sql .= "$targetTableAlias.$sourceColumn = $joinTableAlias.$relationColumn";
|
|
|
|
}
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
|
|
|
|
$discrSql = $this->_generateDiscriminatorColumnConditionSql($joinedDqlAlias);
|
|
|
|
if ($discrSql) {
|
|
|
|
$sql .= ' AND ' . $discrSql;
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
|
|
|
if ($targetQComp['metadata']->isInheritanceTypeJoined()) {
|
|
|
|
$sql .= $this->_generateClassTableInheritanceJoins($targetQComp['metadata'], $joinedDqlAlias);
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Walks down a SelectExpression AST node and generates the corresponding SQL.
|
|
|
|
*
|
2009-03-28 20:10:41 +03:00
|
|
|
* @param SelectExpression $selectExpression
|
|
|
|
* @return string The SQL.
|
2009-01-19 21:40:12 +03:00
|
|
|
*/
|
|
|
|
public function walkSelectExpression($selectExpression)
|
|
|
|
{
|
|
|
|
$sql = '';
|
2009-04-03 15:06:58 +04:00
|
|
|
$expr = $selectExpression->getExpression();
|
2009-06-14 21:34:28 +04:00
|
|
|
if ($expr instanceof AST\PathExpression) {
|
|
|
|
if ($expr->getType() == AST\PathExpression::TYPE_STATE_FIELD) {
|
2009-04-09 22:12:48 +04:00
|
|
|
$parts = $expr->getParts();
|
2009-01-19 21:40:12 +03:00
|
|
|
$numParts = count($parts);
|
2009-06-14 21:34:28 +04:00
|
|
|
$dqlAlias = $expr->getIdentificationVariable();
|
2009-01-19 21:40:12 +03:00
|
|
|
$fieldName = $parts[$numParts-1];
|
2009-04-09 22:12:48 +04:00
|
|
|
$qComp = $this->_queryComponents[$dqlAlias];
|
2009-01-19 21:40:12 +03:00
|
|
|
$class = $qComp['metadata'];
|
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
if ( ! isset($this->_selectedClasses[$dqlAlias])) {
|
|
|
|
$this->_selectedClasses[$dqlAlias] = $class;
|
|
|
|
}
|
|
|
|
|
2009-07-21 13:25:14 +04:00
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($class->getTableName(), $dqlAlias);
|
2009-04-09 22:12:48 +04:00
|
|
|
$columnName = $class->getColumnName($fieldName);
|
|
|
|
$columnAlias = $this->getSqlColumnAlias($columnName);
|
|
|
|
$sql .= $sqlTableAlias . '.' . $columnName . ' AS ' . $columnAlias;
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName);
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
} else {
|
2009-03-23 20:39:33 +03:00
|
|
|
throw DoctrineException::updateMe("Encountered invalid PathExpression during SQL construction.");
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
} else if ($expr instanceof AST\AggregateExpression) {
|
2009-01-19 21:40:12 +03:00
|
|
|
if ( ! $selectExpression->getFieldIdentificationVariable()) {
|
2009-04-09 22:12:48 +04:00
|
|
|
$resultAlias = $this->_scalarResultCounter++;
|
2009-01-19 21:40:12 +03:00
|
|
|
} else {
|
2009-04-09 22:12:48 +04:00
|
|
|
$resultAlias = $selectExpression->getFieldIdentificationVariable();
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
$columnAlias = 'sclr' . $this->_aliasCounter++;
|
|
|
|
$sql .= $this->walkAggregateExpression($expr) . ' AS ' . $columnAlias;
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm->addScalarResult($columnAlias, $resultAlias);
|
2009-04-09 22:12:48 +04:00
|
|
|
} else if ($expr instanceof AST\Subselect) {
|
2009-04-03 15:06:58 +04:00
|
|
|
$sql .= $this->walkSubselect($expr);
|
|
|
|
} else if ($expr instanceof AST\Functions\FunctionNode) {
|
2009-03-28 20:10:41 +03:00
|
|
|
if ( ! $selectExpression->getFieldIdentificationVariable()) {
|
2009-04-09 22:12:48 +04:00
|
|
|
$resultAlias = $this->_scalarResultCounter++;
|
2009-03-28 20:10:41 +03:00
|
|
|
} else {
|
2009-04-09 22:12:48 +04:00
|
|
|
$resultAlias = $selectExpression->getFieldIdentificationVariable();
|
2009-03-28 20:10:41 +03:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
$columnAlias = 'sclr' . $this->_aliasCounter++;
|
|
|
|
$sql .= $this->walkFunction($expr) . ' AS ' . $columnAlias;
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm->addScalarResult($columnAlias, $resultAlias);
|
2009-03-19 15:43:48 +03:00
|
|
|
} else {
|
2009-05-21 12:53:40 +04:00
|
|
|
// IdentificationVariable
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2009-04-03 15:06:58 +04:00
|
|
|
$dqlAlias = $expr;
|
2009-04-09 22:12:48 +04:00
|
|
|
$queryComp = $this->_queryComponents[$dqlAlias];
|
2009-01-19 21:40:12 +03:00
|
|
|
$class = $queryComp['metadata'];
|
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
if ( ! isset($this->_selectedClasses[$dqlAlias])) {
|
|
|
|
$this->_selectedClasses[$dqlAlias] = $class;
|
|
|
|
}
|
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
$beginning = true;
|
|
|
|
if ($class->isInheritanceTypeJoined()) {
|
|
|
|
// Select all fields from the queried class
|
|
|
|
foreach ($class->fieldMappings as $fieldName => $mapping) {
|
|
|
|
if (isset($mapping['inherited'])) {
|
|
|
|
$tableName = $this->_em->getClassMetadata($mapping['inherited'])->primaryTable['name'];
|
|
|
|
} else {
|
|
|
|
$tableName = $class->primaryTable['name'];
|
|
|
|
}
|
|
|
|
if ($beginning) $beginning = false; else $sql .= ', ';
|
2009-07-21 13:25:14 +04:00
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($tableName, $dqlAlias);
|
2009-05-21 12:53:40 +04:00
|
|
|
$columnAlias = $this->getSqlColumnAlias($mapping['columnName']);
|
2009-05-29 14:23:13 +04:00
|
|
|
$sql .= $sqlTableAlias . '.' . $this->_conn->quoteIdentifier($mapping['columnName']) . ' AS ' . $columnAlias;
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName);
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
// Add any additional fields of subclasses (not inherited fields)
|
|
|
|
foreach ($class->subClasses as $subClassName) {
|
|
|
|
$subClass = $this->_em->getClassMetadata($subClassName);
|
|
|
|
foreach ($subClass->fieldMappings as $fieldName => $mapping) {
|
|
|
|
if (isset($mapping['inherited'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($beginning) $beginning = false; else $sql .= ', ';
|
2009-07-21 13:25:14 +04:00
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($subClass->primaryTable['name'], $dqlAlias);
|
2009-05-21 12:53:40 +04:00
|
|
|
$columnAlias = $this->getSqlColumnAlias($mapping['columnName']);
|
2009-05-29 14:23:13 +04:00
|
|
|
$sql .= $sqlTableAlias . '.' . $this->_conn->quoteIdentifier($mapping['columnName']) . ' AS ' . $columnAlias;
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName);
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$fieldMappings = $class->fieldMappings;
|
|
|
|
foreach ($class->subClasses as $subclassName) {
|
|
|
|
$fieldMappings = array_merge(
|
2009-04-09 22:12:48 +04:00
|
|
|
$fieldMappings,
|
2009-05-14 14:03:09 +04:00
|
|
|
$this->_em->getClassMetadata($subclassName)->fieldMappings
|
2009-05-21 12:53:40 +04:00
|
|
|
);
|
|
|
|
}
|
2009-07-21 13:25:14 +04:00
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($class->getTableName(), $dqlAlias);
|
2009-05-21 12:53:40 +04:00
|
|
|
foreach ($fieldMappings as $fieldName => $mapping) {
|
|
|
|
if ($beginning) $beginning = false; else $sql .= ', ';
|
|
|
|
$columnAlias = $this->getSqlColumnAlias($mapping['columnName']);
|
2009-05-29 14:23:13 +04:00
|
|
|
$sql .= $sqlTableAlias . '.' . $this->_conn->quoteIdentifier($mapping['columnName']) . ' AS ' . $columnAlias;
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-07-20 19:30:54 +04:00
|
|
|
|
2009-07-21 13:25:14 +04:00
|
|
|
// Append foreign keys if necessary.
|
2009-07-25 20:52:19 +04:00
|
|
|
//FIXME: Evaluate HINT_INCLUDE_META_COLUMNS
|
|
|
|
//FIXME: Needs to be done in the case of Class Table Inheritance, too
|
|
|
|
// (see upper block of the if/else)
|
2009-07-21 13:25:14 +04:00
|
|
|
if ( ! $this->_em->getConfiguration()->getAllowPartialObjects() &&
|
|
|
|
! $this->_query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
|
2009-07-20 19:30:54 +04:00
|
|
|
foreach ($class->associationMappings as $assoc) {
|
|
|
|
if ($assoc->isOwningSide && $assoc->isOneToOne()) {
|
|
|
|
foreach ($assoc->targetToSourceKeyColumns as $srcColumn) {
|
|
|
|
$columnAlias = $this->getSqlColumnAlias($srcColumn);
|
|
|
|
$sql .= ', ' . $sqlTableAlias . '.' . $this->_conn->quoteIdentifier($srcColumn) . ' AS ' . $columnAlias;
|
|
|
|
$this->_rsm->addMetaResult($dqlAlias, $columnAlias, $srcColumn);
|
|
|
|
}
|
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|
|
|
|
}
|
2009-07-20 19:30:54 +04:00
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a QuantifiedExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param QuantifiedExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkQuantifiedExpression($qExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
if ($qExpr->isAll()) $sql .= ' ALL';
|
|
|
|
else if ($qExpr->isAny()) $sql .= ' ANY';
|
|
|
|
else if ($qExpr->isSome()) $sql .= ' SOME';
|
|
|
|
return $sql .= '(' . $this->walkSubselect($qExpr->getSubselect()) . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Walks down a Subselect AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param Subselect
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-19 15:43:48 +03:00
|
|
|
public function walkSubselect($subselect)
|
|
|
|
{
|
2009-05-03 14:58:16 +04:00
|
|
|
$useAliasesBefore = $this->_useSqlTableAliases;
|
|
|
|
$this->_useSqlTableAliases = true;
|
2009-03-19 15:43:48 +03:00
|
|
|
$sql = $this->walkSimpleSelectClause($subselect->getSimpleSelectClause());
|
|
|
|
$sql .= $this->walkSubselectFromClause($subselect->getSubselectFromClause());
|
|
|
|
$sql .= $subselect->getWhereClause() ? $this->walkWhereClause($subselect->getWhereClause()) : '';
|
|
|
|
$sql .= $subselect->getGroupByClause() ? $this->walkGroupByClause($subselect->getGroupByClause()) : '';
|
2009-03-28 20:10:41 +03:00
|
|
|
$sql .= $subselect->getHavingClause() ? $this->walkHavingClause($subselect->getHavingClause()) : '';
|
|
|
|
$sql .= $subselect->getOrderByClause() ? $this->walkOrderByClause($subselect->getOrderByClause()) : '';
|
2009-05-03 14:58:16 +04:00
|
|
|
$this->_useSqlTableAliases = $useAliasesBefore;
|
2009-03-19 15:43:48 +03:00
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a SubselectFromClause AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param SubselectFromClause
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-19 15:43:48 +03:00
|
|
|
public function walkSubselectFromClause($subselectFromClause)
|
|
|
|
{
|
|
|
|
$sql = ' FROM ';
|
|
|
|
$identificationVarDecls = $subselectFromClause->getSubselectIdentificationVariableDeclarations();
|
|
|
|
$firstIdentificationVarDecl = $identificationVarDecls[0];
|
|
|
|
$rangeDecl = $firstIdentificationVarDecl->getRangeVariableDeclaration();
|
|
|
|
$sql .= $rangeDecl->getClassMetadata()->getTableName() . ' '
|
2009-07-21 13:25:14 +04:00
|
|
|
. $this->getSqlTableAlias($rangeDecl->getClassMetadata()->getTableName(), $rangeDecl->getAliasIdentificationVariable());
|
2009-03-19 15:43:48 +03:00
|
|
|
|
|
|
|
foreach ($firstIdentificationVarDecl->getJoinVariableDeclarations() as $joinVarDecl) {
|
|
|
|
$sql .= $this->walkJoinVariableDeclaration($joinVarDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a SimpleSelectClause AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param SimpleSelectClause
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-19 15:43:48 +03:00
|
|
|
public function walkSimpleSelectClause($simpleSelectClause)
|
|
|
|
{
|
|
|
|
$sql = 'SELECT';
|
|
|
|
if ($simpleSelectClause->isDistinct()) {
|
|
|
|
$sql .= ' DISTINCT';
|
|
|
|
}
|
|
|
|
$sql .= $this->walkSimpleSelectExpression($simpleSelectClause->getSimpleSelectExpression());
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a SimpleSelectExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param SimpleSelectExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-19 15:43:48 +03:00
|
|
|
public function walkSimpleSelectExpression($simpleSelectExpression)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
$expr = $simpleSelectExpression->getExpression();
|
2009-06-14 21:34:28 +04:00
|
|
|
if ($expr instanceof AST\PathExpression) {
|
2009-03-28 20:10:41 +03:00
|
|
|
$sql .= ' ' . $this->walkPathExpression($expr);
|
2009-03-19 15:43:48 +03:00
|
|
|
//...
|
|
|
|
} else if ($expr instanceof AST\AggregateExpression) {
|
|
|
|
if ( ! $simpleSelectExpression->getFieldIdentificationVariable()) {
|
|
|
|
$alias = $this->_scalarAliasCounter++;
|
|
|
|
} else {
|
|
|
|
$alias = $simpleSelectExpression->getFieldIdentificationVariable();
|
|
|
|
}
|
|
|
|
$sql .= $this->walkAggregateExpression($expr) . ' AS dctrn__' . $alias;
|
|
|
|
} else {
|
2009-05-17 23:27:12 +04:00
|
|
|
// IdentificationVariable
|
|
|
|
// FIXME: Composite key support, or select all columns? Does that make
|
|
|
|
// in a subquery?
|
|
|
|
$class = $this->_queryComponents[$expr]['metadata'];
|
2009-07-21 13:25:14 +04:00
|
|
|
$sql .= ' ' . $this->getSqlTableAlias($class->getTableName(), $expr) . '.';
|
2009-05-17 23:27:12 +04:00
|
|
|
$sql .= $class->getColumnName($class->identifier[0]);
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down an AggregateExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param AggregateExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-19 15:43:48 +03:00
|
|
|
public function walkAggregateExpression($aggExpression)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
$parts = $aggExpression->getPathExpression()->getParts();
|
2009-06-14 21:34:28 +04:00
|
|
|
$dqlAlias = $aggExpression->getPathExpression()->getIdentificationVariable();
|
|
|
|
$fieldName = $parts[0];
|
2009-03-19 15:43:48 +03:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
$qComp = $this->_queryComponents[$dqlAlias];
|
2009-03-19 15:43:48 +03:00
|
|
|
$columnName = $qComp['metadata']->getColumnName($fieldName);
|
|
|
|
|
|
|
|
$sql .= $aggExpression->getFunctionName() . '(';
|
|
|
|
if ($aggExpression->isDistinct()) $sql .= 'DISTINCT ';
|
2009-07-21 13:25:14 +04:00
|
|
|
$sql .= $this->getSqlTableAlias($qComp['metadata']->getTableName(), $dqlAlias) . '.' . $columnName;
|
2009-03-19 15:43:48 +03:00
|
|
|
$sql .= ')';
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a GroupByClause AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param GroupByClause
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-19 21:40:12 +03:00
|
|
|
public function walkGroupByClause($groupByClause)
|
|
|
|
{
|
|
|
|
return ' GROUP BY '
|
2009-03-28 20:10:41 +03:00
|
|
|
. implode(', ', array_map(array($this, 'walkGroupByItem'),
|
2009-01-19 21:40:12 +03:00
|
|
|
$groupByClause->getGroupByItems()));
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a GroupByItem AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param GroupByItem
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function walkGroupByItem(AST\PathExpression $pathExpr)
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
//TODO: support general SingleValuedPathExpression, not just state field
|
|
|
|
$parts = $pathExpr->getParts();
|
2009-06-14 21:34:28 +04:00
|
|
|
$dqlAlias = $pathExpr->getIdentificationVariable();
|
|
|
|
$qComp = $this->_queryComponents[$dqlAlias];
|
|
|
|
$columnName = $qComp['metadata']->getColumnName($parts[0]);
|
2009-07-21 13:25:14 +04:00
|
|
|
return $this->getSqlTableAlias($qComp['metadata']->getTableName(), $dqlAlias) . '.' . $columnName;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down an UpdateStatement AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param UpdateStatement
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
public function walkUpdateStatement(AST\UpdateStatement $AST)
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-05-03 14:58:16 +04:00
|
|
|
$this->_useSqlTableAliases = false; // TODO: Ask platform instead?
|
2009-03-21 22:58:52 +03:00
|
|
|
$sql = $this->walkUpdateClause($AST->getUpdateClause());
|
2009-05-03 14:58:16 +04:00
|
|
|
if ($whereClause = $AST->getWhereClause()) {
|
|
|
|
$sql .= $this->walkWhereClause($whereClause);
|
|
|
|
} else if ($discSql = $this->_generateDiscriminatorColumnConditionSql($this->_currentRootAlias)) {
|
|
|
|
$sql .= ' WHERE ' . $discSql;
|
|
|
|
}
|
2009-03-21 22:58:52 +03:00
|
|
|
return $sql;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a DeleteStatement AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param DeleteStatement
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
public function walkDeleteStatement(AST\DeleteStatement $AST)
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-05-03 14:58:16 +04:00
|
|
|
$this->_useSqlTableAliases = false; // TODO: Ask platform instead?
|
2009-03-21 00:28:19 +03:00
|
|
|
$sql = $this->walkDeleteClause($AST->getDeleteClause());
|
2009-05-03 14:58:16 +04:00
|
|
|
if ($whereClause = $AST->getWhereClause()) {
|
|
|
|
$sql .= $this->walkWhereClause($whereClause);
|
|
|
|
} else if ($discSql = $this->_generateDiscriminatorColumnConditionSql($this->_currentRootAlias)) {
|
|
|
|
$sql .= ' WHERE ' . $discSql;
|
|
|
|
}
|
2009-03-21 00:28:19 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a DeleteClause AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param DeleteClause
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-21 00:28:19 +03:00
|
|
|
public function walkDeleteClause(AST\DeleteClause $deleteClause)
|
|
|
|
{
|
|
|
|
$sql = 'DELETE FROM ';
|
|
|
|
$class = $this->_em->getClassMetadata($deleteClause->getAbstractSchemaName());
|
|
|
|
$sql .= $class->getTableName();
|
2009-05-03 14:58:16 +04:00
|
|
|
if ($this->_useSqlTableAliases) {
|
2009-04-09 22:12:48 +04:00
|
|
|
$sql .= ' ' . $this->getSqlTableAlias($class->getTableName());
|
2009-03-21 00:28:19 +03:00
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
$this->_currentRootAlias = $deleteClause->getAliasIdentificationVariable();
|
|
|
|
|
2009-03-21 00:28:19 +03:00
|
|
|
return $sql;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down an UpdateClause AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param UpdateClause
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-21 22:58:52 +03:00
|
|
|
public function walkUpdateClause($updateClause)
|
|
|
|
{
|
|
|
|
$sql = 'UPDATE ';
|
|
|
|
$class = $this->_em->getClassMetadata($updateClause->getAbstractSchemaName());
|
|
|
|
$sql .= $class->getTableName();
|
2009-05-03 14:58:16 +04:00
|
|
|
if ($this->_useSqlTableAliases) {
|
2009-04-09 22:12:48 +04:00
|
|
|
$sql .= ' ' . $this->getSqlTableAlias($class->getTableName());
|
2009-03-21 22:58:52 +03:00
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
$this->_currentRootAlias = $updateClause->getAliasIdentificationVariable();
|
|
|
|
|
2009-03-21 22:58:52 +03:00
|
|
|
$sql .= ' SET ' . implode(', ', array_map(array($this, 'walkUpdateItem'),
|
|
|
|
$updateClause->getUpdateItems()));
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down an UpdateItem AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param UpdateItem
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-21 22:58:52 +03:00
|
|
|
public function walkUpdateItem($updateItem)
|
|
|
|
{
|
2009-06-23 21:50:13 +04:00
|
|
|
$useTableAliasesBefore = $this->_useSqlTableAliases;
|
|
|
|
$this->_useSqlTableAliases = false;
|
|
|
|
|
2009-03-21 22:58:52 +03:00
|
|
|
$sql = '';
|
2009-05-21 23:18:14 +04:00
|
|
|
$dqlAlias = $updateItem->getIdentificationVariable();
|
2009-04-09 22:12:48 +04:00
|
|
|
$qComp = $this->_queryComponents[$dqlAlias];
|
2009-03-21 22:58:52 +03:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
if ($this->_useSqlTableAliases) {
|
|
|
|
$sql .= $this->getSqlTableAlias($qComp['metadata']->getTableName()) . '.';
|
|
|
|
}
|
|
|
|
$sql .= $qComp['metadata']->getColumnName($updateItem->getField()) . ' = ';
|
2009-03-21 22:58:52 +03:00
|
|
|
|
|
|
|
$newValue = $updateItem->getNewValue();
|
2009-03-23 20:39:33 +03:00
|
|
|
|
|
|
|
if ($newValue instanceof AST\Node) {
|
|
|
|
$sql .= $newValue->dispatch($this);
|
|
|
|
} else if (is_string($newValue)) {
|
|
|
|
if (strcasecmp($newValue, 'NULL') === 0) {
|
|
|
|
$sql .= 'NULL';
|
|
|
|
} else {
|
2009-06-14 21:34:28 +04:00
|
|
|
$sql .= $this->_conn->quote($newValue);
|
2009-03-23 20:39:33 +03:00
|
|
|
}
|
|
|
|
}
|
2009-06-23 21:50:13 +04:00
|
|
|
|
|
|
|
$this->_useSqlTableAliases = $useTableAliasesBefore;
|
2009-03-21 22:58:52 +03:00
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a WhereClause AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param WhereClause
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkWhereClause($whereClause)
|
|
|
|
{
|
|
|
|
$sql = ' WHERE ';
|
|
|
|
$condExpr = $whereClause->getConditionalExpression();
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2009-03-23 20:39:33 +03:00
|
|
|
$sql .= implode(' OR ', array_map(array($this, 'walkConditionalTerm'),
|
2009-01-21 21:25:05 +03:00
|
|
|
$condExpr->getConditionalTerms()));
|
2009-05-03 14:58:16 +04:00
|
|
|
|
|
|
|
$discrSql = $this->_generateDiscriminatorColumnConditionSql($this->_currentRootAlias);
|
|
|
|
if ($discrSql) {
|
2009-05-17 23:27:12 +04:00
|
|
|
if ($termsSql) $sql .= ' AND';
|
|
|
|
$sql .= ' ' . $discrSql;
|
2009-05-03 14:58:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-05-03 22:07:57 +04:00
|
|
|
/**
|
|
|
|
* Generates a discriminator column SQL condition for the class with the given DQL alias.
|
|
|
|
*
|
|
|
|
* @param string $dqlAlias
|
|
|
|
* @return string
|
|
|
|
*/
|
2009-05-03 14:58:16 +04:00
|
|
|
private function _generateDiscriminatorColumnConditionSql($dqlAlias)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
if ($dqlAlias) {
|
|
|
|
$class = $this->_queryComponents[$dqlAlias]['metadata'];
|
|
|
|
if ($class->isInheritanceTypeSingleTable()) {
|
|
|
|
$conn = $this->_em->getConnection();
|
2009-05-14 14:03:09 +04:00
|
|
|
$values = array($conn->quote($class->discriminatorValue));
|
|
|
|
foreach ($class->subClasses as $subclassName) {
|
|
|
|
$values[] = $conn->quote($this->_em->getClassMetadata($subclassName)->discriminatorValue);
|
2009-05-03 14:58:16 +04:00
|
|
|
}
|
2009-05-14 14:03:09 +04:00
|
|
|
$discrColumn = $class->discriminatorColumn;
|
2009-05-03 14:58:16 +04:00
|
|
|
if ($this->_useSqlTableAliases) {
|
2009-07-21 13:25:14 +04:00
|
|
|
$sql .= $this->getSqlTableAlias($class->getTableName(), $dqlAlias) . '.';
|
2009-05-03 14:58:16 +04:00
|
|
|
}
|
|
|
|
$sql .= $discrColumn['name'] . ' IN (' . implode(', ', $values) . ')';
|
|
|
|
}
|
|
|
|
}
|
2009-01-20 20:07:07 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a ConditionalTerm AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param ConditionalTerm
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkConditionalTerm($condTerm)
|
|
|
|
{
|
2009-03-23 20:39:33 +03:00
|
|
|
return implode(' AND ', array_map(array($this, 'walkConditionalFactor'),
|
2009-01-21 21:25:05 +03:00
|
|
|
$condTerm->getConditionalFactors()));
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a ConditionalFactor AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param ConditionalFactor
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkConditionalFactor($factor)
|
|
|
|
{
|
|
|
|
$sql = '';
|
2009-03-21 00:28:19 +03:00
|
|
|
if ($factor->isNot()) $sql .= 'NOT ';
|
2009-01-20 20:07:07 +03:00
|
|
|
$primary = $factor->getConditionalPrimary();
|
|
|
|
if ($primary->isSimpleConditionalExpression()) {
|
2009-03-23 20:39:33 +03:00
|
|
|
$sql .= $primary->getSimpleConditionalExpression()->dispatch($this);
|
2009-01-21 21:25:05 +03:00
|
|
|
} else if ($primary->isConditionalExpression()) {
|
2009-03-23 20:39:33 +03:00
|
|
|
$sql .= '(' . implode(' OR ', array_map(array($this, 'walkConditionalTerm'),
|
2009-01-21 21:25:05 +03:00
|
|
|
$primary->getConditionalExpression()->getConditionalTerms())) . ')';
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
2009-01-21 21:25:05 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
2009-01-20 20:07:07 +03:00
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down an ExistsExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param ExistsExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-23 20:39:33 +03:00
|
|
|
public function walkExistsExpression($existsExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
if ($existsExpr->isNot()) $sql .= ' NOT';
|
2009-05-17 23:27:12 +04:00
|
|
|
$sql .= 'EXISTS (' . $this->walkSubselect($existsExpr->getSubselect()) . ')';
|
2009-03-23 20:39:33 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
2009-06-14 21:34:28 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Walks down a CollectionMemberExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param CollectionMemberExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkCollectionMemberExpression($collMemberExpr)
|
|
|
|
{
|
|
|
|
$sql = $collMemberExpr->isNot ? 'NOT ' : '';
|
|
|
|
$sql .= 'EXISTS (SELECT 1 FROM ';
|
|
|
|
$entityExpr = $collMemberExpr->entityExpression;
|
|
|
|
$collPathExpr = $collMemberExpr->collectionValuedPathExpression;
|
|
|
|
$parts = $collPathExpr->getParts();
|
|
|
|
$dqlAlias = $collPathExpr->getIdentificationVariable();
|
|
|
|
|
|
|
|
$class = $this->_queryComponents[$dqlAlias]['metadata'];
|
|
|
|
|
|
|
|
if ($entityExpr instanceof AST\InputParameter) {
|
|
|
|
$dqlParamKey = $entityExpr->isNamed() ? $entityExpr->getName() : $entityExpr->getPosition();
|
|
|
|
$entity = $this->_query->getParameter($dqlParamKey);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
throw DoctrineException::notImplemented();
|
|
|
|
}
|
|
|
|
|
|
|
|
$assoc = $class->associationMappings[$parts[0]];
|
|
|
|
if ($assoc->isOneToMany()) {
|
|
|
|
$targetClass = $this->_em->getClassMetadata($assoc->targetEntityName);
|
|
|
|
$targetTableAlias = $this->getSqlTableAlias($targetClass->primaryTable['name']);
|
2009-07-21 13:25:14 +04:00
|
|
|
$sourceTableAlias = $this->getSqlTableAlias($class->primaryTable['name'], $dqlAlias);
|
2009-06-14 21:34:28 +04:00
|
|
|
|
|
|
|
$sql .= $this->_conn->quoteIdentifier($targetClass->primaryTable['name'])
|
|
|
|
. ' ' . $targetTableAlias . ' WHERE ';
|
|
|
|
|
|
|
|
$owningAssoc = $targetClass->associationMappings[$assoc->mappedByFieldName];
|
|
|
|
|
|
|
|
$first = true;
|
|
|
|
foreach ($owningAssoc->targetToSourceKeyColumns as $targetColumn => $sourceColumn) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
|
|
|
$sql .= $sourceTableAlias . '.' . $targetColumn . ' = ' . $targetTableAlias . '.' . $sourceColumn;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ' AND ';
|
|
|
|
$first = true;
|
|
|
|
foreach ($targetClass->identifier as $idField) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
|
|
|
$this->_parserResult->addParameterMapping($dqlParamKey, $this->_sqlParamIndex++);
|
|
|
|
$sql .= $targetTableAlias . '.' . $targetClass->columnNames[$idField] . ' = ?';
|
|
|
|
}
|
|
|
|
} else { // many-to-many
|
|
|
|
$targetClass = $this->_em->getClassMetadata($assoc->targetEntityName);
|
2009-07-21 13:25:14 +04:00
|
|
|
$sourceTableAlias = $this->getSqlTableAlias($class->primaryTable['name'], $dqlAlias);
|
2009-06-14 21:34:28 +04:00
|
|
|
$joinTable = $assoc->isOwningSide ? $assoc->joinTable : $targetClass->associationMappings[$assoc->mappedByFieldName]->joinTable;
|
|
|
|
$joinTableAlias = $this->getSqlTableAlias($joinTable['name']);
|
|
|
|
$targetTableAlias = $this->getSqlTableAlias($targetClass->primaryTable['name']);
|
|
|
|
|
|
|
|
// join to target table
|
|
|
|
$sql .= $this->_conn->quoteIdentifier($joinTable['name'])
|
|
|
|
. ' ' . $joinTableAlias . ' INNER JOIN '
|
|
|
|
. $this->_conn->quoteIdentifier($targetClass->primaryTable['name'])
|
|
|
|
. ' ' . $targetTableAlias . ' ON ';
|
|
|
|
|
|
|
|
// join conditions
|
|
|
|
$joinColumns = $assoc->isOwningSide ? $joinTable['joinColumns'] : $joinTable['inverseJoinColumns'];
|
|
|
|
$first = true;
|
|
|
|
foreach ($joinColumns as $joinColumn) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
|
|
|
$sql .= $joinTableAlias . '.' . $joinColumn['name'] . ' = ' . $sourceTableAlias . '.' . $joinColumn['referencedColumnName'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ' WHERE ';
|
|
|
|
|
|
|
|
$joinColumns = $assoc->isOwningSide ? $joinTable['inverseJoinColumns'] : $joinTable['joinColumns'];
|
|
|
|
$first = true;
|
|
|
|
foreach ($joinColumns as $joinColumn) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
|
|
|
$sql .= $joinTableAlias . '.' . $joinColumn['name'] . ' = ' . $targetTableAlias . '.' . $joinColumn['referencedColumnName'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ' AND ';
|
|
|
|
$first = true;
|
|
|
|
foreach ($targetClass->identifier as $idField) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
|
|
|
$this->_parserResult->addParameterMapping($dqlParamKey, $this->_sqlParamIndex++);
|
|
|
|
$sql .= $targetTableAlias . '.' . $targetClass->columnNames[$idField] . ' = ?';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql . ')';
|
|
|
|
}
|
2009-03-23 20:39:33 +03:00
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a NullComparisonExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param NullComparisonExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-21 15:49:58 +03:00
|
|
|
public function walkNullComparisonExpression($nullCompExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
2009-03-23 20:39:33 +03:00
|
|
|
$innerExpr = $nullCompExpr->getExpression();
|
|
|
|
if ($innerExpr instanceof AST\InputParameter) {
|
2009-06-14 21:34:28 +04:00
|
|
|
$dqlParamKey = $innerExpr->isNamed() ? $innerExpr->getName() : $innerExpr->getPosition();
|
|
|
|
$this->_parserResult->addParameterMapping($dqlParamKey, $this->_sqlParamIndex++);
|
|
|
|
$sql .= ' ?';// . ($innerExpr->isNamed() ? ':' . $innerExpr->getName() : '?');
|
2009-03-21 15:49:58 +03:00
|
|
|
} else {
|
2009-03-23 20:39:33 +03:00
|
|
|
$sql .= $this->walkPathExpression($innerExpr);
|
2009-03-21 15:49:58 +03:00
|
|
|
}
|
|
|
|
$sql .= ' IS' . ($nullCompExpr->isNot() ? ' NOT' : '') . ' NULL';
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down an InExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param InExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-21 15:49:58 +03:00
|
|
|
public function walkInExpression($inExpr)
|
|
|
|
{
|
|
|
|
$sql = $this->walkPathExpression($inExpr->getPathExpression());
|
|
|
|
if ($inExpr->isNot()) $sql .= ' NOT';
|
|
|
|
$sql .= ' IN (';
|
|
|
|
if ($inExpr->getSubselect()) {
|
|
|
|
$sql .= $this->walkSubselect($inExpr->getSubselect());
|
|
|
|
} else {
|
2009-03-21 22:58:52 +03:00
|
|
|
$sql .= implode(', ', array_map(array($this, 'walkLiteral'), $inExpr->getLiterals()));
|
2009-03-21 15:49:58 +03:00
|
|
|
}
|
|
|
|
$sql .= ')';
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a literal that represents an AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param mixed
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-21 22:58:52 +03:00
|
|
|
public function walkLiteral($literal)
|
|
|
|
{
|
|
|
|
if ($literal instanceof AST\InputParameter) {
|
2009-06-14 21:34:28 +04:00
|
|
|
$dqlParamKey = $literal->isNamed() ? $literal->getName() : $literal->getPosition();
|
|
|
|
$this->_parserResult->addParameterMapping($dqlParamKey, $this->_sqlParamIndex++);
|
|
|
|
return '?';
|
2009-03-21 22:58:52 +03:00
|
|
|
} else {
|
2009-03-23 20:39:33 +03:00
|
|
|
return $literal; //TODO: quote() ?
|
2009-03-21 22:58:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a BetweenExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param BetweenExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-21 15:49:58 +03:00
|
|
|
public function walkBetweenExpression($betweenExpr)
|
|
|
|
{
|
|
|
|
$sql = $this->walkArithmeticExpression($betweenExpr->getBaseExpression());
|
|
|
|
if ($betweenExpr->getNot()) $sql .= ' NOT';
|
|
|
|
$sql .= ' BETWEEN ' . $this->walkArithmeticExpression($betweenExpr->getLeftBetweenExpression())
|
|
|
|
. ' AND ' . $this->walkArithmeticExpression($betweenExpr->getRightBetweenExpression());
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a LikeExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param LikeExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-21 21:25:05 +03:00
|
|
|
public function walkLikeExpression($likeExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
$stringExpr = $likeExpr->getStringExpression();
|
2009-03-23 20:39:33 +03:00
|
|
|
|
|
|
|
$sql .= $stringExpr->dispatch($this);
|
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
if ($likeExpr->isNot()) $sql .= ' NOT';
|
|
|
|
$sql .= ' LIKE ';
|
2009-03-23 20:39:33 +03:00
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
if ($likeExpr->getStringPattern() instanceof AST\InputParameter) {
|
|
|
|
$inputParam = $likeExpr->getStringPattern();
|
2009-06-14 21:34:28 +04:00
|
|
|
$dqlParamKey = $inputParam->isNamed() ? $inputParam->getName() : $inputParam->getPosition();
|
|
|
|
$this->_parserResult->addParameterMapping($dqlParamKey, $this->_sqlParamIndex++);
|
|
|
|
$sql .= '?';
|
2009-03-21 15:49:58 +03:00
|
|
|
} else {
|
2009-06-14 21:34:28 +04:00
|
|
|
$sql .= $this->_conn->quote($likeExpr->getStringPattern());
|
2009-03-21 15:49:58 +03:00
|
|
|
}
|
|
|
|
if ($likeExpr->getEscapeChar()) {
|
2009-06-14 21:34:28 +04:00
|
|
|
$sql .= ' ESCAPE ' . $this->_conn->quote($likeExpr->getEscapeChar());
|
2009-03-21 15:49:58 +03:00
|
|
|
}
|
2009-01-20 20:07:07 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a StateFieldPathExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param StateFieldPathExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-23 20:39:33 +03:00
|
|
|
public function walkStateFieldPathExpression($stateFieldPathExpression)
|
|
|
|
{
|
|
|
|
return $this->walkPathExpression($stateFieldPathExpression);
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a ComparisonExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param ComparisonExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkComparisonExpression($compExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
2009-03-23 20:39:33 +03:00
|
|
|
$leftExpr = $compExpr->getLeftExpression();
|
|
|
|
$rightExpr = $compExpr->getRightExpression();
|
|
|
|
|
|
|
|
if ($leftExpr instanceof AST\Node) {
|
|
|
|
$sql .= $leftExpr->dispatch($this);
|
|
|
|
} else {
|
2009-07-18 18:53:21 +04:00
|
|
|
$sql .= $this->_conn->quote($leftExpr);
|
2009-03-23 20:39:33 +03:00
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
$sql .= ' ' . $compExpr->getOperator() . ' ';
|
2009-03-23 20:39:33 +03:00
|
|
|
|
|
|
|
if ($rightExpr instanceof AST\Node) {
|
|
|
|
$sql .= $rightExpr->dispatch($this);
|
|
|
|
} else {
|
2009-07-18 18:53:21 +04:00
|
|
|
$sql .= $this->_conn->quote($rightExpr);
|
2009-03-23 20:39:33 +03:00
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down an InputParameter AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param InputParameter
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-23 20:39:33 +03:00
|
|
|
public function walkInputParameter($inputParam)
|
|
|
|
{
|
2009-06-14 21:34:28 +04:00
|
|
|
$dqlParamKey = $inputParam->isNamed() ? $inputParam->getName() : $inputParam->getPosition();
|
|
|
|
$this->_parserResult->addParameterMapping($dqlParamKey, $this->_sqlParamIndex++);
|
|
|
|
return '?';
|
2009-03-23 20:39:33 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down an ArithmeticExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param ArithmeticExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkArithmeticExpression($arithmeticExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
if ($arithmeticExpr->isSimpleArithmeticExpression()) {
|
|
|
|
foreach ($arithmeticExpr->getSimpleArithmeticExpression()->getArithmeticTerms() as $term) {
|
|
|
|
$sql .= $this->walkArithmeticTerm($term);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$sql .= $this->walkSubselect($arithmeticExpr->getSubselect());
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down an ArithmeticTerm AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param mixed
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkArithmeticTerm($term)
|
|
|
|
{
|
2009-01-21 21:25:05 +03:00
|
|
|
if (is_string($term)) return $term;
|
2009-03-23 20:39:33 +03:00
|
|
|
|
|
|
|
return implode(' ', array_map(array($this, 'walkArithmeticFactor'),
|
2009-01-21 21:25:05 +03:00
|
|
|
$term->getArithmeticFactors()));
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a StringPrimary that represents an AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param mixed
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-03-23 20:39:33 +03:00
|
|
|
public function walkStringPrimary($stringPrimary)
|
|
|
|
{
|
|
|
|
if (is_string($stringPrimary)) {
|
2009-06-14 21:34:28 +04:00
|
|
|
return $this->_conn->quote($stringPrimary);
|
2009-03-23 20:39:33 +03:00
|
|
|
} else {
|
|
|
|
return $stringPrimary->dispatch($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down an ArithmeticFactor that represents an AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param mixed
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkArithmeticFactor($factor)
|
|
|
|
{
|
2009-01-21 21:25:05 +03:00
|
|
|
if (is_string($factor)) return $factor;
|
2009-03-23 20:39:33 +03:00
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
$sql = '';
|
|
|
|
$primary = $factor->getArithmeticPrimary();
|
2009-01-21 21:25:05 +03:00
|
|
|
if (is_numeric($primary)) {
|
2009-03-23 20:39:33 +03:00
|
|
|
$sql .= $primary; //TODO: quote() ?
|
2009-01-22 22:38:10 +03:00
|
|
|
} else if (is_string($primary)) {
|
2009-06-14 21:34:28 +04:00
|
|
|
$sql .= $this->_conn->quote($primary);
|
2009-01-22 22:38:10 +03:00
|
|
|
} else if ($primary instanceof AST\SimpleArithmeticExpression) {
|
2009-01-21 21:25:05 +03:00
|
|
|
$sql .= '(' . $this->walkSimpleArithmeticExpression($primary) . ')';
|
2009-03-23 20:39:33 +03:00
|
|
|
} else if ($primary instanceof AST\Node) {
|
|
|
|
$sql .= $primary->dispatch($this);
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down an SimpleArithmeticExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param SimpleArithmeticExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-21 21:25:05 +03:00
|
|
|
public function walkSimpleArithmeticExpression($simpleArithmeticExpr)
|
|
|
|
{
|
2009-03-23 20:39:33 +03:00
|
|
|
return implode(' ', array_map(array($this, 'walkArithmeticTerm'),
|
2009-01-21 21:25:05 +03:00
|
|
|
$simpleArithmeticExpr->getArithmeticTerms()));
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down an PathExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param mixed
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkPathExpression($pathExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
2009-06-14 21:34:28 +04:00
|
|
|
if ($pathExpr->getType() == AST\PathExpression::TYPE_STATE_FIELD) {
|
2009-01-20 20:07:07 +03:00
|
|
|
$parts = $pathExpr->getParts();
|
|
|
|
$numParts = count($parts);
|
2009-06-14 21:34:28 +04:00
|
|
|
$dqlAlias = $pathExpr->getIdentificationVariable();
|
2009-01-20 20:07:07 +03:00
|
|
|
$fieldName = $parts[$numParts-1];
|
2009-04-09 22:12:48 +04:00
|
|
|
$qComp = $this->_queryComponents[$dqlAlias];
|
2009-01-20 20:07:07 +03:00
|
|
|
$class = $qComp['metadata'];
|
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
if ($this->_useSqlTableAliases) {
|
2009-06-23 21:50:13 +04:00
|
|
|
if ($class->isInheritanceTypeJoined() && isset($class->fieldMappings[$fieldName]['inherited'])) {
|
|
|
|
$sql .= $this->getSqlTableAlias($this->_em->getClassMetadata(
|
2009-07-21 13:25:14 +04:00
|
|
|
$class->fieldMappings[$fieldName]['inherited'])->getTableName(), $dqlAlias) . '.';
|
2009-06-23 21:50:13 +04:00
|
|
|
} else {
|
2009-07-21 13:25:14 +04:00
|
|
|
$sql .= $this->getSqlTableAlias($class->getTableName(), $dqlAlias) . '.';
|
2009-06-23 21:50:13 +04:00
|
|
|
}
|
2009-05-17 23:27:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($class->associationMappings[$fieldName])) {
|
2009-05-18 00:42:58 +04:00
|
|
|
//FIXME: Inverse side support
|
|
|
|
//FIXME: Throw exception on composite key
|
2009-05-17 23:27:12 +04:00
|
|
|
$sql .= $class->associationMappings[$fieldName]->joinColumns[0]['name'];
|
|
|
|
} else {
|
2009-05-30 13:37:56 +04:00
|
|
|
$sql .= $this->_conn->quoteIdentifier($class->getColumnName($fieldName));
|
2009-05-03 14:58:16 +04:00
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
} else if ($pathExpr->isSimpleStateFieldAssociationPathExpression()) {
|
2009-03-23 20:39:33 +03:00
|
|
|
throw DoctrineException::updateMe("Not yet implemented.");
|
2009-01-20 20:07:07 +03:00
|
|
|
} else {
|
2009-03-23 20:39:33 +03:00
|
|
|
throw DoctrineException::updateMe("Encountered invalid PathExpression during SQL construction.");
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
2009-05-30 13:37:56 +04:00
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
/**
|
2009-02-17 13:54:18 +03:00
|
|
|
* Generates a unique, short SQL table alias.
|
2009-01-19 21:40:12 +03:00
|
|
|
*
|
2009-04-09 22:12:48 +04:00
|
|
|
* @param string $dqlAlias The DQL alias.
|
2009-02-17 13:54:18 +03:00
|
|
|
* @return string Generated table alias.
|
2009-01-19 21:40:12 +03:00
|
|
|
*/
|
2009-07-21 13:25:14 +04:00
|
|
|
public function getSqlTableAlias($tableName, $dqlAlias = '')
|
2009-04-09 22:12:48 +04:00
|
|
|
{
|
2009-07-21 13:25:14 +04:00
|
|
|
$tableName .= $dqlAlias;
|
2009-04-09 22:12:48 +04:00
|
|
|
if ( ! isset($this->_dqlToSqlAliasMap[$tableName])) {
|
|
|
|
$this->_dqlToSqlAliasMap[$tableName] = strtolower(substr($tableName, 0, 1)) . $this->_tableAliasCounter++ . '_';
|
|
|
|
}
|
|
|
|
return $this->_dqlToSqlAliasMap[$tableName];
|
|
|
|
}
|
2009-06-22 22:48:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function setSqlTableAlias($tableName, $alias)
|
|
|
|
{
|
|
|
|
$this->_dqlToSqlAliasMap[$tableName] = $alias;
|
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2009-06-22 22:48:42 +04:00
|
|
|
/**
|
|
|
|
* Gets an SQL column alias for a column name.
|
|
|
|
*
|
|
|
|
* @param string $columnName
|
|
|
|
* @return string
|
|
|
|
*/
|
2009-04-09 22:12:48 +04:00
|
|
|
public function getSqlColumnAlias($columnName)
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-05-29 14:23:13 +04:00
|
|
|
return trim($columnName, '`') . $this->_aliasCounter++;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
|
|
|
/**
|
2009-07-21 13:25:14 +04:00
|
|
|
* Generates the SQL JOINs that are necessary for Class Table Inheritance
|
2009-05-21 12:53:40 +04:00
|
|
|
* for the given class.
|
|
|
|
*
|
2009-07-21 13:25:14 +04:00
|
|
|
* @param ClassMetadata $class The class for which to generate the joins.
|
|
|
|
* @param string $dqlAlias The DQL alias of the class.
|
|
|
|
* @return string The SQL.
|
2009-05-21 12:53:40 +04:00
|
|
|
*/
|
|
|
|
private function _generateClassTableInheritanceJoins($class, $dqlAlias)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
|
2009-07-21 13:25:14 +04:00
|
|
|
$baseTableAlias = $this->getSqlTableAlias($class->primaryTable['name'], $dqlAlias);
|
2009-05-21 12:53:40 +04:00
|
|
|
$idColumns = $class->getIdentifierColumnNames();
|
|
|
|
|
|
|
|
// INNER JOIN parent class tables
|
|
|
|
foreach ($class->parentClasses as $parentClassName) {
|
|
|
|
$parentClass = $this->_em->getClassMetadata($parentClassName);
|
2009-07-21 13:25:14 +04:00
|
|
|
$tableAlias = $this->getSqlTableAlias($parentClass->primaryTable['name'], $dqlAlias);
|
2009-05-21 12:53:40 +04:00
|
|
|
$sql .= ' INNER JOIN ' . $parentClass->primaryTable['name'] . ' ' . $tableAlias . ' ON ';
|
|
|
|
$first = true;
|
|
|
|
foreach ($idColumns as $idColumn) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
|
|
|
$sql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LEFT JOIN subclass tables
|
|
|
|
foreach ($class->subClasses as $subClassName) {
|
|
|
|
$subClass = $this->_em->getClassMetadata($subClassName);
|
2009-07-21 13:25:14 +04:00
|
|
|
$tableAlias = $this->getSqlTableAlias($subClass->primaryTable['name'], $dqlAlias);
|
2009-05-21 12:53:40 +04:00
|
|
|
$sql .= ' LEFT JOIN ' . $subClass->primaryTable['name'] . ' ' . $tableAlias . ' ON ';
|
|
|
|
$first = true;
|
|
|
|
foreach ($idColumns as $idColumn) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
|
|
|
$sql .= $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
2009-07-21 13:25:14 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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->getDeleteClause()->getAbstractSchemaName()
|
|
|
|
);
|
|
|
|
if ($primaryClass->isInheritanceTypeJoined()) {
|
|
|
|
return new Exec\MultiTableDeleteExecutor($AST, $this);
|
|
|
|
} else {
|
|
|
|
return new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
|
|
|
|
}
|
|
|
|
} else if ($isUpdateStatement) {
|
|
|
|
$primaryClass = $this->_em->getClassMetadata(
|
|
|
|
$AST->getUpdateClause()->getAbstractSchemaName()
|
|
|
|
);
|
|
|
|
if ($primaryClass->isInheritanceTypeJoined()) {
|
|
|
|
return new Exec\MultiTableUpdateExecutor($AST, $this);
|
|
|
|
} else {
|
|
|
|
return new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return new Exec\SingleSelectExecutor($AST, $this);
|
|
|
|
}
|
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|