Passed
Branch master (b75f30)
by Wilmer
03:42
created

DQLQueryBuilder::extractAlias()   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 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use Yiisoft\Db\Exception\Exception;
8
use Yiisoft\Db\Exception\InvalidArgumentException;
9
use Yiisoft\Db\Expression\Expression;
10
use Yiisoft\Db\Mssql\Builder\InConditionBuilder;
11
use Yiisoft\Db\Mssql\Builder\LikeConditionBuilder;
12
use Yiisoft\Db\QueryBuilder\DQLQueryBuilder as AbstractDQLQueryBuilder;
13
use Yiisoft\Db\QueryBuilder\Condition\InCondition;
14
use Yiisoft\Db\QueryBuilder\Condition\LikeCondition;
15
16
use function array_merge;
17
use function preg_match;
18
19
final class DQLQueryBuilder extends AbstractDQLQueryBuilder
20
{
21 207
    public function buildOrderByAndLimit(string $sql, array $orderBy, $limit, $offset, array &$params = []): string
22
    {
23 207
        if (!$this->hasOffset($offset) && !$this->hasLimit($limit)) {
24 198
            $orderByString = $this->buildOrderBy($orderBy, $params);
25
26 198
            return $orderByString === '' ? $sql : $sql . $this->separator . $orderByString;
27
        }
28
29 13
        return $this->newBuildOrderByAndLimit($sql, $orderBy, $limit, $offset, $params);
30
    }
31
32 2
    public function selectExists(string $rawSql): string
33
    {
34 2
        return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END';
35
    }
36
37 488
    protected function defaultExpressionBuilders(): array
38
    {
39 488
        return array_merge(parent::defaultExpressionBuilders(), [
40
            InCondition::class => InConditionBuilder::class,
41
            LikeCondition::class => LikeConditionBuilder::class,
42
        ]);
43
    }
44
45
    /**
46
     * Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2012 or newer.
47
     *
48
     * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET).
49
     * @param array $orderBy the order by columns. See {@see Query::orderBy} for more details on how to specify
50
     * this parameter.
51
     * @param Expression|int|null $limit the limit number. See {@see Query::limit} for more details.
52
     * @param Expression|int|null $offset the offset number. See {@see Query::offset} for more details.
53
     * @param array $params the binding parameters to be populated.
54
     *
55
     * @throws Exception|InvalidArgumentException
56
     *
57
     * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any).
58
     */
59 13
    protected function newBuildOrderByAndLimit(
60
        string $sql,
61
        array $orderBy,
62
        Expression|int|null $limit,
63
        Expression|int|null $offset,
64
        array &$params = []
65
    ): string {
66 13
        $orderByString = $this->buildOrderBy($orderBy, $params);
67
68 13
        if ($orderByString === '') {
69
            /** ORDER BY clause is required when FETCH and OFFSET are in the SQL */
70 9
            $orderByString = 'ORDER BY (SELECT NULL)';
71
        }
72
73 13
        $sql .= $this->separator . $orderByString;
74
75
        /**
76
         * {@see http://technet.microsoft.com/en-us/library/gg699618.aspx}
77
         */
78 13
        $offsetString = $this->hasOffset($offset) ? (string) $offset : '0';
79 13
        $sql .= $this->separator . 'OFFSET ' . $offsetString . ' ROWS';
80
81 13
        if ($this->hasLimit($limit)) {
82 12
            $sql .= $this->separator . 'FETCH NEXT ' . (string) $limit . ' ROWS ONLY';
83
        }
84
85 13
        return $sql;
86
    }
87
88
    /**
89
     * Extracts table alias if there is one or returns false
90
     *
91
     * @psalm-return string[]|bool
92
     */
93 74
    protected function extractAlias(string $table): array|bool
94
    {
95 74
        if (preg_match('/^\[.*]$/', $table)) {
96 1
            return false;
97
        }
98
99 74
        return parent::extractAlias($table);
100
    }
101
}
102