|
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\Conditions\InCondition; |
|
14
|
|
|
use Yiisoft\Db\QueryBuilder\Conditions\LikeCondition; |
|
15
|
|
|
|
|
16
|
|
|
use function array_merge; |
|
17
|
|
|
use function preg_match; |
|
18
|
|
|
|
|
19
|
|
|
final class DQLQueryBuilder extends AbstractDQLQueryBuilder |
|
20
|
|
|
{ |
|
21
|
178 |
|
public function buildOrderByAndLimit(string $sql, array $orderBy, $limit, $offset, array &$params = []): string |
|
22
|
|
|
{ |
|
23
|
178 |
|
if (!$this->hasOffset($offset) && !$this->hasLimit($limit)) { |
|
24
|
169 |
|
$orderByString = $this->buildOrderBy($orderBy, $params); |
|
25
|
|
|
|
|
26
|
169 |
|
return $orderByString === '' ? $sql : $sql . $this->separator . $orderByString; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
13 |
|
return $this->newBuildOrderByAndLimit($sql, $orderBy, $limit, $offset, $params); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
public function selectExists(string $rawSql): string |
|
33
|
|
|
{ |
|
34
|
1 |
|
return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END'; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
396 |
|
protected function defaultExpressionBuilders(): array |
|
38
|
|
|
{ |
|
39
|
396 |
|
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
|
10 |
|
$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
|
75 |
|
protected function extractAlias(string $table): array|bool |
|
94
|
|
|
{ |
|
95
|
75 |
|
if (preg_match('/^\[.*]$/', $table)) { |
|
96
|
6 |
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
69 |
|
return parent::extractAlias($table); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|