Test Failed
Push — master ( f6eae3...245d3e )
by Sergei
09:52 queued 03:04
created

DQLQueryBuilder::buildWithQueries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 2
rs 10
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 221
{
23
    public function buildOrderByAndLimit(
24
        string $sql,
25
        array $orderBy,
26
        ExpressionInterface|int|null $limit,
27
        ExpressionInterface|int|null $offset,
28
        array &$params = []
29 221
    ): string {
30
        $orderByString = $this->buildOrderBy($orderBy, $params);
31 221
32 6
        if ($orderByString !== '') {
33
            $sql .= $this->separator . $orderByString;
34
        }
35 221
36
        $filters = [];
37 221
38 2
        if ($this->hasOffset($offset)) {
39 2
            $filters[] = 'rowNumId > ' .
40
                ($offset instanceof ExpressionInterface ? $this->buildExpression($offset) : (string)$offset);
41
        }
42 221
43 14
        if ($this->hasLimit($limit)) {
44 14
            $filters[] = 'rownum <= ' .
45
                ($limit instanceof ExpressionInterface ? $this->buildExpression($limit) : (string)$limit);
46
        }
47 221
48 212
        if (empty($filters)) {
49
            return $sql;
50
        }
51 15
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
        SQL;
57
    }
58 1
59
    public function selectExists(string $rawSql): string
60 1
    {
61
        return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END FROM DUAL';
62
    }
63 511
64
    public function buildWithQueries(array $withs, array &$params): string
65 511
    {
66 511
        /** @psalm-var array{query:string|Query, alias:ExpressionInterface|string, recursive:bool}[] $withs */
67 511
        foreach ($withs as &$with) {
68 511
            $with['recursive'] = false;
69 511
        }
70 511
71 511
        return parent::buildWithQueries($withs, $params);
72
    }
73
74
    protected function defaultExpressionBuilders(): array
75
    {
76
        return array_merge(
77
            parent::defaultExpressionBuilders(),
78
            [
79
                InCondition::class => InConditionBuilder::class,
80
                LikeCondition::class => LikeConditionBuilder::class,
81
            ],
82
        );
83
    }
84
}
85