Passed
Pull Request — master (#164)
by Def
06:20
created

DQLQueryBuilder::buildOrderByAndLimit()   B

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\ExpressionInterface;
8
use Yiisoft\Db\Oracle\Builder\InConditionBuilder;
9
use Yiisoft\Db\Oracle\Builder\LikeConditionBuilder;
10
use Yiisoft\Db\QueryBuilder\AbstractDQLQueryBuilder;
11
use Yiisoft\Db\QueryBuilder\Condition\InCondition;
12
use Yiisoft\Db\QueryBuilder\Condition\LikeCondition;
13
14
use function array_merge;
15
use function implode;
16
17
final class DQLQueryBuilder extends AbstractDQLQueryBuilder
18
{
19 218
    public function buildOrderByAndLimit(
20
        string $sql,
21
        array $orderBy,
22
        ExpressionInterface|int|null $limit,
23
        ExpressionInterface|int|null $offset,
24
        array &$params = []
25
    ): string {
26 218
        $orderByString = $this->buildOrderBy($orderBy, $params);
27
28 218
        if ($orderByString !== '') {
29 5
            $sql .= $this->separator . $orderByString;
30
        }
31
32 218
        $filters = [];
33
34 218
        if ($this->hasOffset($offset)) {
35 2
            $filters[] = 'rowNumId > ' .
36 2
                ($offset instanceof ExpressionInterface ? $this->buildExpression($offset) : (string)$offset);
37
        }
38
39 218
        if ($this->hasLimit($limit)) {
40 13
            $filters[] = 'rownum <= ' .
41 13
                ($limit instanceof ExpressionInterface ? $this->buildExpression($limit) : (string)$limit);
42
        }
43
44 218
        if (empty($filters)) {
45 210
            return $sql;
46
        }
47
48 14
        $filter = implode(' AND ', $filters);
49 14
        return <<<SQL
50 14
        WITH USER_SQL AS ($sql), PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL)
51 14
        SELECT * FROM PAGINATION WHERE $filter
52 14
        SQL;
53
    }
54
55 1
    public function selectExists(string $rawSql): string
56
    {
57 1
        return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END FROM DUAL';
58
    }
59
60 496
    protected function defaultExpressionBuilders(): array
61
    {
62 496
        return array_merge(
63 496
            parent::defaultExpressionBuilders(),
64 496
            [
65 496
                InCondition::class => InConditionBuilder::class,
66 496
                LikeCondition::class => LikeConditionBuilder::class,
67 496
            ],
68 496
        );
69
    }
70
}
71