1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\QueryBuilder; |
6
|
|
|
|
7
|
|
|
use JsonException; |
8
|
|
|
use Yiisoft\Db\Constraint\Constraint; |
9
|
|
|
use Yiisoft\Db\Constraint\IndexConstraint; |
10
|
|
|
use Yiisoft\Db\Exception\Exception; |
11
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
12
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
13
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
14
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
15
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
16
|
|
|
use Yiisoft\Db\Schema\ColumnSchemaInterface; |
17
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
18
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
19
|
|
|
|
20
|
|
|
use function array_combine; |
21
|
|
|
use function array_diff; |
22
|
|
|
use function array_filter; |
23
|
|
|
use function array_keys; |
24
|
|
|
use function array_map; |
25
|
|
|
use function array_merge; |
26
|
|
|
use function array_unique; |
27
|
|
|
use function array_values; |
28
|
|
|
use function implode; |
29
|
|
|
use function in_array; |
30
|
|
|
use function is_string; |
31
|
|
|
use function json_encode; |
32
|
|
|
use function preg_match; |
33
|
|
|
use function sort; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* It's used to manipulate data in tables. |
37
|
|
|
* |
38
|
|
|
* This manipulation involves inserting data into database tables, retrieving existing data, deleting data from existing |
39
|
|
|
* tables and modifying existing data. |
40
|
|
|
* |
41
|
|
|
* @link https://en.wikipedia.org/wiki/Data_manipulation_language |
42
|
|
|
*/ |
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 |
157
|
|
|
{ |
158
|
|
|
/** @psalm-var string[] $select */ |
159
|
|
|
$select = $columns->getSelect(); |
160
|
|
|
|
161
|
|
|
if (empty($select) || in_array('*', $select, true)) { |
162
|
|
|
throw new InvalidArgumentException('Expected select query object with enumerated (named) parameters'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
[$values, $params] = $this->queryBuilder->build($columns, $params); |
166
|
|
|
|
167
|
|
|
$names = []; |
168
|
|
|
|
169
|
|
|
foreach ($select as $title => $field) { |
170
|
|
|
if (is_string($title)) { |
171
|
|
|
$names[] = $this->quoter->quoteColumnName($title); |
172
|
|
|
} else { |
173
|
|
|
if ($field instanceof ExpressionInterface) { |
174
|
|
|
$field = $this->queryBuilder->buildExpression($field, $params); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-_.]+)$/', $field, $matches)) { |
178
|
|
|
$names[] = $this->quoter->quoteColumnName($matches[2]); |
179
|
|
|
} else { |
180
|
|
|
$names[] = $this->quoter->quoteColumnName($field); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return [$names, $values, $params]; |
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 |
243
|
|
|
{ |
244
|
|
|
$sets = []; |
245
|
|
|
$columns = $this->normalizeColumnNames('', $columns); |
246
|
|
|
$columnSchemas = $this->schema->getTableSchema($table)?->getColumns() ?? []; |
247
|
|
|
|
248
|
|
|
foreach ($columns as $name => $value) { |
249
|
|
|
if (isset($columnSchemas[$name])) { |
250
|
|
|
$value = $columnSchemas[$name]->dbTypecast($value); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
if ($value instanceof ExpressionInterface) { |
254
|
|
|
$placeholder = $this->queryBuilder->buildExpression($value, $params); |
255
|
|
|
} else { |
256
|
|
|
$placeholder = $this->queryBuilder->bindParam($value, $params); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$sets[] = $this->quoter->quoteColumnName($name) . '=' . $placeholder; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return [$sets, $params]; |
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 |
|
|
|
|
432
|
|
|
{ |
433
|
|
|
$normalizedNames = []; |
434
|
|
|
|
435
|
|
|
foreach ($columns as $name) { |
436
|
|
|
$normalizedName = $this->quoter->ensureColumnName($name); |
437
|
|
|
$normalizedNames[] = $this->quoter->unquoteSimpleColumnName($normalizedName); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
return $normalizedNames; |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
|