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->getTableName(), '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]['parameters'][] = $sqlWalker->getQuery()->getParameter($paramKey); |
---|
110 | $this->_sqlParameters[$i]['types'][] = $sqlWalker->getQuery()->getParameterType($paramKey); |
---|
111 | |
---|
112 | ++$this->_numParametersInUpdateClause; |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | if ($affected) { |
---|
118 | $this->_sqlStatements[$i] = $updateSql . ' WHERE (' . $idColumnList . ') IN (' . $idSubselect . ')'; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | // Append WHERE clause to insertSql, if there is one. |
---|
123 | if ($AST->whereClause) { |
---|
124 | $this->_insertSql .= $sqlWalker->walkWhereClause($AST->whereClause); |
---|
125 | } |
---|
126 | |
---|
127 | // 4. Store DDL for temporary identifier table. |
---|
128 | $columnDefinitions = array(); |
---|
129 | |
---|
130 | foreach ($idColumnNames as $idColumnName) { |
---|
131 | $columnDefinitions[$idColumnName] = array( |
---|
132 | 'notnull' => true, |
---|
133 | 'type' => Type::getType($rootClass->getTypeOfColumn($idColumnName)) |
---|
134 | ); |
---|
135 | } |
---|
136 | |
---|
137 | $this->_createTempTableSql = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' (' |
---|
138 | . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')'; |
---|
139 | |
---|
140 | $this->_dropTempTableSql = $platform->getDropTemporaryTableSQL($tempTable); |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * {@inheritDoc} |
---|
145 | */ |
---|
146 | public function execute(Connection $conn, array $params, array $types) |
---|
147 | { |
---|
148 | $numUpdated = 0; |
---|
149 | |
---|
150 | // Create temporary id table |
---|
151 | $conn->executeUpdate($this->_createTempTableSql); |
---|
152 | |
---|
153 | // Insert identifiers. Parameters from the update clause are cut off. |
---|
154 | $numUpdated = $conn->executeUpdate( |
---|
155 | $this->_insertSql, |
---|
156 | array_slice($params, $this->_numParametersInUpdateClause), |
---|
157 | array_slice($types, $this->_numParametersInUpdateClause) |
---|
158 | ); |
---|
159 | |
---|
160 | // Execute UPDATE statements |
---|
161 | for ($i=0, $count=count($this->_sqlStatements); $i<$count; ++$i) { |
---|
162 | $parameters = array(); |
---|
163 | $types = array(); |
---|
164 | |
---|
165 | if (isset($this->_sqlParameters[$i])) { |
---|
166 | $parameters = isset($this->_sqlParameters[$i]['parameters']) ? $this->_sqlParameters[$i]['parameters'] : array(); |
---|
167 | $types = isset($this->_sqlParameters[$i]['types']) ? $this->_sqlParameters[$i]['types'] : array(); |
---|
168 | } |
---|
169 | |
---|
170 | $conn->executeUpdate($this->_sqlStatements[$i], $parameters, $types); |
---|
171 | } |
---|
172 | |
---|
173 | // Drop temporary table |
---|
174 | $conn->executeUpdate($this->_dropTempTableSql); |
---|
175 | |
---|
176 | return $numUpdated; |
---|
177 | } |
---|
178 | } |
---|