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
|
342 |
|
public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache) |
19
|
|
|
{ |
20
|
342 |
|
parent::__construct($queryCache); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @inheritDoc |
25
|
|
|
*/ |
26
|
|
|
public function insertEx(string $table, array $columns): bool|array |
27
|
|
|
{ |
28
|
|
|
$params = []; |
29
|
|
|
$sql = $this->db->getQueryBuilder()->insertEx($table, $columns, $params); |
30
|
|
|
$this->setSql($sql)->bindValues($params); |
31
|
|
|
|
32
|
|
|
if (!$this->execute()) { |
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$tableSchema = $this->queryBuilder()->schema()->getTableSchema($table); |
37
|
|
|
$tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? []; |
38
|
|
|
|
39
|
|
|
$result = []; |
40
|
|
|
foreach ($tablePrimaryKeys as $name) { |
41
|
|
|
if ($tableSchema?->getColumn($name)?->isAutoIncrement()) { |
42
|
|
|
$result[$name] = $this->queryBuilder()->schema()->getLastInsertID((string) $tableSchema?->getSequenceName()); |
43
|
|
|
continue; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** @var mixed */ |
47
|
|
|
$result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $result; |
51
|
|
|
} |
52
|
|
|
|
53
|
171 |
|
public function queryBuilder(): QueryBuilderInterface |
54
|
|
|
{ |
55
|
171 |
|
return $this->db->getQueryBuilder(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @throws Exception|InvalidConfigException|PDOException |
60
|
|
|
*/ |
61
|
162 |
|
public function prepare(?bool $forRead = null): void |
62
|
|
|
{ |
63
|
162 |
|
if (isset($this->pdoStatement)) { |
64
|
5 |
|
$this->bindPendingParams(); |
65
|
|
|
|
66
|
5 |
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
162 |
|
$sql = $this->getSql() ?? ''; |
70
|
|
|
|
71
|
162 |
|
if ($this->db->getTransaction()) { |
72
|
|
|
/** master is in a transaction. use the same connection. */ |
73
|
5 |
|
$forRead = false; |
74
|
|
|
} |
75
|
|
|
|
76
|
162 |
|
if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) { |
77
|
156 |
|
$pdo = $this->db->getSlavePdo(); |
78
|
|
|
} else { |
79
|
59 |
|
$pdo = $this->db->getMasterPdo(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
try { |
83
|
162 |
|
$this->pdoStatement = $pdo?->prepare($sql); |
84
|
162 |
|
$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, ?int $fetchMode, string $rawSql): array |
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
__CLASS__, |
98
|
|
|
$method, |
99
|
|
|
$fetchMode, |
100
|
2 |
|
$this->db->getDriver()->getDsn(), |
101
|
2 |
|
$this->db->getDriver()->getUsername(), |
102
|
|
|
$rawSql, |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
161 |
|
protected function internalExecute(?string $rawSql): void |
107
|
|
|
{ |
108
|
161 |
|
$attempt = 0; |
109
|
|
|
|
110
|
161 |
|
while (true) { |
111
|
|
|
try { |
112
|
|
|
if ( |
113
|
161 |
|
++$attempt === 1 |
114
|
161 |
|
&& $this->isolationLevel !== null |
115
|
161 |
|
&& $this->db->getTransaction() === null |
116
|
|
|
) { |
117
|
|
|
$this->db->transaction(fn (?string $rawSql) => $this->internalExecute($rawSql), $this->isolationLevel); |
118
|
|
|
} else { |
119
|
161 |
|
$this->pdoStatement?->execute(); |
120
|
|
|
} |
121
|
158 |
|
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