1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
<?php
/**
* `SELECT` statement.
*/
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Statements;
use PhpMyAdmin\SqlParser\Components\ArrayObj;
use PhpMyAdmin\SqlParser\Components\Condition;
use PhpMyAdmin\SqlParser\Components\Expression;
use PhpMyAdmin\SqlParser\Components\FunctionCall;
use PhpMyAdmin\SqlParser\Components\IndexHint;
use PhpMyAdmin\SqlParser\Components\IntoKeyword;
use PhpMyAdmin\SqlParser\Components\JoinKeyword;
use PhpMyAdmin\SqlParser\Components\Limit;
use PhpMyAdmin\SqlParser\Components\OptionsArray;
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
use PhpMyAdmin\SqlParser\Statement;
/**
* `SELECT` statement.
*
* SELECT
* [ALL | DISTINCT | DISTINCTROW ]
* [HIGH_PRIORITY]
* [MAX_STATEMENT_TIME = N]
* [STRAIGHT_JOIN]
* [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
* [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
* select_expr [, select_expr ...]
* [FROM table_references
* [PARTITION partition_list]
* [WHERE where_condition]
* [GROUP BY {col_name | expr | position}
* [ASC | DESC], ... [WITH ROLLUP]]
* [HAVING where_condition]
* [ORDER BY {col_name | expr | position}
* [ASC | DESC], ...]
* [LIMIT {[offset,] row_count | row_count OFFSET offset}]
* [PROCEDURE procedure_name(argument_list)]
* [INTO OUTFILE 'file_name'
* [CHARACTER SET charset_name]
* export_options
* | INTO DUMPFILE 'file_name'
* | INTO var_name [, var_name]]
* [FOR UPDATE | LOCK IN SHARE MODE]]
*/
class SelectStatement extends Statement
{
/**
* Options for `SELECT` statements and their slot ID.
*
* @var array
*/
public static $OPTIONS = [
'ALL' => 1,
'DISTINCT' => 1,
'DISTINCTROW' => 1,
'HIGH_PRIORITY' => 2,
'MAX_STATEMENT_TIME' => [
3,
'var=',
],
'STRAIGHT_JOIN' => 4,
'SQL_SMALL_RESULT' => 5,
'SQL_BIG_RESULT' => 6,
'SQL_BUFFER_RESULT' => 7,
'SQL_CACHE' => 8,
'SQL_NO_CACHE' => 8,
'SQL_CALC_FOUND_ROWS' => 9,
];
public static $END_OPTIONS = [
'FOR UPDATE' => 1,
'LOCK IN SHARE MODE' => 1,
];
/**
* The clauses of this statement, in order.
*
* @see Statement::$CLAUSES
*
* @var array
*/
public static $CLAUSES = [
'SELECT' => [
'SELECT',
2,
],
// Used for options.
'_OPTIONS' => [
'_OPTIONS',
1,
],
// Used for selected expressions.
'_SELECT' => [
'SELECT',
1,
],
'INTO' => [
'INTO',
3,
],
'FROM' => [
'FROM',
3,
],
'FORCE' => [
'FORCE',
1,
],
'USE' => [
'USE',
1,
],
'IGNORE' => [
'IGNORE',
3,
],
'PARTITION' => [
'PARTITION',
3,
],
'JOIN' => [
'JOIN',
1,
],
'FULL JOIN' => [
'FULL JOIN',
1,
],
'INNER JOIN' => [
'INNER JOIN',
1,
],
'LEFT JOIN' => [
'LEFT JOIN',
1,
],
'LEFT OUTER JOIN' => [
'LEFT OUTER JOIN',
1,
],
'RIGHT JOIN' => [
'RIGHT JOIN',
1,
],
'RIGHT OUTER JOIN' => [
'RIGHT OUTER JOIN',
1,
],
'NATURAL JOIN' => [
'NATURAL JOIN',
1,
],
'NATURAL LEFT JOIN' => [
'NATURAL LEFT JOIN',
1,
],
'NATURAL RIGHT JOIN' => [
'NATURAL RIGHT JOIN',
1,
],
'NATURAL LEFT OUTER JOIN' => [
'NATURAL LEFT OUTER JOIN',
1,
],
'NATURAL RIGHT OUTER JOIN' => [
'NATURAL RIGHT JOIN',
1,
],
'WHERE' => [
'WHERE',
3,
],
'GROUP BY' => [
'GROUP BY',
3,
],
'HAVING' => [
'HAVING',
3,
],
'ORDER BY' => [
'ORDER BY',
3,
],
'LIMIT' => [
'LIMIT',
3,
],
'PROCEDURE' => [
'PROCEDURE',
3,
],
'UNION' => [
'UNION',
1,
],
'EXCEPT' => [
'EXCEPT',
1,
],
'INTERSECT' => [
'INTERSECT',
1,
],
'_END_OPTIONS' => [
'_END_OPTIONS',
1,
],
// These are available only when `UNION` is present.
// 'ORDER BY' => ['ORDER BY', 3],
// 'LIMIT' => ['LIMIT', 3],
];
/**
* Expressions that are being selected by this statement.
*
* @var Expression[]
*/
public $expr = [];
/**
* Tables used as sources for this statement.
*
* @var Expression[]
*/
public $from = [];
/**
* Index hints
*
* @var IndexHint[]
*/
public $index_hints;
/**
* Partitions used as source for this statement.
*
* @var ArrayObj
*/
public $partition;
/**
* Conditions used for filtering each row of the result set.
*
* @var Condition[]
*/
public $where;
/**
* Conditions used for grouping the result set.
*
* @var OrderKeyword[]
*/
public $group;
/**
* Conditions used for filtering the result set.
*
* @var Condition[]
*/
public $having;
/**
* Specifies the order of the rows in the result set.
*
* @var OrderKeyword[]
*/
public $order;
/**
* Conditions used for limiting the size of the result set.
*
* @var Limit
*/
public $limit;
/**
* Procedure that should process the data in the result set.
*
* @var FunctionCall
*/
public $procedure;
/**
* Destination of this result set.
*
* @var IntoKeyword
*/
public $into;
/**
* Joins.
*
* @var JoinKeyword[]
*/
public $join;
/**
* Unions.
*
* @var SelectStatement[]
*/
public $union = [];
/**
* The end options of this query.
*
* @see static::$END_OPTIONS
*
* @var OptionsArray
*/
public $end_options;
/**
* Gets the clauses of this statement.
*
* @return array
*/
public function getClauses()
{
// This is a cheap fix for `SELECT` statements that contain `UNION`.
// The `ORDER BY` and `LIMIT` clauses should be at the end of the
// statement.
if (! empty($this->union)) {
$clauses = static::$CLAUSES;
unset($clauses['ORDER BY'], $clauses['LIMIT']);
$clauses['ORDER BY'] = [
'ORDER BY',
3,
];
$clauses['LIMIT'] = [
'LIMIT',
3,
];
return $clauses;
}
return static::$CLAUSES;
}
}
|