MultiTableUpdateExecutor.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Query\Exec;
  20. use Doctrine\DBAL\Connection,
  21. Doctrine\DBAL\Types\Type,
  22. Doctrine\ORM\Query\AST;
  23. /**
  24. * Executes the SQL statements for bulk DQL UPDATE statements on classes in
  25. * Class Table Inheritance (JOINED).
  26. *
  27. * @author Roman Borschel <roman@code-factory.org>
  28. * @since 2.0
  29. */
  30. class MultiTableUpdateExecutor extends AbstractSqlExecutor
  31. {
  32. private $_createTempTableSql;
  33. private $_dropTempTableSql;
  34. private $_insertSql;
  35. private $_sqlParameters = array();
  36. private $_numParametersInUpdateClause = 0;
  37. /**
  38. * Initializes a new <tt>MultiTableUpdateExecutor</tt>.
  39. *
  40. * @param Node $AST The root AST node of the DQL query.
  41. * @param SqlWalker $sqlWalker The walker used for SQL generation from the AST.
  42. * @internal Any SQL construction and preparation takes place in the constructor for
  43. * best performance. With a query cache the executor will be cached.
  44. */
  45. public function __construct(AST\Node $AST, $sqlWalker)
  46. {
  47. $em = $sqlWalker->getEntityManager();
  48. $conn = $em->getConnection();
  49. $platform = $conn->getDatabasePlatform();
  50. $updateClause = $AST->updateClause;
  51. $primaryClass = $sqlWalker->getEntityManager()->getClassMetadata($updateClause->abstractSchemaName);
  52. $rootClass = $em->getClassMetadata($primaryClass->rootEntityName);
  53. $updateItems = $updateClause->updateItems;
  54. $tempTable = $platform->getTemporaryTableName($rootClass->getTemporaryIdTableName());
  55. $idColumnNames = $rootClass->getIdentifierColumnNames();
  56. $idColumnList = implode(', ', $idColumnNames);
  57. // 1. Create an INSERT INTO temptable ... SELECT identifiers WHERE $AST->getWhereClause()
  58. $sqlWalker->setSQLTableAlias($primaryClass->getTableName(), 't0', $updateClause->aliasIdentificationVariable);
  59. $this->_insertSql = 'INSERT INTO ' . $tempTable . ' (' . $idColumnList . ')'
  60. . ' SELECT t0.' . implode(', t0.', $idColumnNames);
  61. $rangeDecl = new AST\RangeVariableDeclaration($primaryClass->name, $updateClause->aliasIdentificationVariable);
  62. $fromClause = new AST\FromClause(array(new AST\IdentificationVariableDeclaration($rangeDecl, null, array())));
  63. $this->_insertSql .= $sqlWalker->walkFromClause($fromClause);
  64. // 2. Create ID subselect statement used in UPDATE ... WHERE ... IN (subselect)
  65. $idSubselect = 'SELECT ' . $idColumnList . ' FROM ' . $tempTable;
  66. // 3. Create and store UPDATE statements
  67. $classNames = array_merge($primaryClass->parentClasses, array($primaryClass->name), $primaryClass->subClasses);
  68. $i = -1;
  69. foreach (array_reverse($classNames) as $className) {
  70. $affected = false;
  71. $class = $em->getClassMetadata($className);
  72. $updateSql = 'UPDATE ' . $class->getQuotedTableName($platform) . ' SET ';
  73. foreach ($updateItems as $updateItem) {
  74. $field = $updateItem->pathExpression->field;
  75. if (isset($class->fieldMappings[$field]) && ! isset($class->fieldMappings[$field]['inherited']) ||
  76. isset($class->associationMappings[$field]) && ! isset($class->associationMappings[$field]['inherited'])) {
  77. $newValue = $updateItem->newValue;
  78. if ( ! $affected) {
  79. $affected = true;
  80. ++$i;
  81. } else {
  82. $updateSql .= ', ';
  83. }
  84. $updateSql .= $sqlWalker->walkUpdateItem($updateItem);
  85. //FIXME: parameters can be more deeply nested. traverse the tree.
  86. //FIXME (URGENT): With query cache the parameter is out of date. Move to execute() stage.
  87. if ($newValue instanceof AST\InputParameter) {
  88. $paramKey = $newValue->name;
  89. $this->_sqlParameters[$i]['parameters'][] = $sqlWalker->getQuery()->getParameter($paramKey);
  90. $this->_sqlParameters[$i]['types'][] = $sqlWalker->getQuery()->getParameterType($paramKey);
  91. ++$this->_numParametersInUpdateClause;
  92. }
  93. }
  94. }
  95. if ($affected) {
  96. $this->_sqlStatements[$i] = $updateSql . ' WHERE (' . $idColumnList . ') IN (' . $idSubselect . ')';
  97. }
  98. }
  99. // Append WHERE clause to insertSql, if there is one.
  100. if ($AST->whereClause) {
  101. $this->_insertSql .= $sqlWalker->walkWhereClause($AST->whereClause);
  102. }
  103. // 4. Store DDL for temporary identifier table.
  104. $columnDefinitions = array();
  105. foreach ($idColumnNames as $idColumnName) {
  106. $columnDefinitions[$idColumnName] = array(
  107. 'notnull' => true,
  108. 'type' => Type::getType($rootClass->getTypeOfColumn($idColumnName))
  109. );
  110. }
  111. $this->_createTempTableSql = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
  112. . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
  113. $this->_dropTempTableSql = $platform->getDropTemporaryTableSQL($tempTable);
  114. }
  115. /**
  116. * {@inheritDoc}
  117. */
  118. public function execute(Connection $conn, array $params, array $types)
  119. {
  120. $numUpdated = 0;
  121. // Create temporary id table
  122. $conn->executeUpdate($this->_createTempTableSql);
  123. // Insert identifiers. Parameters from the update clause are cut off.
  124. $numUpdated = $conn->executeUpdate(
  125. $this->_insertSql,
  126. array_slice($params, $this->_numParametersInUpdateClause),
  127. array_slice($types, $this->_numParametersInUpdateClause)
  128. );
  129. // Execute UPDATE statements
  130. for ($i=0, $count=count($this->_sqlStatements); $i<$count; ++$i) {
  131. $parameters = array();
  132. $types = array();
  133. if (isset($this->_sqlParameters[$i])) {
  134. $parameters = isset($this->_sqlParameters[$i]['parameters']) ? $this->_sqlParameters[$i]['parameters'] : array();
  135. $types = isset($this->_sqlParameters[$i]['types']) ? $this->_sqlParameters[$i]['types'] : array();
  136. }
  137. $conn->executeUpdate($this->_sqlStatements[$i], $parameters, $types);
  138. }
  139. // Drop temporary table
  140. $conn->executeUpdate($this->_dropTempTableSql);
  141. return $numUpdated;
  142. }
  143. }