Issues (43)

src/QueryBuilder/AbstractDMLQueryBuilder.php (5 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\QueryBuilder;
6
7
use JsonException;
8
use Traversable;
9
use Yiisoft\Db\Connection\ConnectionInterface;
10
use Yiisoft\Db\Constraint\Constraint;
11
use Yiisoft\Db\Exception\Exception;
12
use Yiisoft\Db\Exception\InvalidArgumentException;
13
use Yiisoft\Db\Exception\InvalidConfigException;
14
use Yiisoft\Db\Exception\NotSupportedException;
15
use Yiisoft\Db\Expression\ExpressionInterface;
16
use Yiisoft\Db\Query\QueryInterface;
17
use Yiisoft\Db\Schema\ColumnSchemaInterface;
18
use Yiisoft\Db\Schema\QuoterInterface;
19
use Yiisoft\Db\Schema\SchemaInterface;
20
21
use function array_combine;
22
use function array_diff;
23
use function array_fill_keys;
24
use function array_filter;
25
use function array_key_exists;
26
use function array_keys;
27
use function array_map;
28
use function array_merge;
29
use function array_unique;
30
use function array_values;
31
use function count;
32
use function get_object_vars;
33
use function implode;
34
use function in_array;
35
use function is_array;
36
use function is_object;
37
use function is_string;
38
use function iterator_to_array;
39
use function json_encode;
40
use function preg_match;
41
use function reset;
42
use function sort;
43
44
/**
45
 * It's used to manipulate data in tables.
46
 *
47
 * This manipulation involves inserting data into database tables, retrieving existing data, deleting data from existing
48
 * tables and modifying existing data.
49
 *
50
 * @link https://en.wikipedia.org/wiki/Data_manipulation_language
51
 *
52
 * @psalm-import-type ParamsType from ConnectionInterface
53
 */
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) {
0 ignored issues
show
$columns is never a sub-type of Yiisoft\Db\Query\QueryInterface.
Loading history...
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) {
0 ignored issues
show
$insertColumns is never a sub-type of Yiisoft\Db\Query\QueryInterface.
Loading history...
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) {
0 ignored issues
show
The condition $updateColumns === true is always false.
Loading history...
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
403
    {
404
        $primaryKey = $this->schema->getTablePrimaryKey($name);
405
406
        if ($primaryKey !== null) {
407
            $constraints[] = $primaryKey;
408
        }
409
410
        $tableIndexes = $this->schema->getTableIndexes($name);
411
412
        foreach ($tableIndexes as $constraint) {
413
            if ($constraint->isUnique()) {
414
                $constraints[] = $constraint;
415
            }
416
        }
417
418
        $constraints = array_merge($constraints, $this->schema->getTableUniques($name));
419
420
        /**
421
         * Remove duplicates
422
         *
423
         * @psalm-var Constraint[] $constraints
424
         */
425
        $constraints = array_combine(
426
            array_map(
427
                static function (Constraint $constraint): string {
428
                    $columns = (array) $constraint->getColumnNames();
429
                    sort($columns, SORT_STRING);
430
                    return json_encode($columns, JSON_THROW_ON_ERROR);
431
                },
432
                $constraints
433
            ),
434
            $constraints
435
        );
436
437
        $columnNames = [];
438
        $quoter = $this->quoter;
439
440
        // Remove all constraints which don't cover the specified column list.
441
        $constraints = array_values(
442
            array_filter(
443
                $constraints,
444
                static function (Constraint $constraint) use ($quoter, $columns, &$columnNames): bool {
445
                    /** @psalm-var string[] $constraintColumnNames */
446
                    $constraintColumnNames = (array) $constraint->getColumnNames();
447
448
                    $constraintColumnNames = array_map(
449
                        [$quoter, 'quoteColumnName'],
450
                        $constraintColumnNames,
451
                    );
452
453
                    $result = empty(array_diff($constraintColumnNames, $columns));
454
455
                    if ($result) {
456
                        $columnNames = array_merge($columnNames, $constraintColumnNames);
457
                    }
458
459
                    return $result;
460
                }
461
            )
462
        );
463
464
        /** @psalm-var string[] $columnNames */
465
        return array_unique($columnNames);
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
0 ignored issues
show
The parameter $table is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

492
    protected function normalizeColumnNames(/** @scrutinizer ignore-unused */ string $table, array $columns): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
0 ignored issues
show
The parameter $table is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

509
    protected function getNormalizeColumnNames(/** @scrutinizer ignore-unused */ string $table, array $columns): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
510
    {
511
        foreach ($columns as &$name) {
512
            $name = $this->quoter->ensureColumnName($name);
513
            $name = $this->quoter->unquoteSimpleColumnName($name);
514
        }
515
516
        return $columns;
517
    }
518
}
519