Total Complexity | 59 |
Total Lines | 420 |
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-param string[] $columns |
||
46 | * @psalm-suppress MixedArrayOffset |
||
47 | */ |
||
48 | public function batchInsert(string $table, array $columns, iterable|Generator $rows, array &$params = []): string |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
96 | */ |
||
97 | public function delete(string $table, array|string $condition, array &$params): string |
||
103 | } |
||
104 | |||
105 | public function insert(string $table, QueryInterface|array $columns, array &$params = []): string |
||
106 | { |
||
107 | /** |
||
108 | * @psalm-var string[] $names |
||
109 | * @psalm-var string[] $placeholders |
||
110 | * @psalm-var string $values |
||
111 | */ |
||
112 | [$names, $placeholders, $values, $params] = $this->prepareInsertValues($table, $columns, $params); |
||
113 | |||
114 | return 'INSERT INTO ' |
||
115 | . $this->quoter->quoteTableName($table) |
||
116 | . (!empty($names) ? ' (' . implode(', ', $names) . ')' : '') |
||
117 | . (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : $values); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
122 | */ |
||
123 | public function insertEx(string $table, QueryInterface|array $columns, array &$params = []): string |
||
124 | { |
||
125 | throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.'); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @throws NotSupportedException |
||
130 | */ |
||
131 | public function resetSequence(string $tableName, int|string|null $value = null): string |
||
132 | { |
||
133 | throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.'); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @psalm-suppress MixedArgument |
||
138 | */ |
||
139 | public function update(string $table, array $columns, array|string $condition, array &$params = []): string |
||
140 | { |
||
141 | /** @psalm-var string[] $lines */ |
||
142 | [$lines, $params] = $this->prepareUpdateSets($table, $columns, $params); |
||
143 | $sql = 'UPDATE ' . $this->quoter->quoteTableName($table) . ' SET ' . implode(', ', $lines); |
||
144 | $where = $this->queryBuilder->buildWhere($condition, $params); |
||
145 | |||
146 | return $where === '' ? $sql : $sql . ' ' . $where; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @throws NotSupportedException |
||
151 | */ |
||
152 | public function upsert( |
||
153 | string $table, |
||
154 | QueryInterface|array $insertColumns, |
||
155 | bool|array $updateColumns, |
||
156 | array &$params |
||
157 | ): string { |
||
158 | throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.'); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
||
163 | * |
||
164 | * @param QueryInterface $columns Object, which represents select query. |
||
165 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will be included |
||
166 | * in the result with the additional parameters generated during the query building process. |
||
167 | * |
||
168 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
169 | * |
||
170 | * @return array array of column names, values and params. |
||
171 | */ |
||
172 | protected function prepareInsertSelectSubQuery(QueryInterface $columns, array $params = []): array |
||
173 | { |
||
174 | if (empty($columns->getSelect()) || in_array('*', $columns->getSelect(), true)) { |
||
175 | throw new InvalidArgumentException('Expected select query object with enumerated (named) parameters'); |
||
176 | } |
||
177 | |||
178 | [$values, $params] = $this->queryBuilder->build($columns, $params); |
||
179 | |||
180 | $names = []; |
||
181 | $values = ' ' . $values; |
||
182 | /** @psalm-var string[] $select */ |
||
183 | $select = $columns->getSelect(); |
||
184 | |||
185 | foreach ($select as $title => $field) { |
||
186 | if (is_string($title)) { |
||
187 | $names[] = $this->quoter->quoteColumnName($title); |
||
188 | } elseif (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-_.]+)$/', $field, $matches)) { |
||
189 | $names[] = $this->quoter->quoteColumnName($matches[2]); |
||
190 | } else { |
||
191 | $names[] = $this->quoter->quoteColumnName($field); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | return [$names, $values, $params]; |
||
196 | } |
||
197 | |||
198 | protected function prepareInsertValues(string $table, array|QueryInterface $columns, array $params = []): array |
||
199 | { |
||
200 | $tableSchema = $this->schema->getTableSchema($table); |
||
201 | $columnSchemas = $tableSchema !== null ? $tableSchema->getColumns() : []; |
||
202 | $names = []; |
||
203 | $placeholders = []; |
||
204 | $values = ' DEFAULT VALUES'; |
||
205 | |||
206 | if ($columns instanceof QueryInterface) { |
||
|
|||
207 | [$names, $values, $params] = $this->prepareInsertSelectSubQuery($columns, $params); |
||
208 | } else { |
||
209 | $columns = $this->normalizeColumnNames($table, $columns); |
||
210 | /** |
||
211 | * @var mixed $value |
||
212 | * @psalm-var array<string, mixed> $columns |
||
213 | */ |
||
214 | foreach ($columns as $name => $value) { |
||
215 | $names[] = $this->quoter->quoteColumnName($name); |
||
216 | /** @var mixed $value */ |
||
217 | $value = $this->getTypecastValue($value, $columnSchemas[$name] ?? null); |
||
218 | |||
219 | if ($value instanceof ExpressionInterface) { |
||
220 | $placeholders[] = $this->queryBuilder->buildExpression($value, $params); |
||
221 | } else { |
||
222 | $placeholders[] = $this->queryBuilder->bindParam($value, $params); |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | |||
227 | return [$names, $placeholders, $values, $params]; |
||
228 | } |
||
229 | |||
230 | protected function prepareUpdateSets(string $table, array $columns, array $params = []): array |
||
231 | { |
||
232 | $tableSchema = $this->schema->getTableSchema($table); |
||
233 | |||
234 | $columnSchemas = $tableSchema !== null ? $tableSchema->getColumns() : []; |
||
235 | |||
236 | $sets = []; |
||
237 | |||
238 | $columns = $this->normalizeColumnNames($table, $columns); |
||
239 | |||
240 | /** |
||
241 | * @psalm-var array<string, mixed> $columns |
||
242 | * @psalm-var mixed $value |
||
243 | */ |
||
244 | foreach ($columns as $name => $value) { |
||
245 | /** @psalm-var mixed $value */ |
||
246 | $value = isset($columnSchemas[$name]) ? $columnSchemas[$name]->dbTypecast($value) : $value; |
||
247 | if ($value instanceof ExpressionInterface) { |
||
248 | $placeholder = $this->queryBuilder->buildExpression($value, $params); |
||
249 | } else { |
||
250 | $placeholder = $this->queryBuilder->bindParam($value, $params); |
||
251 | } |
||
252 | |||
253 | $sets[] = $this->quoter->quoteColumnName($name) . '=' . $placeholder; |
||
254 | } |
||
255 | |||
256 | return [$sets, $params]; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @psalm-param Constraint[] $constraints |
||
261 | * |
||
262 | * @throws Exception|InvalidArgumentException|InvalidConfigException|JsonException|NotSupportedException |
||
263 | */ |
||
264 | protected function prepareUpsertColumns( |
||
265 | string $table, |
||
266 | QueryInterface|array $insertColumns, |
||
267 | QueryInterface|bool|array $updateColumns, |
||
268 | array &$constraints = [] |
||
269 | ): array { |
||
270 | $insertNames = []; |
||
271 | |||
272 | if (!$insertColumns instanceof QueryInterface) { |
||
273 | $insertColumns = $this->normalizeColumnNames($table, $insertColumns); |
||
274 | } |
||
275 | |||
276 | if (is_array($updateColumns)) { |
||
277 | $updateColumns = $this->normalizeColumnNames($table, $updateColumns); |
||
278 | } |
||
279 | |||
280 | if ($insertColumns instanceof QueryInterface) { |
||
281 | /** @psalm-var list<string> $insertNames */ |
||
282 | [$insertNames] = $this->prepareInsertSelectSubQuery($insertColumns); |
||
283 | } else { |
||
284 | /** @psalm-var array<string, string> $insertColumns */ |
||
285 | foreach ($insertColumns as $key => $_value) { |
||
286 | $insertNames[] = $this->quoter->quoteColumnName($key); |
||
287 | } |
||
288 | } |
||
289 | |||
290 | /** @psalm-var string[] $uniqueNames */ |
||
291 | $uniqueNames = $this->getTableUniqueColumnNames($table, $insertNames, $constraints); |
||
292 | |||
293 | foreach ($uniqueNames as $key => $name) { |
||
294 | $insertNames[$key] = $this->quoter->quoteColumnName($name); |
||
295 | } |
||
296 | |||
297 | if ($updateColumns !== true) { |
||
298 | return [$uniqueNames, $insertNames, null]; |
||
299 | } |
||
300 | |||
301 | return [$uniqueNames, $insertNames, array_diff($insertNames, $uniqueNames)]; |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Returns all column names belonging to constraints enforcing uniqueness (`PRIMARY KEY`, `UNIQUE INDEX`, etc.) |
||
306 | * for the named table removing constraints which did not cover the specified column list. |
||
307 | * |
||
308 | * The column list will be unique by column names. |
||
309 | * |
||
310 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||
311 | * @param string[] $columns source column list. |
||
312 | * @param Constraint[] $constraints this parameter optionally receives a matched constraint list. The constraints |
||
313 | * will be unique by their column names. |
||
314 | * |
||
315 | * @throws JsonException |
||
316 | * |
||
317 | * @return array column list. |
||
318 | * @psalm-suppress ReferenceConstraintViolation |
||
319 | */ |
||
320 | private function getTableUniqueColumnNames(string $name, array $columns, array &$constraints = []): array |
||
385 | } |
||
386 | |||
387 | protected function getTypecastValue(mixed $value, ColumnSchemaInterface $columnSchema = null): mixed |
||
388 | { |
||
389 | if ($columnSchema) { |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Normalizes column names |
||
398 | * |
||
399 | * @param string $table the table that data will be saved into. |
||
400 | * @param array $columns the column data (name => value) to be saved into the table or instance of |
||
401 | * {@see QueryInterface} to perform INSERT INTO ... SELECT SQL statement. Passing of |
||
402 | * {@see QueryInterface}. |
||
403 | * |
||
404 | * @return array normalized columns. |
||
405 | */ |
||
406 | protected function normalizeColumnNames(string $table, array $columns): array |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Get map of normalized columns |
||
430 | * |
||
431 | * @param string $table |
||
432 | * @param string[] $columns |
||
433 | * |
||
434 | * @return string[] |
||
435 | */ |
||
436 | protected function getNormalizeColumnNames(string $table, array $columns): array |
||
455 | } |
||
456 | } |
||
457 |