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