Test Failed
Pull Request — dev (#104)
by Wilmer
03:26
created

DQLQueryBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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