-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathAbstractCommand.php
650 lines (555 loc) · 20 KB
/
AbstractCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Command;
use Closure;
use Throwable;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Constant\GettypeResult;
use Yiisoft\Db\Query\Data\DataReaderInterface;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\DMLQueryBuilderInterface;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use function explode;
use function get_resource_type;
use function gettype;
use function is_array;
use function is_int;
use function is_resource;
use function is_scalar;
use function is_string;
use function preg_replace_callback;
use function str_starts_with;
use function stream_get_contents;
/**
* Represents an SQL statement to execute in a database.
*
* It's usually created by calling {@see \Yiisoft\Db\Connection\ConnectionInterface::createCommand()}.
*
* You can get the SQL statement it represents via the {@see getSql()} method.
*
* To execute a non-query SQL (such as `INSERT`, `DELETE`, `UPDATE`), call {@see execute()}.
*
* To execute a SQL statement that returns a result (such as `SELECT`), use {@see queryAll()}, {@see queryOne()},
* {@see queryColumn()}, {@see queryScalar()}, or {@see query()}.
*
* For example,
*
* ```php
* $users = $connectionInterface->createCommand('SELECT * FROM user')->queryAll();
* ```
*
* Abstract command supports SQL prepared statements and parameter binding.
*
* Call {@see bindValue()} to bind a value to a SQL parameter.
* Call {@see bindParam()} to bind a PHP variable to a SQL parameter.
*
* When binding a parameter, the SQL statement is automatically prepared. You may also call {@see prepare()} explicitly
* to do it.
*
* Abstract command supports building some SQL statements using methods such as {@see insert()}, {@see update()}, {@see delete()},
* etc.
*
* For example, the following code will create and execute an `INSERT` SQL statement:
*
* ```php
* $connectionInterface->createCommand()->insert(
* 'user',
* ['name' => 'Sam', 'age' => 30],
* )->execute();
* ```
*
* To build `SELECT` SQL statements, please use {@see QueryInterface} and its implementations instead.
*
* @psalm-import-type BatchValues from DMLQueryBuilderInterface
*/
abstract class AbstractCommand implements CommandInterface
{
/**
* Command in this query mode returns count of affected rows.
*
* @see execute()
*/
protected const QUERY_MODE_EXECUTE = 1;
/**
* Command in this query mode returns the first row of selected data.
*
* @see queryOne()
*/
protected const QUERY_MODE_ROW = 2;
/**
* Command in this query mode returns all rows of selected data.
*
* @see queryAll()
*/
protected const QUERY_MODE_ALL = 4;
/**
* Command in this query mode returns all rows with the first column of selected data.
*
* @see queryColumn()
*/
protected const QUERY_MODE_COLUMN = 8;
/**
* Command in this query mode returns {@see DataReaderInterface}, an abstraction for database cursor for
* selected data.
*
* @see query()
*/
protected const QUERY_MODE_CURSOR = 16;
/**
* Command in this query mode returns the first column in the first row of the query result
*
* @see queryScalar()
*/
protected const QUERY_MODE_SCALAR = 32;
/**
* @var string|null Transaction isolation level.
*/
protected string|null $isolationLevel = null;
/**
* @var ParamInterface[] Parameters to use.
*/
protected array $params = [];
/**
* @var string|null Name of the table to refresh schema for. Null means not to refresh the schema.
*/
protected string|null $refreshTableName = null;
protected Closure|null $retryHandler = null;
/**
* @var string The SQL statement to execute.
*/
private string $sql = '';
public function addCheck(string $table, string $name, string $expression): static
{
$sql = $this->getQueryBuilder()->addCheck($table, $name, $expression);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function addColumn(string $table, string $column, ColumnInterface|string $type): static
{
$sql = $this->getQueryBuilder()->addColumn($table, $column, $type);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function addCommentOnColumn(string $table, string $column, string $comment): static
{
$sql = $this->getQueryBuilder()->addCommentOnColumn($table, $column, $comment);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function addCommentOnTable(string $table, string $comment): static
{
$sql = $this->getQueryBuilder()->addCommentOnTable($table, $comment);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function addDefaultValue(string $table, string $name, string $column, mixed $value): static
{
$sql = $this->getQueryBuilder()->addDefaultValue($table, $name, $column, $value);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function addForeignKey(
string $table,
string $name,
array|string $columns,
string $referenceTable,
array|string $referenceColumns,
string $delete = null,
string $update = null
): static {
$sql = $this->getQueryBuilder()->addForeignKey(
$table,
$name,
$columns,
$referenceTable,
$referenceColumns,
$delete,
$update
);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function addPrimaryKey(string $table, string $name, array|string $columns): static
{
$sql = $this->getQueryBuilder()->addPrimaryKey($table, $name, $columns);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function addUnique(string $table, string $name, array|string $columns): static
{
$sql = $this->getQueryBuilder()->addUnique($table, $name, $columns);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function alterColumn(string $table, string $column, ColumnInterface|string $type): static
{
$sql = $this->getQueryBuilder()->alterColumn($table, $column, $type);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
/**
* @param string[] $columns
*
* @psalm-param BatchValues $rows
*
* @deprecated Use {@see insertBatch()} instead. It will be removed in version 3.0.0.
*/
public function batchInsert(string $table, array $columns, iterable $rows): static
{
return $this->insertBatch($table, $rows, $columns);
}
public function insertBatch(string $table, iterable $rows, array $columns = []): static
{
$table = $this->getQueryBuilder()->quoter()->getRawTableName($table);
$params = [];
$sql = $this->getQueryBuilder()->insertBatch($table, $rows, $columns, $params);
$this->setRawSql($sql);
$this->bindValues($params);
return $this;
}
abstract public function bindValue(int|string $name, mixed $value, int $dataType = null): static;
abstract public function bindValues(array $values): static;
public function checkIntegrity(string $schema, string $table, bool $check = true): static
{
$sql = $this->getQueryBuilder()->checkIntegrity($schema, $table, $check);
return $this->setSql($sql);
}
public function createIndex(
string $table,
string $name,
array|string $columns,
string $indexType = null,
string $indexMethod = null
): static {
$sql = $this->getQueryBuilder()->createIndex($table, $name, $columns, $indexType, $indexMethod);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function createTable(string $table, array $columns, string $options = null): static
{
$sql = $this->getQueryBuilder()->createTable($table, $columns, $options);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function createView(string $viewName, QueryInterface|string $subQuery): static
{
$sql = $this->getQueryBuilder()->createView($viewName, $subQuery);
return $this->setSql($sql)->requireTableSchemaRefresh($viewName);
}
public function delete(string $table, array|string $condition = '', array $params = []): static
{
$sql = $this->getQueryBuilder()->delete($table, $condition, $params);
return $this->setSql($sql)->bindValues($params);
}
public function dropCheck(string $table, string $name): static
{
$sql = $this->getQueryBuilder()->dropCheck($table, $name);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function dropColumn(string $table, string $column): static
{
$sql = $this->getQueryBuilder()->dropColumn($table, $column);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function dropCommentFromColumn(string $table, string $column): static
{
$sql = $this->getQueryBuilder()->dropCommentFromColumn($table, $column);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function dropCommentFromTable(string $table): static
{
$sql = $this->getQueryBuilder()->dropCommentFromTable($table);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function dropDefaultValue(string $table, string $name): static
{
$sql = $this->getQueryBuilder()->dropDefaultValue($table, $name);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function dropForeignKey(string $table, string $name): static
{
$sql = $this->getQueryBuilder()->dropForeignKey($table, $name);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function dropIndex(string $table, string $name): static
{
$sql = $this->getQueryBuilder()->dropIndex($table, $name);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function dropPrimaryKey(string $table, string $name): static
{
$sql = $this->getQueryBuilder()->dropPrimaryKey($table, $name);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function dropTable(string $table): static
{
$sql = $this->getQueryBuilder()->dropTable($table);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function dropUnique(string $table, string $name): static
{
$sql = $this->getQueryBuilder()->dropUnique($table, $name);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function dropView(string $viewName): static
{
$sql = $this->getQueryBuilder()->dropView($viewName);
return $this->setSql($sql)->requireTableSchemaRefresh($viewName);
}
public function getParams(bool $asValues = true): array
{
if (!$asValues) {
return $this->params;
}
$buildParams = [];
foreach ($this->params as $name => $value) {
/** @psalm-var mixed */
$buildParams[$name] = $value->getValue();
}
return $buildParams;
}
public function getRawSql(): string
{
if (empty($this->params)) {
return $this->sql;
}
$params = [];
$quoter = $this->getQueryBuilder()->quoter();
foreach ($this->params as $name => $param) {
if (is_string($name) && !str_starts_with($name, ':')) {
$name = ':' . $name;
}
$value = $param->getValue();
$params[$name] = match ($param->getType()) {
DataType::INTEGER => (string) (int) $value,
DataType::STRING, DataType::LOB => match (gettype($value)) {
GettypeResult::RESOURCE => $name,
GettypeResult::DOUBLE => (string) $value,
default => $value instanceof Expression
? (string) $value
: $quoter->quoteValue((string) $value),
},
DataType::BOOLEAN => $value ? 'TRUE' : 'FALSE',
DataType::NULL => 'NULL',
default => $name,
};
}
/** @var string[] $params */
if (!isset($params[0])) {
return preg_replace_callback(
'#(:\w+)#',
static fn (array $matches): string => $params[$matches[1]] ?? $matches[1],
$this->sql
);
}
// Support unnamed placeholders should be dropped
$sql = '';
foreach (explode('?', $this->sql) as $i => $part) {
$sql .= $part . ($params[$i] ?? '');
}
return $sql;
}
public function getSql(): string
{
return $this->sql;
}
public function insert(string $table, QueryInterface|array $columns): static
{
$params = [];
$sql = $this->getQueryBuilder()->insert($table, $columns, $params);
return $this->setSql($sql)->bindValues($params);
}
public function insertWithReturningPks(string $table, array $columns): bool|array
{
$params = [];
$sql = $this->getQueryBuilder()->insertWithReturningPks($table, $columns, $params);
$this->setSql($sql)->bindValues($params);
/** @psalm-var array|bool $result */
$result = $this->queryInternal(self::QUERY_MODE_ROW | self::QUERY_MODE_EXECUTE);
return is_array($result) ? $result : false;
}
public function execute(): int
{
$sql = $this->getSql();
if ($sql === '') {
return 0;
}
/** @psalm-var int|bool $execute */
$execute = $this->queryInternal(self::QUERY_MODE_EXECUTE);
return is_int($execute) ? $execute : 0;
}
public function query(): DataReaderInterface
{
/** @psalm-var DataReaderInterface */
return $this->queryInternal(self::QUERY_MODE_CURSOR);
}
public function queryAll(): array
{
/** @psalm-var array<array-key, array>|null $results */
$results = $this->queryInternal(self::QUERY_MODE_ALL);
return $results ?? [];
}
public function queryColumn(): array
{
/** @psalm-var mixed $results */
$results = $this->queryInternal(self::QUERY_MODE_COLUMN);
return is_array($results) ? $results : [];
}
public function queryOne(): array|null
{
/** @psalm-var mixed $results */
$results = $this->queryInternal(self::QUERY_MODE_ROW);
return is_array($results) ? $results : null;
}
public function queryScalar(): bool|string|null|int|float
{
/** @psalm-var mixed $result */
$result = $this->queryInternal(self::QUERY_MODE_SCALAR);
if (is_resource($result) && get_resource_type($result) === 'stream') {
return stream_get_contents($result);
}
return is_scalar($result) ? $result : null;
}
public function renameColumn(string $table, string $oldName, string $newName): static
{
$sql = $this->getQueryBuilder()->renameColumn($table, $oldName, $newName);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function renameTable(string $table, string $newName): static
{
$sql = $this->getQueryBuilder()->renameTable($table, $newName);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public function resetSequence(string $table, int|string $value = null): static
{
$sql = $this->getQueryBuilder()->resetSequence($table, $value);
return $this->setSql($sql);
}
public function setRawSql(string $sql): static
{
if ($sql !== $this->sql) {
$this->cancel();
$this->reset();
$this->sql = $sql;
}
return $this;
}
public function setSql(string $sql): static
{
$this->cancel();
$this->reset();
$this->sql = $this->getQueryBuilder()->quoter()->quoteSql($sql);
return $this;
}
public function setRetryHandler(Closure|null $handler): static
{
$this->retryHandler = $handler;
return $this;
}
public function truncateTable(string $table): static
{
$sql = $this->getQueryBuilder()->truncateTable($table);
return $this->setSql($sql);
}
public function update(string $table, array $columns, array|string $condition = '', array $params = []): static
{
$sql = $this->getQueryBuilder()->update($table, $columns, $condition, $params);
return $this->setSql($sql)->bindValues($params);
}
public function upsert(
string $table,
QueryInterface|array $insertColumns,
bool|array $updateColumns = true,
array $params = []
): static {
$sql = $this->getQueryBuilder()->upsert($table, $insertColumns, $updateColumns, $params);
return $this->setSql($sql)->bindValues($params);
}
/**
* @return QueryBuilderInterface The query builder instance.
*/
abstract protected function getQueryBuilder(): QueryBuilderInterface;
/**
* Returns the query result.
*
* @param int $queryMode Query mode, `QUERY_MODE_*`.
*
* @throws Exception
* @throws Throwable
*/
abstract protected function internalGetQueryResult(int $queryMode): mixed;
/**
* Executes a prepared statement.
*
* @throws Exception
* @throws Throwable
*/
abstract protected function internalExecute(): void;
/**
* Check if the value has a given flag.
*
* @param int $value Flags value to check.
* @param int $flag Flag to look for in the value.
*
* @return bool Whether the value has a given flag.
*/
protected function is(int $value, int $flag): bool
{
return ($value & $flag) === $flag;
}
/**
* The method is called after the query is executed.
*
* @param int $queryMode Query mode, `QUERY_MODE_*`.
*
* @throws Exception
* @throws Throwable
*/
protected function queryInternal(int $queryMode): mixed
{
$isReadMode = $this->isReadMode($queryMode);
$this->prepare($isReadMode);
$this->internalExecute();
/** @psalm-var mixed $result */
$result = $this->internalGetQueryResult($queryMode);
if (!$isReadMode) {
$this->refreshTableSchema();
}
return $result;
}
/**
* Refreshes table schema, which was marked by {@see requireTableSchemaRefresh()}.
*/
abstract protected function refreshTableSchema(): void;
/**
* Marks a specified table schema to be refreshed after command execution.
*
* @param string $name Name of the table, which schema should be refreshed.
*/
protected function requireTableSchemaRefresh(string $name): static
{
$this->refreshTableName = $name;
return $this;
}
/**
* Marks the command to execute in transaction.
*
* @param string|null $isolationLevel The isolation level to use for this transaction.
*
* {@see \Yiisoft\Db\Transaction\TransactionInterface::begin()} for details.
*/
protected function requireTransaction(string $isolationLevel = null): static
{
$this->isolationLevel = $isolationLevel;
return $this;
}
/**
* Resets the command object, so it can be reused to build another SQL statement.
*/
protected function reset(): void
{
$this->sql = '';
$this->params = [];
$this->refreshTableName = null;
$this->isolationLevel = null;
$this->retryHandler = null;
}
/**
* Checks if the query mode is a read mode.
*/
private function isReadMode(int $queryMode): bool
{
return !$this->is($queryMode, self::QUERY_MODE_EXECUTE);
}
}