1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Sqlite\PDO; |
6
|
|
|
|
7
|
|
|
use Generator; |
8
|
|
|
use Yiisoft\Db\Command\CommandInterface; |
9
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
10
|
|
|
use Yiisoft\Db\Expression\Expression; |
11
|
|
|
use Yiisoft\Db\Expression\ExpressionBuilder; |
12
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
13
|
|
|
use Yiisoft\Db\Query\Conditions\InCondition; |
|
|
|
|
14
|
|
|
use Yiisoft\Db\Query\Conditions\LikeCondition; |
|
|
|
|
15
|
|
|
use Yiisoft\Db\Query\Query; |
16
|
|
|
use Yiisoft\Db\Query\QueryBuilder; |
|
|
|
|
17
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
18
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
19
|
|
|
use Yiisoft\Db\Schema\Schema; |
20
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
21
|
|
|
use Yiisoft\Db\Sqlite\Builder\InConditionBuilder; |
22
|
|
|
use Yiisoft\Db\Sqlite\Builder\LikeConditionBuilder; |
23
|
|
|
use Yiisoft\Db\Sqlite\DDLQueryBuilder; |
24
|
|
|
use Yiisoft\Db\Sqlite\DMLQueryBuilder; |
25
|
|
|
|
26
|
|
|
use function array_filter; |
27
|
|
|
use function array_merge; |
28
|
|
|
use function implode; |
29
|
|
|
use function trim; |
30
|
|
|
|
31
|
|
|
final class QueryBuilderPDOSqlite extends QueryBuilder |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var array mapping from abstract column types (keys) to physical column types (values). |
35
|
|
|
* |
36
|
|
|
* @psalm-var string[] $typeMap |
37
|
|
|
*/ |
38
|
|
|
protected array $typeMap = [ |
39
|
|
|
Schema::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', |
40
|
|
|
Schema::TYPE_UPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', |
41
|
|
|
Schema::TYPE_BIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', |
42
|
|
|
Schema::TYPE_UBIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', |
43
|
|
|
Schema::TYPE_CHAR => 'char(1)', |
44
|
|
|
Schema::TYPE_STRING => 'varchar(255)', |
45
|
|
|
Schema::TYPE_TEXT => 'text', |
46
|
|
|
Schema::TYPE_TINYINT => 'tinyint', |
47
|
|
|
Schema::TYPE_SMALLINT => 'smallint', |
48
|
|
|
Schema::TYPE_INTEGER => 'integer', |
49
|
|
|
Schema::TYPE_BIGINT => 'bigint', |
50
|
|
|
Schema::TYPE_FLOAT => 'float', |
51
|
|
|
Schema::TYPE_DOUBLE => 'double', |
52
|
|
|
Schema::TYPE_DECIMAL => 'decimal(10,0)', |
53
|
|
|
Schema::TYPE_DATETIME => 'datetime', |
54
|
|
|
Schema::TYPE_TIMESTAMP => 'timestamp', |
55
|
|
|
Schema::TYPE_TIME => 'time', |
56
|
|
|
Schema::TYPE_DATE => 'date', |
57
|
|
|
Schema::TYPE_BINARY => 'blob', |
58
|
|
|
Schema::TYPE_BOOLEAN => 'boolean', |
59
|
|
|
Schema::TYPE_MONEY => 'decimal(19,4)', |
60
|
|
|
]; |
61
|
|
|
|
62
|
323 |
|
public function __construct( |
63
|
|
|
private CommandInterface $command, |
64
|
|
|
private QuoterInterface $quoter, |
65
|
|
|
private SchemaInterface $schema |
66
|
|
|
) { |
67
|
323 |
|
$this->ddlBuilder = new DDLQueryBuilder($this); |
|
|
|
|
68
|
323 |
|
$this->dmlBuilder = new DMLQueryBuilder($this); |
|
|
|
|
69
|
323 |
|
parent::__construct($quoter, $schema); |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
public function addForeignKey( |
73
|
|
|
string $name, |
74
|
|
|
string $table, |
75
|
|
|
array|string $columns, |
76
|
|
|
string $refTable, |
77
|
|
|
array|string $refColumns, |
78
|
|
|
?string $delete = null, |
79
|
|
|
?string $update = null |
80
|
|
|
): string { |
81
|
3 |
|
return $this->ddlBuilder->addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @throws NotSupportedException |
86
|
|
|
*/ |
87
|
|
|
public function addPrimaryKey(string $name, string $table, array|string $columns): string |
88
|
|
|
{ |
89
|
|
|
return $this->ddlBuilder->addPrimaryKey($name, $table, $columns); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @throws NotSupportedException |
94
|
|
|
*/ |
95
|
1 |
|
public function addUnique(string $name, string $table, array|string $columns): string |
96
|
|
|
{ |
97
|
1 |
|
return $this->ddlBuilder->addUnique($name, $table, $columns); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @throws NotSupportedException |
102
|
|
|
*/ |
103
|
|
|
public function alterColumn(string $table, string $column, string $type): string |
104
|
|
|
{ |
105
|
|
|
return $this->ddlBuilder->alterColumn($table, $column, $type); |
106
|
|
|
} |
107
|
|
|
|
108
|
13 |
|
public function batchInsert(string $table, array $columns, iterable|Generator $rows, array &$params = []): string |
109
|
|
|
{ |
110
|
13 |
|
return $this->dmlBuilder->batchInsert($table, $columns, $rows, $params); |
111
|
|
|
} |
112
|
|
|
|
113
|
163 |
|
public function build(QueryInterface $query, array $params = []): array |
114
|
|
|
{ |
115
|
163 |
|
$query = $query->prepare($this); |
116
|
|
|
|
117
|
163 |
|
$params = empty($params) ? $query->getParams() : array_merge($params, $query->getParams()); |
118
|
|
|
|
119
|
163 |
|
$clauses = [ |
120
|
163 |
|
$this->buildSelect($query->getSelect(), $params, $query->getDistinct(), $query->getSelectOption()), |
121
|
163 |
|
$this->buildFrom($query->getFrom(), $params), |
122
|
163 |
|
$this->buildJoin($query->getJoin(), $params), |
123
|
163 |
|
$this->buildWhere($query->getWhere(), $params), |
124
|
163 |
|
$this->buildGroupBy($query->getGroupBy()), |
125
|
163 |
|
$this->buildHaving($query->getHaving(), $params), |
126
|
|
|
]; |
127
|
|
|
|
128
|
163 |
|
$sql = implode($this->separator, array_filter($clauses)); |
129
|
163 |
|
$sql = $this->buildOrderByAndLimit($sql, $query->getOrderBy(), $query->getLimit(), $query->getOffset()); |
130
|
|
|
|
131
|
163 |
|
if (!empty($query->getOrderBy())) { |
132
|
6 |
|
foreach ($query->getOrderBy() as $expression) { |
133
|
6 |
|
if ($expression instanceof ExpressionInterface) { |
134
|
1 |
|
$this->buildExpression($expression, $params); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
163 |
|
if (!empty($query->getGroupBy())) { |
140
|
2 |
|
foreach ($query->getGroupBy() as $expression) { |
141
|
2 |
|
if ($expression instanceof ExpressionInterface) { |
142
|
1 |
|
$this->buildExpression($expression, $params); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
163 |
|
$union = $this->buildUnion($query->getUnion(), $params); |
148
|
|
|
|
149
|
163 |
|
if ($union !== '') { |
150
|
3 |
|
$sql = "$sql$this->separator$union"; |
151
|
|
|
} |
152
|
|
|
|
153
|
163 |
|
$with = $this->buildWithQueries($query->getWithQueries(), $params); |
154
|
|
|
|
155
|
163 |
|
if ($with !== '') { |
156
|
2 |
|
$sql = "$with$this->separator$sql"; |
157
|
|
|
} |
158
|
|
|
|
159
|
163 |
|
return [$sql, $params]; |
160
|
|
|
} |
161
|
|
|
|
162
|
163 |
|
public function buildLimit(Expression|int|null $limit, Expression|int|null $offset): string |
163
|
|
|
{ |
164
|
163 |
|
$sql = ''; |
165
|
|
|
|
166
|
163 |
|
if ($this->hasLimit($limit)) { |
167
|
9 |
|
$sql = "LIMIT $limit"; |
168
|
9 |
|
if ($this->hasOffset($offset)) { |
169
|
9 |
|
$sql .= " OFFSET $offset"; |
170
|
|
|
} |
171
|
157 |
|
} elseif ($this->hasOffset($offset)) { |
172
|
|
|
/** |
173
|
|
|
* limit is not optional in SQLite. |
174
|
|
|
* |
175
|
|
|
* {@see http://www.sqlite.org/syntaxdiagrams.html#select-stmt} |
176
|
|
|
*/ |
177
|
|
|
$sql = "LIMIT 9223372036854775807 OFFSET $offset"; // 2^63-1 |
178
|
|
|
} |
179
|
|
|
|
180
|
163 |
|
return $sql; |
181
|
|
|
} |
182
|
|
|
|
183
|
163 |
|
public function buildUnion(array $unions, array &$params = []): string |
184
|
|
|
{ |
185
|
163 |
|
if (empty($unions)) { |
186
|
163 |
|
return ''; |
187
|
|
|
} |
188
|
|
|
|
189
|
3 |
|
$result = ''; |
190
|
|
|
|
191
|
3 |
|
foreach ($unions as $i => $union) { |
192
|
3 |
|
$query = $union['query']; |
193
|
3 |
|
if ($query instanceof Query) { |
194
|
3 |
|
[$unions[$i]['query'], $params] = $this->build($query, $params); |
195
|
|
|
} |
196
|
|
|
|
197
|
3 |
|
$result .= ' UNION ' . ($union['all'] ? 'ALL ' : '') . ' ' . $unions[$i]['query']; |
198
|
|
|
} |
199
|
|
|
|
200
|
3 |
|
return trim($result); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string |
204
|
|
|
{ |
205
|
|
|
return $this->ddlBuilder->checkIntegrity($schema, $table, $check); |
206
|
|
|
} |
207
|
|
|
|
208
|
7 |
|
public function createIndex(string $name, string $table, $columns, bool $unique = false): string |
209
|
|
|
{ |
210
|
7 |
|
return $this->ddlBuilder->createIndex($name, $table, $columns, $unique); |
211
|
|
|
} |
212
|
|
|
|
213
|
1 |
|
public function command(): CommandInterface |
214
|
|
|
{ |
215
|
1 |
|
return $this->command; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @throws NotSupportedException |
220
|
|
|
*/ |
221
|
|
|
public function dropColumn(string $table, string $column): string |
222
|
|
|
{ |
223
|
|
|
return $this->ddlBuilder->dropColumn($table, $column); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @throws NotSupportedException |
228
|
|
|
*/ |
229
|
2 |
|
public function dropForeignKey(string $name, string $table): string |
230
|
|
|
{ |
231
|
2 |
|
return $this->ddlBuilder->dropForeignKey($name, $table); |
232
|
|
|
} |
233
|
|
|
|
234
|
2 |
|
public function dropIndex(string $name, string $table): string |
235
|
|
|
{ |
236
|
2 |
|
return $this->ddlBuilder->dropIndex($name, $table); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @throws NotSupportedException |
241
|
|
|
*/ |
242
|
|
|
public function dropPrimaryKey(string $name, string $table): string |
243
|
|
|
{ |
244
|
|
|
return $this->ddlBuilder->dropPrimaryKey($name, $table); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @throws NotSupportedException |
249
|
|
|
*/ |
250
|
1 |
|
public function dropUnique(string $name, string $table): string |
251
|
|
|
{ |
252
|
1 |
|
return $this->ddlBuilder->dropUnique($name, $table); |
253
|
|
|
} |
254
|
|
|
|
255
|
256 |
|
public function quoter(): QuoterInterface |
256
|
|
|
{ |
257
|
256 |
|
return $this->quoter; |
258
|
|
|
} |
259
|
|
|
|
260
|
68 |
|
public function schema(): SchemaInterface |
261
|
|
|
{ |
262
|
68 |
|
return $this->schema; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @throws NotSupportedException |
267
|
|
|
*/ |
268
|
|
|
public function renameColumn(string $table, string $oldName, string $newName): string |
269
|
|
|
{ |
270
|
|
|
return $this->ddlBuilder->renameColumn($table, $oldName, $newName); |
271
|
|
|
} |
272
|
|
|
|
273
|
3 |
|
public function renameTable(string $oldName, string $newName): string |
274
|
|
|
{ |
275
|
3 |
|
return $this->ddlBuilder->renameTable($oldName, $newName); |
276
|
|
|
} |
277
|
|
|
|
278
|
1 |
|
public function truncateTable(string $table): string |
279
|
|
|
{ |
280
|
1 |
|
return $this->ddlBuilder->truncateTable($table); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Contains array of default expression builders. Extend this method and override it, if you want to change default |
285
|
|
|
* expression builders for this query builder. |
286
|
|
|
* |
287
|
|
|
* @return array |
288
|
|
|
* |
289
|
|
|
* See {@see ExpressionBuilder} docs for details. |
290
|
|
|
*/ |
291
|
323 |
|
protected function defaultExpressionBuilders(): array |
292
|
|
|
{ |
293
|
323 |
|
return array_merge(parent::defaultExpressionBuilders(), [ |
294
|
|
|
LikeCondition::class => LikeConditionBuilder::class, |
295
|
|
|
InCondition::class => InConditionBuilder::class, |
296
|
|
|
]); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
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