2009-01-19 21:40:12 +03:00
|
|
|
<?php
|
2009-02-17 13:54:18 +03:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
2009-01-19 21:40:12 +03:00
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\ORM\Query;
|
|
|
|
|
2009-03-21 22:58:52 +03:00
|
|
|
use Doctrine\ORM\Query\Parser;
|
2009-01-22 22:38:10 +03:00
|
|
|
use Doctrine\ORM\Query\AST;
|
2009-02-18 13:39:51 +03:00
|
|
|
use Doctrine\Common\DoctrineException;
|
2009-01-22 22:38:10 +03:00
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
/**
|
2009-02-05 20:34:44 +03:00
|
|
|
* The SqlWalker walks over an AST that represents a DQL query and constructs
|
|
|
|
* the corresponding SQL. The walking can start at any node, not only at some root
|
|
|
|
* node. Therefore it is possible to only generate SQL parts by simply walking over
|
|
|
|
* certain subtrees of the AST.
|
2009-01-19 21:40:12 +03:00
|
|
|
*
|
|
|
|
* @author robo
|
2009-02-05 20:34:44 +03:00
|
|
|
* @since 2.0
|
2009-01-19 21:40:12 +03:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class SqlWalker
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-21 22:58:52 +03:00
|
|
|
const SQLALIAS_SEPARATOR = '__';
|
|
|
|
|
2009-02-17 13:54:18 +03:00
|
|
|
private $_tableAliasCounter = 0;
|
2009-01-19 21:40:12 +03:00
|
|
|
private $_parserResult;
|
|
|
|
private $_em;
|
|
|
|
private $_dqlToSqlAliasMap = array();
|
|
|
|
private $_scalarAliasCounter = 0;
|
|
|
|
|
2009-02-05 20:34:44 +03:00
|
|
|
/**
|
|
|
|
* Initializes a new SqlWalker instance.
|
|
|
|
*/
|
2009-01-19 21:40:12 +03:00
|
|
|
public function __construct($em, $parserResult)
|
|
|
|
{
|
|
|
|
$this->_em = $em;
|
|
|
|
$this->_parserResult = $parserResult;
|
|
|
|
$sqlToDqlAliasMap = array();
|
|
|
|
foreach ($parserResult->getQueryComponents() as $dqlAlias => $qComp) {
|
2009-03-21 22:58:52 +03:00
|
|
|
if ($dqlAlias != Parser::SCALAR_QUERYCOMPONENT_ALIAS) {
|
2009-02-17 13:54:18 +03:00
|
|
|
$sqlAlias = $this->generateSqlTableAlias($qComp['metadata']->getTableName());
|
2009-01-19 21:40:12 +03:00
|
|
|
$sqlToDqlAliasMap[$sqlAlias] = $dqlAlias;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// SQL => DQL alias stored in ParserResult, needed for hydration.
|
|
|
|
$parserResult->setTableAliasMap($sqlToDqlAliasMap);
|
|
|
|
// DQL => SQL alias stored only locally, needed for SQL construction.
|
|
|
|
$this->_dqlToSqlAliasMap = array_flip($sqlToDqlAliasMap);
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
public function walkSelectStatement(AST\SelectStatement $AST)
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
$sql = $this->walkSelectClause($AST->getSelectClause());
|
|
|
|
$sql .= $this->walkFromClause($AST->getFromClause());
|
2009-01-20 20:07:07 +03:00
|
|
|
$sql .= $AST->getWhereClause() ? $this->walkWhereClause($AST->getWhereClause()) : '';
|
2009-01-19 21:40:12 +03:00
|
|
|
$sql .= $AST->getGroupByClause() ? $this->walkGroupByClause($AST->getGroupByClause()) : '';
|
2009-01-20 20:07:07 +03:00
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
//... more clauses
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function walkSelectClause($selectClause)
|
|
|
|
{
|
|
|
|
return 'SELECT ' . (($selectClause->isDistinct()) ? 'DISTINCT ' : '')
|
2009-03-23 20:39:33 +03:00
|
|
|
. implode(', ', array_map(array($this, 'walkSelectExpression'),
|
2009-01-19 21:40:12 +03:00
|
|
|
$selectClause->getSelectExpressions()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function walkFromClause($fromClause)
|
|
|
|
{
|
|
|
|
$sql = ' FROM ';
|
|
|
|
$identificationVarDecls = $fromClause->getIdentificationVariableDeclarations();
|
|
|
|
$firstIdentificationVarDecl = $identificationVarDecls[0];
|
|
|
|
$rangeDecl = $firstIdentificationVarDecl->getRangeVariableDeclaration();
|
|
|
|
$sql .= $rangeDecl->getClassMetadata()->getTableName() . ' '
|
|
|
|
. $this->_dqlToSqlAliasMap[$rangeDecl->getAliasIdentificationVariable()];
|
|
|
|
|
|
|
|
foreach ($firstIdentificationVarDecl->getJoinVariableDeclarations() as $joinVarDecl) {
|
|
|
|
$sql .= $this->walkJoinVariableDeclaration($joinVarDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-23 20:39:33 +03:00
|
|
|
public function walkFunction($function)
|
|
|
|
{
|
|
|
|
return $function->getSql($this);
|
|
|
|
}
|
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
/**
|
|
|
|
* Walks down a JoinVariableDeclaration AST node and creates the corresponding SQL.
|
|
|
|
*
|
|
|
|
* @param JoinVariableDeclaration $joinVarDecl
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function walkJoinVariableDeclaration($joinVarDecl)
|
|
|
|
{
|
|
|
|
$join = $joinVarDecl->getJoin();
|
|
|
|
$joinType = $join->getJoinType();
|
2009-03-23 20:39:33 +03:00
|
|
|
if ($joinType == AST\Join::JOIN_TYPE_LEFT || $joinType == AST\Join::JOIN_TYPE_LEFTOUTER) {
|
2009-01-19 21:40:12 +03:00
|
|
|
$sql = ' LEFT JOIN ';
|
|
|
|
} else {
|
|
|
|
$sql = ' INNER JOIN ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$joinAssocPathExpr = $join->getJoinAssociationPathExpression();
|
|
|
|
$sourceQComp = $this->_parserResult->getQueryComponent($joinAssocPathExpr->getIdentificationVariable());
|
|
|
|
$targetQComp = $this->_parserResult->getQueryComponent($join->getAliasIdentificationVariable());
|
|
|
|
$targetTableName = $targetQComp['metadata']->getTableName();
|
|
|
|
$targetTableAlias = $this->_dqlToSqlAliasMap[$join->getAliasIdentificationVariable()];
|
|
|
|
$sourceTableAlias = $this->_dqlToSqlAliasMap[$joinAssocPathExpr->getIdentificationVariable()];
|
|
|
|
|
|
|
|
$sql .= $targetTableName . ' ' . $targetTableAlias . ' ON ';
|
|
|
|
|
|
|
|
if ( ! $targetQComp['relation']->isOwningSide()) {
|
|
|
|
$assoc = $targetQComp['metadata']->getAssociationMapping($targetQComp['relation']->getMappedByFieldName());
|
|
|
|
} else {
|
|
|
|
$assoc = $targetQComp['relation'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($targetQComp['relation']->isOneToOne() || $targetQComp['relation']->isOneToMany()) {
|
|
|
|
$joinColumns = $assoc->getSourceToTargetKeyColumns();
|
|
|
|
$first = true;
|
|
|
|
foreach ($joinColumns as $sourceColumn => $targetColumn) {
|
|
|
|
if ( ! $first) $sql .= ' AND ';
|
|
|
|
if ($targetQComp['relation']->isOwningSide()) {
|
|
|
|
$sql .= "$sourceTableAlias.$sourceColumn = $targetTableAlias.$targetColumn";
|
|
|
|
} else {
|
|
|
|
$sql .= "$sourceTableAlias.$targetColumn = $targetTableAlias.$sourceColumn";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // ManyToMany
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Walks down a SelectExpression AST node and generates the corresponding SQL.
|
|
|
|
*
|
|
|
|
* @param <type> $selectExpression
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function walkSelectExpression($selectExpression)
|
|
|
|
{
|
|
|
|
$sql = '';
|
2009-03-21 15:49:58 +03:00
|
|
|
if ($selectExpression->getExpression() instanceof AST\StateFieldPathExpression) {
|
2009-01-19 21:40:12 +03:00
|
|
|
$pathExpression = $selectExpression->getExpression();
|
|
|
|
if ($pathExpression->isSimpleStateFieldPathExpression()) {
|
|
|
|
$parts = $pathExpression->getParts();
|
|
|
|
$numParts = count($parts);
|
|
|
|
$dqlAlias = $parts[0];
|
|
|
|
$fieldName = $parts[$numParts-1];
|
|
|
|
$qComp = $this->_parserResult->getQueryComponent($dqlAlias);
|
|
|
|
$class = $qComp['metadata'];
|
|
|
|
|
|
|
|
if ($numParts > 2) {
|
|
|
|
for ($i = 1; $i < $numParts-1; ++$i) {
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$sqlTableAlias = $this->_dqlToSqlAliasMap[$dqlAlias];
|
|
|
|
$sql .= $sqlTableAlias . '.' . $class->getColumnName($fieldName) .
|
|
|
|
' AS ' . $sqlTableAlias . '__' . $class->getColumnName($fieldName);
|
|
|
|
} else if ($pathExpression->isSimpleStateFieldAssociationPathExpression()) {
|
2009-03-23 20:39:33 +03:00
|
|
|
throw DoctrineException::updateMe("Not yet implemented.");
|
2009-01-19 21:40:12 +03:00
|
|
|
} else {
|
2009-03-23 20:39:33 +03:00
|
|
|
throw DoctrineException::updateMe("Encountered invalid PathExpression during SQL construction.");
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
else if ($selectExpression->getExpression() instanceof AST\AggregateExpression) {
|
2009-01-19 21:40:12 +03:00
|
|
|
$aggExpr = $selectExpression->getExpression();
|
|
|
|
if ( ! $selectExpression->getFieldIdentificationVariable()) {
|
|
|
|
$alias = $this->_scalarAliasCounter++;
|
|
|
|
} else {
|
|
|
|
$alias = $selectExpression->getFieldIdentificationVariable();
|
|
|
|
}
|
2009-03-19 15:43:48 +03:00
|
|
|
$sql .= $this->walkAggregateExpression($aggExpr) . ' AS dctrn__' . $alias;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-03-19 15:43:48 +03:00
|
|
|
else if ($selectExpression->getExpression() instanceof AST\Subselect) {
|
|
|
|
$sql .= $this->walkSubselect($selectExpression->getExpression());
|
|
|
|
} else {
|
2009-01-19 21:40:12 +03:00
|
|
|
$dqlAlias = $selectExpression->getExpression();
|
|
|
|
$queryComp = $this->_parserResult->getQueryComponent($dqlAlias);
|
|
|
|
$class = $queryComp['metadata'];
|
|
|
|
|
|
|
|
$sqlTableAlias = $this->_dqlToSqlAliasMap[$dqlAlias];
|
|
|
|
$beginning = true;
|
|
|
|
foreach ($class->getFieldMappings() as $fieldName => $fieldMapping) {
|
|
|
|
if ($beginning) {
|
|
|
|
$beginning = false;
|
|
|
|
} else {
|
|
|
|
$sql .= ', ';
|
|
|
|
}
|
|
|
|
$sql .= $sqlTableAlias . '.' . $fieldMapping['columnName'] .
|
|
|
|
' AS ' . $sqlTableAlias . '__' . $fieldMapping['columnName'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-19 15:43:48 +03:00
|
|
|
public function walkSubselect($subselect)
|
|
|
|
{
|
|
|
|
$sql = $this->walkSimpleSelectClause($subselect->getSimpleSelectClause());
|
|
|
|
$sql .= $this->walkSubselectFromClause($subselect->getSubselectFromClause());
|
|
|
|
$sql .= $subselect->getWhereClause() ? $this->walkWhereClause($subselect->getWhereClause()) : '';
|
|
|
|
$sql .= $subselect->getGroupByClause() ? $this->walkGroupByClause($subselect->getGroupByClause()) : '';
|
|
|
|
|
|
|
|
//... more clauses
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function walkSubselectFromClause($subselectFromClause)
|
|
|
|
{
|
|
|
|
$sql = ' FROM ';
|
|
|
|
$identificationVarDecls = $subselectFromClause->getSubselectIdentificationVariableDeclarations();
|
|
|
|
$firstIdentificationVarDecl = $identificationVarDecls[0];
|
|
|
|
$rangeDecl = $firstIdentificationVarDecl->getRangeVariableDeclaration();
|
|
|
|
$sql .= $rangeDecl->getClassMetadata()->getTableName() . ' '
|
|
|
|
. $this->_dqlToSqlAliasMap[$rangeDecl->getAliasIdentificationVariable()];
|
|
|
|
|
|
|
|
foreach ($firstIdentificationVarDecl->getJoinVariableDeclarations() as $joinVarDecl) {
|
|
|
|
$sql .= $this->walkJoinVariableDeclaration($joinVarDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function walkSimpleSelectClause($simpleSelectClause)
|
|
|
|
{
|
|
|
|
$sql = 'SELECT';
|
|
|
|
if ($simpleSelectClause->isDistinct()) {
|
|
|
|
$sql .= ' DISTINCT';
|
|
|
|
}
|
|
|
|
$sql .= $this->walkSimpleSelectExpression($simpleSelectClause->getSimpleSelectExpression());
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function walkSimpleSelectExpression($simpleSelectExpression)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
$expr = $simpleSelectExpression->getExpression();
|
2009-03-21 15:49:58 +03:00
|
|
|
if ($expr instanceof AST\StateFieldPathExpression) {
|
2009-03-19 15:43:48 +03:00
|
|
|
//...
|
|
|
|
} else if ($expr instanceof AST\AggregateExpression) {
|
|
|
|
if ( ! $simpleSelectExpression->getFieldIdentificationVariable()) {
|
|
|
|
$alias = $this->_scalarAliasCounter++;
|
|
|
|
} else {
|
|
|
|
$alias = $simpleSelectExpression->getFieldIdentificationVariable();
|
|
|
|
}
|
|
|
|
$sql .= $this->walkAggregateExpression($expr) . ' AS dctrn__' . $alias;
|
|
|
|
} else {
|
|
|
|
// $expr is IdentificationVariable
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function walkAggregateExpression($aggExpression)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
$parts = $aggExpression->getPathExpression()->getParts();
|
|
|
|
$dqlAlias = $parts[0];
|
|
|
|
$fieldName = $parts[1];
|
|
|
|
|
|
|
|
$qComp = $this->_parserResult->getQueryComponent($dqlAlias);
|
|
|
|
$columnName = $qComp['metadata']->getColumnName($fieldName);
|
|
|
|
|
|
|
|
$sql .= $aggExpression->getFunctionName() . '(';
|
|
|
|
if ($aggExpression->isDistinct()) $sql .= 'DISTINCT ';
|
|
|
|
$sql .= $this->_dqlToSqlAliasMap[$dqlAlias] . '.' . $columnName;
|
|
|
|
$sql .= ')';
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
public function walkGroupByClause($groupByClause)
|
|
|
|
{
|
|
|
|
return ' GROUP BY '
|
|
|
|
. implode(', ', array_map(array(&$this, 'walkGroupByItem'),
|
|
|
|
$groupByClause->getGroupByItems()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function walkGroupByItem($pathExpr)
|
|
|
|
{
|
|
|
|
//TODO: support general SingleValuedPathExpression, not just state field
|
|
|
|
$parts = $pathExpr->getParts();
|
|
|
|
$qComp = $this->_parserResult->getQueryComponent($parts[0]);
|
|
|
|
$columnName = $qComp['metadata']->getColumnName($parts[1]);
|
|
|
|
return $this->_dqlToSqlAliasMap[$parts[0]] . '.' . $columnName;
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
public function walkUpdateStatement(AST\UpdateStatement $AST)
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-21 22:58:52 +03:00
|
|
|
$sql = $this->walkUpdateClause($AST->getUpdateClause());
|
|
|
|
$sql .= $AST->getWhereClause() ? $this->walkWhereClause($AST->getWhereClause()) : '';
|
|
|
|
return $sql;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
public function walkDeleteStatement(AST\DeleteStatement $AST)
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-21 00:28:19 +03:00
|
|
|
$sql = $this->walkDeleteClause($AST->getDeleteClause());
|
|
|
|
$sql .= $AST->getWhereClause() ? $this->walkWhereClause($AST->getWhereClause()) : '';
|
|
|
|
return $sql;
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-03-21 00:28:19 +03:00
|
|
|
public function walkDeleteClause(AST\DeleteClause $deleteClause)
|
|
|
|
{
|
|
|
|
$sql = 'DELETE FROM ';
|
|
|
|
$class = $this->_em->getClassMetadata($deleteClause->getAbstractSchemaName());
|
|
|
|
$sql .= $class->getTableName();
|
|
|
|
if ($deleteClause->getAliasIdentificationVariable()) {
|
|
|
|
$sql .= ' ' . $this->_dqlToSqlAliasMap[$deleteClause->getAliasIdentificationVariable()];
|
|
|
|
}
|
|
|
|
return $sql;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-03-21 22:58:52 +03:00
|
|
|
public function walkUpdateClause($updateClause)
|
|
|
|
{
|
|
|
|
$sql = 'UPDATE ';
|
|
|
|
$class = $this->_em->getClassMetadata($updateClause->getAbstractSchemaName());
|
|
|
|
$sql .= $class->getTableName();
|
|
|
|
if ($updateClause->getAliasIdentificationVariable()) {
|
|
|
|
$sql .= ' ' . $this->_dqlToSqlAliasMap[$updateClause->getAliasIdentificationVariable()];
|
|
|
|
}
|
|
|
|
$sql .= ' SET ' . implode(', ', array_map(array($this, 'walkUpdateItem'),
|
|
|
|
$updateClause->getUpdateItems()));
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function walkUpdateItem($updateItem)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
$dqlAlias = $updateItem->getIdentificationVariable() ?
|
|
|
|
$updateItem->getIdentificationVariable() :
|
|
|
|
$this->_parserResult->getDefaultQueryComponentAlias();
|
|
|
|
$qComp = $this->_parserResult->getQueryComponent($dqlAlias);
|
|
|
|
|
|
|
|
$sql .= $this->_dqlToSqlAliasMap[$dqlAlias] . '.'
|
|
|
|
. $qComp['metadata']->getColumnName($updateItem->getField())
|
|
|
|
. ' = ';
|
|
|
|
|
|
|
|
$newValue = $updateItem->getNewValue();
|
2009-03-23 20:39:33 +03:00
|
|
|
|
|
|
|
if ($newValue instanceof AST\Node) {
|
|
|
|
$sql .= $newValue->dispatch($this);
|
|
|
|
} else if (is_string($newValue)) {
|
|
|
|
if (strcasecmp($newValue, 'NULL') === 0) {
|
|
|
|
$sql .= 'NULL';
|
|
|
|
} else {
|
|
|
|
$sql .= $newValue; //TODO: quote()
|
|
|
|
}
|
|
|
|
}
|
2009-03-21 22:58:52 +03:00
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkWhereClause($whereClause)
|
|
|
|
{
|
|
|
|
$sql = ' WHERE ';
|
|
|
|
$condExpr = $whereClause->getConditionalExpression();
|
2009-03-23 20:39:33 +03:00
|
|
|
$sql .= implode(' OR ', array_map(array($this, 'walkConditionalTerm'),
|
2009-01-21 21:25:05 +03:00
|
|
|
$condExpr->getConditionalTerms()));
|
2009-01-20 20:07:07 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function walkConditionalTerm($condTerm)
|
|
|
|
{
|
2009-03-23 20:39:33 +03:00
|
|
|
return implode(' AND ', array_map(array($this, 'walkConditionalFactor'),
|
2009-01-21 21:25:05 +03:00
|
|
|
$condTerm->getConditionalFactors()));
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function walkConditionalFactor($factor)
|
|
|
|
{
|
|
|
|
$sql = '';
|
2009-03-21 00:28:19 +03:00
|
|
|
if ($factor->isNot()) $sql .= 'NOT ';
|
2009-01-20 20:07:07 +03:00
|
|
|
$primary = $factor->getConditionalPrimary();
|
|
|
|
if ($primary->isSimpleConditionalExpression()) {
|
2009-03-23 20:39:33 +03:00
|
|
|
$sql .= $primary->getSimpleConditionalExpression()->dispatch($this);
|
2009-01-21 21:25:05 +03:00
|
|
|
} else if ($primary->isConditionalExpression()) {
|
2009-03-23 20:39:33 +03:00
|
|
|
$sql .= '(' . implode(' OR ', array_map(array($this, 'walkConditionalTerm'),
|
2009-01-21 21:25:05 +03:00
|
|
|
$primary->getConditionalExpression()->getConditionalTerms())) . ')';
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
2009-01-21 21:25:05 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
2009-01-20 20:07:07 +03:00
|
|
|
|
2009-03-23 20:39:33 +03:00
|
|
|
public function walkExistsExpression($existsExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
if ($existsExpr->isNot()) $sql .= ' NOT';
|
|
|
|
$sql .= ' EXISTS (' . $this->walkSubselect($existsExpr->getSubselect()) . ')';
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
public function walkNullComparisonExpression($nullCompExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
2009-03-23 20:39:33 +03:00
|
|
|
$innerExpr = $nullCompExpr->getExpression();
|
|
|
|
if ($innerExpr instanceof AST\InputParameter) {
|
|
|
|
$sql .= ' ' . ($innerExpr->isNamed() ? ':' . $innerExpr->getName() : '?');
|
2009-03-21 15:49:58 +03:00
|
|
|
} else {
|
2009-03-23 20:39:33 +03:00
|
|
|
$sql .= $this->walkPathExpression($innerExpr);
|
2009-03-21 15:49:58 +03:00
|
|
|
}
|
|
|
|
$sql .= ' IS' . ($nullCompExpr->isNot() ? ' NOT' : '') . ' NULL';
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function walkInExpression($inExpr)
|
|
|
|
{
|
|
|
|
$sql = $this->walkPathExpression($inExpr->getPathExpression());
|
|
|
|
if ($inExpr->isNot()) $sql .= ' NOT';
|
|
|
|
$sql .= ' IN (';
|
|
|
|
if ($inExpr->getSubselect()) {
|
|
|
|
$sql .= $this->walkSubselect($inExpr->getSubselect());
|
|
|
|
} else {
|
2009-03-21 22:58:52 +03:00
|
|
|
$sql .= implode(', ', array_map(array($this, 'walkLiteral'), $inExpr->getLiterals()));
|
2009-03-21 15:49:58 +03:00
|
|
|
}
|
|
|
|
$sql .= ')';
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-21 22:58:52 +03:00
|
|
|
public function walkLiteral($literal)
|
|
|
|
{
|
|
|
|
if ($literal instanceof AST\InputParameter) {
|
|
|
|
return ($literal->isNamed() ? ':' . $literal->getName() : '?');
|
|
|
|
} else {
|
2009-03-23 20:39:33 +03:00
|
|
|
return $literal; //TODO: quote() ?
|
2009-03-21 22:58:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
public function walkBetweenExpression($betweenExpr)
|
|
|
|
{
|
|
|
|
$sql = $this->walkArithmeticExpression($betweenExpr->getBaseExpression());
|
|
|
|
if ($betweenExpr->getNot()) $sql .= ' NOT';
|
|
|
|
$sql .= ' BETWEEN ' . $this->walkArithmeticExpression($betweenExpr->getLeftBetweenExpression())
|
|
|
|
. ' AND ' . $this->walkArithmeticExpression($betweenExpr->getRightBetweenExpression());
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-01-21 21:25:05 +03:00
|
|
|
public function walkLikeExpression($likeExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
$stringExpr = $likeExpr->getStringExpression();
|
2009-03-23 20:39:33 +03:00
|
|
|
|
|
|
|
$sql .= $stringExpr->dispatch($this);
|
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
if ($likeExpr->isNot()) $sql .= ' NOT';
|
|
|
|
$sql .= ' LIKE ';
|
2009-03-23 20:39:33 +03:00
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
if ($likeExpr->getStringPattern() instanceof AST\InputParameter) {
|
|
|
|
$inputParam = $likeExpr->getStringPattern();
|
|
|
|
$sql .= $inputParam->isNamed() ? ':' . $inputParam->getName() : '?';
|
|
|
|
} else {
|
|
|
|
$sql .= $likeExpr->getStringPattern();
|
|
|
|
}
|
|
|
|
if ($likeExpr->getEscapeChar()) {
|
|
|
|
$sql .= ' ESCAPE ' . $likeExpr->getEscapeChar();
|
|
|
|
}
|
2009-01-20 20:07:07 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-23 20:39:33 +03:00
|
|
|
public function walkStateFieldPathExpression($stateFieldPathExpression)
|
|
|
|
{
|
|
|
|
return $this->walkPathExpression($stateFieldPathExpression);
|
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkComparisonExpression($compExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
2009-03-23 20:39:33 +03:00
|
|
|
$leftExpr = $compExpr->getLeftExpression();
|
|
|
|
$rightExpr = $compExpr->getRightExpression();
|
|
|
|
|
|
|
|
if ($leftExpr instanceof AST\Node) {
|
|
|
|
$sql .= $leftExpr->dispatch($this);
|
|
|
|
} else {
|
|
|
|
$sql .= $leftExpr; //TODO: quote()
|
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
$sql .= ' ' . $compExpr->getOperator() . ' ';
|
2009-03-23 20:39:33 +03:00
|
|
|
|
|
|
|
if ($rightExpr instanceof AST\Node) {
|
|
|
|
$sql .= $rightExpr->dispatch($this);
|
|
|
|
} else {
|
|
|
|
$sql .= $rightExpr; //TODO: quote()
|
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-03-23 20:39:33 +03:00
|
|
|
public function walkInputParameter($inputParam)
|
|
|
|
{
|
|
|
|
return $inputParam->isNamed() ? ':' . $inputParam->getName() : '?';
|
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkArithmeticExpression($arithmeticExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
if ($arithmeticExpr->isSimpleArithmeticExpression()) {
|
|
|
|
foreach ($arithmeticExpr->getSimpleArithmeticExpression()->getArithmeticTerms() as $term) {
|
|
|
|
$sql .= $this->walkArithmeticTerm($term);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$sql .= $this->walkSubselect($arithmeticExpr->getSubselect());
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function walkArithmeticTerm($term)
|
|
|
|
{
|
2009-01-21 21:25:05 +03:00
|
|
|
if (is_string($term)) return $term;
|
2009-03-23 20:39:33 +03:00
|
|
|
|
|
|
|
return implode(' ', array_map(array($this, 'walkArithmeticFactor'),
|
2009-01-21 21:25:05 +03:00
|
|
|
$term->getArithmeticFactors()));
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
2009-03-23 20:39:33 +03:00
|
|
|
public function walkStringPrimary($stringPrimary)
|
|
|
|
{
|
|
|
|
if (is_string($stringPrimary)) {
|
|
|
|
return $stringPrimary;
|
|
|
|
} else {
|
|
|
|
return $stringPrimary->dispatch($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkArithmeticFactor($factor)
|
|
|
|
{
|
2009-01-21 21:25:05 +03:00
|
|
|
if (is_string($factor)) return $factor;
|
2009-03-23 20:39:33 +03:00
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
$sql = '';
|
|
|
|
$primary = $factor->getArithmeticPrimary();
|
2009-01-21 21:25:05 +03:00
|
|
|
if (is_numeric($primary)) {
|
2009-03-23 20:39:33 +03:00
|
|
|
$sql .= $primary; //TODO: quote() ?
|
2009-01-22 22:38:10 +03:00
|
|
|
} else if (is_string($primary)) {
|
|
|
|
//TODO: quote string according to platform
|
|
|
|
$sql .= $primary;
|
|
|
|
} else if ($primary instanceof AST\SimpleArithmeticExpression) {
|
2009-01-21 21:25:05 +03:00
|
|
|
$sql .= '(' . $this->walkSimpleArithmeticExpression($primary) . ')';
|
2009-03-23 20:39:33 +03:00
|
|
|
} else if ($primary instanceof AST\Node) {
|
|
|
|
$sql .= $primary->dispatch($this);
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-01-21 21:25:05 +03:00
|
|
|
public function walkSimpleArithmeticExpression($simpleArithmeticExpr)
|
|
|
|
{
|
2009-03-23 20:39:33 +03:00
|
|
|
return implode(' ', array_map(array($this, 'walkArithmeticTerm'),
|
2009-01-21 21:25:05 +03:00
|
|
|
$simpleArithmeticExpr->getArithmeticTerms()));
|
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
public function walkPathExpression($pathExpr)
|
|
|
|
{
|
|
|
|
$sql = '';
|
|
|
|
if ($pathExpr->isSimpleStateFieldPathExpression()) {
|
|
|
|
$parts = $pathExpr->getParts();
|
|
|
|
$numParts = count($parts);
|
|
|
|
$dqlAlias = $parts[0];
|
|
|
|
$fieldName = $parts[$numParts-1];
|
|
|
|
$qComp = $this->_parserResult->getQueryComponent($dqlAlias);
|
|
|
|
$class = $qComp['metadata'];
|
|
|
|
|
|
|
|
if ($numParts > 2) {
|
|
|
|
for ($i = 1; $i < $numParts-1; ++$i) {
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$sqlTableAlias = $this->_dqlToSqlAliasMap[$dqlAlias];
|
|
|
|
$sql .= $sqlTableAlias . '.' . $class->getColumnName($fieldName);
|
|
|
|
} else if ($pathExpr->isSimpleStateFieldAssociationPathExpression()) {
|
2009-03-23 20:39:33 +03:00
|
|
|
throw DoctrineException::updateMe("Not yet implemented.");
|
2009-01-20 20:07:07 +03:00
|
|
|
} else {
|
2009-03-23 20:39:33 +03:00
|
|
|
throw DoctrineException::updateMe("Encountered invalid PathExpression during SQL construction.");
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
/**
|
2009-02-17 13:54:18 +03:00
|
|
|
* Generates a unique, short SQL table alias.
|
2009-01-19 21:40:12 +03:00
|
|
|
*
|
2009-02-17 13:54:18 +03:00
|
|
|
* @param string $tableName Table name.
|
|
|
|
* @return string Generated table alias.
|
2009-01-19 21:40:12 +03:00
|
|
|
*/
|
2009-02-17 13:54:18 +03:00
|
|
|
public function generateSqlTableAlias($tableName)
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-02-17 13:54:18 +03:00
|
|
|
return strtolower(substr($tableName, 0, 1)) . $this->_tableAliasCounter++;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|