|
0
|
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 |
|
|
|
20 |
namespace Doctrine\ORM\Query\Exec; |
|
|
21 |
|
|
|
22 |
use Doctrine\DBAL\Connection, |
|
|
23 |
Doctrine\DBAL\Types\Type, |
|
|
24 |
Doctrine\ORM\Query\AST; |
|
|
25 |
|
|
|
26 |
/** |
|
|
27 |
* Executes the SQL statements for bulk DQL UPDATE statements on classes in |
|
|
28 |
* Class Table Inheritance (JOINED). |
|
|
29 |
* |
|
|
30 |
* @author Roman Borschel <roman@code-factory.org> |
|
|
31 |
* @since 2.0 |
|
|
32 |
*/ |
|
|
33 |
class MultiTableUpdateExecutor extends AbstractSqlExecutor |
|
|
34 |
{ |
|
|
35 |
private $_createTempTableSql; |
|
|
36 |
private $_dropTempTableSql; |
|
|
37 |
private $_insertSql; |
|
|
38 |
private $_sqlParameters = array(); |
|
|
39 |
private $_numParametersInUpdateClause = 0; |
|
|
40 |
|
|
|
41 |
/** |
|
|
42 |
* Initializes a new <tt>MultiTableUpdateExecutor</tt>. |
|
|
43 |
* |
|
|
44 |
* @param Node $AST The root AST node of the DQL query. |
|
|
45 |
* @param SqlWalker $sqlWalker The walker used for SQL generation from the AST. |
|
|
46 |
* @internal Any SQL construction and preparation takes place in the constructor for |
|
|
47 |
* best performance. With a query cache the executor will be cached. |
|
|
48 |
*/ |
|
|
49 |
public function __construct(AST\Node $AST, $sqlWalker) |
|
|
50 |
{ |
|
|
51 |
$em = $sqlWalker->getEntityManager(); |
|
|
52 |
$conn = $em->getConnection(); |
|
|
53 |
$platform = $conn->getDatabasePlatform(); |
|
|
54 |
|
|
|
55 |
$updateClause = $AST->updateClause; |
|
|
56 |
|
|
|
57 |
$primaryClass = $sqlWalker->getEntityManager()->getClassMetadata($updateClause->abstractSchemaName); |
|
|
58 |
$rootClass = $em->getClassMetadata($primaryClass->rootEntityName); |
|
|
59 |
|
|
|
60 |
$updateItems = $updateClause->updateItems; |
|
|
61 |
|
|
|
62 |
$tempTable = $platform->getTemporaryTableName($rootClass->getTemporaryIdTableName()); |
|
|
63 |
$idColumnNames = $rootClass->getIdentifierColumnNames(); |
|
|
64 |
$idColumnList = implode(', ', $idColumnNames); |
|
|
65 |
|
|
|
66 |
// 1. Create an INSERT INTO temptable ... SELECT identifiers WHERE $AST->getWhereClause() |
|
|
67 |
$sqlWalker->setSQLTableAlias($primaryClass->table['name'], 't0', $updateClause->aliasIdentificationVariable); |
|
|
68 |
|
|
|
69 |
$this->_insertSql = 'INSERT INTO ' . $tempTable . ' (' . $idColumnList . ')' |
|
|
70 |
. ' SELECT t0.' . implode(', t0.', $idColumnNames); |
|
|
71 |
|
|
|
72 |
$rangeDecl = new AST\RangeVariableDeclaration($primaryClass->name, $updateClause->aliasIdentificationVariable); |
|
|
73 |
$fromClause = new AST\FromClause(array(new AST\IdentificationVariableDeclaration($rangeDecl, null, array()))); |
|
|
74 |
|
|
|
75 |
$this->_insertSql .= $sqlWalker->walkFromClause($fromClause); |
|
|
76 |
|
|
|
77 |
// 2. Create ID subselect statement used in UPDATE ... WHERE ... IN (subselect) |
|
|
78 |
$idSubselect = 'SELECT ' . $idColumnList . ' FROM ' . $tempTable; |
|
|
79 |
|
|
|
80 |
// 3. Create and store UPDATE statements |
|
|
81 |
$classNames = array_merge($primaryClass->parentClasses, array($primaryClass->name), $primaryClass->subClasses); |
|
|
82 |
$i = -1; |
|
|
83 |
|
|
|
84 |
foreach (array_reverse($classNames) as $className) { |
|
|
85 |
$affected = false; |
|
|
86 |
$class = $em->getClassMetadata($className); |
|
|
87 |
$updateSql = 'UPDATE ' . $class->getQuotedTableName($platform) . ' SET '; |
|
|
88 |
|
|
|
89 |
foreach ($updateItems as $updateItem) { |
|
|
90 |
$field = $updateItem->pathExpression->field; |
|
|
91 |
|
|
|
92 |
if (isset($class->fieldMappings[$field]) && ! isset($class->fieldMappings[$field]['inherited']) || |
|
|
93 |
isset($class->associationMappings[$field]) && ! isset($class->associationMappings[$field]['inherited'])) { |
|
|
94 |
$newValue = $updateItem->newValue; |
|
|
95 |
|
|
|
96 |
if ( ! $affected) { |
|
|
97 |
$affected = true; |
|
|
98 |
++$i; |
|
|
99 |
} else { |
|
|
100 |
$updateSql .= ', '; |
|
|
101 |
} |
|
|
102 |
|
|
|
103 |
$updateSql .= $sqlWalker->walkUpdateItem($updateItem); |
|
|
104 |
|
|
|
105 |
//FIXME: parameters can be more deeply nested. traverse the tree. |
|
|
106 |
//FIXME (URGENT): With query cache the parameter is out of date. Move to execute() stage. |
|
|
107 |
if ($newValue instanceof AST\InputParameter) { |
|
|
108 |
$paramKey = $newValue->name; |
|
|
109 |
$this->_sqlParameters[$i][] = $sqlWalker->getQuery()->getParameter($paramKey); |
|
|
110 |
|
|
|
111 |
++$this->_numParametersInUpdateClause; |
|
|
112 |
} |
|
|
113 |
} |
|
|
114 |
} |
|
|
115 |
|
|
|
116 |
if ($affected) { |
|
|
117 |
$this->_sqlStatements[$i] = $updateSql . ' WHERE (' . $idColumnList . ') IN (' . $idSubselect . ')'; |
|
|
118 |
} |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
// Append WHERE clause to insertSql, if there is one. |
|
|
122 |
if ($AST->whereClause) { |
|
|
123 |
$this->_insertSql .= $sqlWalker->walkWhereClause($AST->whereClause); |
|
|
124 |
} |
|
|
125 |
|
|
|
126 |
// 4. Store DDL for temporary identifier table. |
|
|
127 |
$columnDefinitions = array(); |
|
|
128 |
|
|
|
129 |
foreach ($idColumnNames as $idColumnName) { |
|
|
130 |
$columnDefinitions[$idColumnName] = array( |
|
|
131 |
'notnull' => true, |
|
|
132 |
'type' => Type::getType($rootClass->getTypeOfColumn($idColumnName)) |
|
|
133 |
); |
|
|
134 |
} |
|
|
135 |
|
|
|
136 |
$this->_createTempTableSql = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' (' |
|
|
137 |
. $platform->getColumnDeclarationListSQL($columnDefinitions) . ')'; |
|
|
138 |
|
|
|
139 |
$this->_dropTempTableSql = 'DROP TABLE ' . $tempTable; |
|
|
140 |
} |
|
|
141 |
|
|
|
142 |
/** |
|
|
143 |
* Executes all SQL statements. |
|
|
144 |
* |
|
|
145 |
* @param Connection $conn The database connection that is used to execute the queries. |
|
|
146 |
* @param array $params The parameters. |
|
|
147 |
* @override |
|
|
148 |
*/ |
|
|
149 |
public function execute(Connection $conn, array $params, array $types) |
|
|
150 |
{ |
|
|
151 |
$numUpdated = 0; |
|
|
152 |
|
|
|
153 |
// Create temporary id table |
|
|
154 |
$conn->executeUpdate($this->_createTempTableSql); |
|
|
155 |
|
|
|
156 |
// Insert identifiers. Parameters from the update clause are cut off. |
|
|
157 |
$numUpdated = $conn->executeUpdate($this->_insertSql, array_slice($params, $this->_numParametersInUpdateClause), $types); |
|
|
158 |
|
|
|
159 |
// Execute UPDATE statements |
|
|
160 |
for ($i=0, $count=count($this->_sqlStatements); $i<$count; ++$i) { |
|
|
161 |
$conn->executeUpdate($this->_sqlStatements[$i], isset($this->_sqlParameters[$i]) ? $this->_sqlParameters[$i] : array()); |
|
|
162 |
} |
|
|
163 |
|
|
|
164 |
// Drop temporary table |
|
|
165 |
$conn->executeUpdate($this->_dropTempTableSql); |
|
|
166 |
|
|
|
167 |
return $numUpdated; |
|
|
168 |
} |
|
|
169 |
} |