Passed
Push — dev ( bae50f...820ac9 )
by Def
33:33 queued 29:55
created

DQLQueryBuilder::buildUnion()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.6111
cc 5
nc 6
nop 2
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite;
6
7
use Yiisoft\Db\Expression\Expression;
8
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
9
use Yiisoft\Db\Expression\ExpressionInterface;
10
use Yiisoft\Db\Query\Conditions\InCondition;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\Conditions\InCondition was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Yiisoft\Db\Query\Conditions\LikeCondition;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\Conditions\LikeCondition was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Yiisoft\Db\Query\DQLQueryBuilder as AbstractDQLQueryBuilder;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\DQLQueryBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yiisoft\Db\Query\Query;
14
use Yiisoft\Db\Query\QueryInterface;
15
use Yiisoft\Db\Sqlite\Builder\InConditionBuilder;
16
use Yiisoft\Db\Sqlite\Builder\LikeConditionBuilder;
17
18
use function array_filter;
19
use function array_merge;
20
use function implode;
21
use function trim;
22
23
final class DQLQueryBuilder extends AbstractDQLQueryBuilder
24
{
25 163
    public function build(QueryInterface $query, array $params = []): array
26
    {
27 163
        $query = $query->prepare($this->queryBuilder);
28
29 163
        $params = empty($params) ? $query->getParams() : array_merge($params, $query->getParams());
30
31 163
        $clauses = [
32 163
            $this->buildSelect($query->getSelect(), $params, $query->getDistinct(), $query->getSelectOption()),
33 163
            $this->buildFrom($query->getFrom(), $params),
34 163
            $this->buildJoin($query->getJoin(), $params),
35 163
            $this->buildWhere($query->getWhere(), $params),
36 163
            $this->buildGroupBy($query->getGroupBy()),
37 163
            $this->buildHaving($query->getHaving(), $params),
38
        ];
39
40 163
        $orderBy = $query->getOrderBy();
41 163
        $sql = implode($this->separator, array_filter($clauses));
42 163
        $sql = $this->buildOrderByAndLimit($sql, $orderBy, $query->getLimit(), $query->getOffset());
43
44 163
        if (!empty($orderBy)) {
45
            /** @psalm-var array<string|ExpressionInterface> $orderBy */
46 6
            foreach ($orderBy as $expression) {
47 6
                if ($expression instanceof ExpressionInterface) {
48 1
                    $this->buildExpression($expression, $params);
49
                }
50
            }
51
        }
52
53 163
        $groupBy = $query->getGroupBy();
54
55 163
        if (!empty($groupBy)) {
56
            /** @psalm-var array<string|ExpressionInterface> $groupBy */
57 2
            foreach ($groupBy as $expression) {
58 2
                if ($expression instanceof ExpressionInterface) {
59 1
                    $this->buildExpression($expression, $params);
60
                }
61
            }
62
        }
63
64 163
        $union = $this->buildUnion($query->getUnion(), $params);
65
66 163
        if ($union !== '') {
67 3
            $sql = "$sql$this->separator$union";
68
        }
69
70 163
        $with = $this->buildWithQueries($query->getWithQueries(), $params);
71
72 163
        if ($with !== '') {
73 2
            $sql = "$with$this->separator$sql";
74
        }
75
76 163
        return [$sql, $params];
77
    }
78
79 163
    public function buildLimit(Expression|int|null $limit, Expression|int|null $offset): string
80
    {
81 163
        $sql = '';
82
83 163
        if ($this->hasLimit($limit)) {
84 9
            $sql = "LIMIT $limit";
85 9
            if ($this->hasOffset($offset)) {
86 9
                $sql .= " OFFSET $offset";
87
            }
88 157
        } elseif ($this->hasOffset($offset)) {
89
            /**
90
             * limit is not optional in SQLite.
91
             *
92
             * {@see http://www.sqlite.org/syntaxdiagrams.html#select-stmt}
93
             */
94
            $sql = "LIMIT 9223372036854775807 OFFSET $offset"; // 2^63-1
95
        }
96
97 163
        return $sql;
98
    }
99
100 163
    public function buildUnion(array $unions, array &$params = []): string
101
    {
102 163
        if (empty($unions)) {
103 163
            return '';
104
        }
105
106 3
        $result = '';
107
108
        /** @psalm-var array<array-key, array{query: Query|null, all: bool}> $unions */
109 3
        foreach ($unions as $i => $union) {
110 3
            $query = $union['query'];
111 3
            if ($query instanceof Query) {
112 3
                [$unions[$i]['query'], $params] = $this->build($query, $params);
113
            }
114
115 3
            $result .= ' UNION ' . ($union['all'] ? 'ALL ' : '') . ' ' . (string) $unions[$i]['query'];
116
        }
117
118 3
        return trim($result);
119
    }
120
121
    /**
122
     * Contains array of default expression builders. Extend this method and override it, if you want to change default
123
     * expression builders for this query builder.
124
     *
125
     * @return array
126
     *
127
     * See {@see ExpressionBuilder} docs for details.
128
     *
129
     * @psalm-return array<string, class-string<ExpressionBuilderInterface>>
130
     */
131 336
    protected function defaultExpressionBuilders(): array
132
    {
133 336
        return array_merge(parent::defaultExpressionBuilders(), [
134
            LikeCondition::class => LikeConditionBuilder::class,
135
            InCondition::class => InConditionBuilder::class,
136
        ]);
137
    }
138
}
139