2009-01-19 21:40:12 +03:00
|
|
|
<?php
|
2009-02-17 13:54:18 +03:00
|
|
|
/*
|
|
|
|
* 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,
|
2010-03-05 19:35:00 +03:00
|
|
|
Doctrine\ORM\Query\QueryException;
|
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>
|
2010-02-14 12:53:38 +03:00
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
2009-02-05 20:34:44 +03:00
|
|
|
* @since 2.0
|
2010-02-20 00:28:17 +03:00
|
|
|
* @todo Rename: SQLWalker
|
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
|
|
|
{
|
2010-02-14 12:53:38 +03:00
|
|
|
/**
|
|
|
|
* @var ResultSetMapping
|
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
private $_rsm;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
/** Counters for generating unique column aliases, table aliases and parameter indexes. */
|
2009-04-09 22:12:48 +04:00
|
|
|
private $_aliasCounter = 0;
|
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
|
|
|
private $_sqlParamIndex = 1;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-14 12:53:38 +03:00
|
|
|
/**
|
|
|
|
* @var ParserResult
|
|
|
|
*/
|
2009-01-19 21:40:12 +03:00
|
|
|
private $_parserResult;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-14 12:53:38 +03:00
|
|
|
/**
|
|
|
|
* @var EntityManager
|
|
|
|
*/
|
2009-01-19 21:40:12 +03:00
|
|
|
private $_em;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-14 12:53:38 +03:00
|
|
|
/**
|
|
|
|
* @var Doctrine\DBAL\Connection
|
|
|
|
*/
|
2009-05-29 14:23:13 +04:00
|
|
|
private $_conn;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-12-08 23:53:01 +03:00
|
|
|
/**
|
|
|
|
* @var AbstractQuery
|
|
|
|
*/
|
2009-04-09 22:12:48 +04:00
|
|
|
private $_query;
|
2009-12-08 23:53:01 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
private $_tableAliasMap = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-09-11 23:50:48 +04:00
|
|
|
/** Map from result variable names to their SQL column alias names. */
|
|
|
|
private $_scalarResultAliasMap = array();
|
2010-03-05 19:35:00 +03: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;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
/** A list of classes that appear in non-scalar SelectExpressions. */
|
|
|
|
private $_selectedClasses = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
/**
|
|
|
|
* The DQL alias of the root class of the currently traversed query.
|
|
|
|
*/
|
2010-07-20 07:51:01 +04:00
|
|
|
private $_rootAliases = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
/**
|
|
|
|
* Flag that indicates whether to generate SQL table aliases in the SQL.
|
2010-02-20 00:28:17 +03:00
|
|
|
* These should only be generated for SELECT queries, not for UPDATE/DELETE.
|
2009-06-14 21:34:28 +04:00
|
|
|
*/
|
2009-05-03 14:58:16 +04:00
|
|
|
private $_useSqlTableAliases = true;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
/**
|
|
|
|
* The database platform abstraction.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-11 14:51:38 +04:00
|
|
|
* @var AbstractPlatform
|
|
|
|
*/
|
|
|
|
private $_platform;
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-02-05 20:34:44 +03:00
|
|
|
/**
|
2010-02-20 00:28:17 +03: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-04-09 22:12:48 +04:00
|
|
|
$this->_query = $query;
|
2010-02-20 00:28:17 +03:00
|
|
|
$this->_parserResult = $parserResult;
|
|
|
|
$this->_queryComponents = $queryComponents;
|
|
|
|
$this->_rsm = $parserResult->getResultSetMapping();
|
2009-04-09 22:12:48 +04:00
|
|
|
$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
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-23 21:50:13 +04:00
|
|
|
/**
|
|
|
|
* Gets the Query instance used by the walker.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-06-23 21:50:13 +04:00
|
|
|
* @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.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
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
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-22 22:48:42 +04:00
|
|
|
/**
|
|
|
|
* Gets the EntityManager used by the walker.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-06-22 22:48:42 +04:00
|
|
|
* @return EntityManager
|
|
|
|
*/
|
|
|
|
public function getEntityManager()
|
|
|
|
{
|
|
|
|
return $this->_em;
|
|
|
|
}
|
2010-03-05 19:35:00 +03: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.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-11 14:51:38 +04:00
|
|
|
* @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];
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
/**
|
|
|
|
* Gets an executor that can be used to execute the result of this walker.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-07 01:42:07 +04:00
|
|
|
* @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
|
|
|
|
);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
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
|
|
|
|
);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
if ($primaryClass->isInheritanceTypeJoined()) {
|
|
|
|
return new Exec\MultiTableUpdateExecutor($AST, $this);
|
|
|
|
} else {
|
|
|
|
return new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
|
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
return new Exec\SingleSelectExecutor($AST, $this);
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
/**
|
|
|
|
* Generates a unique, short SQL table alias.
|
|
|
|
*
|
2010-07-20 07:51:01 +04:00
|
|
|
* @param string $tableName Table name
|
2009-08-07 01:42:07 +04:00
|
|
|
* @param string $dqlAlias The DQL alias.
|
|
|
|
* @return string Generated table alias.
|
|
|
|
*/
|
|
|
|
public function getSqlTableAlias($tableName, $dqlAlias = '')
|
|
|
|
{
|
|
|
|
$tableName .= $dqlAlias;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
if ( ! isset($this->_tableAliasMap[$tableName])) {
|
|
|
|
$this->_tableAliasMap[$tableName] = strtolower(substr($tableName, 0, 1)) . $this->_tableAliasCounter++ . '_';
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
return $this->_tableAliasMap[$tableName];
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +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)
|
|
|
|
{
|
2010-02-20 00:28:17 +03:00
|
|
|
$this->_tableAliasMap[$tableName] = $alias;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
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 = '';
|
|
|
|
|
2010-04-15 20:36:17 +04:00
|
|
|
$baseTableAlias = $this->getSQLTableAlias($class->table['name'], $dqlAlias);
|
2009-08-07 01:42:07 +04:00
|
|
|
|
|
|
|
// INNER JOIN parent class tables
|
|
|
|
foreach ($class->parentClasses as $parentClassName) {
|
|
|
|
$parentClass = $this->_em->getClassMetadata($parentClassName);
|
2010-04-15 20:36:17 +04:00
|
|
|
$tableAlias = $this->getSQLTableAlias($parentClass->table['name'], $dqlAlias);
|
|
|
|
// If this is a joined association we must use left joins to preserve the correct result.
|
|
|
|
$sql .= isset($this->_queryComponents[$dqlAlias]['relation']) ? ' LEFT ' : ' INNER ';
|
|
|
|
$sql .= 'JOIN ' . $parentClass->getQuotedTableName($this->_platform)
|
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 ';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$columnName = $class->getQuotedColumnName($idField, $this->_platform);
|
|
|
|
$sql .= $baseTableAlias . '.' . $columnName
|
2010-03-05 19:35:00 +03:00
|
|
|
. ' = '
|
2009-08-11 14:51:38 +04:00
|
|
|
. $tableAlias . '.' . $columnName;
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-15 20:36:17 +04:00
|
|
|
// LEFT JOIN subclass tables, if partial objects disallowed.
|
2009-10-15 18:39:43 +04:00
|
|
|
if ( ! $this->_query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
|
2009-08-17 21:58:16 +04:00
|
|
|
foreach ($class->subClasses as $subClassName) {
|
|
|
|
$subClass = $this->_em->getClassMetadata($subClassName);
|
2010-04-15 20:36:17 +04:00
|
|
|
$tableAlias = $this->getSQLTableAlias($subClass->table['name'], $dqlAlias);
|
2009-08-17 21:58:16 +04:00
|
|
|
$sql .= ' LEFT JOIN ' . $subClass->getQuotedTableName($this->_platform)
|
2010-02-20 00:28:17 +03:00
|
|
|
. ' ' . $tableAlias . ' ON ';
|
|
|
|
$first = true;
|
2009-08-17 21:58:16 +04:00
|
|
|
foreach ($class->identifier as $idField) {
|
|
|
|
if ($first) $first = false; else $sql .= ' AND ';
|
|
|
|
|
|
|
|
$columnName = $class->getQuotedColumnName($idField, $this->_platform);
|
|
|
|
$sql .= $baseTableAlias . '.' . $columnName
|
2010-02-20 00:28:17 +03:00
|
|
|
. ' = '
|
|
|
|
. $tableAlias . '.' . $columnName;
|
2009-08-17 21:58:16 +04:00
|
|
|
}
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2010-02-21 03:06:34 +03:00
|
|
|
private function _generateOrderedCollectionOrderByItems()
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
foreach ($this->_selectedClasses AS $dqlAlias => $class) {
|
|
|
|
$qComp = $this->_queryComponents[$dqlAlias];
|
2010-04-26 15:02:30 +04:00
|
|
|
if (isset($qComp['relation']->orderBy)) {
|
2010-02-21 03:06:34 +03:00
|
|
|
foreach ($qComp['relation']->orderBy AS $fieldName => $orientation) {
|
|
|
|
if ($qComp['metadata']->isInheritanceTypeJoined()) {
|
|
|
|
$tableName = $this->_em->getUnitOfWork()->getEntityPersister($class->name)->getOwningTable($fieldName);
|
|
|
|
} else {
|
2010-03-29 17:20:41 +04:00
|
|
|
$tableName = $qComp['metadata']->table['name'];
|
2010-02-21 03:06:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($sql != '') {
|
|
|
|
$sql .= ', ';
|
|
|
|
}
|
2010-04-26 15:02:30 +04:00
|
|
|
$sql .= $this->getSqlTableAlias($tableName, $dqlAlias) . '.' .
|
|
|
|
$qComp['metadata']->getQuotedColumnName($fieldName, $this->_platform) . " $orientation";
|
2010-02-21 03:06:34 +03: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.
|
|
|
|
*
|
2010-07-20 07:51:01 +04:00
|
|
|
* @param array $dqlAliases List of root DQL aliases to inspect for discriminator restrictions.
|
2009-08-13 06:17:27 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
2010-07-20 07:51:01 +04:00
|
|
|
private function _generateDiscriminatorColumnConditionSQL(array $dqlAliases)
|
2009-08-13 06:17:27 +04:00
|
|
|
{
|
2010-07-20 07:51:01 +04:00
|
|
|
$encapsulate = false;
|
2009-08-13 06:17:27 +04:00
|
|
|
$sql = '';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
foreach ($dqlAliases as $dqlAlias) {
|
2009-08-13 06:17:27 +04:00
|
|
|
$class = $this->_queryComponents[$dqlAlias]['metadata'];
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
if ($class->isInheritanceTypeSingleTable()) {
|
|
|
|
$conn = $this->_em->getConnection();
|
2010-07-04 15:34:14 +04:00
|
|
|
$values = array();
|
|
|
|
if ($class->discriminatorValue !== null) { // discrimnators can be 0
|
|
|
|
$values[] = $conn->quote($class->discriminatorValue);
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
foreach ($class->subClasses as $subclassName) {
|
|
|
|
$values[] = $conn->quote($this->_em->getClassMetadata($subclassName)->discriminatorValue);
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
if ($sql != '') {
|
|
|
|
$sql .= ' AND ';
|
|
|
|
$encapsulate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ($sql != '' ? ' AND ' : '')
|
|
|
|
. (($this->_useSqlTableAliases) ? $this->getSqlTableAlias($class->table['name'], $dqlAlias) . '.' : '')
|
|
|
|
. $class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')';
|
2009-08-13 06:17:27 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
return ($encapsulate) ? '(' . $sql . ')' : $sql;
|
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);
|
2010-03-05 19:35:00 +03: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);
|
2010-07-20 07:51:01 +04:00
|
|
|
} else if (($discSql = $this->_generateDiscriminatorColumnConditionSQL($this->_rootAliases)) !== '') {
|
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) : '';
|
2010-02-21 03:06:34 +03:00
|
|
|
|
|
|
|
if (($orderByClause = $AST->orderByClause) !== null) {
|
|
|
|
$sql .= $AST->orderByClause ? $this->walkOrderByClause($AST->orderByClause) : '';
|
|
|
|
} else if (($orderBySql = $this->_generateOrderedCollectionOrderByItems()) !== '') {
|
|
|
|
$sql .= ' ORDER BY '.$orderBySql;
|
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
|
2009-11-21 21:52:02 +03:00
|
|
|
$sql = $this->_platform->modifyLimitQuery(
|
2010-02-20 00:28:17 +03:00
|
|
|
$sql, $this->_query->getMaxResults(), $this->_query->getFirstResult()
|
2009-08-05 01:41:53 +04:00
|
|
|
);
|
2009-07-10 21:53:48 +04:00
|
|
|
|
2010-04-09 00:50:06 +04:00
|
|
|
if (($lockMode = $this->_query->getHint(Query::HINT_LOCK_MODE)) !== false) {
|
2010-05-15 13:48:20 +04:00
|
|
|
if ($lockMode == \Doctrine\DBAL\LockMode::PESSIMISTIC_READ) {
|
2010-04-09 00:50:06 +04:00
|
|
|
$sql .= " " . $this->_platform->getReadLockSQL();
|
2010-05-15 13:48:20 +04:00
|
|
|
} else if ($lockMode == \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE) {
|
2010-04-09 00:50:06 +04:00
|
|
|
$sql .= " " . $this->_platform->getWriteLockSQL();
|
2010-05-15 13:48:20 +04:00
|
|
|
} else if ($lockMode == \Doctrine\DBAL\LockMode::OPTIMISTIC) {
|
2010-04-09 00:50:06 +04:00
|
|
|
foreach ($this->_selectedClasses AS $class) {
|
2010-05-15 13:53:28 +04:00
|
|
|
if (!$class->isVersioned) {
|
|
|
|
throw \Doctrine\ORM\OptimisticLockException::lockFailed();
|
2010-04-09 00:50:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
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);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
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);
|
2010-07-20 07:51:01 +04:00
|
|
|
} else if (($discSql = $this->_generateDiscriminatorColumnConditionSQL($this->_rootAliases)) !== '') {
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= ' WHERE ' . $discSql;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
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);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
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);
|
2010-07-20 07:51:01 +04:00
|
|
|
} else if (($discSql = $this->_generateDiscriminatorColumnConditionSQL($this->_rootAliases)) !== '') {
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= ' WHERE ' . $discSql;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
return $sql;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2010-07-23 08:55:33 +04:00
|
|
|
public function walkIdentificationVariable($identificationVariable, $fieldName)
|
2009-08-07 01:42:07 +04:00
|
|
|
{
|
2009-08-13 06:17:27 +04:00
|
|
|
$class = $this->_queryComponents[$identificationVariable]['metadata'];
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
if (
|
2010-03-05 19:35:00 +03:00
|
|
|
$fieldName !== null && $class->isInheritanceTypeJoined() &&
|
2009-08-13 06:17:27 +04:00
|
|
|
isset($class->fieldMappings[$fieldName]['inherited'])
|
|
|
|
) {
|
|
|
|
$class = $this->_em->getClassMetadata($class->fieldMappings[$fieldName]['inherited']);
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-13 02:49:19 +04:00
|
|
|
return $this->getSQLTableAlias($class->table['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 = '';
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
switch ($pathExpr->type) {
|
|
|
|
case AST\PathExpression::TYPE_STATE_FIELD:
|
2010-07-23 08:55:33 +04:00
|
|
|
$fieldName = $pathExpr->field;
|
|
|
|
$dqlAlias = $pathExpr->identificationVariable;
|
2010-02-20 00:28:17 +03:00
|
|
|
$class = $this->_queryComponents[$dqlAlias]['metadata'];
|
2009-08-13 06:17:27 +04:00
|
|
|
|
|
|
|
if ($this->_useSqlTableAliases) {
|
|
|
|
$sql .= $this->walkIdentificationVariable($dqlAlias, $fieldName) . '.';
|
|
|
|
}
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql .= $class->getQuotedColumnName($fieldName, $this->_platform);
|
2009-08-13 06:17:27 +04:00
|
|
|
break;
|
2010-04-14 07:04:44 +04:00
|
|
|
|
2009-12-27 06:26:15 +03:00
|
|
|
case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION:
|
2010-02-20 00:28:17 +03:00
|
|
|
// 1- the owning side:
|
|
|
|
// Just use the foreign key, i.e. u.group_id
|
2010-07-23 08:55:33 +04:00
|
|
|
$fieldName = $pathExpr->field;
|
2009-12-27 06:26:15 +03:00
|
|
|
$dqlAlias = $pathExpr->identificationVariable;
|
2010-02-20 00:28:17 +03:00
|
|
|
$class = $this->_queryComponents[$dqlAlias]['metadata'];
|
2010-04-13 07:29:29 +04:00
|
|
|
|
2010-04-14 19:07:08 +04:00
|
|
|
if (isset($class->associationMappings[$fieldName]->inherited)) {
|
|
|
|
$class = $this->_em->getClassMetadata($class->associationMappings[$fieldName]->inherited);
|
2010-04-13 07:29:29 +04:00
|
|
|
}
|
|
|
|
|
2009-12-27 06:26:15 +03:00
|
|
|
$assoc = $class->associationMappings[$fieldName];
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-12-27 06:26:15 +03:00
|
|
|
if ($assoc->isOwningSide) {
|
2010-02-20 00:28:17 +03:00
|
|
|
// COMPOSITE KEYS NOT (YET?) SUPPORTED
|
|
|
|
if (count($assoc->sourceToTargetKeyColumns) > 1) {
|
|
|
|
throw QueryException::associationPathCompositeKeyNotSupported();
|
|
|
|
}
|
2010-04-22 18:32:01 +04:00
|
|
|
|
|
|
|
if ($this->_useSqlTableAliases) {
|
|
|
|
$sql .= $this->getSqlTableAlias($class->table['name'], $dqlAlias) . '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= reset($assoc->targetToSourceKeyColumns);
|
2010-02-20 00:28:17 +03:00
|
|
|
} else {
|
|
|
|
throw QueryException::associationPathInverseSideNotSupported();
|
2009-12-27 06:26:15 +03:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
break;
|
2010-04-14 07:04:44 +04:00
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
default:
|
2009-12-27 06:26:15 +03:00
|
|
|
throw QueryException::invalidPathExpression($pathExpr);
|
2009-08-13 06:17:27 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
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
|
|
|
);
|
2010-02-20 21:27:05 +03:00
|
|
|
|
2009-10-15 18:39:43 +04:00
|
|
|
$addMetaColumns = ! $this->_query->getHint(Query::HINT_FORCE_PARTIAL_LOAD) &&
|
2009-08-11 14:51:38 +04:00
|
|
|
$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
|
|
|
);
|
|
|
|
}
|
2010-03-05 19:35:00 +03: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);
|
2010-03-29 17:20:41 +04:00
|
|
|
$tblAlias = $this->getSqlTableAlias($rootClass->table['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']);
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql .= ", $tblAlias." . $discrColumn['name'] . ' AS ' . $columnAlias;
|
|
|
|
|
|
|
|
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm->setDiscriminatorColumn($dqlAlias, $columnAlias);
|
2010-03-05 19:35:00 +03:00
|
|
|
$this->_rsm->addMetaResult($dqlAlias, $this->_platform->getSQLResultCasing($columnAlias), $discrColumn['fieldName']);
|
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
// Add foreign key columns to SQL, if necessary
|
|
|
|
if ($addMetaColumns) {
|
2009-10-15 18:39:43 +04:00
|
|
|
//FIXME: Include foreign key columns of child classes also!!??
|
2009-08-11 14:51:38 +04:00
|
|
|
foreach ($class->associationMappings as $assoc) {
|
|
|
|
if ($assoc->isOwningSide && $assoc->isOneToOne()) {
|
2010-04-14 19:07:08 +04:00
|
|
|
if ($assoc->inherited) {
|
|
|
|
$owningClass = $this->_em->getClassMetadata($assoc->inherited);
|
2010-03-29 17:20:41 +04:00
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($owningClass->table['name'], $dqlAlias);
|
2009-08-11 14:51:38 +04:00
|
|
|
} else {
|
2010-03-29 17:20:41 +04:00
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($class->table['name'], $dqlAlias);
|
2009-08-11 14:51:38 +04:00
|
|
|
}
|
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);
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql .= ", $sqlTableAlias." . $srcColumn . ' AS ' . $columnAlias;
|
|
|
|
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
|
|
|
|
$this->_rsm->addMetaResult($dqlAlias, $this->_platform->getSQLResultCasing($columnAlias), $srcColumn);
|
2009-08-11 14:51:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2010-02-20 21:27:05 +03:00
|
|
|
// Add foreign key columns to SQL, if necessary
|
2009-08-11 14:51:38 +04:00
|
|
|
if ($addMetaColumns) {
|
2010-03-29 17:20:41 +04:00
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($class->table['name'], $dqlAlias);
|
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);
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql .= ', ' . $sqlTableAlias . '.' . $srcColumn . ' AS ' . $columnAlias;
|
|
|
|
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
|
|
|
|
$this->_rsm->addMetaResult($dqlAlias, $this->_platform->getSQLResultCasing($columnAlias), $srcColumn);
|
2009-08-11 14:51:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$identificationVarDecls = $fromClause->identificationVariableDeclarations;
|
2010-07-20 07:51:01 +04:00
|
|
|
$sqlParts = array();
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
foreach ($identificationVarDecls as $identificationVariableDecl) {
|
|
|
|
$sql = '';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
$rangeDecl = $identificationVariableDecl->rangeVariableDeclaration;
|
|
|
|
$dqlAlias = $rangeDecl->aliasIdentificationVariable;
|
|
|
|
|
|
|
|
$this->_rootAliases[] = $dqlAlias;
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
$class = $this->_em->getClassMetadata($rangeDecl->abstractSchemaName);
|
|
|
|
$sql .= $class->getQuotedTableName($this->_platform) . ' '
|
|
|
|
. $this->getSqlTableAlias($class->table['name'], $dqlAlias);
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
if ($class->isInheritanceTypeJoined()) {
|
|
|
|
$sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias);
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
foreach ($identificationVariableDecl->joinVariableDeclarations as $joinVarDecl) {
|
|
|
|
$sql .= $this->walkJoinVariableDeclaration($joinVarDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($identificationVariableDecl->indexBy) {
|
|
|
|
$this->_rsm->addIndexBy(
|
|
|
|
$identificationVariableDecl->indexBy->simpleStateFieldPathExpression->identificationVariable,
|
2010-07-23 08:55:33 +04:00
|
|
|
$identificationVariableDecl->indexBy->simpleStateFieldPathExpression->field
|
2010-07-20 07:51:01 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sqlParts[] = $this->_platform->appendLockHint($sql, $this->_query->getHint(Query::HINT_LOCK_MODE));
|
2010-06-29 02:06:19 +04:00
|
|
|
}
|
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
return ' FROM ' . implode(', ', $sqlParts);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2010-02-21 03:06:34 +03:00
|
|
|
$colSql = $this->_generateOrderedCollectionOrderByItems();
|
|
|
|
if ($colSql != '') {
|
|
|
|
$colSql = ", ".$colSql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
// 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)
|
2010-02-21 03:06:34 +03:00
|
|
|
) . $colSql;
|
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)
|
|
|
|
{
|
2010-01-01 01:48:51 +03:00
|
|
|
$sql = '';
|
2009-08-07 01:42:07 +04:00
|
|
|
$expr = $orderByItem->expression;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-09-11 23:50:48 +04:00
|
|
|
if ($expr instanceof AST\PathExpression) {
|
2010-01-01 01:48:51 +03:00
|
|
|
$sql = $this->walkPathExpression($expr);
|
2009-09-11 23:50:48 +04:00
|
|
|
} else {
|
|
|
|
$columnName = $this->_queryComponents[$expr]['token']['value'];
|
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
$sql = $this->_scalarResultAliasMap[$columnName];
|
2009-09-11 23:50:48 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
return $sql . ' ' . 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)
|
|
|
|
{
|
2010-04-30 05:15:36 +04:00
|
|
|
return ' HAVING ' . $this->walkConditionalExpression($havingClause->conditionalExpression);
|
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;
|
2010-03-05 19:35:00 +03: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;
|
2010-04-13 07:29:29 +04:00
|
|
|
$relation = $this->_queryComponents[$joinedDqlAlias]['relation'];
|
|
|
|
$targetClass = $this->_em->getClassMetadata($relation->targetEntityName);
|
|
|
|
$sourceClass = $this->_em->getClassMetadata($relation->sourceEntityName);
|
2009-08-11 14:51:38 +04:00
|
|
|
$targetTableName = $targetClass->getQuotedTableName($this->_platform);
|
2010-04-13 07:29:29 +04:00
|
|
|
$targetTableAlias = $this->getSqlTableAlias($targetClass->table['name'], $joinedDqlAlias);
|
|
|
|
$sourceTableAlias = $this->getSqlTableAlias($sourceClass->table['name'], $joinAssocPathExpr->identificationVariable);
|
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
|
2010-04-13 07:29:29 +04:00
|
|
|
$assoc = ( ! $relation->isOwningSide) ? $targetClass->associationMappings[$relation->mappedBy] : $relation;
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-12-08 23:53:01 +03:00
|
|
|
if ($this->_query->getHint(Query::HINT_INTERNAL_ITERATION) == true) {
|
|
|
|
if ($relation->isOneToMany() || $relation->isManyToMany()) {
|
|
|
|
throw QueryException::iterateWithFetchJoinNotAllowed($assoc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-15 18:39:43 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
foreach ($assoc->sourceToTargetKeyColumns as $sourceColumn => $targetColumn) {
|
2009-10-15 18:39:43 +04:00
|
|
|
if ( ! $first) $sql .= ' AND '; else $first = false;
|
2010-04-13 07:29:29 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
if ($relation->isOwningSide) {
|
2009-10-15 18:39:43 +04:00
|
|
|
$quotedTargetColumn = $targetClass->getQuotedColumnName($targetClass->fieldNames[$targetColumn], $this->_platform);
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql .= $sourceTableAlias . '.' . $sourceColumn
|
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-10-15 18:39:43 +04:00
|
|
|
$quotedTargetColumn = $sourceClass->getQuotedColumnName($sourceClass->fieldNames[$targetColumn], $this->_platform);
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $sourceTableAlias . '.' . $quotedTargetColumn
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2010-03-05 19:35:00 +03:00
|
|
|
. $targetTableAlias . '.' . $sourceColumn;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-07 17:54:01 +04:00
|
|
|
} else if ($assoc->isManyToMany()) {
|
|
|
|
// Join relation table
|
2010-03-15 20:19:00 +03:00
|
|
|
$joinTable = $assoc->joinTable;
|
2010-02-07 16:05:48 +03:00
|
|
|
$joinTableAlias = $this->getSqlTableAlias($joinTable['name'], $joinedDqlAlias);
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql .= $assoc->getQuotedJoinTableName($this->_platform) . ' ' . $joinTableAlias . ' ON ';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-03 01:37:15 +04:00
|
|
|
$first = true;
|
2009-08-11 14:51:38 +04:00
|
|
|
if ($relation->isOwningSide) {
|
2009-11-21 21:52:02 +03:00
|
|
|
foreach ($assoc->relationToSourceKeyColumns as $relationColumn => $sourceColumn) {
|
2010-04-03 01:37:15 +04:00
|
|
|
if ( ! $first) $sql .= ' AND '; else $first = false;
|
|
|
|
|
2009-10-19 00:36:02 +04:00
|
|
|
$sql .= $sourceTableAlias . '.' . $sourceClass->getQuotedColumnName($sourceClass->fieldNames[$sourceColumn], $this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2010-03-05 19:35:00 +03:00
|
|
|
. $joinTableAlias . '.' . $relationColumn;
|
2009-05-07 17:54:01 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-11-21 21:52:02 +03:00
|
|
|
foreach ($assoc->relationToTargetKeyColumns as $relationColumn => $targetColumn) {
|
2010-04-03 01:37:15 +04:00
|
|
|
if ( ! $first) $sql .= ' AND '; else $first = false;
|
|
|
|
|
2009-10-19 00:36:02 +04:00
|
|
|
$sql .= $sourceTableAlias . '.' . $targetClass->getQuotedColumnName($targetClass->fieldNames[$targetColumn], $this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2010-03-05 19:35:00 +03:00
|
|
|
. $joinTableAlias . '.' . $relationColumn;
|
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
|
|
|
|
2010-04-03 01:37:15 +04:00
|
|
|
$first = true;
|
2009-08-11 14:51:38 +04:00
|
|
|
if ($relation->isOwningSide) {
|
2009-11-21 21:52:02 +03:00
|
|
|
foreach ($assoc->relationToTargetKeyColumns as $relationColumn => $targetColumn) {
|
2010-04-03 01:37:15 +04:00
|
|
|
if ( ! $first) $sql .= ' AND '; else $first = false;
|
|
|
|
|
2009-10-19 00:36:02 +04:00
|
|
|
$sql .= $targetTableAlias . '.' . $targetClass->getQuotedColumnName($targetClass->fieldNames[$targetColumn], $this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2010-03-05 19:35:00 +03:00
|
|
|
. $joinTableAlias . '.' . $relationColumn;
|
2009-05-07 17:54:01 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-11-21 21:52:02 +03:00
|
|
|
foreach ($assoc->relationToSourceKeyColumns as $relationColumn => $sourceColumn) {
|
2010-04-03 01:37:15 +04:00
|
|
|
if ( ! $first) $sql .= ' AND '; else $first = false;
|
|
|
|
|
2009-10-19 00:36:02 +04:00
|
|
|
$sql .= $targetTableAlias . '.' . $sourceClass->getQuotedColumnName($sourceClass->fieldNames[$sourceColumn], $this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2010-03-05 19:35:00 +03:00
|
|
|
. $joinTableAlias . '.' . $relationColumn;
|
2009-05-07 17:54:01 +04:00
|
|
|
}
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
// Handle WITH clause
|
2010-04-30 05:15:36 +04:00
|
|
|
if (($condExpr = $join->conditionalExpression) !== null) {
|
|
|
|
// Phase 2 AST optimization: Skip processment of ConditionalExpression
|
|
|
|
// if only one ConditionalTerm is defined
|
|
|
|
$sql .= ' AND (' . $this->walkConditionalExpression($condExpr) . ')';
|
2010-02-14 12:53:38 +03:00
|
|
|
}
|
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
$discrSql = $this->_generateDiscriminatorColumnConditionSQL(array($joinedDqlAlias));
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
if ($discrSql) {
|
|
|
|
$sql .= ' AND ' . $discrSql;
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
// FIXME: these should either be nested or all forced to be left joins (DDC-XXX)
|
2009-08-11 14:51:38 +04:00
|
|
|
if ($targetClass->isInheritanceTypeJoined()) {
|
|
|
|
$sql .= $this->_generateClassTableInheritanceJoins($targetClass, $joinedDqlAlias);
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03: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;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
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) {
|
2010-07-23 08:55:33 +04:00
|
|
|
$fieldName = $expr->field;
|
|
|
|
$dqlAlias = $expr->identificationVariable;
|
2009-04-09 22:12:48 +04:00
|
|
|
$qComp = $this->_queryComponents[$dqlAlias];
|
2009-01-19 21:40:12 +03:00
|
|
|
$class = $qComp['metadata'];
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
if ( ! $selectExpression->fieldIdentificationVariable) {
|
|
|
|
$resultAlias = $fieldName;
|
|
|
|
} else {
|
|
|
|
$resultAlias = $selectExpression->fieldIdentificationVariable;
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2010-02-26 23:29:01 +03:00
|
|
|
|
|
|
|
if ($class->isInheritanceTypeJoined()) {
|
|
|
|
$tableName = $this->_em->getUnitOfWork()->getEntityPersister($class->name)->getOwningTable($fieldName);
|
|
|
|
} else {
|
|
|
|
$tableName = $class->getTableName();
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-26 23:29:01 +03:00
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($tableName, $dqlAlias);
|
2009-08-11 14:51:38 +04:00
|
|
|
$columnName = $class->getQuotedColumnName($fieldName, $this->_platform);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
$columnAlias = $this->getSqlColumnAlias($columnName);
|
|
|
|
$sql .= $sqlTableAlias . '.' . $columnName . ' AS ' . $columnAlias;
|
2010-03-05 19:35:00 +03:00
|
|
|
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
|
2010-02-20 00:28:17 +03:00
|
|
|
$this->_rsm->addScalarResult($columnAlias, $resultAlias);
|
2009-01-19 21:40:12 +03:00
|
|
|
} else {
|
2010-02-20 00:28:17 +03:00
|
|
|
throw QueryException::invalidPathExpression($expr->type);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2010-02-20 00:28:17 +03: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
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
$columnAlias = 'sclr' . $this->_aliasCounter++;
|
|
|
|
$sql .= $this->walkAggregateExpression($expr) . ' AS ' . $columnAlias;
|
2009-09-11 23:50:48 +04:00
|
|
|
$this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
|
|
|
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm->addScalarResult($columnAlias, $resultAlias);
|
2010-02-20 00:28:17 +03:00
|
|
|
}
|
|
|
|
else if ($expr instanceof AST\Subselect) {
|
2010-02-25 23:25:28 +03:00
|
|
|
if ( ! $selectExpression->fieldIdentificationVariable) {
|
|
|
|
$resultAlias = $this->_scalarResultCounter++;
|
|
|
|
} else {
|
|
|
|
$resultAlias = $selectExpression->fieldIdentificationVariable;
|
|
|
|
}
|
|
|
|
|
|
|
|
$columnAlias = 'sclr' . $this->_aliasCounter++;
|
|
|
|
$sql .= '(' . $this->walkSubselect($expr) . ') AS '.$columnAlias;
|
|
|
|
$this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
|
2010-02-25 23:25:28 +03:00
|
|
|
$this->_rsm->addScalarResult($columnAlias, $resultAlias);
|
2010-02-20 00:28:17 +03:00
|
|
|
}
|
|
|
|
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-09-11 23:50:48 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
$columnAlias = 'sclr' . $this->_aliasCounter++;
|
|
|
|
$sql .= $this->walkFunction($expr) . ' AS ' . $columnAlias;
|
2009-09-11 23:50:48 +04:00
|
|
|
$this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
|
|
|
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
|
2010-02-20 00:28:17 +03:00
|
|
|
$this->_rsm->addScalarResult($columnAlias, $resultAlias);
|
|
|
|
}
|
2010-04-30 05:15:36 +04:00
|
|
|
else if (
|
|
|
|
$expr instanceof AST\SimpleArithmeticExpression ||
|
|
|
|
$expr instanceof AST\ArithmeticTerm ||
|
|
|
|
$expr instanceof AST\ArithmeticFactor ||
|
|
|
|
$expr instanceof AST\ArithmeticPrimary
|
|
|
|
) {
|
2010-02-20 00:28:17 +03:00
|
|
|
if ( ! $selectExpression->fieldIdentificationVariable) {
|
|
|
|
$resultAlias = $this->_scalarResultCounter++;
|
|
|
|
} else {
|
|
|
|
$resultAlias = $selectExpression->fieldIdentificationVariable;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
$columnAlias = 'sclr' . $this->_aliasCounter++;
|
|
|
|
$sql .= $this->walkSimpleArithmeticExpression($expr) . ' AS ' . $columnAlias;
|
|
|
|
$this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
|
|
|
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
|
2009-06-14 21:34:28 +04:00
|
|
|
$this->_rsm->addScalarResult($columnAlias, $resultAlias);
|
2009-03-19 15:43:48 +03:00
|
|
|
} else {
|
2010-02-20 00:28:17 +03:00
|
|
|
// IdentificationVariable or PartialObjectExpression
|
|
|
|
if ($expr instanceof AST\PartialObjectExpression) {
|
|
|
|
$dqlAlias = $expr->identificationVariable;
|
|
|
|
$partialFieldSet = $expr->partialFieldSet;
|
|
|
|
} else {
|
|
|
|
$dqlAlias = $expr;
|
|
|
|
$partialFieldSet = array();
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
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) {
|
2010-02-20 00:28:17 +03:00
|
|
|
if ($partialFieldSet && !in_array($fieldName, $partialFieldSet)) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
if (isset($mapping['inherited'])) {
|
2010-03-29 17:20:41 +04:00
|
|
|
$tableName = $this->_em->getClassMetadata($mapping['inherited'])->table['name'];
|
2009-08-11 14:51:38 +04:00
|
|
|
} else {
|
2010-03-29 17:20:41 +04:00
|
|
|
$tableName = $class->table['name'];
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
if ($beginning) $beginning = false; else $sql .= ', ';
|
2010-03-05 19:35:00 +03: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;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
|
|
|
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
|
2009-12-19 16:38:54 +03:00
|
|
|
$this->_rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $class->name);
|
2009-08-11 14:51:38 +04:00
|
|
|
}
|
|
|
|
|
2009-10-22 23:12:00 +04:00
|
|
|
// Add any additional fields of subclasses (excluding inherited fields)
|
2009-08-17 21:58:16 +04:00
|
|
|
// 1) on Single Table Inheritance: always, since its marginal overhead
|
|
|
|
// 2) on Class Table Inheritance only if partial objects are disallowed,
|
|
|
|
// since it requires outer joining subtables.
|
2009-10-15 18:39:43 +04:00
|
|
|
if ($class->isInheritanceTypeSingleTable() || ! $this->_query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
|
2009-08-17 21:58:16 +04:00
|
|
|
foreach ($class->subClasses as $subClassName) {
|
|
|
|
$subClass = $this->_em->getClassMetadata($subClassName);
|
2010-03-29 17:20:41 +04:00
|
|
|
$sqlTableAlias = $this->getSqlTableAlias($subClass->table['name'], $dqlAlias);
|
2009-08-17 21:58:16 +04:00
|
|
|
foreach ($subClass->fieldMappings as $fieldName => $mapping) {
|
2010-02-20 00:28:17 +03:00
|
|
|
if (isset($mapping['inherited']) || $partialFieldSet && !in_array($fieldName, $partialFieldSet)) {
|
2009-08-17 21:58:16 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($beginning) $beginning = false; else $sql .= ', ';
|
|
|
|
|
|
|
|
$columnAlias = $this->getSqlColumnAlias($mapping['columnName']);
|
|
|
|
$sql .= $sqlTableAlias . '.' . $subClass->getQuotedColumnName($fieldName, $this->_platform)
|
2009-10-15 18:39:43 +04:00
|
|
|
. ' AS ' . $columnAlias;
|
2009-08-17 21:58:16 +04:00
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
|
2009-12-19 16:38:54 +03:00
|
|
|
$this->_rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $subClassName);
|
2009-08-11 14:51:38 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-10-22 23:12:00 +04:00
|
|
|
// Add join columns (foreign keys) of the subclass
|
|
|
|
//TODO: Probably better do this in walkSelectClause to honor the INCLUDE_META_COLUMNS hint
|
|
|
|
foreach ($subClass->associationMappings as $fieldName => $assoc) {
|
2010-04-14 19:07:08 +04:00
|
|
|
if ($assoc->isOwningSide && $assoc->isOneToOne() && ! $assoc->inherited) {
|
2009-10-22 23:12:00 +04:00
|
|
|
foreach ($assoc->targetToSourceKeyColumns as $srcColumn) {
|
|
|
|
if ($beginning) $beginning = false; else $sql .= ', ';
|
|
|
|
$columnAlias = $this->getSqlColumnAlias($srcColumn);
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql .= $sqlTableAlias . '.' . $srcColumn . ' AS ' . $columnAlias;
|
|
|
|
$this->_rsm->addMetaResult($dqlAlias, $this->_platform->getSQLResultCasing($columnAlias), $srcColumn);
|
2009-10-22 23:12:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03: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)
|
|
|
|
{
|
2010-03-05 19:35:00 +03:00
|
|
|
return ' ' . strtoupper($qExpr->type)
|
2009-08-13 14:13:06 +04:00
|
|
|
. '(' . $this->walkSubselect($qExpr->subselect) . ')';
|
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;
|
2010-03-05 19:35:00 +03: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) : '';
|
2010-03-05 19:35:00 +03: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;
|
2010-07-20 07:51:01 +04:00
|
|
|
$sqlParts = array ();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
foreach ($identificationVarDecls as $subselectIdVarDecl) {
|
|
|
|
$sql = '';
|
2009-03-19 15:43:48 +03:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
$rangeDecl = $subselectIdVarDecl->rangeVariableDeclaration;
|
|
|
|
$dqlAlias = $rangeDecl->aliasIdentificationVariable;
|
2010-03-15 22:04:14 +03:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
$class = $this->_em->getClassMetadata($rangeDecl->abstractSchemaName);
|
|
|
|
$sql .= $class->getQuotedTableName($this->_platform) . ' '
|
|
|
|
. $this->getSqlTableAlias($class->table['name'], $dqlAlias);
|
|
|
|
|
|
|
|
if ($class->isInheritanceTypeJoined()) {
|
|
|
|
$sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($subselectIdVarDecl->joinVariableDeclarations as $joinVarDecl) {
|
|
|
|
$sql .= $this->walkJoinVariableDeclaration($joinVarDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sqlParts[] = $this->_platform->appendLockHint($sql, $this->_query->getHint(Query::HINT_LOCK_MODE));
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
return ' FROM ' . implode(', ', $sqlParts);
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
|
|
|
|
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;
|
2010-03-05 19:35:00 +03: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) {
|
2010-02-25 23:31:53 +03:00
|
|
|
$alias = $this->_scalarResultCounter++;
|
2009-03-19 15:43:48 +03:00
|
|
|
} else {
|
2009-08-07 01:42:07 +04:00
|
|
|
$alias = $simpleSelectExpression->fieldIdentificationVariable;
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
2010-02-25 23:25:28 +03:00
|
|
|
|
|
|
|
$sql .= ' ' . $this->walkAggregateExpression($expr) . ' AS dctrn__' . $alias;
|
2009-03-19 15:43:48 +03:00
|
|
|
} else {
|
2009-05-17 23:27:12 +04:00
|
|
|
// IdentificationVariable
|
2009-08-13 14:13:06 +04:00
|
|
|
// FIXME: Composite key support, or select all columns? Does that make sense
|
|
|
|
// in a subquery?
|
2009-05-17 23:27:12 +04:00
|
|
|
$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
|
|
|
}
|
2010-03-05 19:35:00 +03: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)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
return $aggExpression->functionName . '(' . ($aggExpression->isDistinct ? 'DISTINCT ' : '')
|
2010-01-01 01:48:51 +03:00
|
|
|
. $this->walkPathExpression($aggExpression->pathExpression) . ')';
|
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
|
|
|
{
|
2010-01-01 01:48:51 +03:00
|
|
|
return $this->walkPathExpression($pathExpr);
|
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);
|
2010-03-05 19:35:00 +03: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
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
$this->_rootAliases[] = $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);
|
2010-03-05 19:35:00 +03: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
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
$this->_rootAliases[] = $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
|
|
|
);
|
2010-03-05 19:35:00 +03: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;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-07-08 19:30:39 +04:00
|
|
|
$sql = $this->walkPathExpression($updateItem->pathExpression) . ' = ';
|
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
|
|
|
|
2010-07-08 19:30:39 +04:00
|
|
|
if ($newValue === null) {
|
|
|
|
$sql .= 'NULL';
|
|
|
|
} else if ($newValue instanceof AST\Node) {
|
2009-03-23 20:39:33 +03:00
|
|
|
$sql .= $newValue->dispatch($this);
|
2010-07-08 19:30:39 +04:00
|
|
|
} else {
|
|
|
|
$sql .= $this->_conn->quote($newValue);
|
2009-03-23 20:39:33 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +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)
|
|
|
|
{
|
2010-07-20 07:51:01 +04:00
|
|
|
$discrSql = $this->_generateDiscriminatorColumnConditionSql($this->_rootAliases);
|
2010-04-30 05:15:36 +04:00
|
|
|
$condSql = $this->walkConditionalExpression($whereClause->conditionalExpression);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
return ' WHERE ' . (( ! $discrSql) ? $condSql : '(' . $condSql . ') AND ' . $discrSql);
|
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
/**
|
|
|
|
* Walk down a ConditionalExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param ConditionalExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkConditionalExpression($condExpr)
|
|
|
|
{
|
|
|
|
// Phase 2 AST optimization: Skip processment of ConditionalExpression
|
|
|
|
// if only one ConditionalTerm is defined
|
|
|
|
return ( ! ($condExpr instanceof AST\ConditionalExpression))
|
|
|
|
? $this->walkConditionalTerm($condExpr)
|
|
|
|
: implode(
|
|
|
|
' OR ', array_map(array($this, 'walkConditionalTerm'), $condExpr->conditionalTerms)
|
|
|
|
);
|
2009-05-03 14:58:16 +04:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2010-04-30 05:15:36 +04:00
|
|
|
// Phase 2 AST optimization: Skip processment of ConditionalTerm
|
|
|
|
// if only one ConditionalFactor is defined
|
|
|
|
return ( ! ($condTerm instanceof AST\ConditionalTerm))
|
|
|
|
? $this->walkConditionalFactor($condTerm)
|
|
|
|
: implode(
|
|
|
|
' AND ', array_map(array($this, 'walkConditionalFactor'), $condTerm->conditionalFactors)
|
|
|
|
);
|
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)
|
|
|
|
{
|
2010-04-30 05:15:36 +04:00
|
|
|
// Phase 2 AST optimization: Skip processment of ConditionalFactor
|
|
|
|
// if only one ConditionalPrimary is defined
|
|
|
|
return ( ! ($factor instanceof AST\ConditionalFactor))
|
|
|
|
? $this->walkConditionalPrimary($factor)
|
|
|
|
: ($factor->not ? 'NOT ' : '') . $this->walkConditionalPrimary($factor->conditionalPrimary);
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
/**
|
|
|
|
* Walks down a ConditionalPrimary AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param ConditionalPrimary
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkConditionalPrimary($primary)
|
|
|
|
{
|
2009-01-20 20:07:07 +03:00
|
|
|
if ($primary->isSimpleConditionalExpression()) {
|
2010-04-30 05:15:36 +04:00
|
|
|
return $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;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
return '(' . $this->walkConditionalExpression($condExpr) . ')';
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
2009-01-21 21:25:05 +03:00
|
|
|
}
|
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 ' : '';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= 'EXISTS (' . $this->walkSubselect($existsExpr->subselect) . ')';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-03-23 20:39:33 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
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;
|
2010-01-01 01:48:51 +03:00
|
|
|
|
2010-07-23 08:55:33 +04:00
|
|
|
$fieldName = $collPathExpr->field;
|
|
|
|
$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
|
2010-03-05 19:35:00 +03:00
|
|
|
throw new \BadMethodCallException("Not implemented");
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
$assoc = $class->associationMappings[$fieldName];
|
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);
|
2010-03-29 17:20:41 +04:00
|
|
|
$targetTableAlias = $this->getSqlTableAlias($targetClass->table['name']);
|
|
|
|
$sourceTableAlias = $this->getSqlTableAlias($class->table['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
|
|
|
|
2010-03-15 20:19:00 +03:00
|
|
|
$owningAssoc = $targetClass->associationMappings[$assoc->mappedBy];
|
2009-06-14 21:34:28 +04:00
|
|
|
|
|
|
|
$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-10-19 00:36:02 +04:00
|
|
|
$sql .= $sourceTableAlias . '.' . $class->getQuotedColumnName($class->fieldNames[$targetColumn], $this->_platform)
|
2009-08-06 19:08:03 +04:00
|
|
|
. ' = '
|
2010-03-05 19:35:00 +03:00
|
|
|
. $targetTableAlias . '.' . $sourceColumn;
|
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
|
|
|
|
2010-03-15 20:19:00 +03:00
|
|
|
$owningAssoc = $assoc->isOwningSide ? $assoc : $targetClass->associationMappings[$assoc->mappedBy];
|
2009-08-11 14:51:38 +04:00
|
|
|
$joinTable = $assoc->isOwningSide ? $assoc->joinTable : $owningAssoc->joinTable;
|
|
|
|
|
|
|
|
// SQL table aliases
|
2009-06-14 21:34:28 +04:00
|
|
|
$joinTableAlias = $this->getSqlTableAlias($joinTable['name']);
|
2010-03-29 17:20:41 +04:00
|
|
|
$targetTableAlias = $this->getSqlTableAlias($targetClass->table['name']);
|
|
|
|
$sourceTableAlias = $this->getSqlTableAlias($class->table['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
|
2010-03-27 01:07:31 +03:00
|
|
|
? $joinTable['inverseJoinColumns']
|
|
|
|
: $joinTable['joinColumns'];
|
2010-03-29 17:20:41 +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-11 14:51:38 +04:00
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql .= $joinTableAlias . '.' . $joinColumn['name'] . ' = '
|
2010-03-27 01:07:31 +03:00
|
|
|
. $targetTableAlias . '.' . $targetClass->getQuotedColumnName(
|
|
|
|
$targetClass->fieldNames[$joinColumn['referencedColumnName']],
|
2009-10-19 00:36:02 +04:00
|
|
|
$this->_platform);
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
2010-03-29 17:20:41 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
$sql .= ' WHERE ';
|
2010-03-29 17:20:41 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$joinColumns = $assoc->isOwningSide
|
2010-03-27 01:07:31 +03:00
|
|
|
? $joinTable['joinColumns']
|
|
|
|
: $joinTable['inverseJoinColumns'];
|
2010-03-29 17:20:41 +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 ';
|
2010-03-27 01:07:31 +03:00
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql .= $joinTableAlias . '.' . $joinColumn['name'] . ' = '
|
2010-03-27 01:07:31 +03:00
|
|
|
. $sourceTableAlias . '.' . $class->getQuotedColumnName(
|
|
|
|
$class->fieldNames[$joinColumn['referencedColumnName']],
|
2009-10-19 00:36:02 +04:00
|
|
|
$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;
|
2010-03-05 19:35:00 +03: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;
|
2010-03-05 19:35:00 +03: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
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= ' IS' . ($nullCompExpr->not ? ' NOT' : '') . ' NULL';
|
2010-03-05 19:35:00 +03: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)
|
|
|
|
{
|
2010-03-05 19:35:00 +03:00
|
|
|
$sql = $this->walkPathExpression($inExpr->pathExpression)
|
2009-08-07 01:42:07 +04:00
|
|
|
. ($inExpr->not ? ' NOT' : '') . ' IN (';
|
2010-03-05 19:35:00 +03: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-10-13 14:48:46 +04:00
|
|
|
$sql .= implode(', ', array_map(array($this, 'walkInParameter'), $inExpr->literals));
|
2009-03-21 15:49:58 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
$sql .= ')';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-10-13 14:48:46 +04:00
|
|
|
public function walkInParameter($inParam)
|
|
|
|
{
|
|
|
|
return $inParam instanceof AST\InputParameter ?
|
|
|
|
$this->walkInputParameter($inParam) :
|
|
|
|
$this->walkLiteral($inParam);
|
|
|
|
}
|
2009-03-21 15:49:58 +03:00
|
|
|
|
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)
|
|
|
|
{
|
2009-10-13 14:48:46 +04:00
|
|
|
switch ($literal->type) {
|
|
|
|
case AST\Literal::STRING:
|
|
|
|
return $this->_conn->quote($literal->value);
|
|
|
|
case AST\Literal::BOOLEAN:
|
|
|
|
$bool = strtolower($literal->value) == 'true' ? true : false;
|
|
|
|
$boolVal = $this->_conn->getDatabasePlatform()->convertBooleans($bool);
|
|
|
|
return is_string($boolVal) ? $this->_conn->quote($boolVal) : $boolVal;
|
|
|
|
case AST\Literal::NUMERIC:
|
|
|
|
return $literal->value;
|
|
|
|
default:
|
2010-03-05 19:35:00 +03:00
|
|
|
throw QueryException::invalidLiteral($literal);
|
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);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
if ($betweenExpr->not) $sql .= ' NOT';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= ' BETWEEN ' . $this->walkArithmeticExpression($betweenExpr->leftBetweenExpression)
|
|
|
|
. ' AND ' . $this->walkArithmeticExpression($betweenExpr->rightBetweenExpression);
|
2010-03-05 19:35:00 +03: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 ';
|
2010-03-05 19:35:00 +03: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
|
|
|
}
|
2010-03-05 19:35:00 +03: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
|
|
|
}
|
2010-03-05 19:35:00 +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-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-08-13 14:13:06 +04:00
|
|
|
$sql .= is_numeric($leftExpr) ? $leftExpr : $this->_conn->quote($leftExpr);
|
2009-03-23 20:39:33 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +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-08-13 14:13:06 +04:00
|
|
|
$sql .= is_numeric($rightExpr) ? $rightExpr : $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++);
|
2010-03-05 19:35:00 +03: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)
|
2010-05-16 15:30:40 +04:00
|
|
|
: '(' . $this->walkSubselect($arithmeticExpr->subselect) . ')';
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
/**
|
|
|
|
* Walks down an SimpleArithmeticExpression AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
|
|
|
* @param SimpleArithmeticExpression
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkSimpleArithmeticExpression($simpleArithmeticExpr)
|
|
|
|
{
|
|
|
|
return ( ! ($simpleArithmeticExpr instanceof AST\SimpleArithmeticExpression))
|
|
|
|
? $this->walkArithmeticTerm($simpleArithmeticExpr)
|
|
|
|
: implode(
|
|
|
|
' ', array_map(array($this, 'walkArithmeticTerm'), $simpleArithmeticExpr->arithmeticTerms)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2010-04-30 05:15:36 +04:00
|
|
|
if (is_string($term)) {
|
|
|
|
return $term;
|
|
|
|
}
|
2009-03-23 20:39:33 +03:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
// Phase 2 AST optimization: Skip processment of ArithmeticTerm
|
|
|
|
// if only one ArithmeticFactor is defined
|
|
|
|
return ( ! ($term instanceof AST\ArithmeticTerm))
|
|
|
|
? $this->walkArithmeticFactor($term)
|
|
|
|
: implode(
|
|
|
|
' ', array_map(array($this, 'walkArithmeticFactor'), $term->arithmeticFactors)
|
|
|
|
);
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2010-04-30 05:15:36 +04:00
|
|
|
* Walks down an ArithmeticFactor that represents an AST node, thereby generating the appropriate SQL.
|
2009-03-28 20:10:41 +03:00
|
|
|
*
|
|
|
|
* @param mixed
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2010-04-30 05:15:36 +04:00
|
|
|
public function walkArithmeticFactor($factor)
|
2009-03-23 20:39:33 +03:00
|
|
|
{
|
2010-04-30 05:15:36 +04:00
|
|
|
if (is_string($factor)) {
|
|
|
|
return $factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Phase 2 AST optimization: Skip processment of ArithmeticFactor
|
|
|
|
// if only one ArithmeticPrimary is defined
|
|
|
|
return ( ! ($factor instanceof AST\ArithmeticFactor))
|
|
|
|
? $this->walkArithmeticPrimary($factor)
|
|
|
|
: ($factor->isNegativeSigned() ? '-' : ($factor->isPositiveSigned() ? '+' : ''))
|
|
|
|
. $this->walkArithmeticPrimary($factor->arithmeticPrimary);
|
2009-03-23 20:39:33 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2010-04-30 05:15:36 +04:00
|
|
|
* Walks down an ArithmeticPrimary that represents an AST node, thereby generating the appropriate SQL.
|
2009-03-28 20:10:41 +03:00
|
|
|
*
|
|
|
|
* @param mixed
|
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2010-04-30 05:15:36 +04:00
|
|
|
public function walkArithmeticPrimary($primary)
|
2009-01-20 20:07:07 +03:00
|
|
|
{
|
2009-10-13 14:48:46 +04:00
|
|
|
if ($primary instanceof AST\SimpleArithmeticExpression) {
|
2010-04-30 05:15:36 +04:00
|
|
|
return '(' . $this->walkSimpleArithmeticExpression($primary) . ')';
|
2009-03-23 20:39:33 +03:00
|
|
|
} else if ($primary instanceof AST\Node) {
|
2010-04-30 05:15:36 +04:00
|
|
|
return $primary->dispatch($this);
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
// We need to deal with IdentificationVariable here
|
|
|
|
return '';
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2010-04-30 05:15:36 +04:00
|
|
|
* Walks down a StringPrimary that represents an AST node, thereby generating the appropriate SQL.
|
2009-03-28 20:10:41 +03:00
|
|
|
*
|
2010-04-30 05:15:36 +04:00
|
|
|
* @param mixed
|
2009-03-28 20:10:41 +03:00
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2010-04-30 05:15:36 +04:00
|
|
|
public function walkStringPrimary($stringPrimary)
|
2009-01-21 21:25:05 +03:00
|
|
|
{
|
2010-04-30 05:15:36 +04:00
|
|
|
return (is_string($stringPrimary))
|
|
|
|
? $this->_conn->quote($stringPrimary)
|
|
|
|
: $stringPrimary->dispatch($this);
|
2009-01-21 21:25:05 +03:00
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|