2008-09-23 06:47:11 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* 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-05-24 14:38:37 +04:00
|
|
|
* <http://www.doctrine-project.org>.
|
2008-09-23 06:47:11 +04:00
|
|
|
*/
|
|
|
|
|
2009-02-20 08:46:20 +03:00
|
|
|
namespace Doctrine\ORM\Query\Exec;
|
|
|
|
|
2012-05-28 20:16:42 +04:00
|
|
|
use Doctrine\DBAL\Connection;
|
|
|
|
use Doctrine\DBAL\Types\Type;
|
|
|
|
|
|
|
|
use Doctrine\ORM\Query\ParameterTypeInferer;
|
|
|
|
use Doctrine\ORM\Query\AST;
|
2009-06-23 21:50:13 +04:00
|
|
|
|
2008-09-23 06:47:11 +04:00
|
|
|
/**
|
|
|
|
* Executes the SQL statements for bulk DQL UPDATE statements on classes in
|
|
|
|
* Class Table Inheritance (JOINED).
|
|
|
|
*
|
2010-04-14 19:07:08 +04:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @since 2.0
|
2008-09-23 06:47:11 +04:00
|
|
|
*/
|
2009-07-21 13:25:14 +04:00
|
|
|
class MultiTableUpdateExecutor extends AbstractSqlExecutor
|
2008-09-23 06:47:11 +04:00
|
|
|
{
|
2012-12-13 15:57:46 +04:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2009-06-23 21:50:13 +04:00
|
|
|
private $_createTempTableSql;
|
2012-12-13 15:57:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2009-06-23 21:50:13 +04:00
|
|
|
private $_dropTempTableSql;
|
2012-12-13 15:57:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2009-06-23 21:50:13 +04:00
|
|
|
private $_insertSql;
|
2012-12-13 15:57:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-06-23 21:50:13 +04:00
|
|
|
private $_sqlParameters = array();
|
2012-12-13 15:57:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
2009-06-23 21:50:13 +04:00
|
|
|
private $_numParametersInUpdateClause = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new <tt>MultiTableUpdateExecutor</tt>.
|
|
|
|
*
|
2012-12-13 15:57:46 +04:00
|
|
|
* @param \Doctrine\ORM\Query\AST\Node $AST The root AST node of the DQL query.
|
|
|
|
* @param \Doctrine\ORM\Query\SqlWalker $sqlWalker The walker used for SQL generation from the AST.
|
|
|
|
*
|
2009-06-23 21:50:13 +04:00
|
|
|
* @internal Any SQL construction and preparation takes place in the constructor for
|
|
|
|
* best performance. With a query cache the executor will be cached.
|
|
|
|
*/
|
|
|
|
public function __construct(AST\Node $AST, $sqlWalker)
|
2008-09-23 06:47:11 +04:00
|
|
|
{
|
2012-06-04 21:26:05 +04:00
|
|
|
$em = $sqlWalker->getEntityManager();
|
|
|
|
$conn = $em->getConnection();
|
|
|
|
$platform = $conn->getDatabasePlatform();
|
2012-06-12 03:14:17 +04:00
|
|
|
$quoteStrategy = $em->getConfiguration()->getQuoteStrategy();
|
2011-02-20 07:30:58 +03:00
|
|
|
|
2012-06-04 21:26:05 +04:00
|
|
|
$updateClause = $AST->updateClause;
|
|
|
|
$primaryClass = $sqlWalker->getEntityManager()->getClassMetadata($updateClause->abstractSchemaName);
|
|
|
|
$rootClass = $em->getClassMetadata($primaryClass->rootEntityName);
|
2009-06-23 21:50:13 +04:00
|
|
|
|
2012-06-04 21:26:05 +04:00
|
|
|
$updateItems = $updateClause->updateItems;
|
2009-06-23 21:50:13 +04:00
|
|
|
|
2012-06-04 21:26:05 +04:00
|
|
|
$tempTable = $platform->getTemporaryTableName($rootClass->getTemporaryIdTableName());
|
|
|
|
$idColumnNames = $rootClass->getIdentifierColumnNames();
|
|
|
|
$idColumnList = implode(', ', $idColumnNames);
|
2009-06-23 21:50:13 +04:00
|
|
|
|
|
|
|
// 1. Create an INSERT INTO temptable ... SELECT identifiers WHERE $AST->getWhereClause()
|
2011-09-07 08:48:19 +04:00
|
|
|
$sqlWalker->setSQLTableAlias($primaryClass->getTableName(), 't0', $updateClause->aliasIdentificationVariable);
|
2011-02-20 07:30:58 +03:00
|
|
|
|
2009-06-23 21:50:13 +04:00
|
|
|
$this->_insertSql = 'INSERT INTO ' . $tempTable . ' (' . $idColumnList . ')'
|
|
|
|
. ' SELECT t0.' . implode(', t0.', $idColumnNames);
|
2011-02-20 07:30:58 +03:00
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
$rangeDecl = new AST\RangeVariableDeclaration($primaryClass->name, $updateClause->aliasIdentificationVariable);
|
2009-06-23 21:50:13 +04:00
|
|
|
$fromClause = new AST\FromClause(array(new AST\IdentificationVariableDeclaration($rangeDecl, null, array())));
|
2011-02-20 07:30:58 +03:00
|
|
|
|
2009-06-23 21:50:13 +04:00
|
|
|
$this->_insertSql .= $sqlWalker->walkFromClause($fromClause);
|
|
|
|
|
|
|
|
// 2. Create ID subselect statement used in UPDATE ... WHERE ... IN (subselect)
|
|
|
|
$idSubselect = 'SELECT ' . $idColumnList . ' FROM ' . $tempTable;
|
|
|
|
|
|
|
|
// 3. Create and store UPDATE statements
|
|
|
|
$classNames = array_merge($primaryClass->parentClasses, array($primaryClass->name), $primaryClass->subClasses);
|
|
|
|
$i = -1;
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-06-23 21:50:13 +04:00
|
|
|
foreach (array_reverse($classNames) as $className) {
|
|
|
|
$affected = false;
|
|
|
|
$class = $em->getClassMetadata($className);
|
2012-06-12 03:14:17 +04:00
|
|
|
$updateSql = 'UPDATE ' . $quoteStrategy->getTableName($class, $platform) . ' SET ';
|
2009-06-23 21:50:13 +04:00
|
|
|
|
|
|
|
foreach ($updateItems as $updateItem) {
|
2010-07-23 08:55:33 +04:00
|
|
|
$field = $updateItem->pathExpression->field;
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-07-08 19:30:39 +04:00
|
|
|
if (isset($class->fieldMappings[$field]) && ! isset($class->fieldMappings[$field]['inherited']) ||
|
2011-02-20 07:30:58 +03:00
|
|
|
isset($class->associationMappings[$field]) && ! isset($class->associationMappings[$field]['inherited'])) {
|
2009-08-07 01:42:07 +04:00
|
|
|
$newValue = $updateItem->newValue;
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-06-23 21:50:13 +04:00
|
|
|
if ( ! $affected) {
|
|
|
|
$affected = true;
|
|
|
|
++$i;
|
|
|
|
} else {
|
|
|
|
$updateSql .= ', ';
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-06-23 21:50:13 +04:00
|
|
|
$updateSql .= $sqlWalker->walkUpdateItem($updateItem);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-06-23 21:50:13 +04:00
|
|
|
//FIXME: parameters can be more deeply nested. traverse the tree.
|
2010-03-29 17:20:41 +04:00
|
|
|
//FIXME (URGENT): With query cache the parameter is out of date. Move to execute() stage.
|
2009-06-23 21:50:13 +04:00
|
|
|
if ($newValue instanceof AST\InputParameter) {
|
2012-05-28 20:16:42 +04:00
|
|
|
$parameterName = $newValue->name;
|
|
|
|
$parameter = $sqlWalker->getQuery()->getParameter($parameterName);
|
|
|
|
|
|
|
|
$value = $sqlWalker->getQuery()->processParameterValue($parameter->getValue());
|
|
|
|
$type = ($parameter->getValue() === $value)
|
|
|
|
? $parameter->getType()
|
|
|
|
: ParameterTypeInferer::inferType($value);
|
|
|
|
|
|
|
|
$this->_sqlParameters[$i]['parameters'][] = $value;
|
|
|
|
$this->_sqlParameters[$i]['types'][] = $type;
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-06-23 21:50:13 +04:00
|
|
|
++$this->_numParametersInUpdateClause;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($affected) {
|
|
|
|
$this->_sqlStatements[$i] = $updateSql . ' WHERE (' . $idColumnList . ') IN (' . $idSubselect . ')';
|
|
|
|
}
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-06-23 21:50:13 +04:00
|
|
|
// Append WHERE clause to insertSql, if there is one.
|
2009-08-07 01:42:07 +04:00
|
|
|
if ($AST->whereClause) {
|
|
|
|
$this->_insertSql .= $sqlWalker->walkWhereClause($AST->whereClause);
|
2009-06-23 21:50:13 +04:00
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-06-23 21:50:13 +04:00
|
|
|
// 4. Store DDL for temporary identifier table.
|
|
|
|
$columnDefinitions = array();
|
2011-02-20 07:30:58 +03:00
|
|
|
|
2009-06-23 21:50:13 +04:00
|
|
|
foreach ($idColumnNames as $idColumnName) {
|
|
|
|
$columnDefinitions[$idColumnName] = array(
|
|
|
|
'notnull' => true,
|
2010-03-29 17:20:41 +04:00
|
|
|
'type' => Type::getType($rootClass->getTypeOfColumn($idColumnName))
|
2009-06-23 21:50:13 +04:00
|
|
|
);
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
$this->_createTempTableSql = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
|
2010-04-18 21:12:38 +04:00
|
|
|
. $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-09-25 21:01:37 +04:00
|
|
|
$this->_dropTempTableSql = $platform->getDropTemporaryTableSQL($tempTable);
|
2008-09-23 06:47:11 +04:00
|
|
|
}
|
2009-06-23 21:50:13 +04:00
|
|
|
|
2008-09-23 06:47:11 +04:00
|
|
|
/**
|
2011-10-24 01:28:23 +04:00
|
|
|
* {@inheritDoc}
|
2008-09-23 06:47:11 +04:00
|
|
|
*/
|
2010-03-29 17:20:41 +04:00
|
|
|
public function execute(Connection $conn, array $params, array $types)
|
2008-09-23 06:47:11 +04:00
|
|
|
{
|
2009-06-23 21:50:13 +04:00
|
|
|
$numUpdated = 0;
|
|
|
|
|
|
|
|
// Create temporary id table
|
2009-08-11 02:43:27 +04:00
|
|
|
$conn->executeUpdate($this->_createTempTableSql);
|
2009-06-23 21:50:13 +04:00
|
|
|
|
2012-05-23 08:55:05 +04:00
|
|
|
try {
|
|
|
|
// Insert identifiers. Parameters from the update clause are cut off.
|
|
|
|
$numUpdated = $conn->executeUpdate(
|
|
|
|
$this->_insertSql,
|
|
|
|
array_slice($params, $this->_numParametersInUpdateClause),
|
|
|
|
array_slice($types, $this->_numParametersInUpdateClause)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Execute UPDATE statements
|
|
|
|
for ($i=0, $count=count($this->_sqlStatements); $i<$count; ++$i) {
|
|
|
|
$parameters = array();
|
|
|
|
$types = array();
|
|
|
|
|
|
|
|
if (isset($this->_sqlParameters[$i])) {
|
|
|
|
$parameters = isset($this->_sqlParameters[$i]['parameters']) ? $this->_sqlParameters[$i]['parameters'] : array();
|
|
|
|
$types = isset($this->_sqlParameters[$i]['types']) ? $this->_sqlParameters[$i]['types'] : array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$conn->executeUpdate($this->_sqlStatements[$i], $parameters, $types);
|
2011-08-28 20:48:15 +04:00
|
|
|
}
|
2012-05-23 08:55:05 +04:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
// FAILURE! Drop temporary table to avoid possible collisions
|
|
|
|
$conn->executeUpdate($this->_dropTempTableSql);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-05-23 08:55:05 +04:00
|
|
|
// Re-throw exception
|
|
|
|
throw $exception;
|
2009-06-23 21:50:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Drop temporary table
|
2009-08-11 02:43:27 +04:00
|
|
|
$conn->executeUpdate($this->_dropTempTableSql);
|
2009-06-23 21:50:13 +04:00
|
|
|
|
|
|
|
return $numUpdated;
|
2008-09-23 06:47:11 +04:00
|
|
|
}
|
2012-05-26 16:37:00 +04:00
|
|
|
}
|