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
|
2012-05-26 16:37:00 +04:00
|
|
|
* and is licensed under the MIT license. For more information, see
|
2009-02-17 13:54:18 +03:00
|
|
|
* <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;
|
|
|
|
|
2012-10-12 15:53:20 +04:00
|
|
|
use Doctrine\DBAL\LockMode;
|
|
|
|
use Doctrine\DBAL\Types\Type;
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
|
|
|
use Doctrine\ORM\Query;
|
|
|
|
use Doctrine\ORM\Query\QueryException;
|
2013-06-05 07:50:43 +04:00
|
|
|
use Doctrine\ORM\OptimisticLockException;
|
2012-10-12 15:53:20 +04:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
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
|
|
|
*
|
2011-11-14 07:36:39 +04:00
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
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>
|
2011-12-06 01:05:42 +04:00
|
|
|
* @author Alexander <iam.asm89@gmail.com>
|
2012-08-11 19:56:47 +04:00
|
|
|
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
|
2011-11-14 07:36:39 +04: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
|
|
|
{
|
2012-01-25 02:34:20 +04:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const HINT_DISTINCT = 'doctrine.distinct';
|
2012-03-12 05:14:08 +04:00
|
|
|
|
2010-02-14 12:53:38 +03:00
|
|
|
/**
|
|
|
|
* @var ResultSetMapping
|
|
|
|
*/
|
2012-06-01 01:03:01 +04:00
|
|
|
private $rsm;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* Counter for generating unique column aliases.
|
2012-06-01 01:03:01 +04:00
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
private $aliasCounter = 0;
|
|
|
|
|
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* Counter for generating unique table aliases.
|
2012-06-01 01:03:01 +04:00
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
private $tableAliasCounter = 0;
|
|
|
|
|
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* Counter for generating unique scalar result.
|
2012-06-01 01:03:01 +04:00
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
private $scalarResultCounter = 1;
|
|
|
|
|
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* Counter for generating unique parameter indexes.
|
2012-06-01 01:03:01 +04:00
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
private $sqlParamIndex = 0;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-01-21 23:39:32 +04:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* Counter for generating indexes.
|
2012-01-21 23:39:32 +04:00
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
2012-07-22 01:07:56 +04:00
|
|
|
private $newObjectCounter = 0;
|
2012-01-21 23:39:32 +04:00
|
|
|
|
2010-02-14 12:53:38 +03:00
|
|
|
/**
|
|
|
|
* @var ParserResult
|
|
|
|
*/
|
2012-06-01 01:03:01 +04:00
|
|
|
private $parserResult;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-14 12:53:38 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* @var \Doctrine\ORM\EntityManager
|
2010-02-14 12:53:38 +03:00
|
|
|
*/
|
2012-06-01 01:03:01 +04:00
|
|
|
private $em;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-14 12:53:38 +03:00
|
|
|
/**
|
2011-12-12 00:52:29 +04:00
|
|
|
* @var \Doctrine\DBAL\Connection
|
2010-02-14 12:53:38 +03:00
|
|
|
*/
|
2012-06-01 01:03:01 +04:00
|
|
|
private $conn;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-12-08 23:53:01 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* @var \Doctrine\ORM\AbstractQuery
|
2009-12-08 23:53:01 +03:00
|
|
|
*/
|
2012-06-01 01:03:01 +04:00
|
|
|
private $query;
|
2009-12-08 23:53:01 +03:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $tableAliasMap = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
/**
|
|
|
|
* Map from result variable names to their SQL column alias names.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $scalarResultAliasMap = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2013-06-05 07:50:43 +04:00
|
|
|
/**
|
|
|
|
* Map from Table-Alias + Column-Name to OrderBy-Direction.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $orderedColumnsMap = array();
|
|
|
|
|
2011-10-16 21:23:20 +04:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* Map from DQL-Alias + Field-Name to SQL Column Alias.
|
2011-10-16 21:23:20 +04:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-06-01 01:03:01 +04:00
|
|
|
private $scalarFields = array();
|
2011-10-16 21:23:20 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
/**
|
|
|
|
* Map of all components/classes that appear in the DQL query.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $queryComponents;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
/**
|
|
|
|
* A list of classes that appear in non-scalar SelectExpressions.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
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.
|
2012-10-11 17:25:39 +04:00
|
|
|
*
|
2012-06-01 01:03:01 +04:00
|
|
|
* @var array
|
2009-05-03 14:58:16 +04:00
|
|
|
*/
|
2012-06-01 01:03: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.
|
2012-06-01 01:03:01 +04:00
|
|
|
*
|
|
|
|
* @var boolean
|
2009-06-14 21:34:28 +04:00
|
|
|
*/
|
2012-06-01 01:03:01 +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
|
|
|
*
|
2012-12-13 20:36:14 +04:00
|
|
|
* @var \Doctrine\DBAL\Platforms\AbstractPlatform
|
2009-08-11 14:51:38 +04:00
|
|
|
*/
|
2012-06-01 01:03:01 +04:00
|
|
|
private $platform;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The quote strategy.
|
|
|
|
*
|
|
|
|
* @var \Doctrine\ORM\Mapping\QuoteStrategy
|
|
|
|
*/
|
|
|
|
private $quoteStrategy;
|
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
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->query = $query;
|
|
|
|
$this->parserResult = $parserResult;
|
|
|
|
$this->queryComponents = $queryComponents;
|
|
|
|
$this->rsm = $parserResult->getResultSetMapping();
|
|
|
|
$this->em = $query->getEntityManager();
|
|
|
|
$this->conn = $this->em->getConnection();
|
|
|
|
$this->platform = $this->conn->getDatabasePlatform();
|
2012-06-12 03:14:17 +04:00
|
|
|
$this->quoteStrategy = $this->em->getConfiguration()->getQuoteStrategy();
|
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()
|
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
return $this->query;
|
2009-06-23 21:50:13 +04:00
|
|
|
}
|
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
|
|
|
*
|
2012-12-13 20:36:14 +04:00
|
|
|
* @return \Doctrine\DBAL\Connection
|
2009-05-03 14:58:16 +04:00
|
|
|
*/
|
2009-03-28 20:10:41 +03:00
|
|
|
public function getConnection()
|
|
|
|
{
|
2012-06-01 01:03:01 +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
|
|
|
*
|
2012-12-13 15:57:46 +04:00
|
|
|
* @return \Doctrine\ORM\EntityManager
|
2009-06-22 22:48:42 +04:00
|
|
|
*/
|
|
|
|
public function getEntityManager()
|
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
return $this->em;
|
2009-06-22 22:48:42 +04:00
|
|
|
}
|
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.
|
2012-12-13 20:36:14 +04:00
|
|
|
*
|
2009-08-07 01:42:07 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2009-08-05 19:47:41 +04:00
|
|
|
public function getQueryComponent($dqlAlias)
|
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
return $this->queryComponents[$dqlAlias];
|
2009-08-05 19:47:41 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-10-11 17:25:39 +04:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2012-10-11 17:25:39 +04:00
|
|
|
*/
|
|
|
|
public function getQueryComponents()
|
|
|
|
{
|
|
|
|
return $this->queryComponents;
|
|
|
|
}
|
|
|
|
|
2012-10-06 13:15:07 +04:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2012-10-06 13:15:07 +04:00
|
|
|
*/
|
2012-10-11 17:25:39 +04:00
|
|
|
public function setQueryComponent($dqlAlias, array $queryComponent)
|
2012-10-06 13:15:07 +04:00
|
|
|
{
|
|
|
|
$requiredKeys = array('metadata', 'parent', 'relation', 'map', 'nestingLevel', 'token');
|
|
|
|
|
|
|
|
if (array_diff($requiredKeys, array_keys($queryComponent))) {
|
|
|
|
throw QueryException::invalidQueryComponent($dqlAlias);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->queryComponents[$dqlAlias] = $queryComponent;
|
|
|
|
}
|
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-08-07 01:42:07 +04:00
|
|
|
*/
|
|
|
|
public function getExecutor($AST)
|
|
|
|
{
|
2011-10-28 20:25:12 +04:00
|
|
|
switch (true) {
|
|
|
|
case ($AST instanceof AST\DeleteStatement):
|
2012-06-01 01:03:01 +04:00
|
|
|
$primaryClass = $this->em->getClassMetadata($AST->deleteClause->abstractSchemaName);
|
2009-08-07 01:42:07 +04:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
return ($primaryClass->isInheritanceTypeJoined())
|
|
|
|
? new Exec\MultiTableDeleteExecutor($AST, $this)
|
|
|
|
: new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
case ($AST instanceof AST\UpdateStatement):
|
2012-06-01 01:03:01 +04:00
|
|
|
$primaryClass = $this->em->getClassMetadata($AST->updateClause->abstractSchemaName);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-10-30 15:12:18 +04:00
|
|
|
return ($primaryClass->isInheritanceTypeJoined())
|
2011-10-28 20:25:12 +04:00
|
|
|
? new Exec\MultiTableUpdateExecutor($AST, $this)
|
|
|
|
: new Exec\SingleTableDeleteUpdateExecutor($AST, $this);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
default:
|
|
|
|
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
|
2012-12-13 20:36:14 +04:00
|
|
|
* @param string $dqlAlias The DQL alias.
|
|
|
|
*
|
2009-08-07 01:42:07 +04:00
|
|
|
* @return string Generated table alias.
|
|
|
|
*/
|
2011-02-20 00:50:58 +03:00
|
|
|
public function getSQLTableAlias($tableName, $dqlAlias = '')
|
2009-08-07 01:42:07 +04:00
|
|
|
{
|
2011-02-20 07:30:58 +03:00
|
|
|
$tableName .= ($dqlAlias) ? '@[' . $dqlAlias . ']' : '';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-06-01 01:03:01 +04: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
|
|
|
|
2012-06-01 01:03:01 +04: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
|
2011-01-13 23:16:08 +03:00
|
|
|
* @param string $dqlAlias
|
2012-12-13 20:36:14 +04:00
|
|
|
*
|
2011-01-13 23:16:08 +03:00
|
|
|
* @return string
|
2009-08-07 01:42:07 +04:00
|
|
|
*/
|
2011-02-20 00:50:58 +03:00
|
|
|
public function setSQLTableAlias($tableName, $alias, $dqlAlias = '')
|
2009-08-07 01:42:07 +04:00
|
|
|
{
|
2011-02-20 07:30:58 +03:00
|
|
|
$tableName .= ($dqlAlias) ? '@[' . $dqlAlias . ']' : '';
|
2011-01-13 23:16:08 +03:00
|
|
|
|
2012-06-01 01:03:01 +04: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
|
2012-12-13 20:36:14 +04:00
|
|
|
*
|
2009-08-07 01:42:07 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
2011-02-20 00:50:58 +03:00
|
|
|
public function getSQLColumnAlias($columnName)
|
2009-08-07 01:42:07 +04:00
|
|
|
{
|
2012-06-12 03:14:17 +04:00
|
|
|
return $this->quoteStrategy->getColumnAlias($columnName, $this->aliasCounter++, $this->platform);
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the SQL JOINs that are necessary for Class Table Inheritance
|
|
|
|
* for the given class.
|
|
|
|
*
|
2012-12-13 20:36:14 +04:00
|
|
|
* @param ClassMetadata $class The class for which to generate the joins.
|
|
|
|
* @param string $dqlAlias The DQL alias of the class.
|
|
|
|
*
|
2009-08-07 01:42:07 +04:00
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
private function _generateClassTableInheritanceJoins($class, $dqlAlias)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
|
2011-09-07 08:48:19 +04:00
|
|
|
$baseTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
|
2009-08-07 01:42:07 +04:00
|
|
|
|
|
|
|
// INNER JOIN parent class tables
|
|
|
|
foreach ($class->parentClasses as $parentClassName) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$parentClass = $this->em->getClassMetadata($parentClassName);
|
2011-11-03 08:44:50 +04:00
|
|
|
$tableAlias = $this->getSQLTableAlias($parentClass->getTableName(), $dqlAlias);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2010-04-15 20:36:17 +04:00
|
|
|
// If this is a joined association we must use left joins to preserve the correct result.
|
2012-06-01 01:03:01 +04:00
|
|
|
$sql .= isset($this->queryComponents[$dqlAlias]['relation']) ? ' LEFT ' : ' INNER ';
|
2012-06-12 03:14:17 +04:00
|
|
|
$sql .= 'JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON ';
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
$sqlParts = array();
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2012-06-12 03:14:17 +04:00
|
|
|
foreach ($this->quoteStrategy->getIdentifierColumnNames($class, $this->platform) as $columnName) {
|
2011-11-03 08:44:50 +04:00
|
|
|
$sqlParts[] = $baseTableAlias . '.' . $columnName . ' = ' . $tableAlias . '.' . $columnName;
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-12-07 13:02:15 +04:00
|
|
|
// Add filters on the root class
|
2011-12-12 00:14:09 +04:00
|
|
|
if ($filterSql = $this->generateFilterConditionSQL($parentClass, $tableAlias)) {
|
2011-12-07 13:02:15 +04:00
|
|
|
$sqlParts[] = $filterSql;
|
|
|
|
}
|
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
$sql .= implode(' AND ', $sqlParts);
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
// Ignore subclassing inclusion if partial objects is disallowed
|
2012-06-01 01:03:01 +04:00
|
|
|
if ($this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
|
2011-11-03 08:44:50 +04:00
|
|
|
return $sql;
|
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
// LEFT JOIN child class tables
|
|
|
|
foreach ($class->subClasses as $subClassName) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$subClass = $this->em->getClassMetadata($subClassName);
|
2011-11-03 08:44:50 +04:00
|
|
|
$tableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2012-06-12 03:14:17 +04:00
|
|
|
$sql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->platform) . ' ' . $tableAlias . ' ON ';
|
2009-08-17 21:58:16 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
$sqlParts = array();
|
|
|
|
|
2012-06-12 03:14:17 +04:00
|
|
|
foreach ($this->quoteStrategy->getIdentifierColumnNames($subClass, $this->platform) as $columnName) {
|
2011-11-03 08:44:50 +04:00
|
|
|
$sqlParts[] = $baseTableAlias . '.' . $columnName . ' = ' . $tableAlias . '.' . $columnName;
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
2011-11-03 08:44:50 +04:00
|
|
|
|
|
|
|
$sql .= implode(' AND ', $sqlParts);
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2012-12-13 20:36:14 +04:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-02-21 03:06:34 +03:00
|
|
|
private function _generateOrderedCollectionOrderByItems()
|
|
|
|
{
|
2013-06-05 07:50:43 +04:00
|
|
|
$orderedColumns = array();
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
foreach ($this->selectedClasses as $selectedClass) {
|
2013-06-05 07:50:43 +04:00
|
|
|
$dqlAlias = $selectedClass['dqlAlias'];
|
|
|
|
$qComp = $this->queryComponents[$dqlAlias];
|
|
|
|
$persister = $this->em->getUnitOfWork()->getEntityPersister($qComp['metadata']->name);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2013-06-05 07:50:43 +04:00
|
|
|
if ( ! isset($qComp['relation']['orderBy'])) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($qComp['relation']['orderBy'] as $fieldName => $orientation) {
|
2012-06-12 03:14:17 +04:00
|
|
|
$columnName = $this->quoteStrategy->getColumnName($fieldName, $qComp['metadata'], $this->platform);
|
2011-11-03 08:44:50 +04:00
|
|
|
$tableName = ($qComp['metadata']->isInheritanceTypeJoined())
|
2013-06-05 07:50:43 +04:00
|
|
|
? $persister->getOwningTable($fieldName)
|
2011-11-03 08:44:50 +04:00
|
|
|
: $qComp['metadata']->getTableName();
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2013-06-05 07:50:43 +04:00
|
|
|
$orderedColumn = $this->getSQLTableAlias($tableName, $dqlAlias) . '.' . $columnName;
|
|
|
|
|
|
|
|
// OrderByClause should replace an ordered relation. see - DDC-2475
|
|
|
|
if (isset($this->orderedColumnsMap[$orderedColumn])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->orderedColumnsMap[$orderedColumn] = $orientation;
|
|
|
|
$orderedColumns[] = $orderedColumn . ' ' . $orientation;
|
2010-02-21 03:06:34 +03:00
|
|
|
}
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2013-06-05 07:50:43 +04:00
|
|
|
return implode(', ', $orderedColumns);
|
2010-02-21 03:06:34 +03:00
|
|
|
}
|
|
|
|
|
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.
|
2012-12-13 20:36:14 +04:00
|
|
|
*
|
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
|
|
|
{
|
2011-11-03 08:44:50 +04:00
|
|
|
$sqlParts = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-07-20 07:51:01 +04:00
|
|
|
foreach ($dqlAliases as $dqlAlias) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$class = $this->queryComponents[$dqlAlias]['metadata'];
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
if ( ! $class->isInheritanceTypeSingleTable()) continue;
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$conn = $this->em->getConnection();
|
2011-11-03 08:44:50 +04:00
|
|
|
$values = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2013-04-06 18:11:52 +04:00
|
|
|
if ($class->discriminatorValue !== null) { // discriminators can be 0
|
2011-11-03 08:44:50 +04:00
|
|
|
$values[] = $conn->quote($class->discriminatorValue);
|
|
|
|
}
|
2010-07-20 07:51:01 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
foreach ($class->subClasses as $subclassName) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$values[] = $conn->quote($this->em->getClassMetadata($subclassName)->discriminatorValue);
|
2009-08-13 06:17:27 +04:00
|
|
|
}
|
2011-11-03 08:44:50 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$sqlParts[] = (($this->useSqlTableAliases) ? $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.' : '')
|
2011-11-03 08:44:50 +04:00
|
|
|
. $class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')';
|
2009-08-13 06:17:27 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
$sql = implode(' AND ', $sqlParts);
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
return (count($sqlParts) > 1) ? '(' . $sql . ')' : $sql;
|
2009-08-13 06:17:27 +04:00
|
|
|
}
|
2009-03-28 20:10:41 +03:00
|
|
|
|
2011-12-06 00:14:31 +04:00
|
|
|
/**
|
|
|
|
* Generates the filter SQL for a given entity and table alias.
|
|
|
|
*
|
2012-12-13 20:36:14 +04:00
|
|
|
* @param ClassMetadata $targetEntity Metadata of the target entity.
|
|
|
|
* @param string $targetTableAlias The table alias of the joined/selected table.
|
2011-12-06 00:14:31 +04:00
|
|
|
*
|
|
|
|
* @return string The SQL query part to add to a query.
|
|
|
|
*/
|
2011-08-16 14:03:47 +04:00
|
|
|
private function generateFilterConditionSQL(ClassMetadata $targetEntity, $targetTableAlias)
|
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
if (!$this->em->hasFilters()) {
|
2011-12-07 13:02:15 +04:00
|
|
|
return '';
|
|
|
|
}
|
2011-08-16 14:03:47 +04:00
|
|
|
|
2011-12-07 13:02:15 +04:00
|
|
|
switch($targetEntity->inheritanceType) {
|
|
|
|
case ClassMetadata::INHERITANCE_TYPE_NONE:
|
|
|
|
break;
|
|
|
|
case ClassMetadata::INHERITANCE_TYPE_JOINED:
|
|
|
|
// The classes in the inheritance will be added to the query one by one,
|
|
|
|
// but only the root node is getting filtered
|
|
|
|
if ($targetEntity->name !== $targetEntity->rootEntityName) {
|
|
|
|
return '';
|
2011-09-15 23:34:51 +04:00
|
|
|
}
|
2011-12-07 13:02:15 +04:00
|
|
|
break;
|
|
|
|
case ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE:
|
|
|
|
// With STI the table will only be queried once, make sure that the filters
|
|
|
|
// are added to the root entity
|
2012-06-01 01:03:01 +04:00
|
|
|
$targetEntity = $this->em->getClassMetadata($targetEntity->rootEntityName);
|
2011-12-07 13:02:15 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//@todo: throw exception?
|
|
|
|
return '';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$filterClauses = array();
|
2012-06-01 01:03:01 +04:00
|
|
|
foreach ($this->em->getFilters()->getEnabledFilters() as $filter) {
|
2011-12-07 13:02:15 +04:00
|
|
|
if ('' !== $filterExpr = $filter->addFilterConstraint($targetEntity, $targetTableAlias)) {
|
|
|
|
$filterClauses[] = '(' . $filterExpr . ')';
|
2011-08-16 14:03:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-06 01:05:42 +04:00
|
|
|
return implode(' AND ', $filterClauses);
|
2011-08-16 14:03:47 +04:00
|
|
|
}
|
2012-12-13 20:36:14 +04:00
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
public function walkSelectStatement(AST\SelectStatement $AST)
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2013-06-05 07:50:43 +04:00
|
|
|
$limit = $this->query->getMaxResults();
|
|
|
|
$offset = $this->query->getFirstResult();
|
|
|
|
$lockMode = $this->query->getHint(Query::HINT_LOCK_MODE);
|
|
|
|
$sql = $this->walkSelectClause($AST->selectClause)
|
|
|
|
. $this->walkFromClause($AST->fromClause)
|
|
|
|
. $this->walkWhereClause($AST->whereClause);
|
|
|
|
|
|
|
|
if ($AST->groupByClause) {
|
|
|
|
$sql .= $this->walkGroupByClause($AST->groupByClause);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($AST->havingClause) {
|
|
|
|
$sql .= $this->walkHavingClause($AST->havingClause);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($AST->orderByClause) {
|
|
|
|
$sql .= $this->walkOrderByClause($AST->orderByClause);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $AST->orderByClause && ($orderBySql = $this->_generateOrderedCollectionOrderByItems())) {
|
2011-09-07 08:48:19 +04:00
|
|
|
$sql .= ' ORDER BY ' . $orderBySql;
|
2010-02-21 03:06:34 +03:00
|
|
|
}
|
|
|
|
|
2013-06-05 07:50:43 +04:00
|
|
|
if ($limit !== null || $offset !== null) {
|
|
|
|
$sql = $this->platform->modifyLimitQuery($sql, $limit, $offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lockMode === false || $lockMode === LockMode::NONE) {
|
|
|
|
return $sql;
|
|
|
|
}
|
2009-07-10 21:53:48 +04:00
|
|
|
|
2013-06-05 07:50:43 +04:00
|
|
|
if ($lockMode === LockMode::PESSIMISTIC_READ) {
|
|
|
|
return $sql . ' ' . $this->platform->getReadLockSQL();
|
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2013-06-05 07:50:43 +04:00
|
|
|
if ($lockMode === LockMode::PESSIMISTIC_WRITE) {
|
|
|
|
return $sql . ' ' . $this->platform->getWriteLockSQL();
|
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2013-06-05 07:50:43 +04:00
|
|
|
if ($lockMode !== LockMode::OPTIMISTIC) {
|
|
|
|
throw QueryException::invalidLockMode();
|
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2013-06-05 07:50:43 +04:00
|
|
|
foreach ($this->selectedClasses as $selectedClass) {
|
|
|
|
if ( ! $selectedClass['class']->isVersioned) {
|
|
|
|
throw OptimisticLockException::lockFailed($selectedClass['class']->name);
|
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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-08-07 01:42:07 +04:00
|
|
|
*/
|
|
|
|
public function walkUpdateStatement(AST\UpdateStatement $AST)
|
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->useSqlTableAliases = false;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-12-04 00:19:21 +04:00
|
|
|
return $this->walkUpdateClause($AST->updateClause)
|
|
|
|
. $this->walkWhereClause($AST->whereClause);
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-08-07 01:42:07 +04:00
|
|
|
*/
|
|
|
|
public function walkDeleteStatement(AST\DeleteStatement $AST)
|
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->useSqlTableAliases = false;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-12-04 00:19:21 +04:00
|
|
|
return $this->walkDeleteClause($AST->deleteClause)
|
|
|
|
. $this->walkWhereClause($AST->whereClause);
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-12-04 00:19:21 +04:00
|
|
|
/**
|
|
|
|
* Walks down an IdentificationVariable AST node, thereby generating the appropriate SQL.
|
|
|
|
* This one differs of ->walkIdentificationVariable() because it generates the entity identifiers.
|
|
|
|
*
|
|
|
|
* @param string $identVariable
|
2012-12-13 20:36:14 +04:00
|
|
|
*
|
2011-12-04 00:19:21 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function walkEntityIdentificationVariable($identVariable)
|
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
$class = $this->queryComponents[$identVariable]['metadata'];
|
2011-12-04 00:19:21 +04:00
|
|
|
$tableAlias = $this->getSQLTableAlias($class->getTableName(), $identVariable);
|
|
|
|
$sqlParts = array();
|
|
|
|
|
2012-06-12 03:14:17 +04:00
|
|
|
foreach ($this->quoteStrategy->getIdentifierColumnNames($class, $this->platform) as $columnName) {
|
2011-12-04 00:19:21 +04:00
|
|
|
$sqlParts[] = $tableAlias . '.' . $columnName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(', ', $sqlParts);
|
|
|
|
}
|
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
|
2010-07-30 08:30:02 +04:00
|
|
|
* @param string $fieldName
|
2012-12-13 20:36:14 +04:00
|
|
|
*
|
2009-08-07 01:42:07 +04:00
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
2010-07-30 08:30:02 +04:00
|
|
|
public function walkIdentificationVariable($identificationVariable, $fieldName = null)
|
2009-08-07 01:42:07 +04:00
|
|
|
{
|
2012-06-01 01:03:01 +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'])
|
|
|
|
) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$class = $this->em->getClassMetadata($class->fieldMappings[$fieldName]['inherited']);
|
2009-08-07 01:42:07 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-09-07 08:48:19 +04:00
|
|
|
return $this->getSQLTableAlias($class->getTableName(), $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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-08-13 06:17:27 +04:00
|
|
|
*/
|
|
|
|
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;
|
2012-06-01 01:03:01 +04:00
|
|
|
$class = $this->queryComponents[$dqlAlias]['metadata'];
|
2009-08-13 06:17:27 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
if ($this->useSqlTableAliases) {
|
2011-11-21 18:08:36 +04:00
|
|
|
$sql .= $this->walkIdentificationVariable($dqlAlias, $fieldName) . '.';
|
2011-11-19 03:35:29 +04:00
|
|
|
}
|
|
|
|
|
2012-06-12 03:14:17 +04:00
|
|
|
$sql .= $this->quoteStrategy->getColumnName($fieldName, $class, $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;
|
2012-06-01 01:03:01 +04:00
|
|
|
$class = $this->queryComponents[$dqlAlias]['metadata'];
|
2010-04-13 07:29:29 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if (isset($class->associationMappings[$fieldName]['inherited'])) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$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
|
|
|
|
2011-08-14 07:46:02 +04:00
|
|
|
if ( ! $assoc['isOwningSide']) {
|
2010-02-20 00:28:17 +03:00
|
|
|
throw QueryException::associationPathInverseSideNotSupported();
|
2009-12-27 06:26:15 +03:00
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-14 07:46:02 +04:00
|
|
|
// COMPOSITE KEYS NOT (YET?) SUPPORTED
|
|
|
|
if (count($assoc['sourceToTargetKeyColumns']) > 1) {
|
|
|
|
throw QueryException::associationPathCompositeKeyNotSupported();
|
|
|
|
}
|
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
if ($this->useSqlTableAliases) {
|
2011-09-07 08:48:19 +04:00
|
|
|
$sql .= $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.';
|
2011-08-14 07:46:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= reset($assoc['targetToSourceKeyColumns']);
|
2010-02-20 00:28:17 +03:00
|
|
|
break;
|
2011-10-30 15:12:18 +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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-01-19 21:40:12 +03:00
|
|
|
public function walkSelectClause($selectClause)
|
|
|
|
{
|
2011-08-15 19:47:17 +04:00
|
|
|
$sql = 'SELECT ' . (($selectClause->isDistinct) ? 'DISTINCT ' : '');
|
|
|
|
$sqlSelectExpressions = array_filter(array_map(array($this, 'walkSelectExpression'), $selectClause->selectExpressions));
|
2010-02-20 21:27:05 +03:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
if ($this->query->getHint(Query::HINT_INTERNAL_ITERATION) == true && $selectClause->isDistinct) {
|
|
|
|
$this->query->setHint(self::HINT_DISTINCT, true);
|
2012-01-25 01:51:21 +04:00
|
|
|
}
|
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$addMetaColumns = ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD) &&
|
|
|
|
$this->query->getHydrationMode() == Query::HYDRATE_OBJECT
|
2009-08-11 14:51:38 +04:00
|
|
|
||
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->query->getHydrationMode() != Query::HYDRATE_OBJECT &&
|
|
|
|
$this->query->getHint(Query::HINT_INCLUDE_META_COLUMNS);
|
2009-08-11 14:51:38 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
foreach ($this->selectedClasses as $selectedClass) {
|
2011-11-15 07:09:48 +04:00
|
|
|
$class = $selectedClass['class'];
|
|
|
|
$dqlAlias = $selectedClass['dqlAlias'];
|
|
|
|
$resultAlias = $selectedClass['resultAlias'];
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
// Register as entity or joined entity result
|
2012-06-01 01:03:01 +04:00
|
|
|
if ($this->queryComponents[$dqlAlias]['relation'] === null) {
|
|
|
|
$this->rsm->addEntityResult($class->name, $dqlAlias, $resultAlias);
|
2009-04-09 22:12:48 +04:00
|
|
|
} else {
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rsm->addJoinedEntityResult(
|
2011-11-14 07:36:39 +04:00
|
|
|
$class->name,
|
|
|
|
$dqlAlias,
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->queryComponents[$dqlAlias]['parent'],
|
|
|
|
$this->queryComponents[$dqlAlias]['relation']['fieldName']
|
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
|
2012-06-01 01:03:01 +04:00
|
|
|
$rootClass = $this->em->getClassMetadata($class->rootEntityName);
|
2011-11-03 08:44:50 +04:00
|
|
|
$tblAlias = $this->getSQLTableAlias($rootClass->getTableName(), $dqlAlias);
|
2009-05-21 12:53:40 +04:00
|
|
|
$discrColumn = $rootClass->discriminatorColumn;
|
2011-02-20 00:50:58 +03:00
|
|
|
$columnAlias = $this->getSQLColumnAlias($discrColumn['name']);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-15 19:47:17 +04:00
|
|
|
$sqlSelectExpressions[] = $tblAlias . '.' . $discrColumn['name'] . ' AS ' . $columnAlias;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rsm->setDiscriminatorColumn($dqlAlias, $columnAlias);
|
|
|
|
$this->rsm->addMetaResult($dqlAlias, $columnAlias, $discrColumn['fieldName']);
|
2011-11-03 08:44:50 +04:00
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
// Add foreign key columns to SQL, if necessary
|
2012-02-20 20:48:34 +04:00
|
|
|
if ( ! $addMetaColumns && ! $class->containsForeignIdentifier) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
// Add foreign key columns of class and also parent classes
|
|
|
|
foreach ($class->associationMappings as $assoc) {
|
2012-02-20 20:48:34 +04:00
|
|
|
if ( ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE)) {
|
|
|
|
continue;
|
|
|
|
} else if ( !$addMetaColumns && !isset($assoc['id'])) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$owningClass = (isset($assoc['inherited'])) ? $this->em->getClassMetadata($assoc['inherited']) : $class;
|
2011-11-03 08:44:50 +04:00
|
|
|
$sqlTableAlias = $this->getSQLTableAlias($owningClass->getTableName(), $dqlAlias);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
|
|
|
|
$columnAlias = $this->getSQLColumnAlias($srcColumn);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
$sqlSelectExpressions[] = $sqlTableAlias . '.' . $srcColumn . ' AS ' . $columnAlias;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rsm->addMetaResult($dqlAlias, $columnAlias, $srcColumn, (isset($assoc['id']) && $assoc['id'] === true));
|
2009-08-11 14:51:38 +04:00
|
|
|
}
|
2011-11-03 08:44:50 +04:00
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2012-02-20 20:48:34 +04:00
|
|
|
// Add foreign key columns to SQL, if necessary
|
|
|
|
if ( ! $addMetaColumns) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
// Add foreign key columns of subclasses
|
|
|
|
foreach ($class->subClasses as $subClassName) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$subClass = $this->em->getClassMetadata($subClassName);
|
2011-11-03 08:44:50 +04:00
|
|
|
$sqlTableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
|
|
|
|
|
|
|
|
foreach ($subClass->associationMappings as $assoc) {
|
|
|
|
// Skip if association is inherited
|
|
|
|
if (isset($assoc['inherited'])) continue;
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
if ( ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE)) continue;
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
|
|
|
|
$columnAlias = $this->getSQLColumnAlias($srcColumn);
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
$sqlSelectExpressions[] = $sqlTableAlias . '.' . $srcColumn . ' AS ' . $columnAlias;
|
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rsm->addMetaResult($dqlAlias, $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
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-15 19:47:17 +04:00
|
|
|
$sql .= implode(', ', $sqlSelectExpressions);
|
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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
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) {
|
2013-02-21 22:02:21 +04:00
|
|
|
$sql = $this->platform->appendLockHint(
|
|
|
|
$this->walkRangeVariableDeclaration($identificationVariableDecl->rangeVariableDeclaration),
|
|
|
|
$this->query->getHint(Query::HINT_LOCK_MODE)
|
|
|
|
);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-06-11 00:14:25 +04:00
|
|
|
foreach ($identificationVariableDecl->joins as $join) {
|
|
|
|
$sql .= $this->walkJoin($join);
|
2010-07-20 07:51:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($identificationVariableDecl->indexBy) {
|
2011-10-16 21:23:20 +04:00
|
|
|
$alias = $identificationVariableDecl->indexBy->simpleStateFieldPathExpression->identificationVariable;
|
|
|
|
$field = $identificationVariableDecl->indexBy->simpleStateFieldPathExpression->field;
|
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
if (isset($this->scalarFields[$alias][$field])) {
|
|
|
|
$this->rsm->addIndexByScalar($this->scalarFields[$alias][$field]);
|
2011-10-16 21:23:20 +04:00
|
|
|
} else {
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rsm->addIndexBy(
|
2011-10-16 21:23:20 +04:00
|
|
|
$identificationVariableDecl->indexBy->simpleStateFieldPathExpression->identificationVariable,
|
|
|
|
$identificationVariableDecl->indexBy->simpleStateFieldPathExpression->field
|
|
|
|
);
|
|
|
|
}
|
2010-07-20 07:51:01 +04:00
|
|
|
}
|
|
|
|
|
2013-02-21 22:02:21 +04:00
|
|
|
$sqlParts[] = $sql;
|
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
|
|
|
}
|
|
|
|
|
2012-06-11 00:14:25 +04:00
|
|
|
/**
|
|
|
|
* Walks down a RangeVariableDeclaration AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
2012-12-13 20:36:14 +04:00
|
|
|
* @param AST\RangeVariableDeclaration $rangeVariableDeclaration
|
|
|
|
*
|
2012-06-11 00:14:25 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function walkRangeVariableDeclaration($rangeVariableDeclaration)
|
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
$class = $this->em->getClassMetadata($rangeVariableDeclaration->abstractSchemaName);
|
2012-06-11 00:14:25 +04:00
|
|
|
$dqlAlias = $rangeVariableDeclaration->aliasIdentificationVariable;
|
|
|
|
|
2013-08-16 08:07:06 +04:00
|
|
|
if ($rangeVariableDeclaration->isRoot) {
|
|
|
|
$this->rootAliases[] = $dqlAlias;
|
|
|
|
}
|
2012-06-11 00:14:25 +04:00
|
|
|
|
2013-06-07 02:08:22 +04:00
|
|
|
$sql = $this->quoteStrategy->getTableName($class,$this->platform) . ' '
|
|
|
|
. $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
|
2012-06-11 00:14:25 +04:00
|
|
|
|
|
|
|
if ($class->isInheritanceTypeJoined()) {
|
|
|
|
$sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Walks down a JoinAssociationDeclaration AST node, thereby generating the appropriate SQL.
|
|
|
|
*
|
2012-12-13 20:36:14 +04:00
|
|
|
* @param AST\JoinAssociationDeclaration $joinAssociationDeclaration
|
2013-08-13 09:07:34 +04:00
|
|
|
* @param int $joinType
|
|
|
|
* @param AST\ConditionalExpression $condExpr
|
2012-12-13 20:36:14 +04:00
|
|
|
*
|
2012-06-11 00:14:25 +04:00
|
|
|
* @return string
|
2012-12-13 20:36:14 +04:00
|
|
|
*
|
|
|
|
* @throws QueryException
|
2012-06-11 00:14:25 +04:00
|
|
|
*/
|
2013-08-13 09:07:34 +04:00
|
|
|
public function walkJoinAssociationDeclaration($joinAssociationDeclaration, $joinType = AST\Join::JOIN_TYPE_INNER, $condExpr = null)
|
2012-06-11 00:14:25 +04:00
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
|
|
|
|
$associationPathExpression = $joinAssociationDeclaration->joinAssociationPathExpression;
|
|
|
|
$joinedDqlAlias = $joinAssociationDeclaration->aliasIdentificationVariable;
|
|
|
|
$indexBy = $joinAssociationDeclaration->indexBy;
|
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$relation = $this->queryComponents[$joinedDqlAlias]['relation'];
|
|
|
|
$targetClass = $this->em->getClassMetadata($relation['targetEntity']);
|
|
|
|
$sourceClass = $this->em->getClassMetadata($relation['sourceEntity']);
|
2013-06-07 19:49:49 +04:00
|
|
|
$targetTableName = $this->quoteStrategy->getTableName($targetClass,$this->platform);
|
2012-06-11 00:14:25 +04:00
|
|
|
|
|
|
|
$targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName(), $joinedDqlAlias);
|
|
|
|
$sourceTableAlias = $this->getSQLTableAlias($sourceClass->getTableName(), $associationPathExpression->identificationVariable);
|
|
|
|
|
|
|
|
// Ensure we got the owning side, since it has all mapping info
|
|
|
|
$assoc = ( ! $relation['isOwningSide']) ? $targetClass->associationMappings[$relation['mappedBy']] : $relation;
|
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
if ($this->query->getHint(Query::HINT_INTERNAL_ITERATION) == true && (!$this->query->getHint(self::HINT_DISTINCT) || isset($this->selectedClasses[$joinedDqlAlias]))) {
|
2012-06-11 00:14:25 +04:00
|
|
|
if ($relation['type'] == ClassMetadata::ONE_TO_MANY || $relation['type'] == ClassMetadata::MANY_TO_MANY) {
|
|
|
|
throw QueryException::iterateWithFetchJoinNotAllowed($assoc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This condition is not checking ClassMetadata::MANY_TO_ONE, because by definition it cannot
|
|
|
|
// be the owning side and previously we ensured that $assoc is always the owning side of the associations.
|
|
|
|
// The owning side is necessary at this point because only it contains the JoinColumn information.
|
|
|
|
switch (true) {
|
|
|
|
case ($assoc['type'] & ClassMetadata::TO_ONE):
|
|
|
|
$conditions = array();
|
|
|
|
|
2012-06-12 02:10:38 +04:00
|
|
|
foreach ($assoc['joinColumns'] as $joinColumn) {
|
|
|
|
$quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
|
|
|
|
$quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
|
2012-06-11 00:14:25 +04:00
|
|
|
|
2012-06-12 02:10:38 +04:00
|
|
|
if ($relation['isOwningSide']) {
|
|
|
|
$conditions[] = $sourceTableAlias . '.' . $quotedSourceColumn . ' = ' . $targetTableAlias . '.' . $quotedTargetColumn;
|
2012-06-11 00:14:25 +04:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-06-12 02:10:38 +04:00
|
|
|
$conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $targetTableAlias . '.' . $quotedSourceColumn;
|
2012-06-11 00:14:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply remaining inheritance restrictions
|
|
|
|
$discrSql = $this->_generateDiscriminatorColumnConditionSQL(array($joinedDqlAlias));
|
|
|
|
|
|
|
|
if ($discrSql) {
|
|
|
|
$conditions[] = $discrSql;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the filters
|
|
|
|
$filterExpr = $this->generateFilterConditionSQL($targetClass, $targetTableAlias);
|
|
|
|
|
|
|
|
if ($filterExpr) {
|
|
|
|
$conditions[] = $filterExpr;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ' . implode(' AND ', $conditions);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ($assoc['type'] == ClassMetadata::MANY_TO_MANY):
|
|
|
|
// Join relation table
|
|
|
|
$joinTable = $assoc['joinTable'];
|
|
|
|
$joinTableAlias = $this->getSQLTableAlias($joinTable['name'], $joinedDqlAlias);
|
2013-06-07 19:56:58 +04:00
|
|
|
$joinTableName = $this->quoteStrategy->getJoinTableName($assoc, $sourceClass, $this->platform);
|
2012-06-11 00:14:25 +04:00
|
|
|
|
|
|
|
$conditions = array();
|
|
|
|
$relationColumns = ($relation['isOwningSide'])
|
2012-06-12 02:10:38 +04:00
|
|
|
? $assoc['joinTable']['joinColumns']
|
|
|
|
: $assoc['joinTable']['inverseJoinColumns'];
|
2012-06-11 00:14:25 +04:00
|
|
|
|
2012-06-12 02:10:38 +04:00
|
|
|
foreach ($relationColumns as $joinColumn) {
|
|
|
|
$quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
|
|
|
|
$quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
|
2012-06-11 00:14:25 +04:00
|
|
|
|
2012-06-12 02:10:38 +04:00
|
|
|
$conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $quotedSourceColumn;
|
2012-06-11 00:14:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= $joinTableName . ' ' . $joinTableAlias . ' ON ' . implode(' AND ', $conditions);
|
|
|
|
|
|
|
|
// Join target table
|
|
|
|
$sql .= ($joinType == AST\Join::JOIN_TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE_LEFTOUTER) ? ' LEFT JOIN ' : ' INNER JOIN ';
|
|
|
|
|
|
|
|
$conditions = array();
|
|
|
|
$relationColumns = ($relation['isOwningSide'])
|
2012-06-12 02:10:38 +04:00
|
|
|
? $assoc['joinTable']['inverseJoinColumns']
|
|
|
|
: $assoc['joinTable']['joinColumns'];
|
2012-06-11 00:14:25 +04:00
|
|
|
|
2012-06-12 02:10:38 +04:00
|
|
|
foreach ($relationColumns as $joinColumn) {
|
|
|
|
$quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
|
|
|
|
$quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
|
2012-06-11 00:14:25 +04:00
|
|
|
|
2012-06-12 02:10:38 +04:00
|
|
|
$conditions[] = $targetTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableAlias . '.' . $quotedSourceColumn;
|
2012-06-11 00:14:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply remaining inheritance restrictions
|
|
|
|
$discrSql = $this->_generateDiscriminatorColumnConditionSQL(array($joinedDqlAlias));
|
|
|
|
|
|
|
|
if ($discrSql) {
|
|
|
|
$conditions[] = $discrSql;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the filters
|
|
|
|
$filterExpr = $this->generateFilterConditionSQL($targetClass, $targetTableAlias);
|
|
|
|
|
|
|
|
if ($filterExpr) {
|
|
|
|
$conditions[] = $filterExpr;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ' . implode(' AND ', $conditions);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-13 09:07:34 +04:00
|
|
|
// Handle WITH clause
|
|
|
|
if ($condExpr !== null) {
|
|
|
|
// Phase 2 AST optimization: Skip processing of ConditionalExpression
|
|
|
|
// if only one ConditionalTerm is defined
|
|
|
|
$sql .= ' AND (' . $this->walkConditionalExpression($condExpr) . ')';
|
|
|
|
}
|
|
|
|
|
2012-06-11 00:14:25 +04:00
|
|
|
// FIXME: these should either be nested or all forced to be left joins (DDC-XXX)
|
|
|
|
if ($targetClass->isInheritanceTypeJoined()) {
|
|
|
|
$sql .= $this->_generateClassTableInheritanceJoins($targetClass, $joinedDqlAlias);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the indexes
|
|
|
|
if ($indexBy) {
|
|
|
|
// For Many-To-One or One-To-One associations this obviously makes no sense, but is ignored silently.
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rsm->addIndexBy(
|
2012-06-11 00:14:25 +04:00
|
|
|
$indexBy->simpleStateFieldPathExpression->identificationVariable,
|
|
|
|
$indexBy->simpleStateFieldPathExpression->field
|
|
|
|
);
|
|
|
|
} else if (isset($relation['indexBy'])) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rsm->addIndexBy($joinedDqlAlias, $relation['indexBy']);
|
2012-06-11 00:14:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-03-23 20:39:33 +03:00
|
|
|
public function walkFunction($function)
|
|
|
|
{
|
|
|
|
return $function->getSql($this);
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
|
|
|
public function walkOrderByClause($orderByClause)
|
|
|
|
{
|
2011-11-03 08:44:50 +04:00
|
|
|
$orderByItems = array_map(array($this, 'walkOrderByItem'), $orderByClause->orderByItems);
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
if (($collectionOrderByItems = $this->_generateOrderedCollectionOrderByItems()) !== '') {
|
|
|
|
$orderByItems = array_merge($orderByItems, (array) $collectionOrderByItems);
|
2010-02-21 03:06:34 +03:00
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
return ' ORDER BY ' . implode(', ', $orderByItems);
|
2009-03-28 20:10:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
|
|
|
public function walkOrderByItem($orderByItem)
|
|
|
|
{
|
2013-06-05 07:50:43 +04:00
|
|
|
$type = strtoupper($orderByItem->type);
|
2009-08-07 01:42:07 +04:00
|
|
|
$expr = $orderByItem->expression;
|
2012-02-16 03:16:58 +04:00
|
|
|
$sql = ($expr instanceof AST\Node)
|
|
|
|
? $expr->dispatch($this)
|
2012-06-01 01:03:01 +04:00
|
|
|
: $this->walkResultVariable($this->queryComponents[$expr]['token']['value']);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2013-06-05 07:50:43 +04:00
|
|
|
$this->orderedColumnsMap[$sql] = $type;
|
|
|
|
|
|
|
|
return $sql . ' ' . $type;
|
2009-03-28 20:10:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-01-19 21:40:12 +03:00
|
|
|
*/
|
2012-06-11 00:14:25 +04:00
|
|
|
public function walkJoin($join)
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2012-06-11 00:14:25 +04:00
|
|
|
$joinType = $join->joinType;
|
|
|
|
$joinDeclaration = $join->joinAssociationDeclaration;
|
|
|
|
|
|
|
|
$sql = ($joinType == AST\Join::JOIN_TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE_LEFTOUTER)
|
2011-11-03 08:44:50 +04:00
|
|
|
? ' LEFT JOIN '
|
|
|
|
: ' INNER JOIN ';
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2012-06-11 00:14:25 +04:00
|
|
|
switch (true) {
|
|
|
|
case ($joinDeclaration instanceof \Doctrine\ORM\Query\AST\RangeVariableDeclaration):
|
2013-08-16 08:07:06 +04:00
|
|
|
$class = $this->em->getClassMetadata($joinDeclaration->abstractSchemaName);
|
|
|
|
$dqlAlias = $joinDeclaration->aliasIdentificationVariable;
|
|
|
|
$tableAlias = $this->getSQLTableAlias($class->table['name'], $dqlAlias);
|
|
|
|
$condition = '(' . $this->walkConditionalExpression($join->conditionalExpression) . ')';
|
|
|
|
$condExprConjunction = ($class->isInheritanceTypeJoined() && $joinType != AST\Join::JOIN_TYPE_LEFT && $joinType != AST\Join::JOIN_TYPE_LEFTOUTER)
|
2012-11-05 13:19:25 +04:00
|
|
|
? ' AND '
|
|
|
|
: ' ON ';
|
|
|
|
|
2013-08-16 08:07:06 +04:00
|
|
|
$sql .= $this->walkRangeVariableDeclaration($joinDeclaration);
|
|
|
|
|
|
|
|
$conditions = array($condition);
|
|
|
|
|
|
|
|
// Apply remaining inheritance restrictions
|
|
|
|
$discrSql = $this->_generateDiscriminatorColumnConditionSQL(array($dqlAlias));
|
|
|
|
|
|
|
|
if ($discrSql) {
|
|
|
|
$conditions[] = $discrSql;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the filters
|
|
|
|
$filterExpr = $this->generateFilterConditionSQL($class, $tableAlias);
|
|
|
|
|
|
|
|
if ($filterExpr) {
|
|
|
|
$conditions[] = $filterExpr;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= $condExprConjunction . implode(' AND ', $conditions);
|
2012-06-11 00:14:25 +04:00
|
|
|
break;
|
2011-01-01 20:17:19 +03:00
|
|
|
|
2012-06-11 00:14:25 +04:00
|
|
|
case ($joinDeclaration instanceof \Doctrine\ORM\Query\AST\JoinAssociationDeclaration):
|
2013-08-13 09:07:34 +04:00
|
|
|
$sql .= $this->walkJoinAssociationDeclaration($joinDeclaration, $joinType, $join->conditionalExpression);
|
2012-06-11 00:14:25 +04:00
|
|
|
break;
|
2012-06-18 22:31:20 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
/**
|
|
|
|
* Walks down a CaseExpression AST node and generates the corresponding SQL.
|
|
|
|
*
|
2012-12-13 20:36:14 +04:00
|
|
|
* @param AST\CoalesceExpression|AST\NullIfExpression|AST\GeneralCaseExpression|AST\SimpleCaseExpression $expression
|
|
|
|
*
|
2011-08-08 09:09:25 +04:00
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkCaseExpression($expression)
|
|
|
|
{
|
|
|
|
switch (true) {
|
|
|
|
case ($expression instanceof AST\CoalesceExpression):
|
|
|
|
return $this->walkCoalesceExpression($expression);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
case ($expression instanceof AST\NullIfExpression):
|
|
|
|
return $this->walkNullIfExpression($expression);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
case ($expression instanceof AST\GeneralCaseExpression):
|
|
|
|
return $this->walkGeneralCaseExpression($expression);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
case ($expression instanceof AST\SimpleCaseExpression):
|
|
|
|
return $this->walkSimpleCaseExpression($expression);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
default:
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-06-17 23:16:22 +04:00
|
|
|
/**
|
|
|
|
* Walks down a CoalesceExpression AST node and generates the corresponding SQL.
|
|
|
|
*
|
2012-12-13 20:36:14 +04:00
|
|
|
* @param AST\CoalesceExpression $coalesceExpression
|
|
|
|
*
|
2011-06-17 23:16:22 +04:00
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkCoalesceExpression($coalesceExpression)
|
|
|
|
{
|
2011-12-19 19:31:26 +04:00
|
|
|
$sql = 'COALESCE(';
|
|
|
|
|
2011-06-17 23:16:22 +04:00
|
|
|
$scalarExpressions = array();
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-06-17 23:16:22 +04:00
|
|
|
foreach ($coalesceExpression->scalarExpressions as $scalarExpression) {
|
|
|
|
$scalarExpressions[] = $this->walkSimpleArithmeticExpression($scalarExpression);
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-12-19 19:31:26 +04:00
|
|
|
$sql .= implode(', ', $scalarExpressions) . ')';
|
|
|
|
|
|
|
|
return $sql;
|
2011-06-17 23:16:22 +04:00
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-06-17 23:16:22 +04:00
|
|
|
/**
|
|
|
|
* Walks down a NullIfExpression AST node and generates the corresponding SQL.
|
|
|
|
*
|
2012-12-13 20:36:14 +04:00
|
|
|
* @param AST\NullIfExpression $nullIfExpression
|
|
|
|
*
|
2011-06-17 23:16:22 +04:00
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkNullIfExpression($nullIfExpression)
|
|
|
|
{
|
2011-10-30 15:12:18 +04:00
|
|
|
$firstExpression = is_string($nullIfExpression->firstExpression)
|
2012-06-01 01:03:01 +04:00
|
|
|
? $this->conn->quote($nullIfExpression->firstExpression)
|
2011-06-17 23:16:22 +04:00
|
|
|
: $this->walkSimpleArithmeticExpression($nullIfExpression->firstExpression);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
|
|
|
$secondExpression = is_string($nullIfExpression->secondExpression)
|
2012-06-01 01:03:01 +04:00
|
|
|
? $this->conn->quote($nullIfExpression->secondExpression)
|
2011-06-17 23:16:22 +04:00
|
|
|
: $this->walkSimpleArithmeticExpression($nullIfExpression->secondExpression);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-06-17 23:16:22 +04:00
|
|
|
return 'NULLIF(' . $firstExpression . ', ' . $secondExpression . ')';
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
/**
|
|
|
|
* Walks down a GeneralCaseExpression AST node and generates the corresponding SQL.
|
|
|
|
*
|
2012-12-13 20:36:14 +04:00
|
|
|
* @param AST\GeneralCaseExpression $generalCaseExpression
|
|
|
|
*
|
2011-08-08 09:09:25 +04:00
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkGeneralCaseExpression(AST\GeneralCaseExpression $generalCaseExpression)
|
|
|
|
{
|
|
|
|
$sql = 'CASE';
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
foreach ($generalCaseExpression->whenClauses as $whenClause) {
|
|
|
|
$sql .= ' WHEN ' . $this->walkConditionalExpression($whenClause->caseConditionExpression);
|
|
|
|
$sql .= ' THEN ' . $this->walkSimpleArithmeticExpression($whenClause->thenScalarExpression);
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
$sql .= ' ELSE ' . $this->walkSimpleArithmeticExpression($generalCaseExpression->elseScalarExpression) . ' END';
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
return $sql;
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
/**
|
|
|
|
* Walks down a SimpleCaseExpression AST node and generates the corresponding SQL.
|
|
|
|
*
|
2012-12-13 20:36:14 +04:00
|
|
|
* @param AST\SimpleCaseExpression $simpleCaseExpression
|
|
|
|
*
|
2011-08-08 09:09:25 +04:00
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkSimpleCaseExpression($simpleCaseExpression)
|
|
|
|
{
|
|
|
|
$sql = 'CASE ' . $this->walkStateFieldPathExpression($simpleCaseExpression->caseOperand);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
foreach ($simpleCaseExpression->simpleWhenClauses as $simpleWhenClause) {
|
|
|
|
$sql .= ' WHEN ' . $this->walkSimpleArithmeticExpression($simpleWhenClause->caseScalarExpression);
|
|
|
|
$sql .= ' THEN ' . $this->walkSimpleArithmeticExpression($simpleWhenClause->thenScalarExpression);
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
$sql .= ' ELSE ' . $this->walkSimpleArithmeticExpression($simpleCaseExpression->elseScalarExpression) . ' END';
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
return $sql;
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-01-19 21:40:12 +03:00
|
|
|
*/
|
|
|
|
public function walkSelectExpression($selectExpression)
|
|
|
|
{
|
2011-10-03 09:07:07 +04:00
|
|
|
$sql = '';
|
|
|
|
$expr = $selectExpression->expression;
|
|
|
|
$hidden = $selectExpression->hiddenAliasResultVariable;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
switch (true) {
|
|
|
|
case ($expr instanceof AST\PathExpression):
|
|
|
|
if ($expr->type !== AST\PathExpression::TYPE_STATE_FIELD) {
|
2012-11-06 02:45:57 +04:00
|
|
|
throw QueryException::invalidPathExpression($expr);
|
2011-11-03 08:44:50 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
$fieldName = $expr->field;
|
|
|
|
$dqlAlias = $expr->identificationVariable;
|
2012-06-01 01:03:01 +04:00
|
|
|
$qComp = $this->queryComponents[$dqlAlias];
|
2011-11-03 08:44:50 +04:00
|
|
|
$class = $qComp['metadata'];
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
$resultAlias = $selectExpression->fieldIdentificationVariable ?: $fieldName;
|
|
|
|
$tableName = ($class->isInheritanceTypeJoined())
|
2012-06-01 01:03:01 +04:00
|
|
|
? $this->em->getUnitOfWork()->getEntityPersister($class->name)->getOwningTable($fieldName)
|
2011-11-03 08:44:50 +04:00
|
|
|
: $class->getTableName();
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
$sqlTableAlias = $this->getSQLTableAlias($tableName, $dqlAlias);
|
2012-06-12 03:14:17 +04:00
|
|
|
$columnName = $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform);
|
2012-03-12 02:27:15 +04:00
|
|
|
$columnAlias = $this->getSQLColumnAlias($class->fieldMappings[$fieldName]['columnName']);
|
2011-11-19 03:35:29 +04:00
|
|
|
|
|
|
|
$col = $sqlTableAlias . '.' . $columnName;
|
|
|
|
|
2012-01-14 05:46:59 +04:00
|
|
|
$fieldType = $class->getTypeOfField($fieldName);
|
|
|
|
|
2011-11-20 22:50:51 +04:00
|
|
|
if (isset($class->fieldMappings[$fieldName]['requireSQLConversion'])) {
|
2012-01-14 05:46:59 +04:00
|
|
|
$type = Type::getType($fieldType);
|
2012-06-01 01:03:01 +04:00
|
|
|
$col = $type->convertToPHPValueSQL($col, $this->conn->getDatabasePlatform());
|
2011-11-19 03:35:29 +04:00
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-19 03:35:29 +04:00
|
|
|
$sql .= $col . ' AS ' . $columnAlias;
|
2011-11-03 08:44:50 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->scalarResultAliasMap[$resultAlias] = $columnAlias;
|
2011-12-02 08:52:35 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
if ( ! $hidden) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldType);
|
|
|
|
$this->scalarFields[$dqlAlias][$fieldName] = $columnAlias;
|
2011-11-03 08:44:50 +04:00
|
|
|
}
|
|
|
|
break;
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
case ($expr instanceof AST\AggregateExpression):
|
|
|
|
case ($expr instanceof AST\Functions\FunctionNode):
|
|
|
|
case ($expr instanceof AST\SimpleArithmeticExpression):
|
|
|
|
case ($expr instanceof AST\ArithmeticTerm):
|
|
|
|
case ($expr instanceof AST\ArithmeticFactor):
|
2013-03-16 21:33:19 +04:00
|
|
|
case ($expr instanceof AST\ParenthesisExpression):
|
2011-11-03 08:44:50 +04:00
|
|
|
case ($expr instanceof AST\Literal):
|
|
|
|
case ($expr instanceof AST\NullIfExpression):
|
|
|
|
case ($expr instanceof AST\CoalesceExpression):
|
|
|
|
case ($expr instanceof AST\GeneralCaseExpression):
|
|
|
|
case ($expr instanceof AST\SimpleCaseExpression):
|
|
|
|
$columnAlias = $this->getSQLColumnAlias('sclr');
|
2012-06-01 01:03:01 +04:00
|
|
|
$resultAlias = $selectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
$sql .= $expr->dispatch($this) . ' AS ' . $columnAlias;
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->scalarResultAliasMap[$resultAlias] = $columnAlias;
|
2011-11-03 08:44:50 +04:00
|
|
|
|
|
|
|
if ( ! $hidden) {
|
2012-01-14 05:46:59 +04:00
|
|
|
// We cannot resolve field type here; assume 'string'.
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rsm->addScalarResult($columnAlias, $resultAlias, 'string');
|
2011-11-03 08:44:50 +04:00
|
|
|
}
|
|
|
|
break;
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
case ($expr instanceof AST\Subselect):
|
|
|
|
$columnAlias = $this->getSQLColumnAlias('sclr');
|
2012-06-01 01:03:01 +04:00
|
|
|
$resultAlias = $selectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
|
2011-11-14 07:36:39 +04:00
|
|
|
|
|
|
|
$sql .= '(' . $this->walkSubselect($expr) . ') AS ' . $columnAlias;
|
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->scalarResultAliasMap[$resultAlias] = $columnAlias;
|
2011-11-03 08:44:50 +04:00
|
|
|
|
|
|
|
if ( ! $hidden) {
|
2012-01-14 05:46:59 +04:00
|
|
|
// We cannot resolve field type here; assume 'string'.
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rsm->addScalarResult($columnAlias, $resultAlias, 'string');
|
2011-11-03 08:44:50 +04:00
|
|
|
}
|
|
|
|
break;
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2012-01-03 16:31:54 +04:00
|
|
|
case ($expr instanceof AST\NewObjectExpression):
|
2012-07-01 02:25:54 +04:00
|
|
|
$sql .= $this->walkNewObject($expr);
|
2012-01-03 16:31:54 +04:00
|
|
|
break;
|
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
default:
|
|
|
|
// IdentificationVariable or PartialObjectExpression
|
|
|
|
if ($expr instanceof AST\PartialObjectExpression) {
|
|
|
|
$dqlAlias = $expr->identificationVariable;
|
|
|
|
$partialFieldSet = $expr->partialFieldSet;
|
|
|
|
} else {
|
|
|
|
$dqlAlias = $expr;
|
|
|
|
$partialFieldSet = array();
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$queryComp = $this->queryComponents[$dqlAlias];
|
2011-11-14 07:36:39 +04:00
|
|
|
$class = $queryComp['metadata'];
|
|
|
|
$resultAlias = $selectExpression->fieldIdentificationVariable ?: null;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
if ( ! isset($this->selectedClasses[$dqlAlias])) {
|
|
|
|
$this->selectedClasses[$dqlAlias] = array(
|
2011-11-14 07:36:39 +04:00
|
|
|
'class' => $class,
|
|
|
|
'dqlAlias' => $dqlAlias,
|
|
|
|
'resultAlias' => $resultAlias
|
|
|
|
);
|
2010-02-20 00:28:17 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-11-14 07:36:39 +04:00
|
|
|
$sqlParts = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
// Select all fields from the queried class
|
|
|
|
foreach ($class->fieldMappings as $fieldName => $mapping) {
|
2011-11-14 07:36:39 +04:00
|
|
|
if ($partialFieldSet && ! in_array($fieldName, $partialFieldSet)) {
|
2011-11-03 08:44:50 +04:00
|
|
|
continue;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
$tableName = (isset($mapping['inherited']))
|
2012-06-01 01:03:01 +04:00
|
|
|
? $this->em->getClassMetadata($mapping['inherited'])->getTableName()
|
2011-11-03 08:44:50 +04:00
|
|
|
: $class->getTableName();
|
2009-08-11 14:51:38 +04:00
|
|
|
|
2011-11-14 07:36:39 +04:00
|
|
|
$sqlTableAlias = $this->getSQLTableAlias($tableName, $dqlAlias);
|
|
|
|
$columnAlias = $this->getSQLColumnAlias($mapping['columnName']);
|
2012-06-12 03:14:17 +04:00
|
|
|
$quotedColumnName = $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform);
|
2009-08-17 21:58:16 +04:00
|
|
|
|
2011-11-19 03:35:29 +04:00
|
|
|
$col = $sqlTableAlias . '.' . $quotedColumnName;
|
|
|
|
|
2011-11-20 22:50:51 +04:00
|
|
|
if (isset($class->fieldMappings[$fieldName]['requireSQLConversion'])) {
|
2011-11-19 03:35:29 +04:00
|
|
|
$type = Type::getType($class->getTypeOfField($fieldName));
|
2012-06-01 01:03:01 +04:00
|
|
|
$col = $type->convertToPHPValueSQL($col, $this->platform);
|
2011-11-19 03:35:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$sqlParts[] = $col . ' AS '. $columnAlias;
|
2009-08-17 21:58:16 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->scalarResultAliasMap[$resultAlias][] = $columnAlias;
|
2011-12-02 08:52:35 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $class->name);
|
2011-11-03 08:44:50 +04:00
|
|
|
}
|
2009-08-17 21:58:16 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
// Add any additional fields of subclasses (excluding inherited fields)
|
|
|
|
// 1) on Single Table Inheritance: always, since its marginal overhead
|
|
|
|
// 2) on Class Table Inheritance only if partial objects are disallowed,
|
|
|
|
// since it requires outer joining subtables.
|
2012-06-01 01:03:01 +04:00
|
|
|
if ($class->isInheritanceTypeSingleTable() || ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
|
2011-11-03 08:44:50 +04:00
|
|
|
foreach ($class->subClasses as $subClassName) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$subClass = $this->em->getClassMetadata($subClassName);
|
2011-11-03 08:44:50 +04:00
|
|
|
$sqlTableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
|
|
|
|
|
|
|
|
foreach ($subClass->fieldMappings as $fieldName => $mapping) {
|
|
|
|
if (isset($mapping['inherited']) || $partialFieldSet && !in_array($fieldName, $partialFieldSet)) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-11-14 07:36:39 +04:00
|
|
|
$columnAlias = $this->getSQLColumnAlias($mapping['columnName']);
|
2012-06-12 03:14:17 +04:00
|
|
|
$quotedColumnName = $this->quoteStrategy->getColumnName($fieldName, $subClass, $this->platform);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-11-19 03:35:29 +04:00
|
|
|
$col = $sqlTableAlias . '.' . $quotedColumnName;
|
|
|
|
|
2011-11-20 22:50:51 +04:00
|
|
|
if (isset($subClass->fieldMappings[$fieldName]['requireSQLConversion'])) {
|
2011-11-19 03:35:29 +04:00
|
|
|
$type = Type::getType($subClass->getTypeOfField($fieldName));
|
2012-06-01 01:03:01 +04:00
|
|
|
$col = $type->convertToPHPValueSQL($col, $this->platform);
|
2011-11-19 03:35:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$sqlParts[] = $col . ' AS ' . $columnAlias;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->scalarResultAliasMap[$resultAlias][] = $columnAlias;
|
2011-12-02 08:52:35 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $subClassName);
|
2009-10-22 23:12:00 +04:00
|
|
|
}
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
|
|
|
$sql .= implode(', ', $sqlParts);
|
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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
|
|
|
public function walkQuantifiedExpression($qExpr)
|
|
|
|
{
|
2011-11-14 07:36:39 +04:00
|
|
|
return ' ' . strtoupper($qExpr->type) . '(' . $this->walkSubselect($qExpr->subselect) . ')';
|
2009-03-28 20:10:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-03-19 15:43:48 +03:00
|
|
|
public function walkSubselect($subselect)
|
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
$useAliasesBefore = $this->useSqlTableAliases;
|
|
|
|
$rootAliasesBefore = $this->rootAliases;
|
2011-07-26 02:19:26 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rootAliases = array(); // reset the rootAliases for the subselect
|
|
|
|
$this->useSqlTableAliases = true;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-11-14 07:36:39 +04:00
|
|
|
$sql = $this->walkSimpleSelectClause($subselect->simpleSelectClause);
|
2009-08-07 01:42:07 +04:00
|
|
|
$sql .= $this->walkSubselectFromClause($subselect->subselectFromClause);
|
2011-07-26 02:19:26 +04:00
|
|
|
$sql .= $this->walkWhereClause($subselect->whereClause);
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$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
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rootAliases = $rootAliasesBefore; // put the main aliases back
|
|
|
|
$this->useSqlTableAliases = $useAliasesBefore;
|
2009-03-19 15:43:48 +03:00
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
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) {
|
2013-02-21 22:02:21 +04:00
|
|
|
$sql = $this->platform->appendLockHint(
|
|
|
|
$this->walkRangeVariableDeclaration($subselectIdVarDecl->rangeVariableDeclaration),
|
|
|
|
$this->query->getHint(Query::HINT_LOCK_MODE)
|
|
|
|
);
|
2010-07-20 07:51:01 +04:00
|
|
|
|
2012-06-11 00:14:25 +04:00
|
|
|
foreach ($subselectIdVarDecl->joins as $join) {
|
|
|
|
$sql .= $this->walkJoin($join);
|
2010-07-20 07:51:01 +04:00
|
|
|
}
|
|
|
|
|
2013-02-21 22:02:21 +04:00
|
|
|
$sqlParts[] = $sql;
|
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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2013-03-16 21:33:19 +04:00
|
|
|
/**
|
|
|
|
* @param \Doctrine\ORM\Query\AST\ParenthesisExpression $parenthesisExpression
|
|
|
|
*
|
|
|
|
* @return string.
|
|
|
|
*/
|
|
|
|
public function walkParenthesisExpression(AST\ParenthesisExpression $parenthesisExpression)
|
|
|
|
{
|
|
|
|
return sprintf('(%s)', $parenthesisExpression->expression->dispatch($this));
|
|
|
|
}
|
|
|
|
|
2012-07-01 02:25:54 +04:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* @param AST\NewObjectExpression $newObjectExpression
|
|
|
|
*
|
2012-07-01 02:25:54 +04:00
|
|
|
* @return string The SQL.
|
|
|
|
*/
|
|
|
|
public function walkNewObject($newObjectExpression)
|
|
|
|
{
|
|
|
|
$sqlSelectExpressions = array();
|
2012-08-12 21:40:26 +04:00
|
|
|
$objIndex = $this->newObjectCounter++;
|
2012-08-11 21:01:57 +04:00
|
|
|
|
2012-07-01 02:25:54 +04:00
|
|
|
foreach ($newObjectExpression->args as $argIndex => $e) {
|
2012-07-22 01:07:56 +04:00
|
|
|
$resultAlias = $this->scalarResultCounter++;
|
2012-08-11 19:56:47 +04:00
|
|
|
$columnAlias = $this->getSQLColumnAlias('sclr');
|
2012-07-01 02:25:54 +04:00
|
|
|
|
|
|
|
switch (true) {
|
2012-08-12 21:40:26 +04:00
|
|
|
case ($e instanceof AST\NewObjectExpression):
|
2012-07-01 02:25:54 +04:00
|
|
|
$sqlSelectExpressions[] = $e->dispatch($this);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-08-11 19:56:47 +04:00
|
|
|
$sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' . $columnAlias;
|
2012-07-01 02:25:54 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-08-11 21:01:57 +04:00
|
|
|
switch (true) {
|
|
|
|
case ($e instanceof AST\PathExpression):
|
|
|
|
$fieldName = $e->field;
|
|
|
|
$dqlAlias = $e->identificationVariable;
|
|
|
|
$qComp = $this->queryComponents[$dqlAlias];
|
|
|
|
$class = $qComp['metadata'];
|
|
|
|
$fieldType = $class->getTypeOfField($fieldName);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ($e instanceof AST\Literal):
|
|
|
|
switch ($e->type) {
|
|
|
|
case AST\Literal::BOOLEAN:
|
|
|
|
$fieldType = 'boolean';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AST\Literal::NUMERIC:
|
|
|
|
$fieldType = is_float($e->value) ? 'float' : 'integer';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$fieldType = 'string';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-07-01 02:25:54 +04:00
|
|
|
$this->scalarResultAliasMap[$resultAlias] = $columnAlias;
|
2012-08-11 21:01:57 +04:00
|
|
|
$this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldType);
|
2012-07-01 02:25:54 +04:00
|
|
|
|
|
|
|
$this->rsm->newObjectMappings[$columnAlias] = array(
|
|
|
|
'className' => $newObjectExpression->className,
|
|
|
|
'objIndex' => $objIndex,
|
|
|
|
'argIndex' => $argIndex
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-08-11 19:56:47 +04:00
|
|
|
return implode(', ', $sqlSelectExpressions);
|
2012-07-01 02:25:54 +04:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-03-19 15:43:48 +03:00
|
|
|
public function walkSimpleSelectExpression($simpleSelectExpression)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$expr = $simpleSelectExpression->expression;
|
2011-10-28 18:56:14 +04:00
|
|
|
$sql = ' ';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-10-28 18:56:14 +04:00
|
|
|
switch (true) {
|
|
|
|
case ($expr instanceof AST\PathExpression):
|
|
|
|
$sql .= $this->walkPathExpression($expr);
|
|
|
|
break;
|
2010-02-25 23:25:28 +03:00
|
|
|
|
2011-10-28 18:56:14 +04:00
|
|
|
case ($expr instanceof AST\AggregateExpression):
|
2012-06-01 01:03:01 +04:00
|
|
|
$alias = $simpleSelectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
|
2010-08-12 06:12:44 +04:00
|
|
|
|
2011-10-28 18:56:14 +04:00
|
|
|
$sql .= $this->walkAggregateExpression($expr) . ' AS dctrn__' . $alias;
|
|
|
|
break;
|
2010-08-12 06:12:44 +04:00
|
|
|
|
2011-10-28 18:56:14 +04:00
|
|
|
case ($expr instanceof AST\Subselect):
|
2012-06-01 01:03:01 +04:00
|
|
|
$alias = $simpleSelectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
|
2010-08-12 06:12:44 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$columnAlias = 'sclr' . $this->aliasCounter++;
|
|
|
|
$this->scalarResultAliasMap[$alias] = $columnAlias;
|
2011-09-22 00:30:45 +04:00
|
|
|
|
2011-10-28 18:56:14 +04:00
|
|
|
$sql .= '(' . $this->walkSubselect($expr) . ') AS ' . $columnAlias;
|
|
|
|
break;
|
2010-08-12 06:12:44 +04:00
|
|
|
|
2011-10-28 18:56:14 +04:00
|
|
|
case ($expr instanceof AST\Functions\FunctionNode):
|
|
|
|
case ($expr instanceof AST\SimpleArithmeticExpression):
|
|
|
|
case ($expr instanceof AST\ArithmeticTerm):
|
|
|
|
case ($expr instanceof AST\ArithmeticFactor):
|
2011-10-28 20:25:12 +04:00
|
|
|
case ($expr instanceof AST\Literal):
|
2011-10-28 18:56:14 +04:00
|
|
|
case ($expr instanceof AST\NullIfExpression):
|
|
|
|
case ($expr instanceof AST\CoalesceExpression):
|
|
|
|
case ($expr instanceof AST\GeneralCaseExpression):
|
|
|
|
case ($expr instanceof AST\SimpleCaseExpression):
|
2012-06-01 01:03:01 +04:00
|
|
|
$alias = $simpleSelectExpression->fieldIdentificationVariable ?: $this->scalarResultCounter++;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
|
|
|
$columnAlias = $this->getSQLColumnAlias('sclr');
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->scalarResultAliasMap[$alias] = $columnAlias;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-11-03 08:44:50 +04:00
|
|
|
$sql .= $expr->dispatch($this) . ' AS ' . $columnAlias;
|
2011-10-28 18:56:14 +04:00
|
|
|
break;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2013-10-14 17:04:52 +04:00
|
|
|
case ($expr instanceof AST\ParenthesisExpression):
|
|
|
|
$sql .= $this->walkParenthesisExpression($expr);
|
|
|
|
break;
|
|
|
|
|
2011-10-28 18:56:14 +04:00
|
|
|
default: // IdentificationVariable
|
2011-12-04 00:19:21 +04:00
|
|
|
$sql .= $this->walkEntityIdentificationVariable($expr);
|
2011-10-28 18:56:14 +04:00
|
|
|
break;
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-10-28 18:56:14 +04:00
|
|
|
return $sql;
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
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 ' : '')
|
2011-02-20 07:44:05 +03:00
|
|
|
. $this->walkSimpleArithmeticExpression($aggExpression->pathExpression) . ')';
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-01-19 21:40:12 +03:00
|
|
|
public function walkGroupByClause($groupByClause)
|
|
|
|
{
|
2011-10-28 20:25:12 +04:00
|
|
|
$sqlParts = array();
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($groupByClause->groupByItems as $groupByItem) {
|
2011-12-02 08:52:35 +04:00
|
|
|
$sqlParts[] = $this->walkGroupByItem($groupByItem);
|
2011-03-20 16:07:33 +03:00
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
return ' GROUP BY ' . implode(', ', $sqlParts);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2011-12-02 08:52:35 +04:00
|
|
|
public function walkGroupByItem($groupByItem)
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2011-12-02 08:52:35 +04:00
|
|
|
// StateFieldPathExpression
|
|
|
|
if ( ! is_string($groupByItem)) {
|
|
|
|
return $this->walkPathExpression($groupByItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResultVariable
|
2012-06-01 01:03:01 +04:00
|
|
|
if (isset($this->queryComponents[$groupByItem]['resultVariable'])) {
|
2013-10-01 18:53:53 +04:00
|
|
|
$resultVariable = $this->queryComponents[$groupByItem]['resultVariable'];
|
|
|
|
|
|
|
|
if ($resultVariable instanceof AST\PathExpression) {
|
|
|
|
return $this->walkPathExpression($resultVariable);
|
2013-09-26 16:11:56 +04:00
|
|
|
}
|
2013-10-01 18:53:53 +04:00
|
|
|
|
|
|
|
if (isset($resultVariable->pathExpression)) {
|
|
|
|
return $this->walkPathExpression($resultVariable->pathExpression);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->walkResultVariable($groupByItem);
|
2011-12-02 08:52:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IdentificationVariable
|
|
|
|
$sqlParts = array();
|
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
foreach ($this->queryComponents[$groupByItem]['metadata']->fieldNames as $field) {
|
2011-12-02 08:52:35 +04:00
|
|
|
$item = new AST\PathExpression(AST\PathExpression::TYPE_STATE_FIELD, $groupByItem, $field);
|
|
|
|
$item->type = AST\PathExpression::TYPE_STATE_FIELD;
|
|
|
|
|
|
|
|
$sqlParts[] = $this->walkPathExpression($item);
|
|
|
|
}
|
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
foreach ($this->queryComponents[$groupByItem]['metadata']->associationMappings as $mapping) {
|
2011-12-02 08:52:35 +04:00
|
|
|
if ($mapping['isOwningSide'] && $mapping['type'] & ClassMetadataInfo::TO_ONE) {
|
|
|
|
$item = new AST\PathExpression(AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $groupByItem, $mapping['fieldName']);
|
|
|
|
$item->type = AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
|
|
|
|
|
|
|
|
$sqlParts[] = $this->walkPathExpression($item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(', ', $sqlParts);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-03-21 00:28:19 +03:00
|
|
|
public function walkDeleteClause(AST\DeleteClause $deleteClause)
|
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
$class = $this->em->getClassMetadata($deleteClause->abstractSchemaName);
|
2011-10-28 20:25:12 +04:00
|
|
|
$tableName = $class->getTableName();
|
2012-06-12 03:14:17 +04:00
|
|
|
$sql = 'DELETE FROM ' . $this->quoteStrategy->getTableName($class, $this->platform);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
$this->setSQLTableAlias($tableName, $tableName, $deleteClause->aliasIdentificationVariable);
|
2012-06-01 01:03: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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-03-21 22:58:52 +03:00
|
|
|
public function walkUpdateClause($updateClause)
|
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
$class = $this->em->getClassMetadata($updateClause->abstractSchemaName);
|
2011-10-28 20:25:12 +04:00
|
|
|
$tableName = $class->getTableName();
|
2012-06-12 03:14:17 +04:00
|
|
|
$sql = 'UPDATE ' . $this->quoteStrategy->getTableName($class, $this->platform);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
$this->setSQLTableAlias($tableName, $tableName, $updateClause->aliasIdentificationVariable);
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->rootAliases[] = $updateClause->aliasIdentificationVariable;
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
$sql .= ' SET ' . implode(', ', array_map(array($this, 'walkUpdateItem'), $updateClause->updateItems));
|
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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-03-21 22:58:52 +03:00
|
|
|
public function walkUpdateItem($updateItem)
|
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
$useTableAliasesBefore = $this->useSqlTableAliases;
|
|
|
|
$this->useSqlTableAliases = false;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
$sql = $this->walkPathExpression($updateItem->pathExpression) . ' = ';
|
2009-08-07 01:42:07 +04:00
|
|
|
$newValue = $updateItem->newValue;
|
2009-03-23 20:39:33 +03:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
switch (true) {
|
|
|
|
case ($newValue instanceof AST\Node):
|
|
|
|
$sql .= $newValue->dispatch($this);
|
|
|
|
break;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
case ($newValue === null):
|
|
|
|
$sql .= 'NULL';
|
|
|
|
break;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
default:
|
2012-06-01 01:03:01 +04:00
|
|
|
$sql .= $this->conn->quote($newValue);
|
2011-10-28 20:25:12 +04:00
|
|
|
break;
|
2009-03-23 20:39:33 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->useSqlTableAliases = $useTableAliasesBefore;
|
2009-03-21 22:58:52 +03:00
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkWhereClause($whereClause)
|
|
|
|
{
|
2011-10-28 20:25:12 +04:00
|
|
|
$condSql = null !== $whereClause ? $this->walkConditionalExpression($whereClause->conditionalExpression) : '';
|
2012-06-01 01:03:01 +04:00
|
|
|
$discrSql = $this->_generateDiscriminatorColumnConditionSql($this->rootAliases);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
if ($this->em->hasFilters()) {
|
2011-12-12 00:14:09 +04:00
|
|
|
$filterClauses = array();
|
2012-06-01 01:03:01 +04:00
|
|
|
foreach ($this->rootAliases as $dqlAlias) {
|
|
|
|
$class = $this->queryComponents[$dqlAlias]['metadata'];
|
2011-12-12 00:14:09 +04:00
|
|
|
$tableAlias = $this->getSQLTableAlias($class->table['name'], $dqlAlias);
|
2011-08-16 14:03:47 +04:00
|
|
|
|
2011-12-12 00:14:09 +04:00
|
|
|
if ($filterExpr = $this->generateFilterConditionSQL($class, $tableAlias)) {
|
|
|
|
$filterClauses[] = $filterExpr;
|
|
|
|
}
|
2011-08-16 14:03:47 +04:00
|
|
|
}
|
|
|
|
|
2011-12-12 00:14:09 +04:00
|
|
|
if (count($filterClauses)) {
|
|
|
|
if ($condSql) {
|
2012-11-02 01:43:18 +04:00
|
|
|
$condSql = '(' . $condSql . ') AND ';
|
2011-12-12 00:14:09 +04:00
|
|
|
}
|
2011-08-16 14:03:47 +04:00
|
|
|
|
2011-12-12 00:14:09 +04:00
|
|
|
$condSql .= implode(' AND ', $filterClauses);
|
|
|
|
}
|
2011-08-16 14:03:47 +04:00
|
|
|
}
|
|
|
|
|
2011-07-26 02:19:26 +04:00
|
|
|
if ($condSql) {
|
|
|
|
return ' WHERE ' . (( ! $discrSql) ? $condSql : '(' . $condSql . ') AND ' . $discrSql);
|
2011-10-30 15:12:18 +04:00
|
|
|
}
|
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
if ($discrSql) {
|
2011-07-26 02:19:26 +04:00
|
|
|
return ' WHERE ' . $discrSql;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
2010-04-30 05:15:36 +04:00
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2010-04-30 05:15:36 +04:00
|
|
|
*/
|
|
|
|
public function walkConditionalExpression($condExpr)
|
|
|
|
{
|
2013-03-11 04:08:58 +04:00
|
|
|
// Phase 2 AST optimization: Skip processing of ConditionalExpression
|
2010-04-30 05:15:36 +04:00
|
|
|
// if only one ConditionalTerm is defined
|
2011-10-28 20:25:12 +04:00
|
|
|
if ( ! ($condExpr instanceof AST\ConditionalExpression)) {
|
|
|
|
return $this->walkConditionalTerm($condExpr);
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
return 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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkConditionalTerm($condTerm)
|
|
|
|
{
|
2013-03-11 04:08:58 +04:00
|
|
|
// Phase 2 AST optimization: Skip processing of ConditionalTerm
|
2010-04-30 05:15:36 +04:00
|
|
|
// if only one ConditionalFactor is defined
|
2011-10-28 20:25:12 +04:00
|
|
|
if ( ! ($condTerm instanceof AST\ConditionalTerm)) {
|
|
|
|
return $this->walkConditionalFactor($condTerm);
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
return 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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkConditionalFactor($factor)
|
|
|
|
{
|
2013-03-11 04:08:58 +04:00
|
|
|
// Phase 2 AST optimization: Skip processing of ConditionalFactor
|
2010-04-30 05:15:36 +04:00
|
|
|
// 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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2010-04-30 05:15:36 +04:00
|
|
|
*/
|
|
|
|
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);
|
2011-10-30 15:12:18 +04:00
|
|
|
}
|
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-06-14 21:34:28 +04:00
|
|
|
*/
|
|
|
|
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 ';
|
2011-12-04 00:19:21 +04:00
|
|
|
|
|
|
|
$entityExpr = $collMemberExpr->entityExpression;
|
2009-06-14 21:34:28 +04:00
|
|
|
$collPathExpr = $collMemberExpr->collectionValuedPathExpression;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2010-07-23 08:55:33 +04:00
|
|
|
$fieldName = $collPathExpr->field;
|
2011-12-04 00:19:21 +04:00
|
|
|
$dqlAlias = $collPathExpr->identificationVariable;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
$class = $this->queryComponents[$dqlAlias]['metadata'];
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-12-04 00:19:21 +04:00
|
|
|
switch (true) {
|
|
|
|
// InputParameter
|
|
|
|
case ($entityExpr instanceof AST\InputParameter):
|
|
|
|
$dqlParamKey = $entityExpr->name;
|
|
|
|
$entitySql = '?';
|
|
|
|
break;
|
|
|
|
|
|
|
|
// SingleValuedAssociationPathExpression | IdentificationVariable
|
|
|
|
case ($entityExpr instanceof AST\PathExpression):
|
|
|
|
$entitySql = $this->walkPathExpression($entityExpr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \BadMethodCallException("Not implemented");
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
$assoc = $class->associationMappings[$fieldName];
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc['type'] == ClassMetadata::ONE_TO_MANY) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
|
2011-09-07 08:48:19 +04:00
|
|
|
$targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName());
|
|
|
|
$sourceTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2012-06-12 03:14:17 +04:00
|
|
|
$sql .= $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' ' . $targetTableAlias . ' WHERE ';
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
$owningAssoc = $targetClass->associationMappings[$assoc['mappedBy']];
|
2011-12-04 00:19:21 +04:00
|
|
|
$sqlParts = array();
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
foreach ($owningAssoc['targetToSourceKeyColumns'] as $targetColumn => $sourceColumn) {
|
2012-06-12 03:14:17 +04:00
|
|
|
$targetColumn = $this->quoteStrategy->getColumnName($class->fieldNames[$targetColumn], $class, $this->platform);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-12-04 00:19:21 +04:00
|
|
|
$sqlParts[] = $sourceTableAlias . '.' . $targetColumn . ' = ' . $targetTableAlias . '.' . $sourceColumn;
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2012-06-12 03:14:17 +04:00
|
|
|
foreach ($this->quoteStrategy->getIdentifierColumnNames($targetClass, $this->platform) as $targetColumnName) {
|
2011-12-04 00:19:21 +04:00
|
|
|
if (isset($dqlParamKey)) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->parserResult->addParameterMapping($dqlParamKey, $this->sqlParamIndex++);
|
2011-12-04 00:19:21 +04:00
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-12-04 00:19:21 +04:00
|
|
|
$sqlParts[] = $targetTableAlias . '.' . $targetColumnName . ' = ' . $entitySql;
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
2011-12-04 00:19:21 +04:00
|
|
|
|
|
|
|
$sql .= implode(' AND ', $sqlParts);
|
2009-06-14 21:34:28 +04:00
|
|
|
} else { // many-to-many
|
2012-06-01 01:03:01 +04:00
|
|
|
$targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
$owningAssoc = $assoc['isOwningSide'] ? $assoc : $targetClass->associationMappings[$assoc['mappedBy']];
|
|
|
|
$joinTable = $owningAssoc['joinTable'];
|
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
// SQL table aliases
|
2011-09-07 08:48:19 +04:00
|
|
|
$joinTableAlias = $this->getSQLTableAlias($joinTable['name']);
|
|
|
|
$targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName());
|
|
|
|
$sourceTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
// join to target table
|
2012-06-12 03:14:17 +04:00
|
|
|
$sql .= $this->quoteStrategy->getJoinTableName($owningAssoc, $targetClass, $this->platform) . ' ' . $joinTableAlias
|
|
|
|
. ' INNER JOIN ' . $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' ' . $targetTableAlias . ' ON ';
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
// join conditions
|
2011-12-04 00:19:21 +04:00
|
|
|
$joinColumns = $assoc['isOwningSide'] ? $joinTable['inverseJoinColumns'] : $joinTable['joinColumns'];
|
|
|
|
$joinSqlParts = array();
|
2010-03-29 17:20:41 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
foreach ($joinColumns as $joinColumn) {
|
2012-06-12 03:14:17 +04:00
|
|
|
$targetColumn = $this->quoteStrategy->getColumnName($targetClass->fieldNames[$joinColumn['referencedColumnName']], $targetClass, $this->platform);
|
2009-08-11 14:51:38 +04:00
|
|
|
|
2011-12-04 00:19:21 +04:00
|
|
|
$joinSqlParts[] = $joinTableAlias . '.' . $joinColumn['name'] . ' = ' . $targetTableAlias . '.' . $targetColumn;
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
2010-03-29 17:20:41 +04:00
|
|
|
|
2011-12-04 00:19:21 +04:00
|
|
|
$sql .= implode(' AND ', $joinSqlParts);
|
2009-06-14 21:34:28 +04:00
|
|
|
$sql .= ' WHERE ';
|
2010-03-29 17:20:41 +04:00
|
|
|
|
2011-09-07 08:48:19 +04:00
|
|
|
$joinColumns = $assoc['isOwningSide'] ? $joinTable['joinColumns'] : $joinTable['inverseJoinColumns'];
|
2011-12-04 00:19:21 +04:00
|
|
|
$sqlParts = array();
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
foreach ($joinColumns as $joinColumn) {
|
2012-06-12 03:14:17 +04:00
|
|
|
$targetColumn = $this->quoteStrategy->getColumnName($class->fieldNames[$joinColumn['referencedColumnName']], $class, $this->platform);
|
2010-03-27 01:07:31 +03:00
|
|
|
|
2011-12-04 00:19:21 +04:00
|
|
|
$sqlParts[] = $joinTableAlias . '.' . $joinColumn['name'] . ' = ' . $sourceTableAlias . '.' . $targetColumn;
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2012-06-12 03:14:17 +04:00
|
|
|
foreach ($this->quoteStrategy->getIdentifierColumnNames($targetClass, $this->platform) as $targetColumnName) {
|
2011-12-04 00:19:21 +04:00
|
|
|
if (isset($dqlParamKey)) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$this->parserResult->addParameterMapping($dqlParamKey, $this->sqlParamIndex++);
|
2011-12-04 00:19:21 +04:00
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-12-04 00:19:21 +04:00
|
|
|
$sqlParts[] = $targetTableAlias . '.' . $targetColumnName . ' = ' . $entitySql;
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
2011-12-04 00:19:21 +04:00
|
|
|
|
|
|
|
$sql .= implode(' AND ', $sqlParts);
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
2010-07-30 19:44:33 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
return $sql . ')';
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2009-08-05 01:41:53 +04:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-08-05 01:41:53 +04:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-03-21 15:49:58 +03:00
|
|
|
public function walkNullComparisonExpression($nullCompExpr)
|
|
|
|
{
|
2013-08-19 07:52:32 +04:00
|
|
|
$expression = $nullCompExpr->expression;
|
|
|
|
$comparison = ' IS' . ($nullCompExpr->not ? ' NOT' : '') . ' NULL';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2013-08-04 01:38:55 +04:00
|
|
|
// Handle ResultVariable
|
|
|
|
if (is_string($expression) && isset($this->queryComponents[$expression]['resultVariable'])) {
|
|
|
|
return $this->walkResultVariable($expression) . $comparison;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle InputParameter mapping inclusion to ParserResult
|
2013-01-19 05:18:50 +04:00
|
|
|
if ($expression instanceof AST\InputParameter) {
|
2013-08-19 08:33:45 +04:00
|
|
|
return $this->walkInputParameter($expression) . $comparison;
|
2013-01-19 05:18:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $expression->dispatch($this) . $comparison;
|
2009-03-21 15:49:58 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-03-21 15:49:58 +03:00
|
|
|
public function walkInExpression($inExpr)
|
|
|
|
{
|
2011-12-04 11:41:54 +04:00
|
|
|
$sql = $this->walkArithmeticExpression($inExpr->expression) . ($inExpr->not ? ' NOT' : '') . ' IN (';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2011-10-30 15:12:18 +04:00
|
|
|
$sql .= ($inExpr->subselect)
|
2011-10-28 20:25:12 +04:00
|
|
|
? $this->walkSubselect($inExpr->subselect)
|
|
|
|
: implode(', ', array_map(array($this, 'walkInParameter'), $inExpr->literals));
|
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
|
|
|
|
2010-07-30 08:30:02 +04:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2010-07-30 08:30:02 +04:00
|
|
|
*/
|
2010-07-30 19:44:33 +04:00
|
|
|
public function walkInstanceOfExpression($instanceOfExpr)
|
2010-07-30 08:30:02 +04:00
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
|
|
|
|
$dqlAlias = $instanceOfExpr->identificationVariable;
|
2012-06-01 01:03:01 +04:00
|
|
|
$discrClass = $class = $this->queryComponents[$dqlAlias]['metadata'];
|
2012-10-11 17:25:39 +04:00
|
|
|
|
2010-07-30 19:44:33 +04:00
|
|
|
if ($class->discriminatorColumn) {
|
2012-06-01 01:03:01 +04:00
|
|
|
$discrClass = $this->em->getClassMetadata($class->rootEntityName);
|
2010-07-30 08:30:02 +04:00
|
|
|
}
|
|
|
|
|
2012-06-01 01:03:01 +04:00
|
|
|
if ($this->useSqlTableAliases) {
|
2011-09-07 08:48:19 +04:00
|
|
|
$sql .= $this->getSQLTableAlias($discrClass->getTableName(), $dqlAlias) . '.';
|
2010-07-30 08:30:02 +04:00
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-15 08:53:56 +04:00
|
|
|
$sql .= $class->discriminatorColumn['name'] . ($instanceOfExpr->not ? ' NOT IN ' : ' IN ');
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-15 08:53:56 +04:00
|
|
|
$sqlParameterList = array();
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-15 08:53:56 +04:00
|
|
|
foreach ($instanceOfExpr->value as $parameter) {
|
|
|
|
if ($parameter instanceof AST\InputParameter) {
|
2013-06-08 01:01:52 +04:00
|
|
|
$this->rsm->addMetadataParameterMapping($parameter->name, 'discriminatorValue');
|
|
|
|
|
2013-04-04 22:20:23 +04:00
|
|
|
$sqlParameterList[] = $this->walkInputParameter($parameter);
|
2010-07-30 08:30:02 +04:00
|
|
|
|
2013-06-08 01:01:52 +04:00
|
|
|
continue;
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2013-06-08 01:01:52 +04:00
|
|
|
// Get name from ClassMetadata to resolve aliases.
|
|
|
|
$entityClassName = $this->em->getClassMetadata($parameter)->name;
|
|
|
|
$discriminatorValue = $class->discriminatorValue;
|
|
|
|
|
|
|
|
if ($entityClassName !== $class->name) {
|
|
|
|
$discrMap = array_flip($class->discriminatorMap);
|
2011-08-15 08:53:56 +04:00
|
|
|
|
2013-06-08 01:01:52 +04:00
|
|
|
if ( ! isset($discrMap[$entityClassName])) {
|
|
|
|
throw QueryException::instanceOfUnrelatedClass($entityClassName, $class->rootEntityName);
|
2013-04-04 22:20:23 +04:00
|
|
|
}
|
2013-06-08 01:01:52 +04:00
|
|
|
|
|
|
|
$discriminatorValue = $discrMap[$entityClassName];
|
2011-06-05 12:48:21 +04:00
|
|
|
}
|
2013-06-08 01:01:52 +04:00
|
|
|
|
|
|
|
$sqlParameterList[] = $this->conn->quote($discriminatorValue);
|
2010-07-30 08:30:02 +04:00
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-08-15 08:53:56 +04:00
|
|
|
$sql .= '(' . implode(', ', $sqlParameterList) . ')';
|
2010-07-30 08:30:02 +04:00
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2010-07-30 08:30:02 +04:00
|
|
|
*/
|
2009-10-13 14:48:46 +04:00
|
|
|
public function walkInParameter($inParam)
|
|
|
|
{
|
2011-10-30 15:12:18 +04:00
|
|
|
return $inParam instanceof AST\InputParameter
|
|
|
|
? $this->walkInputParameter($inParam)
|
2011-10-28 20:25:12 +04:00
|
|
|
: $this->walkLiteral($inParam);
|
2009-10-13 14:48:46 +04:00
|
|
|
}
|
2009-03-21 15:49:58 +03:00
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
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:
|
2012-06-01 01:03:01 +04:00
|
|
|
return $this->conn->quote($literal->value);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2009-10-13 14:48:46 +04:00
|
|
|
case AST\Literal::BOOLEAN:
|
|
|
|
$bool = strtolower($literal->value) == 'true' ? true : false;
|
2012-06-01 01:03:01 +04:00
|
|
|
$boolVal = $this->conn->getDatabasePlatform()->convertBooleans($bool);
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-11-21 18:08:36 +04:00
|
|
|
return $boolVal;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2009-10-13 14:48:46 +04:00
|
|
|
case AST\Literal::NUMERIC:
|
2011-11-21 18:08:36 +04:00
|
|
|
return $literal->value;
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2009-10-13 14:48:46 +04:00
|
|
|
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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
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
|
|
|
|
2013-08-19 07:52:32 +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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-01-21 21:25:05 +03:00
|
|
|
public function walkLikeExpression($likeExpr)
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$stringExpr = $likeExpr->stringExpression;
|
2013-08-19 07:52:32 +04:00
|
|
|
$leftExpr = (is_string($stringExpr) && isset($this->queryComponents[$stringExpr]['resultVariable']))
|
|
|
|
? $this->walkResultVariable($stringExpr)
|
|
|
|
: $stringExpr->dispatch($this);
|
|
|
|
|
|
|
|
$sql = $leftExpr . ($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) {
|
2013-08-19 08:33:45 +04:00
|
|
|
$sql .= $this->walkInputParameter($likeExpr->stringPattern);
|
2013-08-19 07:52:32 +04:00
|
|
|
} elseif ($likeExpr->stringPattern instanceof AST\Functions\FunctionNode) {
|
2011-12-29 18:39:32 +04:00
|
|
|
$sql .= $this->walkFunction($likeExpr->stringPattern);
|
|
|
|
} elseif ($likeExpr->stringPattern instanceof AST\PathExpression) {
|
|
|
|
$sql .= $this->walkPathExpression($likeExpr->stringPattern);
|
2009-03-21 15:49:58 +03:00
|
|
|
} else {
|
2012-03-12 05:14:08 +04:00
|
|
|
$sql .= $this->walkLiteral($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) {
|
2012-03-12 05:14:08 +04:00
|
|
|
$sql .= ' ESCAPE ' . $this->walkLiteral($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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-03-23 20:39:33 +03:00
|
|
|
public function walkStateFieldPathExpression($stateFieldPathExpression)
|
|
|
|
{
|
|
|
|
return $this->walkPathExpression($stateFieldPathExpression);
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkComparisonExpression($compExpr)
|
|
|
|
{
|
2011-10-28 20:25:12 +04:00
|
|
|
$leftExpr = $compExpr->leftExpression;
|
2009-08-07 01:42:07 +04:00
|
|
|
$rightExpr = $compExpr->rightExpression;
|
2011-10-28 20:25:12 +04:00
|
|
|
$sql = '';
|
2009-03-23 20:39:33 +03:00
|
|
|
|
2011-10-30 15:12:18 +04:00
|
|
|
$sql .= ($leftExpr instanceof AST\Node)
|
2011-10-28 20:25:12 +04:00
|
|
|
? $leftExpr->dispatch($this)
|
2012-06-01 01:03:01 +04:00
|
|
|
: (is_numeric($leftExpr) ? $leftExpr : $this->conn->quote($leftExpr));
|
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
|
|
|
|
2011-10-30 15:12:18 +04:00
|
|
|
$sql .= ($rightExpr instanceof AST\Node)
|
2011-10-28 20:25:12 +04:00
|
|
|
? $rightExpr->dispatch($this)
|
2012-06-01 01:03:01 +04:00
|
|
|
: (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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-03-23 20:39:33 +03:00
|
|
|
public function walkInputParameter($inputParam)
|
|
|
|
{
|
2012-06-01 01:03:01 +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
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkArithmeticExpression($arithmeticExpr)
|
|
|
|
{
|
2009-08-06 19:48:41 +04:00
|
|
|
return ($arithmeticExpr->isSimpleArithmeticExpression())
|
2012-06-05 23:32:53 +04:00
|
|
|
? $this->walkSimpleArithmeticExpression($arithmeticExpr->simpleArithmeticExpression)
|
|
|
|
: '(' . $this->walkSubselect($arithmeticExpr->subselect) . ')';
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2010-04-30 05:15:36 +04:00
|
|
|
*/
|
|
|
|
public function walkSimpleArithmeticExpression($simpleArithmeticExpr)
|
|
|
|
{
|
2011-10-28 20:25:12 +04:00
|
|
|
if ( ! ($simpleArithmeticExpr instanceof AST\SimpleArithmeticExpression)) {
|
|
|
|
return $this->walkArithmeticTerm($simpleArithmeticExpr);
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
return implode(' ', array_map(array($this, 'walkArithmeticTerm'), $simpleArithmeticExpr->arithmeticTerms));
|
2010-04-30 05:15:36 +04:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkArithmeticTerm($term)
|
|
|
|
{
|
2010-04-30 05:15:36 +04:00
|
|
|
if (is_string($term)) {
|
2012-06-01 01:03:01 +04:00
|
|
|
return (isset($this->queryComponents[$term]))
|
|
|
|
? $this->walkResultVariable($this->queryComponents[$term]['token']['value'])
|
2011-10-28 20:25:12 +04:00
|
|
|
: $term;
|
2010-04-30 05:15:36 +04:00
|
|
|
}
|
2009-03-23 20:39:33 +03:00
|
|
|
|
2013-03-11 04:08:58 +04:00
|
|
|
// Phase 2 AST optimization: Skip processing of ArithmeticTerm
|
2010-04-30 05:15:36 +04:00
|
|
|
// if only one ArithmeticFactor is defined
|
2011-10-28 20:25:12 +04:00
|
|
|
if ( ! ($term instanceof AST\ArithmeticTerm)) {
|
|
|
|
return $this->walkArithmeticFactor($term);
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
return implode(' ', array_map(array($this, 'walkArithmeticFactor'), $term->arithmeticFactors));
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
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;
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2013-03-11 04:08:58 +04:00
|
|
|
// Phase 2 AST optimization: Skip processing of ArithmeticFactor
|
2010-04-30 05:15:36 +04:00
|
|
|
// if only one ArithmeticPrimary is defined
|
2011-10-28 20:25:12 +04:00
|
|
|
if ( ! ($factor instanceof AST\ArithmeticFactor)) {
|
|
|
|
return $this->walkArithmeticPrimary($factor);
|
|
|
|
}
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
$sign = $factor->isNegativeSigned() ? '-' : ($factor->isPositiveSigned() ? '+' : '');
|
2011-10-30 15:12:18 +04:00
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
return $sign . $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
|
|
|
*
|
2012-12-13 20:36:14 +04:00
|
|
|
* @param mixed $primary
|
|
|
|
*
|
2009-03-28 20:10:41 +03:00
|
|
|
* @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) . ')';
|
2011-10-30 15:12:18 +04:00
|
|
|
}
|
|
|
|
|
2011-10-28 20:25:12 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-12-04 00:19:21 +04:00
|
|
|
return $this->walkEntityIdentificationVariable($primary);
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2009-03-28 20:10:41 +03:00
|
|
|
*/
|
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))
|
2012-06-01 01:03:01 +04:00
|
|
|
? $this->conn->quote($stringPrimary)
|
2010-04-30 05:15:36 +04:00
|
|
|
: $stringPrimary->dispatch($this);
|
2009-01-21 21:25:05 +03:00
|
|
|
}
|
2011-12-02 08:52:35 +04:00
|
|
|
|
|
|
|
/**
|
2012-12-13 20:36:14 +04:00
|
|
|
* {@inheritdoc}
|
2011-12-02 08:52:35 +04:00
|
|
|
*/
|
|
|
|
public function walkResultVariable($resultVariable)
|
|
|
|
{
|
2012-06-01 01:03:01 +04:00
|
|
|
$resultAlias = $this->scalarResultAliasMap[$resultVariable];
|
2011-12-02 08:52:35 +04:00
|
|
|
|
|
|
|
if (is_array($resultAlias)) {
|
|
|
|
return implode(', ', $resultAlias);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $resultAlias;
|
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|