Passed
Push — master ( 805e63...8e0536 )
by Sergei
14:45 queued 08:17
created

DQLQueryBuilder::buildFrom()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle;
6
7
use Yiisoft\Db\Expression\ExpressionInterface;
8
use Yiisoft\Db\Oracle\Builder\InConditionBuilder;
9
use Yiisoft\Db\Oracle\Builder\LikeConditionBuilder;
10
use Yiisoft\Db\Query\Query;
11
use Yiisoft\Db\QueryBuilder\AbstractDQLQueryBuilder;
12
use Yiisoft\Db\QueryBuilder\Condition\InCondition;
13
use Yiisoft\Db\QueryBuilder\Condition\LikeCondition;
14
15
use function array_merge;
16
use function implode;
17
18
/**
19
 * Implements a DQL (Data Query Language) SQL statements for Oracle Server.
20
 */
21
final class DQLQueryBuilder extends AbstractDQLQueryBuilder
22
{
23 237
    public function buildOrderByAndLimit(
24
        string $sql,
25
        array $orderBy,
26
        ExpressionInterface|int|null $limit,
27
        ExpressionInterface|int|null $offset,
28
        array &$params = []
29
    ): string {
30 237
        $orderByString = $this->buildOrderBy($orderBy, $params);
31
32 237
        if ($orderByString !== '') {
33 6
            $sql .= $this->separator . $orderByString;
34
        }
35
36 237
        $filters = [];
37
38 237
        if ($this->hasOffset($offset)) {
39 2
            $filters[] = 'rowNumId > ' .
40 2
                ($offset instanceof ExpressionInterface ? $this->buildExpression($offset) : (string)$offset);
41
        }
42
43 237
        if ($this->hasLimit($limit)) {
44 14
            $filters[] = 'rownum <= ' .
45 14
                ($limit instanceof ExpressionInterface ? $this->buildExpression($limit) : (string)$limit);
46
        }
47
48 237
        if (empty($filters)) {
49 228
            return $sql;
50
        }
51
52 15
        $filter = implode(' AND ', $filters);
53 15
        return <<<SQL
54 15
        WITH USER_SQL AS ($sql), PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL)
55 15
        SELECT * FROM PAGINATION WHERE $filter
56 15
        SQL;
57
    }
58
59 1
    public function selectExists(string $rawSql): string
60
    {
61 1
        return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END FROM DUAL';
62
    }
63
64 246
    public function buildFrom(array|null $tables, array &$params): string
65
    {
66 246
        if (empty($tables)) {
67 141
            return 'FROM DUAL';
68
        }
69
70 118
        return parent::buildFrom($tables, $params);
71
    }
72
73 236
    public function buildWithQueries(array $withs, array &$params): string
74
    {
75
        /** @psalm-var array{query:string|Query, alias:ExpressionInterface|string, recursive:bool}[] $withs */
76 236
        foreach ($withs as &$with) {
77 9
            $with['recursive'] = false;
78
        }
79
80 236
        return parent::buildWithQueries($withs, $params);
81
    }
82
83 535
    protected function defaultExpressionBuilders(): array
84
    {
85 535
        return array_merge(
86 535
            parent::defaultExpressionBuilders(),
87 535
            [
88 535
                InCondition::class => InConditionBuilder::class,
89 535
                LikeCondition::class => LikeConditionBuilder::class,
90 535
            ],
91 535
        );
92
    }
93
}
94