1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mysql; |
6
|
|
|
|
7
|
|
|
use PDOException; |
8
|
|
|
use Throwable; |
9
|
|
|
use Yiisoft\Db\Driver\PDO\AbstractCommandPDO; |
10
|
|
|
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface; |
11
|
|
|
use Yiisoft\Db\Exception\ConvertException; |
12
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Implements a database command that can be executed with a PDO (PHP Data Object) database connection for MySQL, |
16
|
|
|
* MariaDB. |
17
|
|
|
*/ |
18
|
|
|
final class CommandPDO extends AbstractCommandPDO |
19
|
|
|
{ |
20
|
6 |
|
public function insertWithReturningPks(string $table, array $columns): bool|array |
21
|
|
|
{ |
22
|
6 |
|
$params = []; |
23
|
6 |
|
$sql = $this->db->getQueryBuilder()->insert($table, $columns, $params); |
24
|
6 |
|
$this->setSql($sql)->bindValues($params); |
25
|
|
|
|
26
|
6 |
|
if (!$this->execute()) { |
27
|
|
|
return false; |
28
|
|
|
} |
29
|
|
|
|
30
|
6 |
|
$tableSchema = $this->db->getSchema()->getTableSchema($table); |
31
|
6 |
|
$tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? []; |
32
|
6 |
|
$result = []; |
33
|
|
|
|
34
|
6 |
|
foreach ($tablePrimaryKeys as $name) { |
35
|
6 |
|
if ($tableSchema?->getColumn($name)?->isAutoIncrement()) { |
36
|
5 |
|
$result[$name] = $this->db->getLastInsertID((string) $tableSchema?->getSequenceName()); |
37
|
5 |
|
continue; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** @psalm-var mixed */ |
41
|
1 |
|
$result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue(); |
42
|
|
|
} |
43
|
|
|
|
44
|
6 |
|
return $result; |
45
|
|
|
} |
46
|
|
|
|
47
|
265 |
|
protected function getQueryBuilder(): QueryBuilderInterface |
48
|
|
|
{ |
49
|
265 |
|
return $this->db->getQueryBuilder(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @psalm-suppress UnusedClosureParam |
54
|
|
|
* |
55
|
|
|
* @throws Throwable |
56
|
|
|
*/ |
57
|
243 |
|
protected function internalExecute(string|null $rawSql): void |
58
|
|
|
{ |
59
|
243 |
|
$attempt = 0; |
60
|
|
|
|
61
|
243 |
|
while (true) { |
62
|
|
|
try { |
63
|
|
|
if ( |
64
|
243 |
|
++$attempt === 1 |
65
|
243 |
|
&& $this->isolationLevel !== null |
66
|
243 |
|
&& $this->db->getTransaction() === null |
67
|
|
|
) { |
68
|
1 |
|
$this->db->transaction( |
69
|
1 |
|
fn (ConnectionPDOInterface $db) => $this->internalExecute($rawSql), |
|
|
|
|
70
|
1 |
|
$this->isolationLevel, |
71
|
1 |
|
); |
72
|
|
|
} else { |
73
|
243 |
|
$this->pdoStatement?->execute(); |
74
|
|
|
} |
75
|
243 |
|
break; |
76
|
42 |
|
} catch (PDOException $e) { |
77
|
42 |
|
$rawSql = $rawSql ?: $this->getRawSql(); |
78
|
42 |
|
$e = (new ConvertException($e, $rawSql))->run(); |
79
|
|
|
|
80
|
42 |
|
if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) { |
81
|
42 |
|
throw $e; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.