1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\QueryBuilder; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Command\CommandInterface; |
8
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
9
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
10
|
|
|
use Yiisoft\Db\QueryBuilder\Condition\Interface\ConditionInterface; |
11
|
|
|
use Yiisoft\Db\Schema\Column\ColumnInterface; |
12
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
13
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
14
|
|
|
|
15
|
|
|
use function count; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Builds a SELECT SQL statement based on the specification given as a {@see QueryInterface} object. |
19
|
|
|
* |
20
|
|
|
* SQL statements are created from {@see QueryInterface} objects using the |
21
|
|
|
* {@see AbstractDQLQueryBuilder::build()}-method. |
22
|
|
|
* |
23
|
|
|
* AbstractQueryBuilder is also used by {@see CommandInterface} to build SQL statements such as {@see insert()}, |
24
|
|
|
* {@see update()}, {@see delete()} and {@see createTable()}. |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractQueryBuilder implements QueryBuilderInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* The prefix for automatically generated query binding parameters. |
30
|
|
|
*/ |
31
|
|
|
public const PARAM_PREFIX = ':qp'; |
32
|
|
|
/** |
33
|
|
|
* @psalm-var string[] The abstract column types mapped to physical column types. |
34
|
|
|
* |
35
|
|
|
* This is mainly used to support creating/modifying tables using DB-independent data type specifications. Child |
36
|
|
|
* classes should override this property to declare supported type mappings. |
37
|
|
|
*/ |
38
|
|
|
protected array $typeMap = []; |
39
|
|
|
|
40
|
|
|
public function __construct( |
41
|
|
|
private QuoterInterface $quoter, |
42
|
|
|
private SchemaInterface $schema, |
43
|
|
|
private AbstractDDLQueryBuilder $ddlBuilder, |
44
|
|
|
private AbstractDMLQueryBuilder $dmlBuilder, |
45
|
|
|
private AbstractDQLQueryBuilder $dqlBuilder, |
46
|
|
|
private ColumnDefinitionBuilder $columnDefinitionBuilder, |
47
|
|
|
) { |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function addCheck(string $table, string $name, string $expression): string |
51
|
|
|
{ |
52
|
|
|
return $this->ddlBuilder->addCheck($table, $name, $expression); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function addColumn(string $table, string $column, ColumnInterface|string $type): string |
56
|
|
|
{ |
57
|
|
|
return $this->ddlBuilder->addColumn($table, $column, $type); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function addCommentOnColumn(string $table, string $column, string $comment): string |
61
|
|
|
{ |
62
|
|
|
return $this->ddlBuilder->addCommentOnColumn($table, $column, $comment); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function addCommentOnTable(string $table, string $comment): string |
66
|
|
|
{ |
67
|
|
|
return $this->ddlBuilder->addCommentOnTable($table, $comment); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function addDefaultValue(string $table, string $name, string $column, mixed $value): string |
71
|
|
|
{ |
72
|
|
|
return $this->ddlBuilder->addDefaultValue($table, $name, $column, $value); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function addForeignKey( |
76
|
|
|
string $table, |
77
|
|
|
string $name, |
78
|
|
|
array|string $columns, |
79
|
|
|
string $referenceTable, |
80
|
|
|
array|string $referenceColumns, |
81
|
|
|
string $delete = null, |
82
|
|
|
string $update = null |
83
|
|
|
): string { |
84
|
|
|
return $this->ddlBuilder->addForeignKey( |
85
|
|
|
$table, |
86
|
|
|
$name, |
87
|
|
|
$columns, |
88
|
|
|
$referenceTable, |
89
|
|
|
$referenceColumns, |
90
|
|
|
$delete, |
91
|
|
|
$update, |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function addPrimaryKey(string $table, string $name, array|string $columns): string |
96
|
|
|
{ |
97
|
|
|
return $this->ddlBuilder->addPrimaryKey($table, $name, $columns); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function addUnique(string $table, string $name, array|string $columns): string |
101
|
|
|
{ |
102
|
|
|
return $this->ddlBuilder->addUnique($table, $name, $columns); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function alterColumn(string $table, string $column, ColumnInterface|string $type): string |
106
|
|
|
{ |
107
|
|
|
return $this->ddlBuilder->alterColumn($table, $column, $type); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function batchInsert(string $table, array $columns, iterable $rows, array &$params = []): string |
111
|
|
|
{ |
112
|
|
|
return $this->dmlBuilder->batchInsert($table, $columns, $rows, $params); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function bindParam(mixed $value, array &$params = []): string |
116
|
|
|
{ |
117
|
|
|
$phName = self::PARAM_PREFIX . count($params); |
118
|
|
|
|
119
|
|
|
$additionalCount = 0; |
120
|
|
|
while (isset($params[$phName])) { |
121
|
|
|
$phName = self::PARAM_PREFIX . count($params) . '_' . $additionalCount; |
122
|
|
|
++$additionalCount; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** @psalm-var mixed */ |
126
|
|
|
$params[$phName] = $value; |
127
|
|
|
|
128
|
|
|
return $phName; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function build(QueryInterface $query, array $params = []): array |
132
|
|
|
{ |
133
|
|
|
return $this->dqlBuilder->build($query, $params); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function buildColumns(array|string $columns): string |
137
|
|
|
{ |
138
|
|
|
return $this->dqlBuilder->buildColumns($columns); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function buildCondition(array|string|ExpressionInterface|null $condition, array &$params = []): string |
142
|
|
|
{ |
143
|
|
|
return $this->dqlBuilder->buildCondition($condition, $params); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function buildExpression(ExpressionInterface $expression, array &$params = []): string |
147
|
|
|
{ |
148
|
|
|
return $this->dqlBuilder->buildExpression($expression, $params); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function buildFrom(array|null $tables, array &$params): string |
152
|
|
|
{ |
153
|
|
|
return $this->dqlBuilder->buildFrom($tables, $params); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function buildGroupBy(array $columns, array &$params = []): string |
157
|
|
|
{ |
158
|
|
|
return $this->dqlBuilder->buildGroupBy($columns, $params); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function buildHaving(array|ExpressionInterface|string|null $condition, array &$params = []): string |
162
|
|
|
{ |
163
|
|
|
return $this->dqlBuilder->buildHaving($condition, $params); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function buildJoin(array $joins, array &$params): string |
167
|
|
|
{ |
168
|
|
|
return $this->dqlBuilder->buildJoin($joins, $params); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function buildLimit(ExpressionInterface|int|null $limit, ExpressionInterface|int|null $offset): string |
172
|
|
|
{ |
173
|
|
|
return $this->dqlBuilder->buildLimit($limit, $offset); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function buildOrderBy(array $columns, array &$params = []): string |
177
|
|
|
{ |
178
|
|
|
return $this->dqlBuilder->buildOrderBy($columns, $params); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function buildOrderByAndLimit( |
182
|
|
|
string $sql, |
183
|
|
|
array $orderBy, |
184
|
|
|
ExpressionInterface|int|null $limit, |
185
|
|
|
ExpressionInterface|int|null $offset, |
186
|
|
|
array &$params = [] |
187
|
|
|
): string { |
188
|
|
|
return $this->dqlBuilder->buildOrderByAndLimit($sql, $orderBy, $limit, $offset, $params); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function buildSelect( |
192
|
|
|
array $columns, |
193
|
|
|
array &$params, |
194
|
|
|
bool|null $distinct = false, |
195
|
|
|
string $selectOption = null |
196
|
|
|
): string { |
197
|
|
|
return $this->dqlBuilder->buildSelect($columns, $params, $distinct, $selectOption); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function buildUnion(array $unions, array &$params): string |
201
|
|
|
{ |
202
|
|
|
return $this->dqlBuilder->buildUnion($unions, $params); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function buildWhere( |
206
|
|
|
array|string|ConditionInterface|ExpressionInterface|null $condition, |
207
|
|
|
array &$params = [] |
208
|
|
|
): string { |
209
|
|
|
return $this->dqlBuilder->buildWhere($condition, $params); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function buildWithQueries(array $withs, array &$params): string |
213
|
|
|
{ |
214
|
|
|
return $this->dqlBuilder->buildWithQueries($withs, $params); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string |
218
|
|
|
{ |
219
|
|
|
return $this->ddlBuilder->checkIntegrity($schema, $table, $check); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function createConditionFromArray(array $condition): ConditionInterface |
223
|
|
|
{ |
224
|
|
|
return $this->dqlBuilder->createConditionFromArray($condition); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function createIndex( |
228
|
|
|
string $table, |
229
|
|
|
string $name, |
230
|
|
|
array|string $columns, |
231
|
|
|
string $indexType = null, |
232
|
|
|
string $indexMethod = null |
233
|
|
|
): string { |
234
|
|
|
return $this->ddlBuilder->createIndex($table, $name, $columns, $indexType, $indexMethod); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function createTable(string $table, array $columns, string $options = null): string |
238
|
|
|
{ |
239
|
|
|
return $this->ddlBuilder->createTable($table, $columns, $options); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function createView(string $viewName, QueryInterface|string $subQuery): string |
243
|
|
|
{ |
244
|
|
|
return $this->ddlBuilder->createView($viewName, $subQuery); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function delete(string $table, array|string $condition, array &$params): string |
248
|
|
|
{ |
249
|
|
|
return $this->dmlBuilder->delete($table, $condition, $params); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function dropCheck(string $table, string $name): string |
253
|
|
|
{ |
254
|
|
|
return $this->ddlBuilder->dropCheck($table, $name); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function dropColumn(string $table, string $column): string |
258
|
|
|
{ |
259
|
|
|
return $this->ddlBuilder->dropColumn($table, $column); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function dropCommentFromColumn(string $table, string $column): string |
263
|
|
|
{ |
264
|
|
|
return $this->ddlBuilder->dropCommentFromColumn($table, $column); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function dropCommentFromTable(string $table): string |
268
|
|
|
{ |
269
|
|
|
return $this->ddlBuilder->dropCommentFromTable($table); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function dropDefaultValue(string $table, string $name): string |
273
|
|
|
{ |
274
|
|
|
return $this->ddlBuilder->dropDefaultValue($table, $name); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function dropForeignKey(string $table, string $name): string |
278
|
|
|
{ |
279
|
|
|
return $this->ddlBuilder->dropForeignKey($table, $name); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function dropIndex(string $table, string $name): string |
283
|
|
|
{ |
284
|
|
|
return $this->ddlBuilder->dropIndex($table, $name); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function dropPrimaryKey(string $table, string $name): string |
288
|
|
|
{ |
289
|
|
|
return $this->ddlBuilder->dropPrimaryKey($table, $name); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function dropTable(string $table): string |
293
|
|
|
{ |
294
|
|
|
return $this->ddlBuilder->dropTable($table); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function dropUnique(string $table, string $name): string |
298
|
|
|
{ |
299
|
|
|
return $this->ddlBuilder->dropUnique($table, $name); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function dropView(string $viewName): string |
303
|
|
|
{ |
304
|
|
|
return $this->ddlBuilder->dropView($viewName); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** @deprecated Use {@see buildColumnDefinition()}. Will be removed in version 3.0.0. */ |
308
|
|
|
public function getColumnType(ColumnInterface|string $type): string |
309
|
|
|
{ |
310
|
|
|
return $this->buildColumnDefinition($type); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
public function getExpressionBuilder(ExpressionInterface $expression): object |
314
|
|
|
{ |
315
|
|
|
return $this->dqlBuilder->getExpressionBuilder($expression); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
public function insert(string $table, QueryInterface|array $columns, array &$params = []): string |
319
|
|
|
{ |
320
|
|
|
return $this->dmlBuilder->insert($table, $columns, $params); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string |
324
|
|
|
{ |
325
|
|
|
return $this->dmlBuilder->insertWithReturningPks($table, $columns, $params); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function quoter(): QuoterInterface |
329
|
|
|
{ |
330
|
|
|
return $this->quoter; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function renameColumn(string $table, string $oldName, string $newName): string |
334
|
|
|
{ |
335
|
|
|
return $this->ddlBuilder->renameColumn($table, $oldName, $newName); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
public function renameTable(string $oldName, string $newName): string |
339
|
|
|
{ |
340
|
|
|
return $this->ddlBuilder->renameTable($oldName, $newName); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function resetSequence(string $table, int|string|null $value = null): string |
344
|
|
|
{ |
345
|
|
|
return $this->dmlBuilder->resetSequence($table, $value); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
public function selectExists(string $rawSql): string |
349
|
|
|
{ |
350
|
|
|
return $this->dqlBuilder->selectExists($rawSql); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function setConditionClasses(array $classes): void |
354
|
|
|
{ |
355
|
|
|
$this->dqlBuilder->setConditionClasses($classes); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
public function setExpressionBuilders(array $builders): void |
359
|
|
|
{ |
360
|
|
|
$this->dqlBuilder->setExpressionBuilders($builders); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function setSeparator(string $separator): void |
364
|
|
|
{ |
365
|
|
|
$this->dqlBuilder->setSeparator($separator); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
public function truncateTable(string $table): string |
369
|
|
|
{ |
370
|
|
|
return $this->ddlBuilder->truncateTable($table); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
public function update(string $table, array $columns, array|string $condition, array &$params = []): string |
374
|
|
|
{ |
375
|
|
|
return $this->dmlBuilder->update($table, $columns, $condition, $params); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
public function upsert( |
379
|
|
|
string $table, |
380
|
|
|
QueryInterface|array $insertColumns, |
381
|
|
|
bool|array $updateColumns, |
382
|
|
|
array &$params = [] |
383
|
|
|
): string { |
384
|
|
|
return $this->dmlBuilder->upsert($table, $insertColumns, $updateColumns, $params); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
public function buildColumnDefinition(ColumnInterface|string $column): string |
388
|
|
|
{ |
389
|
|
|
if (!$column instanceof ColumnInterface) { |
390
|
|
|
$column = $this->schema->getColumnFactory()->fromDefinition($column); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
return $this->columnDefinitionBuilder->build($column); |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|