Total Complexity | 53 |
Total Lines | 364 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like DMLQueryBuilder 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 DMLQueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | abstract class DMLQueryBuilder implements DMLQueryBuilderInterface |
||
36 | { |
||
37 | public function __construct( |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @psalm-suppress MixedArrayOffset |
||
46 | */ |
||
47 | public function batchInsert(string $table, array $columns, iterable|Generator $rows, array &$params = []): string |
||
48 | { |
||
49 | if (empty($rows)) { |
||
50 | return ''; |
||
51 | } |
||
52 | |||
53 | if (($tableSchema = $this->schema->getTableSchema($table)) !== null) { |
||
54 | $columnSchemas = $tableSchema->getColumns(); |
||
55 | } else { |
||
56 | $columnSchemas = []; |
||
57 | } |
||
58 | |||
59 | $values = []; |
||
60 | |||
61 | /** @psalm-var array<array-key, array<array-key, string>> $rows */ |
||
62 | foreach ($rows as $row) { |
||
63 | $placeholders = []; |
||
64 | foreach ($row as $index => $value) { |
||
65 | if (isset($columns[$index], $columnSchemas[$columns[$index]])) { |
||
66 | /** @var mixed $value */ |
||
67 | $value = $this->getTypecastValue($value, $columnSchemas[$columns[$index]]); |
||
68 | } |
||
69 | |||
70 | if ($value instanceof ExpressionInterface) { |
||
71 | $placeholders[] = $this->queryBuilder->buildExpression($value, $params); |
||
72 | } else { |
||
73 | $placeholders[] = $this->queryBuilder->bindParam($value, $params); |
||
74 | } |
||
75 | } |
||
76 | $values[] = '(' . implode(', ', $placeholders) . ')'; |
||
77 | } |
||
78 | |||
79 | if (empty($values)) { |
||
80 | return ''; |
||
81 | } |
||
82 | |||
83 | /** @psalm-var string[] $columns */ |
||
84 | foreach ($columns as $i => $name) { |
||
85 | $columns[$i] = $this->quoter->quoteColumnName($name); |
||
86 | } |
||
87 | |||
88 | return 'INSERT INTO ' |
||
89 | . $this->quoter->quoteTableName($table) |
||
90 | . ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
95 | */ |
||
96 | public function delete(string $table, array|string $condition, array &$params): string |
||
97 | { |
||
98 | $sql = 'DELETE FROM ' . $this->quoter->quoteTableName($table); |
||
99 | $where = $this->queryBuilder->buildWhere($condition, $params); |
||
100 | |||
101 | return $where === '' ? $sql : $sql . ' ' . $where; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
106 | */ |
||
107 | public function insert(string $table, QueryInterface|array $columns, array &$params = []): string |
||
108 | { |
||
109 | /** |
||
110 | * @psalm-var string[] $names |
||
111 | * @psalm-var string[] $placeholders |
||
112 | * @psalm-var string $values |
||
113 | */ |
||
114 | [$names, $placeholders, $values, $params] = $this->prepareInsertValues($table, $columns, $params); |
||
115 | |||
116 | return 'INSERT INTO ' |
||
117 | . $this->quoter->quoteTableName($table) |
||
118 | . (!empty($names) ? ' (' . implode(', ', $names) . ')' : '') |
||
119 | . (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : $values); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
124 | */ |
||
125 | public function insertEx(string $table, QueryInterface|array $columns, array &$params = []): string |
||
126 | { |
||
127 | return $this->insert($table, $columns, $params); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @throws NotSupportedException |
||
132 | */ |
||
133 | public function resetSequence(string $tableName, array|int|string|null $value = null): string |
||
134 | { |
||
135 | throw new NotSupportedException(static::class . ' does not support resetting sequence.'); |
||
136 | } |
||
137 | |||
138 | public function truncateTable(string $table): string |
||
139 | { |
||
140 | return 'TRUNCATE TABLE ' . $this->quoter->quoteTableName($table); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @psalm-suppress MixedArgument |
||
145 | */ |
||
146 | public function update(string $table, array $columns, array|string $condition, array &$params = []): string |
||
147 | { |
||
148 | /** @psalm-var string[] $lines */ |
||
149 | [$lines, $params] = $this->prepareUpdateSets($table, $columns, $params); |
||
150 | $sql = 'UPDATE ' . $this->quoter->quoteTableName($table) . ' SET ' . implode(', ', $lines); |
||
151 | $where = $this->queryBuilder->buildWhere($condition, $params); |
||
152 | |||
153 | return $where === '' ? $sql : $sql . ' ' . $where; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @throws NotSupportedException |
||
158 | */ |
||
159 | public function upsert( |
||
160 | string $table, |
||
161 | QueryInterface|array $insertColumns, |
||
162 | bool|array $updateColumns, |
||
163 | array &$params |
||
164 | ): string { |
||
165 | throw new NotSupportedException(static::class . ' does not support upsert.'); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
||
170 | * |
||
171 | * @param QueryInterface $columns Object, which represents select query. |
||
172 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will be included |
||
173 | * in the result with the additional parameters generated during the query building process. |
||
174 | * |
||
175 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
176 | * |
||
177 | * @return array array of column names, values and params. |
||
178 | */ |
||
179 | protected function prepareInsertSelectSubQuery(QueryInterface $columns, array $params = []): array |
||
180 | { |
||
181 | if (empty($columns->getSelect()) || in_array('*', $columns->getSelect(), true)) { |
||
182 | throw new InvalidArgumentException('Expected select query object with enumerated (named) parameters'); |
||
183 | } |
||
184 | |||
185 | [$values, $params] = $this->queryBuilder->build($columns, $params); |
||
186 | |||
187 | $names = []; |
||
188 | $values = ' ' . $values; |
||
189 | /** @psalm-var string[] */ |
||
190 | $select = $columns->getSelect(); |
||
191 | |||
192 | foreach ($select as $title => $field) { |
||
193 | if (is_string($title)) { |
||
194 | $names[] = $this->quoter->quoteColumnName($title); |
||
195 | } elseif (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-_.]+)$/', $field, $matches)) { |
||
196 | $names[] = $this->quoter->quoteColumnName($matches[2]); |
||
197 | } else { |
||
198 | $names[] = $this->quoter->quoteColumnName($field); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | return [$names, $values, $params]; |
||
203 | } |
||
204 | |||
205 | protected function prepareInsertValues(string $table, array|QueryInterface $columns, array $params = []): array |
||
206 | { |
||
207 | $tableSchema = $this->schema->getTableSchema($table); |
||
208 | $columnSchemas = $tableSchema !== null ? $tableSchema->getColumns() : []; |
||
209 | $names = []; |
||
210 | $placeholders = []; |
||
211 | $values = ' DEFAULT VALUES'; |
||
212 | |||
213 | if ($columns instanceof QueryInterface) { |
||
|
|||
214 | [$names, $values, $params] = $this->prepareInsertSelectSubQuery($columns, $params); |
||
215 | } else { |
||
216 | /** |
||
217 | * @var mixed $value |
||
218 | * @psalm-var array<string, mixed> $columns |
||
219 | */ |
||
220 | foreach ($columns as $name => $value) { |
||
221 | $names[] = $this->quoter->quoteColumnName($name); |
||
222 | /** @var mixed $value */ |
||
223 | $value = $this->getTypecastValue($value, $columnSchemas[$name] ?? null); |
||
224 | |||
225 | if ($value instanceof ExpressionInterface) { |
||
226 | $placeholders[] = $this->queryBuilder->buildExpression($value, $params); |
||
227 | } elseif ($value instanceof QueryInterface) { |
||
228 | [$sql, $params] = $this->queryBuilder->build($value, $params); |
||
229 | $placeholders[] = "($sql)"; |
||
230 | } else { |
||
231 | $placeholders[] = $this->queryBuilder->bindParam($value, $params); |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | |||
236 | return [$names, $placeholders, $values, $params]; |
||
237 | } |
||
238 | |||
239 | protected function prepareUpdateSets(string $table, array $columns, array $params = []): array |
||
240 | { |
||
241 | $tableSchema = $this->schema->getTableSchema($table); |
||
242 | |||
243 | $columnSchemas = $tableSchema !== null ? $tableSchema->getColumns() : []; |
||
244 | |||
245 | $sets = []; |
||
246 | |||
247 | /** |
||
248 | * @psalm-var array<string, mixed> $columns |
||
249 | * @psalm-var mixed $value |
||
250 | */ |
||
251 | foreach ($columns as $name => $value) { |
||
252 | /** @var mixed */ |
||
253 | $value = isset($columnSchemas[$name]) ? $columnSchemas[$name]->dbTypecast($value) : $value; |
||
254 | if ($value instanceof ExpressionInterface) { |
||
255 | $placeholder = $this->queryBuilder->buildExpression($value, $params); |
||
256 | } else { |
||
257 | $placeholder = $this->queryBuilder->bindParam($value, $params); |
||
258 | } |
||
259 | |||
260 | $sets[] = $this->quoter->quoteColumnName($name) . '=' . $placeholder; |
||
261 | } |
||
262 | |||
263 | return [$sets, $params]; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @psalm-param Constraint[] $constraints |
||
268 | * |
||
269 | * @throws Exception|InvalidArgumentException|InvalidConfigException|JsonException|NotSupportedException |
||
270 | */ |
||
271 | protected function prepareUpsertColumns( |
||
272 | string $table, |
||
273 | QueryInterface|array $insertColumns, |
||
274 | QueryInterface|bool|array $updateColumns, |
||
275 | array &$constraints = [] |
||
276 | ): array { |
||
277 | $insertNames = []; |
||
278 | |||
279 | if ($insertColumns instanceof QueryInterface) { |
||
280 | /** @psalm-var list<string> $insertNames */ |
||
281 | [$insertNames] = $this->prepareInsertSelectSubQuery($insertColumns); |
||
282 | } else { |
||
283 | /** @psalm-var array<string, string> $insertColumns */ |
||
284 | foreach ($insertColumns as $key => $_value) { |
||
285 | $insertNames[] = $this->quoter->quoteColumnName($key); |
||
286 | } |
||
287 | } |
||
288 | |||
289 | /** @psalm-var string[] */ |
||
290 | $uniqueNames = $this->getTableUniqueColumnNames($table, $insertNames, $constraints); |
||
291 | |||
292 | foreach ($uniqueNames as $key => $name) { |
||
293 | $insertNames[$key] = $this->quoter->quoteColumnName($name); |
||
294 | } |
||
295 | |||
296 | if ($updateColumns !== true) { |
||
297 | return [$uniqueNames, $insertNames, null]; |
||
298 | } |
||
299 | |||
300 | return [$uniqueNames, $insertNames, array_diff($insertNames, $uniqueNames)]; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Returns all column names belonging to constraints enforcing uniqueness (`PRIMARY KEY`, `UNIQUE INDEX`, etc.) |
||
305 | * for the named table removing constraints which did not cover the specified column list. |
||
306 | * |
||
307 | * The column list will be unique by column names. |
||
308 | * |
||
309 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||
310 | * @param string[] $columns source column list. |
||
311 | * @param Constraint[] $constraints this parameter optionally receives a matched constraint list. The constraints |
||
312 | * will be unique by their column names. |
||
313 | * |
||
314 | * @throws JsonException |
||
315 | * |
||
316 | * @return array column list. |
||
317 | * @psalm-suppress ReferenceConstraintViolation |
||
318 | */ |
||
319 | private function getTableUniqueColumnNames(string $name, array $columns, array &$constraints = []): array |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * @param mixed $value |
||
388 | * @param ColumnSchemaInterface|null $columnSchema |
||
389 | * |
||
390 | * @return mixed |
||
391 | */ |
||
392 | protected function getTypecastValue(mixed $value, ColumnSchemaInterface $columnSchema = null): mixed |
||
393 | { |
||
394 | if ($columnSchema) { |
||
395 | return $columnSchema->dbTypecast($value); |
||
396 | } |
||
401 |