Test Failed
Pull Request — master (#230)
by Wilmer
17:47 queued 14:10
created

DQLQueryBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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