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-08-05 19:47:41 +04:00
|
|
|
use Doctrine\ORM\Query,
|
|
|
|
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-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;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
/** Counter for generating unique column aliases. */
|
2009-04-09 22:12:48 +04:00
|
|
|
private $_aliasCounter = 0;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
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-08-05 01:41:53 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
/** Counter for SQL parameter positions. */
|
|
|
|
private $_sqlParamIndex = 1;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
/** The ParserResult. */
|
2009-01-19 21:40:12 +03:00
|
|
|
private $_parserResult;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
/** The EntityManager. */
|
2009-01-19 21:40:12 +03:00
|
|
|
private $_em;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
/** The Connection of the EntityManager. */
|
2009-05-29 14:23:13 +04:00
|
|
|
private $_conn;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
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-08-05 01:41:53 +04:00
|
|
|
|
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-08-05 01:41:53 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
/** A list of classes that appear in non-scalar SelectExpressions. */
|
|
|
|
private $_selectedClasses = array();
|
2009-08-05 01:41:53 +04:00
|
|
|
|
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-08-05 01:41:53 +04:00
|
|
|
|
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-08-11 14:51:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The database platform abstraction.
|
|
|
|
*
|
|
|
|
* @var AbstractPlatform
|
|
|
|
*/
|
|
|
|
private $_platform;
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-02-05 20:34:44 +03:00
|
|
|
/**
|
2009-08-04 07:33:45 +04:00
|
|
|
* @inheritdoc
|
2009-02-05 20:34:44 +03:00
|
|
|
*/
|
2009-08-04 13:33:36 +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-08-11 14:51:38 +04:00
|
|
|
$this->_platform = $this->_conn->getDatabasePlatform();
|
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()
|
|
|
|
{
|
2009-08-11 14:51:38 +04:00
|
|
|
return $this->_conn;
|
2009-03-28 20:10:41 +03:00
|
|
|
}
|
2009-06-22 22:48:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the EntityManager used by the walker.
|
|
|
|
*
|
|
|
|
* @return EntityManager
|
|
|
|
*/
|
|
|
|
public function getEntityManager()
|
|
|
|
{
|
|
|
|
return $this->_em;
|
|
|
|
}
|
2009-08-05 19:47:41 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
/**
|
2009-08-11 14:51:38 +04:00
|
|
|
* Gets the information about a single query component.
|
|
|
|
*
|
|
|
|
* @param string $dqlAlias The DQL alias.
|
2009-08-07 01:42:07 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2009-08-05 19:47:41 +04:00
|
|
|
public function getQueryComponent($dqlAlias)
|
|
|
|
{
|
|
|
|
return $this->_queryComponents[$dqlAlias];
|
|
|
|
}
|
2009-08-07 01:42:07 +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->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);
|
|
|
|
}
|
|
|
|
}
|
2009-08-13 06:17:27 +04:00
|
|
|
|
|
|
|
return new Exec\SingleSelectExecutor($AST, $this);
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a unique, short SQL table alias.
|
|
|
|
*
|
|
|
|
* @param string $dqlAlias The DQL alias.
|
|
|
|
* @return string Generated table alias.
|
|
|
|
*/
|
|
|
|
public function getSqlTableAlias($tableName, $dqlAlias = '')
|
|
|
|
{
|
|
|
|
$tableName .= $dqlAlias;
|
|
|
|
|
|
|
|
if ( ! isset($this->_dqlToSqlAliasMap[$tableName])) {
|
|
|
|
$this->_dqlToSqlAliasMap[$tableName] = strtolower(substr($tableName, 0, 1)) . $this->_tableAliasCounter++ . '_';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_dqlToSqlAliasMap[$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
|
|
|
|
*/
|
|
|
|
public function setSqlTableAlias($tableName, $alias)
|
|
|
|
{
|
|
|
|
$this->_dqlToSqlAliasMap[$tableName] = $alias;
|
2009-08-13 06:17:27 +04:00
|
|
|
|
|
|
|
return $alias;
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an SQL column alias for a column name.
|
|
|
|
*
|
|
|
|
* @param string $columnName
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSqlColumnAlias($columnName)
|
|
|
|
{
|
2009-08-11 14:51:38 +04:00
|
|
|
return $columnName . $this->_aliasCounter++;
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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->primaryTable['name'], $dqlAlias);
|
|
|
|
|
|
|
|
// INNER JOIN parent class tables
|
|
|
|
foreach ($class->parentClasses as $parentClassName) {
|
|
|
|
$parentClass = $this->_em->getClassMetadata($parentClassName);
|
|
|
|
$tableAlias = $this->getSqlTableAlias($parentClass->primaryTable['name'], $dqlAlias);
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= ' INNER JOIN ' . $parentClass->getQuotedTableName($this->_platform)
|
2009-08-07 01:42:07 +04:00
|
|
|
. ' ' . $tableAlias . ' ON ';
|
|
|
|
$first = true;
|
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
foreach ($class->identifier as $idField) {
|
2009-08-07 01:42:07 +04:00
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
2009-08-13 06:17:27 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$columnName = $class->getQuotedColumnName($idField, $this->_platform);
|
|
|
|
$sql .= $baseTableAlias . '.' . $columnName
|
2009-08-07 01:42:07 +04:00
|
|
|
. ' = '
|
2009-08-11 14:51:38 +04:00
|
|
|
. $tableAlias . '.' . $columnName;
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LEFT JOIN subclass tables
|
|
|
|
foreach ($class->subClasses as $subClassName) {
|
|
|
|
$subClass = $this->_em->getClassMetadata($subClassName);
|
|
|
|
$tableAlias = $this->getSqlTableAlias($subClass->primaryTable['name'], $dqlAlias);
|
2009-08-13 06:17:27 +04:00
|
|
|
$sql .= ' LEFT JOIN ' . $subClass->getQuotedTableName($this->_platform)
|
|
|
|
. ' ' . $tableAlias . ' ON ';
|
2009-08-07 01:42:07 +04:00
|
|
|
$first = true;
|
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
foreach ($class->identifier as $idField) {
|
2009-08-07 01:42:07 +04:00
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
2009-08-13 06:17:27 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$columnName = $class->getQuotedColumnName($idField, $this->_platform);
|
|
|
|
$sql .= $baseTableAlias . '.' . $columnName
|
2009-08-07 01:42:07 +04:00
|
|
|
. ' = '
|
2009-08-11 14:51:38 +04:00
|
|
|
. $tableAlias . '.' . $columnName;
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
/**
|
|
|
|
* Generates a discriminator column SQL condition for the class with the given DQL alias.
|
|
|
|
*
|
|
|
|
* @param string $dqlAlias
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function _generateDiscriminatorColumnConditionSql($dqlAlias)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
|
|
|
|
if ($dqlAlias) {
|
|
|
|
$class = $this->_queryComponents[$dqlAlias]['metadata'];
|
|
|
|
|
|
|
|
if ($class->isInheritanceTypeSingleTable()) {
|
|
|
|
$conn = $this->_em->getConnection();
|
|
|
|
$values = array($conn->quote($class->discriminatorValue));
|
|
|
|
|
|
|
|
foreach ($class->subClasses as $subclassName) {
|
|
|
|
$values[] = $conn->quote($this->_em->getClassMetadata($subclassName)->discriminatorValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= (($this->_useSqlTableAliases)
|
|
|
|
? $this->getSqlTableAlias($class->primaryTable['name'], $dqlAlias) . '.' : ''
|
|
|
|
) . $class->getQuotedDiscriminatorColumnName($this->_platform)
|
|
|
|
. ' IN (' . implode(', ', $values) . ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
2009-03-28 20:10:41 +03:00
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
|
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
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql = $this->walkSelectClause($AST->selectClause);
|
|
|
|
$sql .= $this->walkFromClause($AST->fromClause);
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
if (($whereClause = $AST->whereClause) !== null) {
|
2009-05-03 14:58:16 +04:00
|
|
|
$sql .= $this->walkWhereClause($whereClause);
|
2009-08-13 06:17:27 +04:00
|
|
|
} else if (($discSql = $this->_generateDiscriminatorColumnConditionSql($this->_currentRootAlias)) !== '') {
|
2009-05-03 14:58:16 +04:00
|
|
|
$sql .= ' WHERE ' . $discSql;
|
|
|
|
}
|
2009-05-07 17:54:01 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= $AST->groupByClause ? $this->walkGroupByClause($AST->groupByClause) : '';
|
|
|
|
$sql .= $AST->havingClause ? $this->walkHavingClause($AST->havingClause) : '';
|
|
|
|
$sql .= $AST->orderByClause ? $this->walkOrderByClause($AST->orderByClause) : '';
|
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(
|
2009-08-05 01:41:53 +04:00
|
|
|
$sql, $q->getMaxResults(), $q->getFirstResult()
|
|
|
|
);
|
2009-07-10 21:53:48 +04:00
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
2009-08-07 01:42:07 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
if (($whereClause = $AST->whereClause) !== null) {
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= $this->walkWhereClause($whereClause);
|
2009-08-13 06:17:27 +04:00
|
|
|
} else if (($discSql = $this->_generateDiscriminatorColumnConditionSql($this->_currentRootAlias)) !== '') {
|
2009-08-07 01:42:07 +04:00
|
|
|
$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);
|
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
if (($whereClause = $AST->whereClause) !== null) {
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= $this->walkWhereClause($whereClause);
|
2009-08-13 06:17:27 +04:00
|
|
|
} else if (($discSql = $this->_generateDiscriminatorColumnConditionSql($this->_currentRootAlias)) !== '') {
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= ' WHERE ' . $discSql;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Walks down an IdentificationVariable (no AST node associated), thereby generating the SQL.
|
|
|
|
*
|
2009-08-13 06:17:27 +04:00
|
|
|
* @param string $identificationVariable
|
2009-08-07 01:42:07 +04:00
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-08-13 06:17:27 +04:00
|
|
|
public function walkIdentificationVariable($identificationVariable, $fieldName = null)
|
2009-08-07 01:42:07 +04:00
|
|
|
{
|
2009-08-13 06:17:27 +04:00
|
|
|
$class = $this->_queryComponents[$identificationVariable]['metadata'];
|
|
|
|
|
|
|
|
if (
|
|
|
|
$fieldName !== null && $class->isInheritanceTypeJoined() &&
|
|
|
|
isset($class->fieldMappings[$fieldName]['inherited'])
|
|
|
|
) {
|
|
|
|
$class = $this->_em->getClassMetadata($class->fieldMappings[$fieldName]['inherited']);
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
2009-08-13 06:17:27 +04:00
|
|
|
|
|
|
|
return $this->getSqlTableAlias($class->primaryTable['name'], $identificationVariable);
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
/**
|
|
|
|
* Walks down a PathExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param mixed
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkPathExpression($pathExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
$pathExprType = $pathExpr->type;
|
|
|
|
|
|
|
|
switch ($pathExpr->type) {
|
|
|
|
case AST\PathExpression::TYPE_STATE_FIELD:
|
|
|
|
$parts = $pathExpr->parts;
|
|
|
|
$numParts = count($parts);
|
|
|
|
$dqlAlias = $pathExpr->identificationVariable;
|
|
|
|
$fieldName = $parts[$numParts - 1];
|
|
|
|
$qComp = $this->_queryComponents[$dqlAlias];
|
|
|
|
$class = $qComp['metadata'];
|
|
|
|
|
|
|
|
if ($this->_useSqlTableAliases) {
|
|
|
|
$sql .= $this->walkIdentificationVariable($dqlAlias, $fieldName) . '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($class->associationMappings[$fieldName])) {
|
|
|
|
//FIXME: Inverse side support
|
|
|
|
//FIXME: Throw exception on composite key
|
|
|
|
$assoc = $class->associationMappings[$fieldName];
|
|
|
|
$sql .= $assoc->getQuotedJoinColumnName($assoc->joinColumns[0]['name'], $this->_platform);
|
|
|
|
} else {
|
|
|
|
$sql .= $class->getQuotedColumnName($fieldName, $this->_platform);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AST\PathExpression::TYPE_COLLECTION_VALUED_ASSOCIATION:
|
|
|
|
throw DoctrineException::updateMe("Not yet implemented.");
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw DoctrineException::updateMe(
|
|
|
|
"Encountered invalid PathExpression during SQL construction."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* Walks down a SelectClause AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
2009-08-11 14:51:38 +04:00
|
|
|
* @param $selectClause
|
2009-03-28 20:10:41 +03:00
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2009-01-19 21:40:12 +03:00
|
|
|
public function walkSelectClause($selectClause)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql = 'SELECT ' . (($selectClause->isDistinct) ? 'DISTINCT ' : '') . implode(
|
|
|
|
', ', array_map(array($this, 'walkSelectExpression'), $selectClause->selectExpressions)
|
2009-08-05 01:41:53 +04:00
|
|
|
);
|
2009-08-11 14:51:38 +04:00
|
|
|
|
|
|
|
$addMetaColumns = ! $this->_em->getConfiguration()->getAllowPartialObjects() &&
|
|
|
|
! $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
|
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'],
|
2009-08-11 14:51:38 +04:00
|
|
|
$this->_queryComponents[$dqlAlias]['relation']->sourceFieldName
|
2009-04-09 22:12:48 +04:00
|
|
|
);
|
|
|
|
}
|
2009-05-26 15:30:07 +04:00
|
|
|
|
2009-04-12 23:02:12 +04:00
|
|
|
if ($class->isInheritanceTypeSingleTable() || $class->isInheritanceTypeJoined()) {
|
2009-08-11 14:51:38 +04:00
|
|
|
// Add discriminator columns to SQL
|
2009-05-21 12:53:40 +04:00
|
|
|
$rootClass = $this->_em->getClassMetadata($class->rootEntityName);
|
2009-08-11 14:51:38 +04:00
|
|
|
$tblAlias = $this->getSqlTableAlias($rootClass->primaryTable['name'], $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']);
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= ", $tblAlias." . $rootClass->getQuotedDiscriminatorColumnName($this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' AS ' . $columnAlias;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
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-08-11 14:51:38 +04:00
|
|
|
|
|
|
|
// Add foreign key columns to SQL, if necessary
|
|
|
|
if ($addMetaColumns) {
|
|
|
|
foreach ($class->associationMappings as $assoc) {
|
|
|
|
if ($assoc->isOwningSide && $assoc->isOneToOne()) {
|
|
|
|
if (isset($class->inheritedAssociationFields[$assoc->sourceFieldName])) {
|
|
|
|
$owningClass = $this->_em->getClassMetadata($class->inheritedAssociationFields[$assoc->sourceFieldName]);
|
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($owningClass->primaryTable['name'], $dqlAlias);
|
|
|
|
} else {
|
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($class->primaryTable['name'], $dqlAlias);
|
|
|
|
}
|
2009-08-13 06:17:27 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
foreach ($assoc->targetToSourceKeyColumns as $srcColumn) {
|
|
|
|
$columnAlias = $this->getSqlColumnAlias($srcColumn);
|
2009-08-13 06:17:27 +04:00
|
|
|
$sql .= ", $sqlTableAlias." . $assoc->getQuotedJoinColumnName($srcColumn, $this->_platform)
|
|
|
|
. ' AS ' . $columnAlias;
|
2009-08-11 14:51:38 +04:00
|
|
|
$this->_rsm->addMetaResult($dqlAlias, $columnAlias, $srcColumn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Add foreign key columns to SQL, if necessary
|
|
|
|
if ($addMetaColumns) {
|
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($class->primaryTable['name'], $dqlAlias);
|
2009-08-13 06:17:27 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
foreach ($class->associationMappings as $assoc) {
|
|
|
|
if ($assoc->isOwningSide && $assoc->isOneToOne()) {
|
|
|
|
foreach ($assoc->targetToSourceKeyColumns as $srcColumn) {
|
|
|
|
$columnAlias = $this->getSqlColumnAlias($srcColumn);
|
|
|
|
$sql .= ', ' . $sqlTableAlias . '.' . $assoc->getQuotedJoinColumnName($srcColumn, $this->_platform) . ' AS ' . $columnAlias;
|
|
|
|
$this->_rsm->addMetaResult($dqlAlias, $columnAlias, $srcColumn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 ';
|
2009-08-07 01:42:07 +04:00
|
|
|
$identificationVarDecls = $fromClause->identificationVariableDeclarations;
|
2009-01-19 21:40:12 +03:00
|
|
|
$firstIdentificationVarDecl = $identificationVarDecls[0];
|
2009-08-07 01:42:07 +04:00
|
|
|
$rangeDecl = $firstIdentificationVarDecl->rangeVariableDeclaration;
|
|
|
|
$dqlAlias = $rangeDecl->aliasIdentificationVariable;
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
$this->_currentRootAlias = $dqlAlias;
|
2009-08-13 06:17:27 +04:00
|
|
|
|
|
|
|
$class = $this->_em->getClassMetadata($rangeDecl->abstractSchemaName);
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $class->getQuotedTableName($this->_platform) . ' '
|
2009-08-13 06:17:27 +04:00
|
|
|
. $this->getSqlTableAlias($class->primaryTable['name'], $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
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
foreach ($firstIdentificationVarDecl->joinVariableDeclarations as $joinVarDecl) {
|
2009-01-19 21:40:12 +03:00
|
|
|
$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}*
|
2009-08-05 01:41:53 +04:00
|
|
|
return ' ORDER BY ' . implode(
|
2009-08-07 01:42:07 +04:00
|
|
|
', ', array_map(array($this, 'walkOrderByItem'), $orderByClause->orderByItems)
|
2009-08-05 01:41:53 +04:00
|
|
|
);
|
2009-03-28 20:10:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Walks down an OrderByItem AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param OrderByItem
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkOrderByItem($orderByItem)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$expr = $orderByItem->expression;
|
|
|
|
$parts = $expr->parts;
|
|
|
|
$dqlAlias = $expr->identificationVariable;
|
2009-08-11 14:51:38 +04:00
|
|
|
$class = $this->_queryComponents[$dqlAlias]['metadata'];
|
|
|
|
$columnName = $class->getQuotedColumnName($parts[0], $this->_platform);
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
return $this->getSqlTableAlias($class->getTableName(), $dqlAlias) . '.'
|
|
|
|
. $columnName . ' ' . strtoupper($orderByItem->type);
|
2009-03-28 20:10:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Walks down a HavingClause AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param HavingClause
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkHavingClause($havingClause)
|
|
|
|
{
|
2009-08-05 01:41:53 +04:00
|
|
|
$condExpr = $havingClause->getConditionalExpression();
|
|
|
|
|
|
|
|
return ' HAVING ' . implode(
|
2009-08-07 01:42:07 +04:00
|
|
|
' OR ', array_map(array($this, 'walkConditionalTerm'), $condExpr->conditionalTerms)
|
2009-08-05 01:41:53 +04:00
|
|
|
);
|
2009-03-28 20:10:41 +03:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$join = $joinVarDecl->join;
|
|
|
|
$joinType = $join->joinType;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
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 ';
|
|
|
|
}
|
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$joinAssocPathExpr = $join->joinAssociationPathExpression;
|
|
|
|
$joinedDqlAlias = $join->aliasIdentificationVariable;
|
2009-04-09 22:12:48 +04:00
|
|
|
$targetQComp = $this->_queryComponents[$joinedDqlAlias];
|
2009-08-11 14:51:38 +04:00
|
|
|
$targetClass = $targetQComp['metadata'];
|
|
|
|
$relation = $targetQComp['relation'];
|
|
|
|
$sourceClass = $this->_queryComponents[$joinAssocPathExpr->identificationVariable]['metadata'];
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$targetTableName = $targetClass->getQuotedTableName($this->_platform);
|
|
|
|
$targetTableAlias = $this->getSqlTableAlias($targetClass->getTableName(), $joinedDqlAlias);
|
2009-08-06 19:08:03 +04:00
|
|
|
$sourceTableAlias = $this->getSqlTableAlias(
|
2009-08-11 14:51:38 +04:00
|
|
|
$sourceClass->getTableName(), $joinAssocPathExpr->identificationVariable
|
2009-08-06 19:08:03 +04:00
|
|
|
);
|
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-08-11 14:51:38 +04:00
|
|
|
if ( ! $relation->isOwningSide) {
|
|
|
|
$assoc = $targetClass->associationMappings[$relation->mappedByFieldName];
|
2009-01-19 21:40:12 +03:00
|
|
|
} else {
|
2009-08-11 14:51:38 +04:00
|
|
|
$assoc = $relation;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-05-26 15:30:07 +04:00
|
|
|
if ($assoc->isOneToOne()) {
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ';
|
2009-01-19 21:40:12 +03:00
|
|
|
$first = true;
|
2009-08-07 01:42:07 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
foreach ($assoc->sourceToTargetKeyColumns as $sourceColumn => $targetColumn) {
|
2009-05-07 17:54:01 +04:00
|
|
|
if ( ! $first) {
|
|
|
|
$sql .= ' AND ';
|
|
|
|
} else {
|
|
|
|
$first = false;
|
|
|
|
}
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$quotedSourceColumn = $assoc->getQuotedJoinColumnName($sourceColumn, $this->_platform);
|
|
|
|
$quotedTargetColumn = $sourceClass->getQuotedColumnName($sourceClass->fieldNames[$targetColumn], $this->_platform);
|
|
|
|
|
|
|
|
if ($relation->isOwningSide) {
|
|
|
|
$sql .= $sourceTableAlias . '.' . $quotedSourceColumn
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2009-08-11 14:51:38 +04:00
|
|
|
. $targetTableAlias . '.' . $quotedTargetColumn;
|
2009-01-19 21:40:12 +03:00
|
|
|
} else {
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $sourceTableAlias . '.' . $quotedTargetColumn
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2009-08-11 14:51:38 +04:00
|
|
|
. $targetTableAlias . '.' . $quotedSourceColumn;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-07 17:54:01 +04:00
|
|
|
} else if ($assoc->isManyToMany()) {
|
|
|
|
// Join relation table
|
|
|
|
$joinTable = $assoc->getJoinTable();
|
|
|
|
$joinTableAlias = $this->getSqlTableAlias($joinTable['name']);
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $assoc->getQuotedJoinTableName($this->_platform) . ' ' . $joinTableAlias . ' ON ';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
if ($relation->isOwningSide) {
|
|
|
|
foreach ($assoc->sourceToRelationKeyColumns as $sourceColumn => $relationColumn) {
|
|
|
|
$sql .= $sourceTableAlias . '.' . $sourceClass->getQuotedColumnName($sourceColumn, $this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2009-08-11 14:51:38 +04:00
|
|
|
. $joinTableAlias . '.' . $assoc->getQuotedJoinColumnName($relationColumn, $this->_platform);
|
2009-05-07 17:54:01 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-08-11 14:51:38 +04:00
|
|
|
foreach ($assoc->targetToRelationKeyColumns as $targetColumn => $relationColumn) {
|
|
|
|
$sql .= $sourceTableAlias . '.' . $targetClass->getQuotedColumnName($targetColumn, $this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2009-08-11 14:51:38 +04:00
|
|
|
. $joinTableAlias . '.' . $assoc->getQuotedJoinColumnName($relationColumn, $this->_platform);
|
2009-05-07 17:54:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join target table
|
2009-08-06 19:08:03 +04:00
|
|
|
$sql .= ($joinType == AST\Join::JOIN_TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE_LEFTOUTER)
|
|
|
|
? ' LEFT JOIN ' : ' INNER JOIN ';
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ';
|
2009-05-07 17:54:01 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
if ($relation->isOwningSide) {
|
|
|
|
foreach ($assoc->targetToRelationKeyColumns as $targetColumn => $relationColumn) {
|
|
|
|
$sql .= $targetTableAlias . '.' . $targetClass->getQuotedColumnName($targetColumn, $this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2009-08-11 14:51:38 +04:00
|
|
|
. $joinTableAlias . '.' . $assoc->getQuotedJoinColumnName($relationColumn, $this->_platform);
|
2009-05-07 17:54:01 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-08-11 14:51:38 +04:00
|
|
|
foreach ($assoc->sourceToRelationKeyColumns as $sourceColumn => $relationColumn) {
|
|
|
|
$sql .= $targetTableAlias . '.' . $sourceClass->getQuotedColumnName($sourceColumn, $this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2009-08-11 14:51:38 +04:00
|
|
|
. $joinTableAlias . '.' . $assoc->getQuotedJoinColumnName($relationColumn, $this->_platform);
|
2009-05-07 17:54:01 +04:00
|
|
|
}
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
|
|
|
|
$discrSql = $this->_generateDiscriminatorColumnConditionSql($joinedDqlAlias);
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
if ($discrSql) {
|
|
|
|
$sql .= ' AND ' . $discrSql;
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
if ($targetClass->isInheritanceTypeJoined()) {
|
|
|
|
$sql .= $this->_generateClassTableInheritanceJoins($targetClass, $joinedDqlAlias);
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
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-08-07 01:42:07 +04:00
|
|
|
$expr = $selectExpression->expression;
|
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
if ($expr instanceof AST\PathExpression) {
|
2009-08-07 01:42:07 +04:00
|
|
|
if ($expr->type == AST\PathExpression::TYPE_STATE_FIELD) {
|
|
|
|
$parts = $expr->parts;
|
2009-01-19 21:40:12 +03:00
|
|
|
$numParts = count($parts);
|
2009-08-07 01:42:07 +04:00
|
|
|
$dqlAlias = $expr->identificationVariable;
|
|
|
|
$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-08-11 14:51:38 +04:00
|
|
|
$columnName = $class->getQuotedColumnName($fieldName, $this->_platform);
|
|
|
|
$columnAlias = $this->getSqlColumnAlias($class->columnNames[$fieldName]);
|
|
|
|
$sql .= $sqlTableAlias . '.' . $columnName . ' AS ' . $columnAlias;
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2009-08-05 01:41:53 +04:00
|
|
|
$this->_rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName);
|
2009-01-19 21:40:12 +03:00
|
|
|
} else {
|
2009-08-07 01:42:07 +04: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-08-07 01:42:07 +04:00
|
|
|
if ( ! $selectExpression->fieldIdentificationVariable) {
|
2009-04-09 22:12:48 +04:00
|
|
|
$resultAlias = $this->_scalarResultCounter++;
|
2009-01-19 21:40:12 +03:00
|
|
|
} else {
|
2009-08-07 01:42:07 +04:00
|
|
|
$resultAlias = $selectExpression->fieldIdentificationVariable;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
$columnAlias = 'sclr' . $this->_aliasCounter++;
|
|
|
|
$sql .= $this->walkAggregateExpression($expr) . ' AS ' . $columnAlias;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
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-08-07 01:42:07 +04:00
|
|
|
if ( ! $selectExpression->fieldIdentificationVariable) {
|
2009-04-09 22:12:48 +04:00
|
|
|
$resultAlias = $this->_scalarResultCounter++;
|
2009-03-28 20:10:41 +03:00
|
|
|
} else {
|
2009-08-07 01:42:07 +04:00
|
|
|
$resultAlias = $selectExpression->fieldIdentificationVariable;
|
2009-03-28 20:10:41 +03:00
|
|
|
}
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
$columnAlias = 'sclr' . $this->_aliasCounter++;
|
|
|
|
$sql .= $this->walkFunction($expr) . ' AS ' . $columnAlias;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
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-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;
|
2009-08-11 14:51:38 +04:00
|
|
|
// 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'];
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
if ($beginning) $beginning = false; else $sql .= ', ';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($tableName, $dqlAlias);
|
|
|
|
$columnAlias = $this->getSqlColumnAlias($mapping['columnName']);
|
|
|
|
$sql .= $sqlTableAlias . '.' . $class->getQuotedColumnName($fieldName, $this->_platform)
|
|
|
|
. ' AS ' . $columnAlias;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$this->_rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
if ($beginning) $beginning = false; else $sql .= ', ';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($subClass->primaryTable['name'], $dqlAlias);
|
2009-05-21 12:53:40 +04:00
|
|
|
$columnAlias = $this->getSqlColumnAlias($mapping['columnName']);
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $sqlTableAlias . '.' . $subClass->getQuotedColumnName($fieldName, $this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' AS ' . $columnAlias;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName);
|
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)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
return ' ' . strtoupper($qExpr->type)
|
|
|
|
. '(' . $this->walkSubselect($qExpr->getSubselect()) . ')';
|
2009-03-28 20:10:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-08-05 01:41:53 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$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) : '';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
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)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$identificationVarDecls = $subselectFromClause->identificationVariableDeclarations;
|
2009-03-19 15:43:48 +03:00
|
|
|
$firstIdentificationVarDecl = $identificationVarDecls[0];
|
2009-08-07 01:42:07 +04:00
|
|
|
$rangeDecl = $firstIdentificationVarDecl->rangeVariableDeclaration;
|
2009-08-13 06:17:27 +04:00
|
|
|
$class = $this->_em->getClassMetadata($rangeDecl->abstractSchemaName);
|
|
|
|
$dqlAlias = $rangeDecl->aliasIdentificationVariable;
|
2009-08-06 19:08:03 +04:00
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
$sql = ' FROM ' . $class->getQuotedTableName($this->_platform) . ' '
|
|
|
|
. $this->getSqlTableAlias($class->primaryTable['name'], $dqlAlias);
|
2009-03-19 15:43:48 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
foreach ($firstIdentificationVarDecl->joinVariableDeclarations as $joinVarDecl) {
|
2009-03-19 15:43:48 +03:00
|
|
|
$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)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
return 'SELECT' . ($simpleSelectClause->isDistinct ? ' DISTINCT' : '')
|
|
|
|
. $this->walkSimpleSelectExpression($simpleSelectClause->simpleSelectExpression);
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
|
|
|
|
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 = '';
|
2009-08-07 01:42:07 +04:00
|
|
|
$expr = $simpleSelectExpression->expression;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
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) {
|
2009-08-07 01:42:07 +04:00
|
|
|
if ( ! $simpleSelectExpression->fieldIdentificationVariable) {
|
2009-03-19 15:43:48 +03:00
|
|
|
$alias = $this->_scalarAliasCounter++;
|
|
|
|
} else {
|
2009-08-07 01:42:07 +04:00
|
|
|
$alias = $simpleSelectExpression->fieldIdentificationVariable;
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-03-19 15:43:48 +03:00
|
|
|
$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-08-07 01:42:07 +04:00
|
|
|
$sql .= ' ' . $this->getSqlTableAlias($class->getTableName(), $expr) . '.'
|
2009-08-11 14:51:38 +04:00
|
|
|
. $class->getQuotedColumnName($class->identifier[0], $this->_platform);
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
2009-08-05 01:41:53 +04:00
|
|
|
|
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 = '';
|
2009-08-07 01:42:07 +04:00
|
|
|
$parts = $aggExpression->pathExpression->parts;
|
|
|
|
$dqlAlias = $aggExpression->pathExpression->identificationVariable;
|
2009-06-14 21:34:28 +04:00
|
|
|
$fieldName = $parts[0];
|
2009-03-19 15:43:48 +03:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
$qComp = $this->_queryComponents[$dqlAlias];
|
2009-08-11 14:51:38 +04:00
|
|
|
$columnName = $qComp['metadata']->getQuotedColumnName($fieldName, $this->_platform);
|
2009-03-19 15:43:48 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
return $aggExpression->functionName . '(' . ($aggExpression->isDistinct ? 'DISTINCT ' : '')
|
|
|
|
. $this->getSqlTableAlias($qComp['metadata']->getTableName(), $dqlAlias) . '.'
|
2009-08-11 14:51:38 +04:00
|
|
|
. $columnName . ')';
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2009-08-05 01:41:53 +04:00
|
|
|
return ' GROUP BY ' . implode(
|
2009-08-07 01:42:07 +04:00
|
|
|
', ', array_map(array($this, 'walkGroupByItem'), $groupByClause->groupByItems)
|
2009-08-05 01:41:53 +04:00
|
|
|
);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$parts = $pathExpr->parts;
|
|
|
|
$dqlAlias = $pathExpr->identificationVariable;
|
2009-06-14 21:34:28 +04:00
|
|
|
$qComp = $this->_queryComponents[$dqlAlias];
|
2009-08-11 14:51:38 +04:00
|
|
|
$columnName = $qComp['metadata']->getQuotedColumnName($parts[0], $this->_platform);
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-11 14:51:38 +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 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 ';
|
2009-08-07 01:42:07 +04:00
|
|
|
$class = $this->_em->getClassMetadata($deleteClause->abstractSchemaName);
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $class->getQuotedTableName($this->_platform);
|
2009-08-05 01:41:53 +04:00
|
|
|
|
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-08-05 01:41:53 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$this->_currentRootAlias = $deleteClause->aliasIdentificationVariable;
|
2009-05-03 14:58:16 +04:00
|
|
|
|
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 ';
|
2009-08-07 01:42:07 +04:00
|
|
|
$class = $this->_em->getClassMetadata($updateClause->abstractSchemaName);
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $class->getQuotedTableName($this->_platform);
|
2009-08-05 01:41:53 +04:00
|
|
|
|
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-08-05 01:41:53 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$this->_currentRootAlias = $updateClause->aliasIdentificationVariable;
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2009-08-05 01:41:53 +04:00
|
|
|
$sql .= ' SET ' . implode(
|
2009-08-07 01:42:07 +04:00
|
|
|
', ', array_map(array($this, 'walkUpdateItem'), $updateClause->updateItems)
|
2009-08-05 01:41:53 +04:00
|
|
|
);
|
2009-03-21 22:58:52 +03:00
|
|
|
|
|
|
|
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-08-07 01:42:07 +04:00
|
|
|
$dqlAlias = $updateItem->identificationVariable;
|
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()) . '.';
|
|
|
|
}
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $qComp['metadata']->getQuotedColumnName($updateItem->field, $this->_platform) . ' = ';
|
2009-03-21 22:58:52 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$newValue = $updateItem->newValue;
|
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 ';
|
2009-08-07 01:42:07 +04:00
|
|
|
$condExpr = $whereClause->conditionalExpression;
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2009-08-05 01:41:53 +04:00
|
|
|
$sql .= implode(
|
2009-08-07 01:42:07 +04:00
|
|
|
' OR ', array_map(array($this, 'walkConditionalTerm'), $condExpr->conditionalTerms)
|
2009-08-05 01:41:53 +04:00
|
|
|
);
|
2009-05-03 14:58:16 +04:00
|
|
|
|
|
|
|
$discrSql = $this->_generateDiscriminatorColumnConditionSql($this->_currentRootAlias);
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
if ($discrSql) {
|
2009-05-17 23:27:12 +04:00
|
|
|
if ($termsSql) $sql .= ' AND';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-05-17 23:27:12 +04:00
|
|
|
$sql .= ' ' . $discrSql;
|
2009-05-03 14:58:16 +04: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-08-05 01:41:53 +04:00
|
|
|
return implode(
|
2009-08-07 01:42:07 +04:00
|
|
|
' AND ', array_map(array($this, 'walkConditionalFactor'), $condTerm->conditionalFactors)
|
2009-08-05 01:41:53 +04:00
|
|
|
);
|
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)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql = ($factor->not) ? 'NOT ' : '';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$primary = $factor->conditionalPrimary;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
if ($primary->isSimpleConditionalExpression()) {
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= $primary->simpleConditionalExpression->dispatch($this);
|
2009-01-21 21:25:05 +03:00
|
|
|
} else if ($primary->isConditionalExpression()) {
|
2009-08-07 01:42:07 +04:00
|
|
|
$condExpr = $primary->conditionalExpression;
|
|
|
|
|
2009-08-05 01:41:53 +04:00
|
|
|
$sql .= '(' . implode(
|
2009-08-07 01:42:07 +04:00
|
|
|
' OR ', array_map(array($this, 'walkConditionalTerm'), $condExpr->conditionalTerms)
|
2009-08-05 01:41:53 +04:00
|
|
|
) . ')';
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
2009-08-05 01:41:53 +04: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)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql = ($existsExpr->not) ? 'NOT ' : '';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= 'EXISTS (' . $this->walkSubselect($existsExpr->subselect) . ')';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
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)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql = $collMemberExpr->not ? 'NOT ' : '';
|
2009-06-14 21:34:28 +04:00
|
|
|
$sql .= 'EXISTS (SELECT 1 FROM ';
|
|
|
|
$entityExpr = $collMemberExpr->entityExpression;
|
|
|
|
$collPathExpr = $collMemberExpr->collectionValuedPathExpression;
|
2009-08-07 01:42:07 +04:00
|
|
|
$parts = $collPathExpr->parts;
|
|
|
|
$dqlAlias = $collPathExpr->identificationVariable;
|
2009-06-14 21:34:28 +04:00
|
|
|
|
|
|
|
$class = $this->_queryComponents[$dqlAlias]['metadata'];
|
|
|
|
|
|
|
|
if ($entityExpr instanceof AST\InputParameter) {
|
2009-08-07 01:42:07 +04:00
|
|
|
$dqlParamKey = $entityExpr->name;
|
2009-06-14 21:34:28 +04:00
|
|
|
$entity = $this->_query->getParameter($dqlParamKey);
|
|
|
|
} else {
|
2009-08-05 19:47:41 +04:00
|
|
|
//TODO
|
2009-06-14 21:34:28 +04:00
|
|
|
throw DoctrineException::notImplemented();
|
|
|
|
}
|
|
|
|
|
|
|
|
$assoc = $class->associationMappings[$parts[0]];
|
2009-08-06 19:08:03 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
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
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $targetClass->getQuotedTableName($this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' ' . $targetTableAlias . ' WHERE ';
|
2009-06-14 21:34:28 +04:00
|
|
|
|
|
|
|
$owningAssoc = $targetClass->associationMappings[$assoc->mappedByFieldName];
|
|
|
|
|
|
|
|
$first = true;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
foreach ($owningAssoc->targetToSourceKeyColumns as $targetColumn => $sourceColumn) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $sourceTableAlias . '.' . $class->getQuotedColumnName($targetColumn, $this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2009-08-11 14:51:38 +04:00
|
|
|
. $targetTableAlias . '.' . $owningAssoc->getQuotedJoinColumnName($sourceColumn, $this->_platform);
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ' AND ';
|
|
|
|
$first = true;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
foreach ($targetClass->identifier as $idField) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_parserResult->addParameterMapping($dqlParamKey, $this->_sqlParamIndex++);
|
2009-08-06 19:08:03 +04:00
|
|
|
$sql .= $targetTableAlias . '.'
|
2009-08-11 14:51:38 +04:00
|
|
|
. $targetClass->getQuotedColumnName($idField, $this->_platform) . ' = ?';
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
|
|
|
} else { // many-to-many
|
|
|
|
$targetClass = $this->_em->getClassMetadata($assoc->targetEntityName);
|
2009-08-11 14:51:38 +04:00
|
|
|
|
|
|
|
$owningAssoc = $assoc->isOwningSide ? $assoc : $targetClass->associationMappings[$assoc->mappedByFieldName];
|
|
|
|
$joinTable = $assoc->isOwningSide ? $assoc->joinTable : $owningAssoc->joinTable;
|
|
|
|
|
|
|
|
// SQL table aliases
|
2009-06-14 21:34:28 +04:00
|
|
|
$joinTableAlias = $this->getSqlTableAlias($joinTable['name']);
|
|
|
|
$targetTableAlias = $this->getSqlTableAlias($targetClass->primaryTable['name']);
|
2009-08-11 14:51:38 +04:00
|
|
|
$sourceTableAlias = $this->getSqlTableAlias($class->primaryTable['name'], $dqlAlias);
|
2009-06-14 21:34:28 +04:00
|
|
|
|
|
|
|
// join to target table
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $assoc->getQuotedJoinTableName($this->_platform)
|
2009-08-05 01:41:53 +04:00
|
|
|
. ' ' . $joinTableAlias . ' INNER JOIN '
|
2009-08-11 14:51:38 +04:00
|
|
|
. $targetClass->getQuotedTableName($this->_platform)
|
2009-08-05 01:41:53 +04:00
|
|
|
. ' ' . $targetTableAlias . ' ON ';
|
2009-06-14 21:34:28 +04:00
|
|
|
|
|
|
|
// join conditions
|
2009-08-07 01:42:07 +04:00
|
|
|
$joinColumns = $assoc->isOwningSide
|
|
|
|
? $joinTable['joinColumns']
|
|
|
|
: $joinTable['inverseJoinColumns'];
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$referencedColumnClass = $assoc->isOwningSide ? $class : $targetClass;
|
|
|
|
$first = true;
|
2009-06-14 21:34:28 +04:00
|
|
|
foreach ($joinColumns as $joinColumn) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
2009-08-11 14:51:38 +04:00
|
|
|
|
|
|
|
$sql .= $joinTableAlias . '.' . $owningAssoc->getQuotedJoinColumnName($joinColumn['name'], $this->_platform)
|
|
|
|
. ' = '
|
|
|
|
. $sourceTableAlias . '.' . $referencedColumnClass->getQuotedColumnName($joinColumn['referencedColumnName'], $this->_platform);
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ' WHERE ';
|
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$joinColumns = $assoc->isOwningSide
|
|
|
|
? $joinTable['inverseJoinColumns']
|
|
|
|
: $joinTable['joinColumns'];
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$first = true;
|
2009-06-14 21:34:28 +04:00
|
|
|
foreach ($joinColumns as $joinColumn) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $joinTableAlias . '.' . $owningAssoc->getQuotedJoinColumnName($joinColumn['name'], $this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2009-08-11 14:51:38 +04:00
|
|
|
. $targetTableAlias . '.' . $referencedColumnClass->getQuotedColumnName($joinColumn['referencedColumnName'], $this->_platform);
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ' AND ';
|
|
|
|
$first = true;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
foreach ($targetClass->identifier as $idField) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_parserResult->addParameterMapping($dqlParamKey, $this->_sqlParamIndex++);
|
2009-08-06 19:08:03 +04:00
|
|
|
$sql .= $targetTableAlias . '.'
|
2009-08-11 14:51:38 +04:00
|
|
|
. $targetClass->getQuotedColumnName($idField, $this->_platform) . ' = ?';
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql . ')';
|
|
|
|
}
|
2009-08-05 01:41:53 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Walks down an EmptyCollectionComparisonExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param EmptyCollectionComparisonExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr)
|
|
|
|
{
|
2009-08-05 19:47:41 +04:00
|
|
|
$sizeFunc = new AST\Functions\SizeFunction('size');
|
2009-08-07 01:42:07 +04:00
|
|
|
$sizeFunc->collectionPathExpression = $emptyCollCompExpr->expression;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
return $sizeFunc->getSql($this) . ($emptyCollCompExpr->not ? ' > 0' : ' = 0');
|
2009-08-05 01:41:53 +04:00
|
|
|
}
|
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-08-07 01:42:07 +04:00
|
|
|
$innerExpr = $nullCompExpr->expression;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-03-23 20:39:33 +03:00
|
|
|
if ($innerExpr instanceof AST\InputParameter) {
|
2009-08-07 01:42:07 +04:00
|
|
|
$dqlParamKey = $innerExpr->name;
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_parserResult->addParameterMapping($dqlParamKey, $this->_sqlParamIndex++);
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= ' ?';
|
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
|
|
|
}
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= ' IS' . ($nullCompExpr->not ? ' NOT' : '') . ' NULL';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
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)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql = $this->walkPathExpression($inExpr->pathExpression)
|
|
|
|
. ($inExpr->not ? ' NOT' : '') . ' IN (';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
if ($inExpr->subselect) {
|
|
|
|
$sql .= $this->walkSubselect($inExpr->subselect);
|
2009-03-21 15:49:58 +03:00
|
|
|
} else {
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= implode(', ', array_map(array($this, 'walkLiteral'), $inExpr->literals));
|
2009-03-21 15:49:58 +03:00
|
|
|
}
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
$sql .= ')';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
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-08-07 01:42:07 +04:00
|
|
|
return $this->walkInputParameter($literal);
|
2009-03-21 22:58:52 +03:00
|
|
|
}
|
2009-08-05 01:41:53 +04: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)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql = $this->walkArithmeticExpression($betweenExpr->expression);
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
if ($betweenExpr->not) $sql .= ' NOT';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= ' BETWEEN ' . $this->walkArithmeticExpression($betweenExpr->leftBetweenExpression)
|
|
|
|
. ' AND ' . $this->walkArithmeticExpression($betweenExpr->rightBetweenExpression);
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
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)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$stringExpr = $likeExpr->stringExpression;
|
|
|
|
$sql = $stringExpr->dispatch($this) . ($likeExpr->not ? ' NOT' : '') . ' LIKE ';
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
if ($likeExpr->stringPattern instanceof AST\InputParameter) {
|
|
|
|
$inputParam = $likeExpr->stringPattern;
|
|
|
|
$dqlParamKey = $inputParam->name;
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_parserResult->addParameterMapping($dqlParamKey, $this->_sqlParamIndex++);
|
|
|
|
$sql .= '?';
|
2009-03-21 15:49:58 +03:00
|
|
|
} else {
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= $this->_conn->quote($likeExpr->stringPattern);
|
2009-03-21 15:49:58 +03:00
|
|
|
}
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
if ($likeExpr->escapeChar) {
|
|
|
|
$sql .= ' ESCAPE ' . $this->_conn->quote($likeExpr->escapeChar);
|
2009-03-21 15:49:58 +03:00
|
|
|
}
|
2009-08-05 01:41:53 +04: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-08-07 01:42:07 +04:00
|
|
|
$leftExpr = $compExpr->leftExpression;
|
|
|
|
$rightExpr = $compExpr->rightExpression;
|
2009-03-23 20:39:33 +03:00
|
|
|
|
|
|
|
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-08-07 01:42:07 +04:00
|
|
|
$sql .= ' ' . $compExpr->operator . ' ';
|
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-08-07 01:42:07 +04:00
|
|
|
$this->_parserResult->addParameterMapping($inputParam->name, $this->_sqlParamIndex++);
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
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)
|
|
|
|
{
|
2009-08-06 19:48:41 +04:00
|
|
|
return ($arithmeticExpr->isSimpleArithmeticExpression())
|
|
|
|
? $this->walkSimpleArithmeticExpression($arithmeticExpr->simpleArithmeticExpression)
|
|
|
|
: $this->walkSubselect($arithmeticExpr->subselect);
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2009-08-05 01:41:53 +04:00
|
|
|
return implode(
|
2009-08-07 01:42:07 +04:00
|
|
|
' ', array_map(array($this, 'walkArithmeticFactor'), $term->arithmeticFactors)
|
2009-08-05 01:41:53 +04:00
|
|
|
);
|
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)
|
|
|
|
{
|
2009-08-05 01:41:53 +04:00
|
|
|
return (is_string($stringPrimary))
|
|
|
|
? $this->_conn->quote($stringPrimary)
|
|
|
|
: $stringPrimary->dispatch($this);
|
2009-03-23 20:39:33 +03:00
|
|
|
}
|
|
|
|
|
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-08-07 01:42:07 +04:00
|
|
|
$sql = ($factor->isNegativeSigned() ? '-' : ($factor->isPositiveSigned() ? '+' : ''));
|
|
|
|
$primary = $factor->arithmeticPrimary;
|
2009-08-05 01:41:53 +04:00
|
|
|
|
2009-01-21 21:25:05 +03:00
|
|
|
if (is_numeric($primary)) {
|
2009-08-13 06:17:27 +04:00
|
|
|
$sql .= $primary;
|
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-08-05 01:41:53 +04:00
|
|
|
return implode(
|
2009-08-07 01:42:07 +04:00
|
|
|
' ', array_map(array($this, 'walkArithmeticTerm'), $simpleArithmeticExpr->arithmeticTerms)
|
2009-08-05 01:41:53 +04:00
|
|
|
);
|
2009-01-21 21:25:05 +03:00
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|