| Total Complexity | 48 | 
| Total Lines | 398 | 
| 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  | 
            ||
| 43 | abstract class AbstractDMLQueryBuilder implements DMLQueryBuilderInterface  | 
            ||
| 44 | { | 
            ||
| 45 | public function __construct(  | 
            ||
| 46 | protected QueryBuilderInterface $queryBuilder,  | 
            ||
| 47 | protected QuoterInterface $quoter,  | 
            ||
| 48 | protected SchemaInterface $schema  | 
            ||
| 49 |     ) { | 
            ||
| 50 | }  | 
            ||
| 51 | |||
| 52 | public function batchInsert(string $table, array $columns, iterable $rows, array &$params = []): string  | 
            ||
| 53 |     { | 
            ||
| 54 |         if (empty($rows)) { | 
            ||
| 55 | return '';  | 
            ||
| 56 | }  | 
            ||
| 57 | |||
| 58 | $values = [];  | 
            ||
| 59 |         $columns = $this->getNormalizeColumnNames('', $columns); | 
            ||
| 60 | $columnSchemas = $this->schema->getTableSchema($table)?->getColumns() ?? [];  | 
            ||
| 61 | |||
| 62 |         foreach ($rows as $row) { | 
            ||
| 63 | $i = 0;  | 
            ||
| 64 | $placeholders = [];  | 
            ||
| 65 | |||
| 66 |             foreach ($row as $value) { | 
            ||
| 67 |                 if (isset($columns[$i], $columnSchemas[$columns[$i]])) { | 
            ||
| 68 | $value = $columnSchemas[$columns[$i]]->dbTypecast($value);  | 
            ||
| 69 | }  | 
            ||
| 70 | |||
| 71 |                 if ($value instanceof ExpressionInterface) { | 
            ||
| 72 | $placeholders[] = $this->queryBuilder->buildExpression($value, $params);  | 
            ||
| 73 |                 } else { | 
            ||
| 74 | $placeholders[] = $this->queryBuilder->bindParam($value, $params);  | 
            ||
| 75 | }  | 
            ||
| 76 | |||
| 77 | ++$i;  | 
            ||
| 78 | }  | 
            ||
| 79 |             $values[] = '(' . implode(', ', $placeholders) . ')'; | 
            ||
| 80 | }  | 
            ||
| 81 | |||
| 82 |         if (empty($values)) { | 
            ||
| 83 | return '';  | 
            ||
| 84 | }  | 
            ||
| 85 | |||
| 86 | $columns = array_map(  | 
            ||
| 87 | [$this->quoter, 'quoteColumnName'],  | 
            ||
| 88 | $columns,  | 
            ||
| 89 | );  | 
            ||
| 90 | |||
| 91 | return 'INSERT INTO ' . $this->quoter->quoteTableName($table)  | 
            ||
| 92 |             . ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values); | 
            ||
| 93 | }  | 
            ||
| 94 | |||
| 95 | public function delete(string $table, array|string $condition, array &$params): string  | 
            ||
| 96 |     { | 
            ||
| 97 | $sql = 'DELETE FROM ' . $this->quoter->quoteTableName($table);  | 
            ||
| 98 | $where = $this->queryBuilder->buildWhere($condition, $params);  | 
            ||
| 99 | |||
| 100 | return $where === '' ? $sql : $sql . ' ' . $where;  | 
            ||
| 101 | }  | 
            ||
| 102 | |||
| 103 | public function insert(string $table, QueryInterface|array $columns, array &$params = []): string  | 
            ||
| 104 |     { | 
            ||
| 105 | [$names, $placeholders, $values, $params] = $this->prepareInsertValues($table, $columns, $params);  | 
            ||
| 106 | |||
| 107 | return 'INSERT INTO ' . $this->quoter->quoteTableName($table)  | 
            ||
| 108 |             . (!empty($names) ? ' (' . implode(', ', $names) . ')' : '') | 
            ||
| 109 |             . (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : ' ' . $values); | 
            ||
| 110 | }  | 
            ||
| 111 | |||
| 112 | public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string  | 
            ||
| 113 |     { | 
            ||
| 114 | throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.');  | 
            ||
| 115 | }  | 
            ||
| 116 | |||
| 117 | public function resetSequence(string $table, int|string|null $value = null): string  | 
            ||
| 118 |     { | 
            ||
| 119 | throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.');  | 
            ||
| 120 | }  | 
            ||
| 121 | |||
| 122 | public function update(string $table, array $columns, array|string $condition, array &$params = []): string  | 
            ||
| 123 |     { | 
            ||
| 124 | [$lines, $params] = $this->prepareUpdateSets($table, $columns, $params);  | 
            ||
| 125 | |||
| 126 |         $sql = 'UPDATE ' . $this->quoter->quoteTableName($table) . ' SET ' . implode(', ', $lines); | 
            ||
| 127 | $where = $this->queryBuilder->buildWhere($condition, $params);  | 
            ||
| 128 | |||
| 129 | return $where === '' ? $sql : $sql . ' ' . $where;  | 
            ||
| 130 | }  | 
            ||
| 131 | |||
| 132 | public function upsert(  | 
            ||
| 133 | string $table,  | 
            ||
| 134 | QueryInterface|array $insertColumns,  | 
            ||
| 135 | bool|array $updateColumns,  | 
            ||
| 136 | array &$params  | 
            ||
| 137 |     ): string { | 
            ||
| 138 | throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.');  | 
            ||
| 139 | }  | 
            ||
| 140 | |||
| 141 | /**  | 
            ||
| 142 | * Prepare select-subQuery and field names for `INSERT INTO ... SELECT` SQL statement.  | 
            ||
| 143 | *  | 
            ||
| 144 | * @param QueryInterface $columns Object, which represents a select query.  | 
            ||
| 145 | * @param array $params The parameters to bind to the generated SQL statement. These parameters will be included  | 
            ||
| 146 | * in the result, with the more parameters generated during the query building process.  | 
            ||
| 147 | *  | 
            ||
| 148 | * @throws Exception  | 
            ||
| 149 | * @throws InvalidArgumentException  | 
            ||
| 150 | * @throws InvalidConfigException  | 
            ||
| 151 | * @throws NotSupportedException  | 
            ||
| 152 | *  | 
            ||
| 153 | * @return array Array of quoted column names, values, and params.  | 
            ||
| 154 |      * @psalm-return array{0: string[], 1: string, 2: array} | 
            ||
| 155 | */  | 
            ||
| 156 | protected function prepareInsertSelectSubQuery(QueryInterface $columns, array $params = []): array  | 
            ||
| 186 | }  | 
            ||
| 187 | |||
| 188 | /**  | 
            ||
| 189 | * Prepare column names and placeholders for `INSERT` SQL statement.  | 
            ||
| 190 | *  | 
            ||
| 191 | * @throws Exception  | 
            ||
| 192 | * @throws InvalidConfigException  | 
            ||
| 193 | * @throws InvalidArgumentException  | 
            ||
| 194 | * @throws NotSupportedException  | 
            ||
| 195 | *  | 
            ||
| 196 | * @return array Array of quoted column names, placeholders, values, and params.  | 
            ||
| 197 |      * @psalm-return array{0: string[], 1: string[], 2: string, 3: array} | 
            ||
| 198 | */  | 
            ||
| 199 | protected function prepareInsertValues(string $table, array|QueryInterface $columns, array $params = []): array  | 
            ||
| 200 |     { | 
            ||
| 201 |         if (empty($columns)) { | 
            ||
| 202 | return [[], [], 'DEFAULT VALUES', []];  | 
            ||
| 203 | }  | 
            ||
| 204 | |||
| 205 |         if ($columns instanceof QueryInterface) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 206 | [$names, $values, $params] = $this->prepareInsertSelectSubQuery($columns, $params);  | 
            ||
| 207 | return [$names, [], $values, $params];  | 
            ||
| 208 | }  | 
            ||
| 209 | |||
| 210 | $names = [];  | 
            ||
| 211 | $placeholders = [];  | 
            ||
| 212 |         $columns = $this->normalizeColumnNames('', $columns); | 
            ||
| 213 | $columnSchemas = $this->schema->getTableSchema($table)?->getColumns() ?? [];  | 
            ||
| 214 | |||
| 215 |         foreach ($columns as $name => $value) { | 
            ||
| 216 | $names[] = $this->quoter->quoteColumnName($name);  | 
            ||
| 217 | |||
| 218 |             if (isset($columnSchemas[$name])) { | 
            ||
| 219 | $value = $columnSchemas[$name]->dbTypecast($value);  | 
            ||
| 220 | }  | 
            ||
| 221 | |||
| 222 |             if ($value instanceof ExpressionInterface) { | 
            ||
| 223 | $placeholders[] = $this->queryBuilder->buildExpression($value, $params);  | 
            ||
| 224 |             } else { | 
            ||
| 225 | $placeholders[] = $this->queryBuilder->bindParam($value, $params);  | 
            ||
| 226 | }  | 
            ||
| 227 | }  | 
            ||
| 228 | |||
| 229 | return [$names, $placeholders, '', $params];  | 
            ||
| 230 | }  | 
            ||
| 231 | |||
| 232 | /**  | 
            ||
| 233 | * Prepare column names and placeholders for `UPDATE` SQL statement.  | 
            ||
| 234 | *  | 
            ||
| 235 | * @throws Exception  | 
            ||
| 236 | * @throws InvalidConfigException  | 
            ||
| 237 | * @throws InvalidArgumentException  | 
            ||
| 238 | * @throws NotSupportedException  | 
            ||
| 239 | *  | 
            ||
| 240 |      * @psalm-return array{0: string[], 1: array} | 
            ||
| 241 | */  | 
            ||
| 242 | protected function prepareUpdateSets(string $table, array $columns, array $params = []): array  | 
            ||
| 263 | }  | 
            ||
| 264 | |||
| 265 | /**  | 
            ||
| 266 | * Prepare column names and constraints for "upsert" operation.  | 
            ||
| 267 | *  | 
            ||
| 268 | * @throws Exception  | 
            ||
| 269 | * @throws InvalidArgumentException  | 
            ||
| 270 | * @throws InvalidConfigException  | 
            ||
| 271 | * @throws JsonException  | 
            ||
| 272 | * @throws NotSupportedException  | 
            ||
| 273 | *  | 
            ||
| 274 | * @psalm-param array<string, mixed>|QueryInterface $insertColumns  | 
            ||
| 275 | * @psalm-param Constraint[] $constraints  | 
            ||
| 276 | *  | 
            ||
| 277 | * @return array Array of unique, insert and update quoted column names.  | 
            ||
| 278 |      * @psalm-return array{0: string[], 1: string[], 2: string[]|null} | 
            ||
| 279 | */  | 
            ||
| 280 | protected function prepareUpsertColumns(  | 
            ||
| 281 | string $table,  | 
            ||
| 282 | QueryInterface|array $insertColumns,  | 
            ||
| 283 | QueryInterface|bool|array $updateColumns,  | 
            ||
| 284 | array &$constraints = []  | 
            ||
| 285 |     ): array { | 
            ||
| 286 |         if ($insertColumns instanceof QueryInterface) { | 
            ||
| 287 | [$insertNames] = $this->prepareInsertSelectSubQuery($insertColumns);  | 
            ||
| 288 |         } else { | 
            ||
| 289 |             $insertNames = $this->getNormalizeColumnNames('', array_keys($insertColumns)); | 
            ||
| 290 | |||
| 291 | $insertNames = array_map(  | 
            ||
| 292 | [$this->quoter, 'quoteColumnName'],  | 
            ||
| 293 | $insertNames,  | 
            ||
| 294 | );  | 
            ||
| 295 | }  | 
            ||
| 296 | |||
| 297 | $uniqueNames = $this->getTableUniqueColumnNames($table, $insertNames, $constraints);  | 
            ||
| 298 | |||
| 299 |         if ($updateColumns === true) { | 
            ||
| 300 | return [$uniqueNames, $insertNames, array_diff($insertNames, $uniqueNames)];  | 
            ||
| 301 | }  | 
            ||
| 302 | |||
| 303 | return [$uniqueNames, $insertNames, null];  | 
            ||
| 304 | }  | 
            ||
| 305 | |||
| 306 | /**  | 
            ||
| 307 | * Returns all quoted column names belonging to constraints enforcing uniqueness (`PRIMARY KEY`, `UNIQUE INDEX`, etc.)  | 
            ||
| 308 | * for the named table removing constraints which didn't cover the specified column list.  | 
            ||
| 309 | *  | 
            ||
| 310 | * The column list will be unique by column names.  | 
            ||
| 311 | *  | 
            ||
| 312 | * @param string $name The table name, may contain schema name if any. Don't quote the table name.  | 
            ||
| 313 | * @param string[] $columns Source column list.  | 
            ||
| 314 | * @param array $constraints This parameter optionally receives a matched constraint list. The constraints  | 
            ||
| 315 | * will be unique by their column names.  | 
            ||
| 316 | *  | 
            ||
| 317 | * @throws JsonException  | 
            ||
| 318 | *  | 
            ||
| 319 | * @return string[] The quoted column names.  | 
            ||
| 320 | *  | 
            ||
| 321 | * @psalm-param Constraint[] $constraints  | 
            ||
| 322 | */  | 
            ||
| 323 | private function getTableUniqueColumnNames(string $name, array $columns, array &$constraints = []): array  | 
            ||
| 324 |     { | 
            ||
| 325 | $primaryKey = $this->schema->getTablePrimaryKey($name);  | 
            ||
| 326 | |||
| 327 |         if ($primaryKey !== null) { | 
            ||
| 328 | $constraints[] = $primaryKey;  | 
            ||
| 329 | }  | 
            ||
| 330 | |||
| 331 | /** @psalm-var IndexConstraint[] $tableIndexes */  | 
            ||
| 332 | $tableIndexes = $this->schema->getTableIndexes($name);  | 
            ||
| 333 | |||
| 334 |         foreach ($tableIndexes as $constraint) { | 
            ||
| 335 |             if ($constraint->isUnique()) { | 
            ||
| 336 | $constraints[] = $constraint;  | 
            ||
| 337 | }  | 
            ||
| 338 | }  | 
            ||
| 339 | |||
| 340 | $constraints = array_merge($constraints, $this->schema->getTableUniques($name));  | 
            ||
| 341 | |||
| 342 | /**  | 
            ||
| 343 | * Remove duplicates  | 
            ||
| 344 | *  | 
            ||
| 345 | * @psalm-var Constraint[] $constraints  | 
            ||
| 346 | */  | 
            ||
| 347 | $constraints = array_combine(  | 
            ||
| 348 | array_map(  | 
            ||
| 349 |                 static function (Constraint $constraint): string { | 
            ||
| 350 | $columns = (array) $constraint->getColumnNames();  | 
            ||
| 351 | sort($columns, SORT_STRING);  | 
            ||
| 352 | return json_encode($columns, JSON_THROW_ON_ERROR);  | 
            ||
| 353 | },  | 
            ||
| 354 | $constraints  | 
            ||
| 355 | ),  | 
            ||
| 356 | $constraints  | 
            ||
| 357 | );  | 
            ||
| 358 | |||
| 359 | $columnNames = [];  | 
            ||
| 360 | $quoter = $this->quoter;  | 
            ||
| 361 | |||
| 362 | // Remove all constraints which don't cover the specified column list.  | 
            ||
| 363 | $constraints = array_values(  | 
            ||
| 364 | array_filter(  | 
            ||
| 365 | $constraints,  | 
            ||
| 366 |                 static function (Constraint $constraint) use ($quoter, $columns, &$columnNames): bool { | 
            ||
| 367 | /** @psalm-var string[] $constraintColumnNames */  | 
            ||
| 368 | $constraintColumnNames = (array) $constraint->getColumnNames();  | 
            ||
| 369 | |||
| 370 | $constraintColumnNames = array_map(  | 
            ||
| 371 | [$quoter, 'quoteColumnName'],  | 
            ||
| 372 | $constraintColumnNames,  | 
            ||
| 373 | );  | 
            ||
| 374 | |||
| 375 | $result = empty(array_diff($constraintColumnNames, $columns));  | 
            ||
| 376 | |||
| 377 |                     if ($result) { | 
            ||
| 378 | $columnNames = array_merge((array) $columnNames, $constraintColumnNames);  | 
            ||
| 379 | }  | 
            ||
| 380 | |||
| 381 | return $result;  | 
            ||
| 382 | }  | 
            ||
| 383 | )  | 
            ||
| 384 | );  | 
            ||
| 385 | |||
| 386 | /** @psalm-var string[] $columnNames */  | 
            ||
| 387 | return array_unique($columnNames);  | 
            ||
| 388 | }  | 
            ||
| 389 | |||
| 390 | /**  | 
            ||
| 391 | * @return mixed The typecast value of the given column.  | 
            ||
| 392 | *  | 
            ||
| 393 | * @deprecated will be removed in version 2.0.0  | 
            ||
| 394 | */  | 
            ||
| 395 | protected function getTypecastValue(mixed $value, ColumnSchemaInterface $columnSchema = null): mixed  | 
            ||
| 396 |     { | 
            ||
| 397 |         if ($columnSchema) { | 
            ||
| 398 | return $columnSchema->dbTypecast($value);  | 
            ||
| 399 | }  | 
            ||
| 400 | |||
| 401 | return $value;  | 
            ||
| 402 | }  | 
            ||
| 403 | |||
| 404 | /**  | 
            ||
| 405 | * Normalizes the column names.  | 
            ||
| 406 | *  | 
            ||
| 407 | * @param string $table Not used. Could be empty string. Will be removed in version 2.0.0.  | 
            ||
| 408 | * @param array $columns The column data (name => value).  | 
            ||
| 409 | *  | 
            ||
| 410 | * @return array The normalized column names (name => value).  | 
            ||
| 411 | *  | 
            ||
| 412 | * @psalm-return array<string, mixed>  | 
            ||
| 413 | */  | 
            ||
| 414 | protected function normalizeColumnNames(string $table, array $columns): array  | 
            ||
| 415 |     { | 
            ||
| 416 | /** @var string[] $columnNames */  | 
            ||
| 417 | $columnNames = array_keys($columns);  | 
            ||
| 418 |         $normalizedNames = $this->getNormalizeColumnNames('', $columnNames); | 
            ||
| 419 | |||
| 420 | return array_combine($normalizedNames, $columns);  | 
            ||
| 421 | }  | 
            ||
| 422 | |||
| 423 | /**  | 
            ||
| 424 | * Get normalized column names  | 
            ||
| 425 | *  | 
            ||
| 426 | * @param string $table Not used. Could be empty string. Will be removed in version 2.0.0.  | 
            ||
| 427 | * @param string[] $columns The column names.  | 
            ||
| 428 | *  | 
            ||
| 429 | * @return string[] Normalized column names.  | 
            ||
| 430 | */  | 
            ||
| 431 | protected function getNormalizeColumnNames(string $table, array $columns): array  | 
            ||
| 441 | }  | 
            ||
| 442 | }  | 
            ||
| 443 |