DQLQueryBuilder::buildOrderByAndLimit()   B
last analyzed

Complexity

Conditions 7
Paths 36

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

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