Passed
Pull Request — master (#343)
by Sergei
08:05 queued 05:38
created

QueryBuilder::selectExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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