1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Sqlite\PDO; |
6
|
|
|
|
7
|
|
|
use PDOException; |
8
|
|
|
use Throwable; |
9
|
|
|
use Yiisoft\Db\Command\CommandPDO; |
10
|
|
|
use Yiisoft\Db\Exception\ConvertException; |
11
|
|
|
use Yiisoft\Db\Exception\Exception; |
12
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
13
|
|
|
use Yiisoft\Db\Query\QueryBuilderInterface; |
|
|
|
|
14
|
|
|
use Yiisoft\Db\Sqlite\SqlToken; |
15
|
|
|
use Yiisoft\Db\Sqlite\SqlTokenizer; |
16
|
|
|
use Yiisoft\Strings\StringHelper; |
17
|
|
|
|
18
|
|
|
use function array_pop; |
19
|
|
|
use function count; |
20
|
|
|
use function ltrim; |
21
|
|
|
use function preg_match_all; |
22
|
|
|
use function strpos; |
23
|
|
|
|
24
|
|
|
final class CommandPDOSqlite extends CommandPDO |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
1 |
|
public function insertEx(string $table, array $columns): bool|array |
30
|
|
|
{ |
31
|
1 |
|
$params = []; |
32
|
1 |
|
$sql = $this->db->getQueryBuilder()->insertEx($table, $columns, $params); |
33
|
1 |
|
$this->setSql($sql)->bindValues($params); |
34
|
|
|
|
35
|
1 |
|
if (!$this->execute()) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
$tableSchema = $this->queryBuilder()->schema()->getTableSchema($table); |
40
|
1 |
|
$tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? []; |
41
|
|
|
|
42
|
1 |
|
$result = []; |
43
|
1 |
|
foreach ($tablePrimaryKeys as $name) { |
44
|
1 |
|
if ($tableSchema?->getColumn($name)?->isAutoIncrement()) { |
45
|
1 |
|
$result[$name] = $this->queryBuilder()->schema()->getLastInsertID((string) $tableSchema?->getSequenceName()); |
46
|
1 |
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** @var mixed */ |
50
|
|
|
$result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue(); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
177 |
|
public function queryBuilder(): QueryBuilderInterface |
57
|
|
|
{ |
58
|
177 |
|
return $this->db->getQueryBuilder(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Executes the SQL statement. |
63
|
|
|
* |
64
|
|
|
* This method should only be used for executing non-query SQL statement, such as `INSERT`, `DELETE`, `UPDATE` SQLs. |
65
|
|
|
* No result set will be returned. |
66
|
|
|
* |
67
|
|
|
* @throws Exception|Throwable execution failed. |
68
|
|
|
* |
69
|
|
|
* @return int number of rows affected by the execution. |
70
|
|
|
*/ |
71
|
46 |
|
public function execute(): int |
72
|
|
|
{ |
73
|
46 |
|
$sql = $this->getSql(); |
74
|
|
|
|
75
|
|
|
/** @var array<string, string> */ |
76
|
46 |
|
$params = $this->params; |
77
|
|
|
|
78
|
46 |
|
$statements = $this->splitStatements($sql, $params); |
79
|
|
|
|
80
|
46 |
|
if ($statements === false) { |
|
|
|
|
81
|
41 |
|
return parent::execute(); |
82
|
|
|
} |
83
|
|
|
|
84
|
5 |
|
$result = 0; |
85
|
|
|
|
86
|
|
|
/** @psalm-var array<array-key, array<array-key, string|array>> $statements */ |
87
|
5 |
|
foreach ($statements as $statement) { |
88
|
5 |
|
[$statementSql, $statementParams] = $statement; |
89
|
5 |
|
$statementSql = is_string($statementSql) ? $statementSql : ''; |
90
|
5 |
|
$statementParams = is_array($statementParams) ? $statementParams : []; |
91
|
5 |
|
$this->setSql($statementSql)->bindValues($statementParams); |
92
|
5 |
|
$result = parent::execute(); |
93
|
|
|
} |
94
|
|
|
|
95
|
5 |
|
$this->setSql($sql)->bindValues($params); |
96
|
|
|
|
97
|
5 |
|
return $result; |
98
|
|
|
} |
99
|
|
|
|
100
|
152 |
|
protected function internalExecute(?string $rawSql): void |
101
|
|
|
{ |
102
|
|
|
$attempt = 0; |
103
|
152 |
|
|
104
|
|
|
while (true) { |
105
|
152 |
|
try { |
106
|
152 |
|
if ( |
107
|
|
|
++$attempt === 1 |
108
|
|
|
&& $this->isolationLevel !== null |
109
|
|
|
&& $this->db->getTransaction() === null |
110
|
|
|
) { |
111
|
161 |
|
$this->db->transaction(fn (?string $rawSql) => $this->internalExecute($rawSql), $this->isolationLevel); |
112
|
|
|
} else { |
113
|
161 |
|
$this->pdoStatement?->execute(); |
114
|
|
|
} |
115
|
161 |
|
break; |
116
|
|
|
} catch (PDOException $e) { |
117
|
|
|
$rawSql = $rawSql ?: $this->getRawSql(); |
118
|
161 |
|
$e = (new ConvertException($e, $rawSql))->run(); |
119
|
161 |
|
|
120
|
161 |
|
if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) { |
121
|
|
|
throw $e; |
122
|
|
|
} |
123
|
|
|
} |
124
|
161 |
|
} |
125
|
|
|
} |
126
|
161 |
|
|
127
|
2 |
|
/** |
128
|
2 |
|
* Performs the actual DB query of a SQL statement. |
129
|
2 |
|
* |
130
|
|
|
* @param int $queryMode - return results as DataReader |
131
|
2 |
|
* |
132
|
2 |
|
* @throws Exception|Throwable if the query causes any problem. |
133
|
|
|
* |
134
|
|
|
* @return mixed the method execution result. |
135
|
|
|
*/ |
136
|
|
|
protected function queryInternal(int $queryMode): mixed |
137
|
|
|
{ |
138
|
|
|
$sql = $this->getSql(); |
139
|
|
|
|
140
|
|
|
/** @var array<string, string> */ |
141
|
|
|
$params = $this->params; |
142
|
|
|
|
143
|
|
|
$statements = $this->splitStatements($sql, $params); |
144
|
|
|
|
145
|
|
|
if ($statements === false || $statements === []) { |
|
|
|
|
146
|
|
|
return parent::queryInternal($queryMode); |
147
|
161 |
|
} |
148
|
|
|
|
149
|
161 |
|
[$lastStatementSql, $lastStatementParams] = array_pop($statements); |
150
|
|
|
|
151
|
|
|
/** |
152
|
161 |
|
* @psalm-var array<array-key, array> $statements |
153
|
|
|
*/ |
154
|
161 |
|
foreach ($statements as $statement) { |
155
|
|
|
/** |
156
|
161 |
|
* @var string $statementSql |
157
|
161 |
|
* @var array $statementParams |
158
|
|
|
*/ |
159
|
|
|
[$statementSql, $statementParams] = $statement; |
160
|
1 |
|
$this->setSql($statementSql)->bindValues($statementParams); |
161
|
|
|
parent::execute(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->setSql($lastStatementSql)->bindValues($lastStatementParams); |
165
|
1 |
|
|
166
|
|
|
/** @var string */ |
167
|
|
|
$result = parent::queryInternal($queryMode); |
168
|
|
|
|
169
|
|
|
$this->setSql($sql)->bindValues($params); |
170
|
1 |
|
|
171
|
1 |
|
return $result; |
172
|
1 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
1 |
|
* Splits the specified SQL codes into individual SQL statements and returns them or `false` if there's a single |
176
|
|
|
* statement. |
177
|
|
|
* |
178
|
1 |
|
* @param string $sql |
179
|
|
|
* @param array $params |
180
|
1 |
|
* |
181
|
|
|
* @throws InvalidArgumentException |
182
|
1 |
|
* |
183
|
|
|
* @return array|bool (array|string)[][]|bool |
184
|
|
|
* |
185
|
|
|
* @psalm-param array<string, string> $params |
186
|
|
|
* @psalm-return false|list<array{0: string, 1: array}> |
187
|
|
|
*/ |
188
|
|
|
private function splitStatements(string $sql, array $params): bool|array |
189
|
|
|
{ |
190
|
|
|
$semicolonIndex = strpos($sql, ';'); |
191
|
|
|
|
192
|
|
|
if ($semicolonIndex === false || $semicolonIndex === StringHelper::byteLength($sql) - 1) { |
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$tokenizer = new SqlTokenizer($sql); |
197
|
|
|
|
198
|
|
|
$codeToken = $tokenizer->tokenize(); |
199
|
161 |
|
|
200
|
|
|
if (count($codeToken->getChildren()) === 1) { |
201
|
161 |
|
return false; |
202
|
|
|
} |
203
|
161 |
|
|
204
|
161 |
|
$statements = []; |
205
|
|
|
|
206
|
|
|
foreach ($codeToken->getChildren() as $statement) { |
207
|
5 |
|
$statements[] = [$statement->getSql(), $this->extractUsedParams($statement, $params)]; |
208
|
|
|
} |
209
|
5 |
|
|
210
|
|
|
return $statements; |
211
|
5 |
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns named bindings used in the specified statement token. |
215
|
5 |
|
* |
216
|
|
|
* @param SqlToken $statement |
217
|
5 |
|
* @param array $params |
218
|
5 |
|
* |
219
|
|
|
* @return array |
220
|
|
|
* |
221
|
5 |
|
* @psalm-param array<string, string> $params |
222
|
|
|
*/ |
223
|
|
|
private function extractUsedParams(SqlToken $statement, array $params): array |
224
|
|
|
{ |
225
|
|
|
preg_match_all('/(?P<placeholder>[:][a-zA-Z0-9_]+)/', $statement->getSql(), $matches, PREG_SET_ORDER); |
226
|
|
|
|
227
|
|
|
$result = []; |
228
|
|
|
|
229
|
|
|
foreach ($matches as $match) { |
230
|
|
|
$phName = ltrim($match['placeholder'], ':'); |
231
|
|
|
if (isset($params[$phName])) { |
232
|
|
|
$result[$phName] = $params[$phName]; |
233
|
|
|
} elseif (isset($params[':' . $phName])) { |
234
|
5 |
|
$result[':' . $phName] = $params[':' . $phName]; |
235
|
|
|
} |
236
|
5 |
|
} |
237
|
|
|
|
238
|
5 |
|
return $result; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
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