MultiTableDeleteExecutor.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\ORM\Query\AST;
  22. /**
  23. * Executes the SQL statements for bulk DQL DELETE statements on classes in
  24. * Class Table Inheritance (JOINED).
  25. *
  26. * @author Roman Borschel <roman@code-factory.org>
  27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  28. * @link http://www.doctrine-project.org
  29. * @since 2.0
  30. */
  31. class MultiTableDeleteExecutor extends AbstractSqlExecutor
  32. {
  33. private $_createTempTableSql;
  34. private $_dropTempTableSql;
  35. private $_insertSql;
  36. /**
  37. * Initializes a new <tt>MultiTableDeleteExecutor</tt>.
  38. *
  39. * @param Node $AST The root AST node of the DQL query.
  40. * @param SqlWalker $sqlWalker The walker used for SQL generation from the AST.
  41. * @internal Any SQL construction and preparation takes place in the constructor for
  42. * best performance. With a query cache the executor will be cached.
  43. */
  44. public function __construct(AST\Node $AST, $sqlWalker)
  45. {
  46. $em = $sqlWalker->getEntityManager();
  47. $conn = $em->getConnection();
  48. $platform = $conn->getDatabasePlatform();
  49. $primaryClass = $em->getClassMetadata($AST->deleteClause->abstractSchemaName);
  50. $primaryDqlAlias = $AST->deleteClause->aliasIdentificationVariable;
  51. $rootClass = $em->getClassMetadata($primaryClass->rootEntityName);
  52. $tempTable = $platform->getTemporaryTableName($rootClass->getTemporaryIdTableName());
  53. $idColumnNames = $rootClass->getIdentifierColumnNames();
  54. $idColumnList = implode(', ', $idColumnNames);
  55. // 1. Create an INSERT INTO temptable ... SELECT identifiers WHERE $AST->getWhereClause()
  56. $sqlWalker->setSQLTableAlias($primaryClass->getTableName(), 't0', $primaryDqlAlias);
  57. $this->_insertSql = 'INSERT INTO ' . $tempTable . ' (' . $idColumnList . ')'
  58. . ' SELECT t0.' . implode(', t0.', $idColumnNames);
  59. $rangeDecl = new AST\RangeVariableDeclaration($primaryClass->name, $primaryDqlAlias);
  60. $fromClause = new AST\FromClause(array(new AST\IdentificationVariableDeclaration($rangeDecl, null, array())));
  61. $this->_insertSql .= $sqlWalker->walkFromClause($fromClause);
  62. // Append WHERE clause, if there is one.
  63. if ($AST->whereClause) {
  64. $this->_insertSql .= $sqlWalker->walkWhereClause($AST->whereClause);
  65. }
  66. // 2. Create ID subselect statement used in DELETE ... WHERE ... IN (subselect)
  67. $idSubselect = 'SELECT ' . $idColumnList . ' FROM ' . $tempTable;
  68. // 3. Create and store DELETE statements
  69. $classNames = array_merge($primaryClass->parentClasses, array($primaryClass->name), $primaryClass->subClasses);
  70. foreach (array_reverse($classNames) as $className) {
  71. $tableName = $em->getClassMetadata($className)->getQuotedTableName($platform);
  72. $this->_sqlStatements[] = 'DELETE FROM ' . $tableName
  73. . ' WHERE (' . $idColumnList . ') IN (' . $idSubselect . ')';
  74. }
  75. // 4. Store DDL for temporary identifier table.
  76. $columnDefinitions = array();
  77. foreach ($idColumnNames as $idColumnName) {
  78. $columnDefinitions[$idColumnName] = array(
  79. 'notnull' => true,
  80. 'type' => \Doctrine\DBAL\Types\Type::getType($rootClass->getTypeOfColumn($idColumnName))
  81. );
  82. }
  83. $this->_createTempTableSql = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
  84. . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
  85. $this->_dropTempTableSql = $platform->getDropTemporaryTableSQL($tempTable);
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function execute(Connection $conn, array $params, array $types)
  91. {
  92. $numDeleted = 0;
  93. // Create temporary id table
  94. $conn->executeUpdate($this->_createTempTableSql);
  95. // Insert identifiers
  96. $numDeleted = $conn->executeUpdate($this->_insertSql, $params, $types);
  97. // Execute DELETE statements
  98. foreach ($this->_sqlStatements as $sql) {
  99. $conn->executeUpdate($sql);
  100. }
  101. // Drop temporary table
  102. $conn->executeUpdate($this->_dropTempTableSql);
  103. return $numDeleted;
  104. }
  105. }