Total Complexity | 55 |
Total Lines | 463 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like AbstractDMLQueryBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractDMLQueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | abstract class AbstractDMLQueryBuilder implements DMLQueryBuilderInterface |
||
55 | { |
||
56 | public function __construct( |
||
57 | protected QueryBuilderInterface $queryBuilder, |
||
58 | protected QuoterInterface $quoter, |
||
59 | protected SchemaInterface $schema |
||
60 | ) { |
||
61 | } |
||
62 | |||
63 | public function batchInsert(string $table, array $columns, iterable $rows, array &$params = []): string |
||
64 | { |
||
65 | if (empty($rows)) { |
||
66 | return ''; |
||
67 | } |
||
68 | |||
69 | $columns = $this->extractColumnNames($rows, $columns); |
||
70 | $values = $this->prepareBatchInsertValues($table, $rows, $columns, $params); |
||
71 | |||
72 | if (empty($values)) { |
||
73 | return ''; |
||
74 | } |
||
75 | |||
76 | $query = 'INSERT INTO ' . $this->quoter->quoteTableName($table); |
||
77 | |||
78 | if (count($columns) > 0) { |
||
79 | $quotedColumnNames = array_map([$this->quoter, 'quoteColumnName'], $columns); |
||
80 | |||
81 | $query .= ' (' . implode(', ', $quotedColumnNames) . ')'; |
||
82 | } |
||
83 | |||
84 | return $query . ' VALUES ' . implode(', ', $values); |
||
85 | } |
||
86 | |||
87 | public function delete(string $table, array|string $condition, array &$params): string |
||
88 | { |
||
89 | $sql = 'DELETE FROM ' . $this->quoter->quoteTableName($table); |
||
90 | $where = $this->queryBuilder->buildWhere($condition, $params); |
||
91 | |||
92 | return $where === '' ? $sql : $sql . ' ' . $where; |
||
93 | } |
||
94 | |||
95 | public function insert(string $table, QueryInterface|array $columns, array &$params = []): string |
||
96 | { |
||
97 | [$names, $placeholders, $values, $params] = $this->prepareInsertValues($table, $columns, $params); |
||
98 | |||
99 | return 'INSERT INTO ' . $this->quoter->quoteTableName($table) |
||
100 | . (!empty($names) ? ' (' . implode(', ', $names) . ')' : '') |
||
101 | . (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : ' ' . $values); |
||
102 | } |
||
103 | |||
104 | public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string |
||
105 | { |
||
106 | throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.'); |
||
107 | } |
||
108 | |||
109 | public function resetSequence(string $table, int|string|null $value = null): string |
||
110 | { |
||
111 | throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.'); |
||
112 | } |
||
113 | |||
114 | public function update(string $table, array $columns, array|string $condition, array &$params = []): string |
||
115 | { |
||
116 | [$lines, $params] = $this->prepareUpdateSets($table, $columns, $params); |
||
117 | |||
118 | $sql = 'UPDATE ' . $this->quoter->quoteTableName($table) . ' SET ' . implode(', ', $lines); |
||
119 | $where = $this->queryBuilder->buildWhere($condition, $params); |
||
120 | |||
121 | return $where === '' ? $sql : $sql . ' ' . $where; |
||
122 | } |
||
123 | |||
124 | public function upsert( |
||
125 | string $table, |
||
126 | QueryInterface|array $insertColumns, |
||
127 | bool|array $updateColumns, |
||
128 | array &$params |
||
129 | ): string { |
||
130 | throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.'); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Prepare values for batch insert. |
||
135 | * |
||
136 | * @param string $table The table name. |
||
137 | * @param iterable $rows The rows to be batch inserted into the table. |
||
138 | * @param string[] $columns The column names. |
||
139 | * @param array $params The binding parameters that will be generated by this method. |
||
140 | * |
||
141 | * @return string[] The values. |
||
142 | * |
||
143 | * @psalm-param ParamsType $params |
||
144 | */ |
||
145 | protected function prepareBatchInsertValues(string $table, iterable $rows, array $columns, array &$params): array |
||
146 | { |
||
147 | $values = []; |
||
148 | /** @var string[] $columnNames */ |
||
149 | $columnNames = array_values($columns); |
||
150 | $columnKeys = array_fill_keys($columnNames, false); |
||
151 | $columnSchemas = $this->schema->getTableSchema($table)?->getColumns() ?? []; |
||
152 | |||
153 | foreach ($rows as $row) { |
||
154 | $i = 0; |
||
155 | $placeholders = $columnKeys; |
||
156 | |||
157 | /** @var int|string $key */ |
||
158 | foreach ($row as $key => $value) { |
||
159 | $columnName = $columns[$key] ?? (isset($columnKeys[$key]) ? $key : $columnNames[$i] ?? $i); |
||
160 | |||
161 | if (isset($columnSchemas[$columnName])) { |
||
162 | $value = $columnSchemas[$columnName]->dbTypecast($value); |
||
163 | } |
||
164 | |||
165 | if ($value instanceof ExpressionInterface) { |
||
166 | $placeholders[$columnName] = $this->queryBuilder->buildExpression($value, $params); |
||
167 | } else { |
||
168 | $placeholders[$columnName] = $this->queryBuilder->bindParam($value, $params); |
||
169 | } |
||
170 | |||
171 | ++$i; |
||
172 | } |
||
173 | |||
174 | $values[] = '(' . implode(', ', $placeholders) . ')'; |
||
175 | } |
||
176 | |||
177 | return $values; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Extract column names from columns and rows. |
||
182 | * |
||
183 | * @param string $table The column schemas. |
||
184 | * @param iterable $rows The rows to be batch inserted into the table. |
||
185 | * @param string[] $columns The column names. |
||
186 | * |
||
187 | * @return string[] The column names. |
||
188 | */ |
||
189 | protected function extractColumnNames(iterable $rows, array $columns): array |
||
190 | { |
||
191 | $columns = $this->getNormalizeColumnNames('', $columns); |
||
192 | |||
193 | if ($columns !== [] || !is_array($rows)) { |
||
194 | return $columns; |
||
195 | } |
||
196 | |||
197 | $row = reset($rows); |
||
198 | $row = match (true) { |
||
199 | is_array($row) => $row, |
||
200 | $row instanceof Traversable => iterator_to_array($row), |
||
201 | is_object($row) => get_object_vars($row), |
||
202 | default => [], |
||
203 | }; |
||
204 | |||
205 | if (array_key_exists(0, $row)) { |
||
206 | return []; |
||
207 | } |
||
208 | |||
209 | /** @var string[] $columnNames */ |
||
210 | $columnNames = array_keys($row); |
||
211 | |||
212 | return array_combine($columnNames, $columnNames); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Prepare select-subQuery and field names for `INSERT INTO ... SELECT` SQL statement. |
||
217 | * |
||
218 | * @param QueryInterface $columns Object, which represents a select query. |
||
219 | * @param array $params The parameters to bind to the generated SQL statement. These parameters will be included |
||
220 | * in the result, with the more parameters generated during the query building process. |
||
221 | * |
||
222 | * @throws Exception |
||
223 | * @throws InvalidArgumentException |
||
224 | * @throws InvalidConfigException |
||
225 | * @throws NotSupportedException |
||
226 | * |
||
227 | * @return array Array of quoted column names, values, and params. |
||
228 | * |
||
229 | * @psalm-param ParamsType $params |
||
230 | * @psalm-return array{0: string[], 1: string, 2: array} |
||
231 | */ |
||
232 | protected function prepareInsertSelectSubQuery(QueryInterface $columns, array $params = []): array |
||
233 | { |
||
234 | /** @psalm-var string[] $select */ |
||
235 | $select = $columns->getSelect(); |
||
236 | |||
237 | if (empty($select) || in_array('*', $select, true)) { |
||
238 | throw new InvalidArgumentException('Expected select query object with enumerated (named) parameters'); |
||
239 | } |
||
240 | |||
241 | [$values, $params] = $this->queryBuilder->build($columns, $params); |
||
242 | |||
243 | $names = []; |
||
244 | |||
245 | foreach ($select as $title => $field) { |
||
246 | if (is_string($title)) { |
||
247 | $names[] = $this->quoter->quoteColumnName($title); |
||
248 | } else { |
||
249 | if ($field instanceof ExpressionInterface) { |
||
250 | $field = $this->queryBuilder->buildExpression($field, $params); |
||
251 | } |
||
252 | |||
253 | if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-_.]+)$/', $field, $matches)) { |
||
254 | $names[] = $this->quoter->quoteColumnName($matches[2]); |
||
255 | } else { |
||
256 | $names[] = $this->quoter->quoteColumnName($field); |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | return [$names, $values, $params]; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Prepare column names and placeholders for `INSERT` SQL statement. |
||
266 | * |
||
267 | * @throws Exception |
||
268 | * @throws InvalidConfigException |
||
269 | * @throws InvalidArgumentException |
||
270 | * @throws NotSupportedException |
||
271 | * |
||
272 | * @return array Array of quoted column names, placeholders, values, and params. |
||
273 | * |
||
274 | * @psalm-param ParamsType $params |
||
275 | * @psalm-return array{0: string[], 1: string[], 2: string, 3: array} |
||
276 | */ |
||
277 | protected function prepareInsertValues(string $table, array|QueryInterface $columns, array $params = []): array |
||
278 | { |
||
279 | if (empty($columns)) { |
||
280 | return [[], [], 'DEFAULT VALUES', []]; |
||
281 | } |
||
282 | |||
283 | if ($columns instanceof QueryInterface) { |
||
|
|||
284 | [$names, $values, $params] = $this->prepareInsertSelectSubQuery($columns, $params); |
||
285 | return [$names, [], $values, $params]; |
||
286 | } |
||
287 | |||
288 | $names = []; |
||
289 | $placeholders = []; |
||
290 | $columns = $this->normalizeColumnNames('', $columns); |
||
291 | $columnSchemas = $this->schema->getTableSchema($table)?->getColumns() ?? []; |
||
292 | |||
293 | foreach ($columns as $name => $value) { |
||
294 | $names[] = $this->quoter->quoteColumnName($name); |
||
295 | |||
296 | if (isset($columnSchemas[$name])) { |
||
297 | $value = $columnSchemas[$name]->dbTypecast($value); |
||
298 | } |
||
299 | |||
300 | if ($value instanceof ExpressionInterface) { |
||
301 | $placeholders[] = $this->queryBuilder->buildExpression($value, $params); |
||
302 | } else { |
||
303 | $placeholders[] = $this->queryBuilder->bindParam($value, $params); |
||
304 | } |
||
305 | } |
||
306 | |||
307 | return [$names, $placeholders, '', $params]; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Prepare column names and placeholders for `UPDATE` SQL statement. |
||
312 | * |
||
313 | * @throws Exception |
||
314 | * @throws InvalidConfigException |
||
315 | * @throws InvalidArgumentException |
||
316 | * @throws NotSupportedException |
||
317 | * |
||
318 | * @psalm-param ParamsType $params |
||
319 | * @psalm-return array{0: string[], 1: array} |
||
320 | */ |
||
321 | protected function prepareUpdateSets(string $table, array $columns, array $params = []): array |
||
322 | { |
||
323 | $sets = []; |
||
324 | $columns = $this->normalizeColumnNames('', $columns); |
||
325 | $columnSchemas = $this->schema->getTableSchema($table)?->getColumns() ?? []; |
||
326 | |||
327 | foreach ($columns as $name => $value) { |
||
328 | if (isset($columnSchemas[$name])) { |
||
329 | $value = $columnSchemas[$name]->dbTypecast($value); |
||
330 | } |
||
331 | |||
332 | if ($value instanceof ExpressionInterface) { |
||
333 | $placeholder = $this->queryBuilder->buildExpression($value, $params); |
||
334 | } else { |
||
335 | $placeholder = $this->queryBuilder->bindParam($value, $params); |
||
336 | } |
||
337 | |||
338 | $sets[] = $this->quoter->quoteColumnName($name) . '=' . $placeholder; |
||
339 | } |
||
340 | |||
341 | return [$sets, $params]; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Prepare column names and constraints for "upsert" operation. |
||
346 | * |
||
347 | * @throws Exception |
||
348 | * @throws InvalidArgumentException |
||
349 | * @throws InvalidConfigException |
||
350 | * @throws JsonException |
||
351 | * @throws NotSupportedException |
||
352 | * |
||
353 | * @psalm-param array<string, mixed>|QueryInterface $insertColumns |
||
354 | * @psalm-param Constraint[] $constraints |
||
355 | * |
||
356 | * @return array Array of unique, insert and update quoted column names. |
||
357 | * @psalm-return array{0: string[], 1: string[], 2: string[]|null} |
||
358 | */ |
||
359 | protected function prepareUpsertColumns( |
||
360 | string $table, |
||
361 | QueryInterface|array $insertColumns, |
||
362 | QueryInterface|bool|array $updateColumns, |
||
363 | array &$constraints = [] |
||
364 | ): array { |
||
365 | if ($insertColumns instanceof QueryInterface) { |
||
366 | [$insertNames] = $this->prepareInsertSelectSubQuery($insertColumns); |
||
367 | } else { |
||
368 | $insertNames = $this->getNormalizeColumnNames('', array_keys($insertColumns)); |
||
369 | |||
370 | $insertNames = array_map( |
||
371 | [$this->quoter, 'quoteColumnName'], |
||
372 | $insertNames, |
||
373 | ); |
||
374 | } |
||
375 | |||
376 | $uniqueNames = $this->getTableUniqueColumnNames($table, $insertNames, $constraints); |
||
377 | |||
378 | if ($updateColumns === true) { |
||
379 | return [$uniqueNames, $insertNames, array_diff($insertNames, $uniqueNames)]; |
||
380 | } |
||
381 | |||
382 | return [$uniqueNames, $insertNames, null]; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Returns all quoted column names belonging to constraints enforcing uniqueness (`PRIMARY KEY`, `UNIQUE INDEX`, etc.) |
||
387 | * for the named table removing constraints which didn't cover the specified column list. |
||
388 | * |
||
389 | * The column list will be unique by column names. |
||
390 | * |
||
391 | * @param string $name The table name, may contain schema name if any. Don't quote the table name. |
||
392 | * @param string[] $columns Source column list. |
||
393 | * @param array $constraints This parameter optionally receives a matched constraint list. The constraints |
||
394 | * will be unique by their column names. |
||
395 | * |
||
396 | * @throws JsonException |
||
397 | * |
||
398 | * @return string[] The quoted column names. |
||
399 | * |
||
400 | * @psalm-param Constraint[] $constraints |
||
401 | */ |
||
402 | private function getTableUniqueColumnNames(string $name, array $columns, array &$constraints = []): array |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * @return mixed The typecast value of the given column. |
||
470 | * |
||
471 | * @deprecated will be removed in version 2.0.0 |
||
472 | */ |
||
473 | protected function getTypecastValue(mixed $value, ColumnSchemaInterface $columnSchema = null): mixed |
||
474 | { |
||
475 | if ($columnSchema) { |
||
476 | return $columnSchema->dbTypecast($value); |
||
477 | } |
||
478 | |||
479 | return $value; |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * Normalizes the column names. |
||
484 | * |
||
485 | * @param string $table Not used. Could be empty string. Will be removed in version 2.0.0. |
||
486 | * @param array $columns The column data (name => value). |
||
487 | * |
||
488 | * @return array The normalized column names (name => value). |
||
489 | * |
||
490 | * @psalm-return array<string, mixed> |
||
491 | */ |
||
492 | protected function normalizeColumnNames(string $table, array $columns): array |
||
493 | { |
||
494 | /** @var string[] $columnNames */ |
||
495 | $columnNames = array_keys($columns); |
||
496 | $normalizedNames = $this->getNormalizeColumnNames('', $columnNames); |
||
497 | |||
498 | return array_combine($normalizedNames, $columns); |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * Get normalized column names |
||
503 | * |
||
504 | * @param string $table Not used. Could be empty string. Will be removed in version 2.0.0. |
||
505 | * @param string[] $columns The column names. |
||
506 | * |
||
507 | * @return string[] Normalized column names. |
||
508 | */ |
||
509 | protected function getNormalizeColumnNames(string $table, array $columns): array |
||
517 | } |
||
518 | } |
||
519 |