| Total Complexity | 64 | 
| Total Lines | 453 | 
| Duplicated Lines | 0 % | 
| Changes | 3 | ||
| 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  | 
            ||
| 46 | abstract class AbstractDMLQueryBuilder implements DMLQueryBuilderInterface  | 
            ||
| 47 | { | 
            ||
| 48 | public function __construct(  | 
            ||
| 49 | protected QueryBuilderInterface $queryBuilder,  | 
            ||
| 50 | protected QuoterInterface $quoter,  | 
            ||
| 51 | protected SchemaInterface $schema  | 
            ||
| 52 |     ) { | 
            ||
| 53 | }  | 
            ||
| 54 | |||
| 55 | public function batchInsert(string $table, array $columns, iterable|Generator $rows, array &$params = []): string  | 
            ||
| 99 | }  | 
            ||
| 100 | |||
| 101 | public function delete(string $table, array|string $condition, array &$params): string  | 
            ||
| 102 |     { | 
            ||
| 103 | $sql = 'DELETE FROM ' . $this->quoter->quoteTableName($table);  | 
            ||
| 104 | $condition = $this->quoteCondition($condition);  | 
            ||
| 105 | $where = $this->queryBuilder->buildWhere($condition, $params);  | 
            ||
| 106 | |||
| 107 | return $where === '' ? $sql : $sql . ' ' . $where;  | 
            ||
| 108 | }  | 
            ||
| 109 | |||
| 110 | public function insert(string $table, QueryInterface|array $columns, array &$params = []): string  | 
            ||
| 111 |     { | 
            ||
| 112 | /**  | 
            ||
| 113 | * @psalm-var string[] $names  | 
            ||
| 114 | * @psalm-var string[] $placeholders  | 
            ||
| 115 | * @psalm-var string $values  | 
            ||
| 116 | */  | 
            ||
| 117 | [$names, $placeholders, $values, $params] = $this->prepareInsertValues($table, $columns, $params);  | 
            ||
| 118 | |||
| 119 | return 'INSERT INTO '  | 
            ||
| 120 | . $this->quoter->quoteTableName($table)  | 
            ||
| 121 |             . (!empty($names) ? ' (' . implode(', ', $names) . ')' : '') | 
            ||
| 122 |             . (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : $values); | 
            ||
| 123 | }  | 
            ||
| 124 | |||
| 125 | public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string  | 
            ||
| 126 |     { | 
            ||
| 127 | throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.');  | 
            ||
| 128 | }  | 
            ||
| 129 | |||
| 130 | public function resetSequence(string $table, int|string|null $value = null): string  | 
            ||
| 131 |     { | 
            ||
| 132 | throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.');  | 
            ||
| 133 | }  | 
            ||
| 134 | |||
| 135 | public function update(string $table, array $columns, array|string $condition, array &$params = []): string  | 
            ||
| 136 |     { | 
            ||
| 137 | /** @psalm-var string[] $lines */  | 
            ||
| 138 | [$lines, $params] = $this->prepareUpdateSets($table, $columns, $params);  | 
            ||
| 139 |         $sql = 'UPDATE ' . $this->quoter->quoteTableName($table) . ' SET ' . implode(', ', $lines); | 
            ||
| 140 | /** @psalm-var array $params */  | 
            ||
| 141 | $where = $this->queryBuilder->buildWhere($condition, $params);  | 
            ||
| 142 | |||
| 143 | return $where === '' ? $sql : $sql . ' ' . $where;  | 
            ||
| 144 | }  | 
            ||
| 145 | |||
| 146 | public function upsert(  | 
            ||
| 147 | string $table,  | 
            ||
| 148 | QueryInterface|array $insertColumns,  | 
            ||
| 149 | bool|array $updateColumns,  | 
            ||
| 150 | array &$params  | 
            ||
| 151 |     ): string { | 
            ||
| 152 | throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.');  | 
            ||
| 153 | }  | 
            ||
| 154 | |||
| 155 | /**  | 
            ||
| 156 | * Prepare select-subQuery and field names for `INSERT INTO ... SELECT` SQL statement.  | 
            ||
| 157 | *  | 
            ||
| 158 | * @param QueryInterface $columns Object, which represents a select query.  | 
            ||
| 159 | * @param array $params The parameters to bind to the generated SQL statement. These parameters will be included  | 
            ||
| 160 | * in the result, with the more parameters generated during the query building process.  | 
            ||
| 161 | *  | 
            ||
| 162 | * @throws Exception  | 
            ||
| 163 | * @throws InvalidArgumentException  | 
            ||
| 164 | * @throws InvalidConfigException  | 
            ||
| 165 | * @throws NotSupportedException  | 
            ||
| 166 | *  | 
            ||
| 167 | * @return array Array of column names, values, and params.  | 
            ||
| 168 | */  | 
            ||
| 169 | protected function prepareInsertSelectSubQuery(QueryInterface $columns, array $params = []): array  | 
            ||
| 170 |     { | 
            ||
| 171 |         if (empty($columns->getSelect()) || in_array('*', $columns->getSelect(), true)) { | 
            ||
| 172 |             throw new InvalidArgumentException('Expected select query object with enumerated (named) parameters'); | 
            ||
| 173 | }  | 
            ||
| 174 | |||
| 175 | [$values, $params] = $this->queryBuilder->build($columns, $params);  | 
            ||
| 176 | |||
| 177 | $names = [];  | 
            ||
| 178 | $values = ' ' . $values;  | 
            ||
| 179 | /** @psalm-var string[] $select */  | 
            ||
| 180 | $select = $columns->getSelect();  | 
            ||
| 181 | |||
| 182 |         foreach ($select as $title => $field) { | 
            ||
| 183 |             if (is_string($title)) { | 
            ||
| 184 | $names[] = $this->quoter->quoteColumnName($title);  | 
            ||
| 185 |             } else { | 
            ||
| 186 |                 if ($field instanceof ExpressionInterface) { | 
            ||
| 187 | $field = $this->queryBuilder->buildExpression($field, $params);  | 
            ||
| 188 | }  | 
            ||
| 189 | |||
| 190 |                 if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-_.]+)$/', $field, $matches)) { | 
            ||
| 191 | $names[] = $this->quoter->quoteColumnName($matches[2]);  | 
            ||
| 192 |                 } else { | 
            ||
| 193 | $names[] = $this->quoter->quoteColumnName($field);  | 
            ||
| 194 | }  | 
            ||
| 195 | }  | 
            ||
| 196 | }  | 
            ||
| 197 | |||
| 198 | return [$names, $values, $params];  | 
            ||
| 199 | }  | 
            ||
| 200 | |||
| 201 | /**  | 
            ||
| 202 | * Prepare column names and placeholders for `INSERT` SQL statement.  | 
            ||
| 203 | *  | 
            ||
| 204 | * @throws Exception  | 
            ||
| 205 | * @throws InvalidConfigException  | 
            ||
| 206 | * @throws InvalidArgumentException  | 
            ||
| 207 | * @throws NotSupportedException  | 
            ||
| 208 | */  | 
            ||
| 209 | protected function prepareInsertValues(string $table, array|QueryInterface $columns, array $params = []): array  | 
            ||
| 210 |     { | 
            ||
| 211 | $tableSchema = $this->schema->getTableSchema($table);  | 
            ||
| 212 | $columnSchemas = $tableSchema !== null ? $tableSchema->getColumns() : [];  | 
            ||
| 213 | $names = [];  | 
            ||
| 214 | $placeholders = [];  | 
            ||
| 215 | $values = ' DEFAULT VALUES';  | 
            ||
| 216 | |||
| 217 |         if ($columns instanceof QueryInterface) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 218 | [$names, $values, $params] = $this->prepareInsertSelectSubQuery($columns, $params);  | 
            ||
| 219 |         } else { | 
            ||
| 220 | $columns = $this->normalizeColumnNames($table, $columns);  | 
            ||
| 221 | /**  | 
            ||
| 222 | * @psalm-var mixed $value  | 
            ||
| 223 | * @psalm-var array<string, mixed> $columns  | 
            ||
| 224 | */  | 
            ||
| 225 |             foreach ($columns as $name => $value) { | 
            ||
| 226 | $names[] = $this->quoter->quoteColumnName($name);  | 
            ||
| 227 | /** @var mixed $value */  | 
            ||
| 228 | $value = $this->getTypecastValue($value, $columnSchemas[$name] ?? null);  | 
            ||
| 229 | |||
| 230 |                 if ($value instanceof ExpressionInterface) { | 
            ||
| 231 | $placeholders[] = $this->queryBuilder->buildExpression($value, $params);  | 
            ||
| 232 |                 } else { | 
            ||
| 233 | $placeholders[] = $this->queryBuilder->bindParam($value, $params);  | 
            ||
| 234 | }  | 
            ||
| 235 | }  | 
            ||
| 236 | }  | 
            ||
| 237 | |||
| 238 | return [$names, $placeholders, $values, $params];  | 
            ||
| 239 | }  | 
            ||
| 240 | |||
| 241 | /**  | 
            ||
| 242 | * Prepare column names and placeholders for `UPDATE` SQL statement.  | 
            ||
| 243 | *  | 
            ||
| 244 | * @throws Exception  | 
            ||
| 245 | * @throws InvalidConfigException  | 
            ||
| 246 | * @throws InvalidArgumentException  | 
            ||
| 247 | * @throws NotSupportedException  | 
            ||
| 248 | */  | 
            ||
| 249 | protected function prepareUpdateSets(string $table, array $columns, array $params = []): array  | 
            ||
| 250 |     { | 
            ||
| 251 | $tableSchema = $this->schema->getTableSchema($table);  | 
            ||
| 252 | $columnSchemas = $tableSchema !== null ? $tableSchema->getColumns() : [];  | 
            ||
| 253 | $sets = [];  | 
            ||
| 254 | $columns = $this->normalizeColumnNames($table, $columns);  | 
            ||
| 255 | |||
| 256 | /**  | 
            ||
| 257 | * @psalm-var array<string, mixed> $columns  | 
            ||
| 258 | * @psalm-var mixed $value  | 
            ||
| 259 | */  | 
            ||
| 260 |         foreach ($columns as $name => $value) { | 
            ||
| 261 | /** @psalm-var mixed $value */  | 
            ||
| 262 | $value = isset($columnSchemas[$name]) ? $columnSchemas[$name]->dbTypecast($value) : $value;  | 
            ||
| 263 |             if ($value instanceof ExpressionInterface) { | 
            ||
| 264 | $placeholder = $this->queryBuilder->buildExpression($value, $params);  | 
            ||
| 265 |             } else { | 
            ||
| 266 | $placeholder = $this->queryBuilder->bindParam($value, $params);  | 
            ||
| 267 | }  | 
            ||
| 268 | |||
| 269 | $sets[] = $this->quoter->quoteColumnName($name) . '=' . $placeholder;  | 
            ||
| 270 | }  | 
            ||
| 271 | |||
| 272 | return [$sets, $params];  | 
            ||
| 273 | }  | 
            ||
| 274 | |||
| 275 | /**  | 
            ||
| 276 | * Prepare column names and placeholders for "upsert" operation.  | 
            ||
| 277 | *  | 
            ||
| 278 | * @throws Exception  | 
            ||
| 279 | * @throws InvalidArgumentException  | 
            ||
| 280 | * @throws InvalidConfigException  | 
            ||
| 281 | * @throws JsonException  | 
            ||
| 282 | * @throws NotSupportedException  | 
            ||
| 283 | *  | 
            ||
| 284 | * @psalm-param Constraint[] $constraints  | 
            ||
| 285 | */  | 
            ||
| 286 | protected function prepareUpsertColumns(  | 
            ||
| 287 | string $table,  | 
            ||
| 288 | QueryInterface|array $insertColumns,  | 
            ||
| 289 | QueryInterface|bool|array $updateColumns,  | 
            ||
| 290 | array &$constraints = []  | 
            ||
| 291 |     ): array { | 
            ||
| 292 | $insertNames = [];  | 
            ||
| 293 | |||
| 294 |         if (!$insertColumns instanceof QueryInterface) { | 
            ||
| 295 | $insertColumns = $this->normalizeColumnNames($table, $insertColumns);  | 
            ||
| 296 | }  | 
            ||
| 297 | |||
| 298 |         if (is_array($updateColumns)) { | 
            ||
| 299 | $updateColumns = $this->normalizeColumnNames($table, $updateColumns);  | 
            ||
| 300 | }  | 
            ||
| 301 | |||
| 302 |         if ($insertColumns instanceof QueryInterface) { | 
            ||
| 303 | /** @psalm-var list<string> $insertNames */  | 
            ||
| 304 | [$insertNames] = $this->prepareInsertSelectSubQuery($insertColumns);  | 
            ||
| 305 |         } else { | 
            ||
| 306 | /** @psalm-var array<string, string> $insertColumns */  | 
            ||
| 307 |             foreach ($insertColumns as $key => $_value) { | 
            ||
| 308 | $insertNames[] = $this->quoter->quoteColumnName($key);  | 
            ||
| 309 | }  | 
            ||
| 310 | }  | 
            ||
| 311 | |||
| 312 | /** @psalm-var string[] $uniqueNames */  | 
            ||
| 313 | $uniqueNames = $this->getTableUniqueColumnNames($table, $insertNames, $constraints);  | 
            ||
| 314 | |||
| 315 |         foreach ($uniqueNames as $key => $name) { | 
            ||
| 316 | $insertNames[$key] = $this->quoter->quoteColumnName($name);  | 
            ||
| 317 | }  | 
            ||
| 318 | |||
| 319 |         if ($updateColumns !== true) { | 
            ||
| 320 | return [$uniqueNames, $insertNames, null];  | 
            ||
| 321 | }  | 
            ||
| 322 | |||
| 323 | return [$uniqueNames, $insertNames, array_diff($insertNames, $uniqueNames)];  | 
            ||
| 324 | }  | 
            ||
| 325 | |||
| 326 | /**  | 
            ||
| 327 | * Returns all column names belonging to constraints enforcing uniqueness (`PRIMARY KEY`, `UNIQUE INDEX`, etc.)  | 
            ||
| 328 | * for the named table removing constraints which didn't cover the specified column list.  | 
            ||
| 329 | *  | 
            ||
| 330 | * The column list will be unique by column names.  | 
            ||
| 331 | *  | 
            ||
| 332 | * @param string $name The table name, may contain schema name if any. Don't quote the table name.  | 
            ||
| 333 | * @param string[] $columns Source column list.  | 
            ||
| 334 | * @param array $constraints This parameter optionally receives a matched constraint list. The constraints  | 
            ||
| 335 | * will be unique by their column names.  | 
            ||
| 336 | *  | 
            ||
| 337 | * @throws JsonException  | 
            ||
| 338 | *  | 
            ||
| 339 | * @return array The column list.  | 
            ||
| 340 | *  | 
            ||
| 341 | * @psalm-param Constraint[] $constraints  | 
            ||
| 342 | */  | 
            ||
| 343 | private function getTableUniqueColumnNames(string $name, array $columns, array &$constraints = []): array  | 
            ||
| 411 | }  | 
            ||
| 412 | |||
| 413 | /**  | 
            ||
| 414 | * @return mixed The typecast value of the given column.  | 
            ||
| 415 | */  | 
            ||
| 416 | protected function getTypecastValue(mixed $value, ColumnSchemaInterface $columnSchema = null): mixed  | 
            ||
| 417 |     { | 
            ||
| 418 |         if ($columnSchema) { | 
            ||
| 419 | return $columnSchema->dbTypecast($value);  | 
            ||
| 420 | }  | 
            ||
| 421 | |||
| 422 | return $value;  | 
            ||
| 423 | }  | 
            ||
| 424 | |||
| 425 | /**  | 
            ||
| 426 | * Normalizes the column names for the given table.  | 
            ||
| 427 | *  | 
            ||
| 428 | * @param string $table The table to save the data into.  | 
            ||
| 429 | * @param array $columns The column data (name => value) to save into the table or instance of  | 
            ||
| 430 |      * {@see QueryInterface} to perform `INSERT INTO ... SELECT` SQL statement. Passing of {@see QueryInterface}. | 
            ||
| 431 | *  | 
            ||
| 432 | * @return array The normalized column names (name => value).  | 
            ||
| 433 | */  | 
            ||
| 434 | protected function normalizeColumnNames(string $table, array $columns): array  | 
            ||
| 435 |     { | 
            ||
| 436 | /** @var string[] $columnList */  | 
            ||
| 437 | $columnList = array_keys($columns);  | 
            ||
| 438 | $mappedNames = $this->getNormalizeColumnNames($table, $columnList);  | 
            ||
| 439 | |||
| 440 | /** @psalm-var array $normalizedColumns */  | 
            ||
| 441 | $normalizedColumns = [];  | 
            ||
| 442 | |||
| 443 | /**  | 
            ||
| 444 | * @psalm-var string $name  | 
            ||
| 445 | * @psalm-var mixed $value  | 
            ||
| 446 | */  | 
            ||
| 447 |         foreach ($columns as $name => $value) { | 
            ||
| 448 | $mappedName = $mappedNames[$name] ?? $name;  | 
            ||
| 449 | /** @psalm-var mixed */  | 
            ||
| 450 | $normalizedColumns[$mappedName] = $value;  | 
            ||
| 451 | }  | 
            ||
| 452 | |||
| 453 | return $normalizedColumns;  | 
            ||
| 454 | }  | 
            ||
| 455 | |||
| 456 | /**  | 
            ||
| 457 | * Get a map of normalized columns  | 
            ||
| 458 | *  | 
            ||
| 459 | * @param string $table The table to save the data into.  | 
            ||
| 460 | * @param string[] $columns The column data (name => value) to save into the table or instance of  | 
            ||
| 461 |      * {@see QueryInterface} to perform `INSERT INTO ... SELECT` SQL statement. Passing of {@see QueryInterface}. | 
            ||
| 462 | *  | 
            ||
| 463 | * @return string[] Map of normalized columns.  | 
            ||
| 464 | */  | 
            ||
| 465 | protected function getNormalizeColumnNames(string $table, array $columns): array  | 
            ||
| 484 | }  | 
            ||
| 485 | |||
| 486 | private function quoteCondition(array|string $values): array|string  | 
            ||
| 487 |     { | 
            ||
| 488 |         if (is_string($values)) { | 
            ||
| 489 | return $values;  | 
            ||
| 490 | }  | 
            ||
| 491 | |||
| 492 | $quoted = [];  | 
            ||
| 501 |