|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mysql\PDO; |
|
6
|
|
|
|
|
7
|
|
|
use PDOException; |
|
8
|
|
|
use Yiisoft\Db\Cache\QueryCache; |
|
9
|
|
|
use Yiisoft\Db\Command\Command; |
|
10
|
|
|
use Yiisoft\Db\Connection\ConnectionPDOInterface; |
|
11
|
|
|
use Yiisoft\Db\Exception\ConvertException; |
|
12
|
|
|
use Yiisoft\Db\Exception\Exception; |
|
13
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
|
14
|
|
|
use Yiisoft\Db\Query\QueryBuilderInterface; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
final class CommandPDOMysql extends Command |
|
17
|
|
|
{ |
|
18
|
343 |
|
public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache) |
|
19
|
|
|
{ |
|
20
|
343 |
|
parent::__construct($queryCache); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @inheritDoc |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function insertEx(string $table, array $columns): bool|array |
|
27
|
|
|
{ |
|
28
|
1 |
|
$params = []; |
|
29
|
1 |
|
$sql = $this->db->getQueryBuilder()->insertEx($table, $columns, $params); |
|
30
|
1 |
|
$this->setSql($sql)->bindValues($params); |
|
31
|
|
|
|
|
32
|
1 |
|
if (!$this->execute()) { |
|
33
|
|
|
return false; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
$tableSchema = $this->queryBuilder()->schema()->getTableSchema($table); |
|
37
|
1 |
|
$tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? []; |
|
38
|
|
|
|
|
39
|
1 |
|
$result = []; |
|
40
|
1 |
|
foreach ($tablePrimaryKeys as $name) { |
|
41
|
1 |
|
if ($tableSchema?->getColumn($name)?->isAutoIncrement()) { |
|
42
|
1 |
|
$result[$name] = $this->queryBuilder()->schema()->getLastInsertID((string) $tableSchema?->getSequenceName()); |
|
43
|
1 |
|
continue; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** @var mixed */ |
|
47
|
|
|
$result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
return $result; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
172 |
|
public function queryBuilder(): QueryBuilderInterface |
|
54
|
|
|
{ |
|
55
|
172 |
|
return $this->db->getQueryBuilder(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @throws Exception|InvalidConfigException|PDOException |
|
60
|
|
|
*/ |
|
61
|
163 |
|
public function prepare(?bool $forRead = null): void |
|
62
|
|
|
{ |
|
63
|
163 |
|
if (isset($this->pdoStatement)) { |
|
64
|
5 |
|
$this->bindPendingParams(); |
|
65
|
|
|
|
|
66
|
5 |
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
163 |
|
$sql = $this->getSql(); |
|
70
|
|
|
|
|
71
|
163 |
|
if ($this->db->getTransaction()) { |
|
72
|
|
|
/** master is in a transaction. use the same connection. */ |
|
73
|
5 |
|
$forRead = false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
163 |
|
if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) { |
|
77
|
157 |
|
$pdo = $this->db->getSlavePdo(); |
|
78
|
|
|
} else { |
|
79
|
60 |
|
$pdo = $this->db->getMasterPdo(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
163 |
|
$this->pdoStatement = $pdo?->prepare($sql); |
|
84
|
163 |
|
$this->bindPendingParams(); |
|
85
|
|
|
} catch (PDOException $e) { |
|
86
|
|
|
$message = $e->getMessage() . "\nFailed to prepare SQL: $sql"; |
|
87
|
|
|
/** @var array|null */ |
|
88
|
|
|
$errorInfo = $e->errorInfo ?? null; |
|
89
|
|
|
|
|
90
|
|
|
throw new Exception($message, $errorInfo, $e); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
protected function getCacheKey(string $method, array|int|null $fetchMode, string $rawSql): array |
|
95
|
|
|
{ |
|
96
|
|
|
return [ |
|
97
|
2 |
|
__CLASS__, |
|
98
|
|
|
$method, |
|
99
|
|
|
$fetchMode, |
|
100
|
2 |
|
$this->db->getDriver()->getDsn(), |
|
101
|
2 |
|
$this->db->getDriver()->getUsername(), |
|
102
|
|
|
$rawSql, |
|
103
|
|
|
]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
162 |
|
protected function internalExecute(?string $rawSql): void |
|
107
|
|
|
{ |
|
108
|
162 |
|
$attempt = 0; |
|
109
|
|
|
|
|
110
|
162 |
|
while (true) { |
|
111
|
|
|
try { |
|
112
|
|
|
if ( |
|
113
|
162 |
|
++$attempt === 1 |
|
114
|
162 |
|
&& $this->isolationLevel !== null |
|
115
|
162 |
|
&& $this->db->getTransaction() === null |
|
116
|
|
|
) { |
|
117
|
|
|
$this->db->transaction(fn (?string $rawSql) => $this->internalExecute($rawSql), $this->isolationLevel); |
|
118
|
|
|
} else { |
|
119
|
162 |
|
$this->pdoStatement?->execute(); |
|
120
|
|
|
} |
|
121
|
159 |
|
break; |
|
122
|
23 |
|
} catch (PDOException $e) { |
|
123
|
23 |
|
$rawSql = $rawSql ?: $this->getRawSql(); |
|
124
|
23 |
|
$e = (new ConvertException($e, $rawSql))->run(); |
|
125
|
|
|
|
|
126
|
23 |
|
if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) { |
|
127
|
23 |
|
throw $e; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths