1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of Cycle ORM package. |
||
5 | * |
||
6 | * For the full copyright and license information, please view the LICENSE |
||
7 | * file that was distributed with this source code. |
||
8 | */ |
||
9 | |||
10 | declare(strict_types=1); |
||
11 | |||
12 | namespace Cycle\Database\Driver\SQLServer; |
||
13 | |||
14 | use Cycle\Database\Driver\Compiler; |
||
15 | use Cycle\Database\Driver\Quoter; |
||
16 | use Cycle\Database\Driver\SQLServer\Injection\CompileJson; |
||
17 | use Cycle\Database\Injection\Fragment; |
||
18 | use Cycle\Database\Injection\FragmentInterface; |
||
19 | use Cycle\Database\Injection\Parameter; |
||
20 | use Cycle\Database\Query\QueryParameters; |
||
21 | |||
22 | /** |
||
23 | * Microsoft SQL server specific syntax compiler. |
||
24 | */ |
||
25 | class SQLServerCompiler extends Compiler |
||
26 | { |
||
27 | /** |
||
28 | * Column to be used as ROW_NUMBER in fallback selection mechanism, attention! Amount of columns |
||
29 | * in result set will be increaced by 1! |
||
30 | */ |
||
31 | public const ROW_NUMBER = '_ROW_NUMBER_'; |
||
32 | |||
33 | /** |
||
34 | * @psalm-return non-empty-string |
||
35 | */ |
||
36 | protected function insertQuery(QueryParameters $params, Quoter $q, array $tokens): string |
||
37 | { |
||
38 | if (empty($tokens['return'])) { |
||
39 | return parent::insertQuery($params, $q, $tokens); |
||
40 | 450 | } |
|
41 | |||
42 | 450 | $values = []; |
|
43 | 450 | foreach ($tokens['values'] as $value) { |
|
44 | $values[] = $this->value($params, $q, $value); |
||
45 | 450 | } |
|
46 | |||
47 | 432 | $output = \implode(',', \array_map( |
|
48 | fn(string|FragmentInterface|null $return) => $return instanceof FragmentInterface |
||
49 | ? $this->fragment($params, $q, $return) |
||
50 | : 'INSERTED.' . $this->quoteIdentifier($return), |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
51 | $tokens['return'], |
||
52 | )); |
||
53 | |||
54 | if ($tokens['columns'] === []) { |
||
55 | return \sprintf( |
||
56 | 24 | 'INSERT INTO %s OUTPUT %s DEFAULT VALUES', |
|
57 | 24 | $this->name($params, $q, $tokens['table'], true), |
|
58 | 24 | $output, |
|
59 | 24 | ); |
|
60 | } |
||
61 | |||
62 | return \sprintf( |
||
63 | 24 | 'INSERT INTO %s (%s) OUTPUT %s VALUES %s', |
|
64 | 24 | $this->name($params, $q, $tokens['table'], true), |
|
65 | $this->columns($params, $q, $tokens['columns']), |
||
66 | 24 | $output, |
|
67 | 24 | \implode(', ', $values), |
|
68 | 24 | ); |
|
69 | 24 | } |
|
70 | |||
71 | /** |
||
72 | * {@inheritDoc} |
||
73 | * |
||
74 | * Attention, limiting and ordering UNIONS will fail in SQL SERVER < 2012. |
||
75 | * For future upgrades: think about using top command. |
||
76 | * |
||
77 | * @link http://stackoverflow.com/questions/603724/how-to-implement-limit-with-microsoft-sql-server |
||
78 | * @link http://stackoverflow.com/questions/971964/limit-10-20-in-sql-server |
||
79 | */ |
||
80 | protected function selectQuery(QueryParameters $params, Quoter $q, array $tokens): string |
||
81 | 450 | { |
|
82 | $limit = $tokens['limit']; |
||
83 | $offset = $tokens['offset']; |
||
84 | |||
85 | if (($limit === null && $offset === null) || $tokens['orderBy'] !== []) { |
||
86 | //When no limits are specified we can use normal query syntax |
||
87 | return \call_user_func_array([$this, 'baseSelect'], \func_get_args()); |
||
88 | 450 | } |
|
89 | 446 | ||
90 | /** |
||
91 | * We are going to use fallback mechanism here in order to properly select limited data from |
||
92 | * database. Avoid usage of LIMIT/OFFSET without proper ORDER BY statement. |
||
93 | 30 | * |
|
94 | 6 | * Please see set of alerts raised in SelectQuery builder. |
|
95 | 6 | */ |
|
96 | $tokens['columns'][] = new Fragment( |
||
97 | 6 | \sprintf( |
|
98 | 6 | 'ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS %s', |
|
99 | 6 | $this->name($params, $q, self::ROW_NUMBER), |
|
100 | ), |
||
101 | ); |
||
102 | 6 | ||
103 | $tokens['limit'] = null; |
||
104 | $tokens['offset'] = null; |
||
105 | 24 | ||
106 | return \sprintf( |
||
107 | "SELECT * FROM (\n%s\n) AS [ORD_FALLBACK] %s", |
||
108 | 24 | $this->baseSelect($params, $q, $tokens), |
|
109 | $this->limit($params, $q, $limit, $offset, self::ROW_NUMBER), |
||
110 | 24 | ); |
|
111 | 20 | } |
|
112 | 20 | ||
113 | 20 | /** |
|
114 | * |
||
115 | 4 | * |
|
116 | 4 | * @param string $rowNumber Row used in a fallback sorting mechanism, ONLY when no ORDER BY |
|
117 | * specified. |
||
118 | * |
||
119 | 24 | * @link http://stackoverflow.com/questions/2135418/equivalent-of-limit-and-offset-for-sql-server |
|
120 | */ |
||
121 | protected function limit( |
||
122 | QueryParameters $params, |
||
123 | Quoter $q, |
||
124 | ?int $limit = null, |
||
125 | 450 | ?int $offset = null, |
|
126 | ?string $rowNumber = null, |
||
127 | ): string { |
||
128 | 450 | if ($limit === null && $offset === null) { |
|
129 | 450 | return ''; |
|
130 | 450 | } |
|
131 | |||
132 | 450 | //Modern SQLServer are easier to work with |
|
133 | 70 | if ($rowNumber === null) { |
|
134 | $statement = 'OFFSET ? ROWS '; |
||
135 | $params->push(new Parameter((int) $offset)); |
||
136 | 450 | ||
137 | 450 | if ($limit !== null) { |
|
138 | 450 | $statement .= 'FETCH FIRST ? ROWS ONLY'; |
|
139 | 450 | $params->push(new Parameter($limit)); |
|
140 | 450 | } |
|
141 | 450 | ||
142 | 450 | return \trim($statement); |
|
143 | 450 | } |
|
144 | 450 | ||
145 | 450 | $statement = "WHERE {$this->name($params, $q, $rowNumber)} "; |
|
146 | 450 | ||
147 | 450 | //0 = row_number(1) |
|
148 | 450 | ++$offset; |
|
149 | |||
150 | if ($limit !== null) { |
||
151 | $statement .= 'BETWEEN ? AND ?'; |
||
152 | $params->push(new Parameter((int) $offset)); |
||
153 | $params->push(new Parameter($offset + $limit - 1)); |
||
154 | } else { |
||
155 | $statement .= '>= ?'; |
||
156 | $params->push(new Parameter((int) $offset)); |
||
157 | } |
||
158 | |||
159 | return $statement; |
||
160 | } |
||
161 | |||
162 | protected function compileJsonOrderBy(string $path): FragmentInterface |
||
163 | { |
||
164 | return new CompileJson($path); |
||
165 | } |
||
166 | |||
167 | private function baseSelect(QueryParameters $params, Quoter $q, array $tokens): string |
||
168 | { |
||
169 | // This statement(s) parts should be processed first to define set of table and column aliases |
||
170 | $tables = []; |
||
171 | foreach ($tokens['from'] as $table) { |
||
172 | $tables[] = $this->name($params, $q, $table, true); |
||
173 | } |
||
174 | foreach ($tokens['join'] as $join) { |
||
175 | $this->nameWithAlias(new QueryParameters(), $q, $join['outer'], $join['alias'], true); |
||
176 | } |
||
177 | |||
178 | return \sprintf( |
||
179 | "SELECT%s %s\nFROM %s%s%s%s%s%s%s%s%s%s%s", |
||
180 | $this->optional(' ', $this->distinct($params, $q, $tokens['distinct'])), |
||
181 | $this->columns($params, $q, $tokens['columns']), |
||
182 | \implode(', ', $tables), |
||
183 | $this->optional(' ', $tokens['forUpdate'] ? 'WITH (UPDLOCK,ROWLOCK)' : '', ' '), |
||
184 | $this->optional(' ', $this->joins($params, $q, $tokens['join']), ' '), |
||
185 | $this->optional("\nWHERE", $this->where($params, $q, $tokens['where'])), |
||
186 | $this->optional("\nGROUP BY", $this->groupBy($params, $q, $tokens['groupBy']), ' '), |
||
187 | $this->optional("\nHAVING", $this->where($params, $q, $tokens['having'])), |
||
188 | $this->optional("\n", $this->unions($params, $q, $tokens['union'])), |
||
189 | $this->optional("\n", $this->intersects($params, $q, $tokens['intersect'])), |
||
190 | $this->optional("\n", $this->excepts($params, $q, $tokens['except'])), |
||
191 | $this->optional("\nORDER BY", $this->orderBy($params, $q, $tokens['orderBy'])), |
||
192 | $this->optional("\n", $this->limit($params, $q, $tokens['limit'], $tokens['offset'])), |
||
193 | ); |
||
194 | } |
||
195 | } |
||
196 |