|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* `SELECT` statement. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace PhpMyAdmin\SqlParser\Statements; |
|
8
|
|
|
|
|
9
|
|
|
use PhpMyAdmin\SqlParser\Statement; |
|
10
|
|
|
use PhpMyAdmin\SqlParser\Components\ArrayObj; |
|
11
|
|
|
use PhpMyAdmin\SqlParser\Components\FunctionCall; |
|
12
|
|
|
use PhpMyAdmin\SqlParser\Components\Expression; |
|
13
|
|
|
use PhpMyAdmin\SqlParser\Components\IntoKeyword; |
|
14
|
|
|
use PhpMyAdmin\SqlParser\Components\JoinKeyword; |
|
15
|
|
|
use PhpMyAdmin\SqlParser\Components\Limit; |
|
16
|
|
|
use PhpMyAdmin\SqlParser\Components\OrderKeyword; |
|
17
|
|
|
use PhpMyAdmin\SqlParser\Components\Condition; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* `SELECT` statement. |
|
21
|
|
|
* |
|
22
|
|
|
* SELECT |
|
23
|
|
|
* [ALL | DISTINCT | DISTINCTROW ] |
|
24
|
|
|
* [HIGH_PRIORITY] |
|
25
|
|
|
* [MAX_STATEMENT_TIME = N] |
|
26
|
|
|
* [STRAIGHT_JOIN] |
|
27
|
|
|
* [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] |
|
28
|
|
|
* [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] |
|
29
|
|
|
* select_expr [, select_expr ...] |
|
30
|
|
|
* [FROM table_references |
|
31
|
|
|
* [PARTITION partition_list] |
|
32
|
|
|
* [WHERE where_condition] |
|
33
|
|
|
* [GROUP BY {col_name | expr | position} |
|
34
|
|
|
* [ASC | DESC], ... [WITH ROLLUP]] |
|
35
|
|
|
* [HAVING where_condition] |
|
36
|
|
|
* [ORDER BY {col_name | expr | position} |
|
37
|
|
|
* [ASC | DESC], ...] |
|
38
|
|
|
* [LIMIT {[offset,] row_count | row_count OFFSET offset}] |
|
39
|
|
|
* [PROCEDURE procedure_name(argument_list)] |
|
40
|
|
|
* [INTO OUTFILE 'file_name' |
|
41
|
|
|
* [CHARACTER SET charset_name] |
|
42
|
|
|
* export_options |
|
43
|
|
|
* | INTO DUMPFILE 'file_name' |
|
44
|
|
|
* | INTO var_name [, var_name]] |
|
45
|
|
|
* [FOR UPDATE | LOCK IN SHARE MODE]] |
|
46
|
|
|
* |
|
47
|
|
|
* @category Statements |
|
48
|
|
|
* |
|
49
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ |
|
50
|
|
|
*/ |
|
51
|
|
|
class SelectStatement extends Statement |
|
52
|
|
|
{ |
|
53
|
|
|
/** |
|
54
|
|
|
* Options for `SELECT` statements and their slot ID. |
|
55
|
|
|
* |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
public static $OPTIONS = array( |
|
59
|
|
|
'ALL' => 1, |
|
60
|
|
|
'DISTINCT' => 1, |
|
61
|
|
|
'DISTINCTROW' => 1, |
|
62
|
|
|
'HIGH_PRIORITY' => 2, |
|
63
|
|
|
'MAX_STATEMENT_TIME' => array(3, 'var='), |
|
64
|
|
|
'STRAIGHT_JOIN' => 4, |
|
65
|
|
|
'SQL_SMALL_RESULT' => 5, |
|
66
|
|
|
'SQL_BIG_RESULT' => 6, |
|
67
|
|
|
'SQL_BUFFER_RESULT' => 7, |
|
68
|
|
|
'SQL_CACHE' => 8, |
|
69
|
|
|
'SQL_NO_CACHE' => 8, |
|
70
|
|
|
'SQL_CALC_FOUND_ROWS' => 9, |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
public static $END_OPTIONS = array( |
|
74
|
|
|
'FOR UPDATE' => 1, |
|
75
|
|
|
'LOCK IN SHARE MODE' => 1, |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* The clauses of this statement, in order. |
|
80
|
|
|
* |
|
81
|
|
|
* @see Statement::$CLAUSES |
|
82
|
|
|
* |
|
83
|
|
|
* @var array |
|
84
|
|
|
*/ |
|
85
|
|
|
public static $CLAUSES = array( |
|
86
|
|
|
'SELECT' => array('SELECT', 2), |
|
87
|
|
|
// Used for options. |
|
88
|
|
|
'_OPTIONS' => array('_OPTIONS', 1), |
|
89
|
|
|
// Used for selected expressions. |
|
90
|
|
|
'_SELECT' => array('SELECT', 1), |
|
91
|
|
|
'INTO' => array('INTO', 3), |
|
92
|
|
|
'FROM' => array('FROM', 3), |
|
93
|
|
|
'PARTITION' => array('PARTITION', 3), |
|
94
|
|
|
|
|
95
|
|
|
'JOIN' => array('JOIN', 1), |
|
96
|
|
|
'FULL JOIN' => array('FULL JOIN', 1), |
|
97
|
|
|
'INNER JOIN' => array('INNER JOIN', 1), |
|
98
|
|
|
'LEFT JOIN' => array('LEFT JOIN', 1), |
|
99
|
|
|
'LEFT OUTER JOIN' => array('LEFT OUTER JOIN', 1), |
|
100
|
|
|
'RIGHT JOIN' => array('RIGHT JOIN', 1), |
|
101
|
|
|
'RIGHT OUTER JOIN' => array('RIGHT OUTER JOIN', 1), |
|
102
|
|
|
'NATURAL JOIN' => array('NATURAL JOIN', 1), |
|
103
|
|
|
'NATURAL LEFT JOIN' => array('NATURAL LEFT JOIN', 1), |
|
104
|
|
|
'NATURAL RIGHT JOIN' => array('NATURAL RIGHT JOIN', 1), |
|
105
|
|
|
'NATURAL LEFT OUTER JOIN' => array('NATURAL LEFT OUTER JOIN', 1), |
|
106
|
|
|
'NATURAL RIGHT OUTER JOIN' => array('NATURAL RIGHT JOIN', 1), |
|
107
|
|
|
|
|
108
|
|
|
'WHERE' => array('WHERE', 3), |
|
109
|
|
|
'GROUP BY' => array('GROUP BY', 3), |
|
110
|
|
|
'HAVING' => array('HAVING', 3), |
|
111
|
|
|
'ORDER BY' => array('ORDER BY', 3), |
|
112
|
|
|
'LIMIT' => array('LIMIT', 3), |
|
113
|
|
|
'PROCEDURE' => array('PROCEDURE', 3), |
|
114
|
|
|
'UNION' => array('UNION', 1), |
|
115
|
|
|
'_END_OPTIONS' => array('_END_OPTIONS', 1), |
|
116
|
|
|
// These are available only when `UNION` is present. |
|
117
|
|
|
// 'ORDER BY' => array('ORDER BY', 3), |
|
118
|
|
|
// 'LIMIT' => array('LIMIT', 3), |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Expressions that are being selected by this statement. |
|
123
|
|
|
* |
|
124
|
|
|
* @var Expression[] |
|
125
|
|
|
*/ |
|
126
|
|
|
public $expr = array(); |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Tables used as sources for this statement. |
|
130
|
|
|
* |
|
131
|
|
|
* @var Expression[] |
|
132
|
|
|
*/ |
|
133
|
|
|
public $from = array(); |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Partitions used as source for this statement. |
|
137
|
|
|
* |
|
138
|
|
|
* @var ArrayObj |
|
139
|
|
|
*/ |
|
140
|
|
|
public $partition; |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Conditions used for filtering each row of the result set. |
|
144
|
|
|
* |
|
145
|
|
|
* @var Condition[] |
|
146
|
|
|
*/ |
|
147
|
|
|
public $where; |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Conditions used for grouping the result set. |
|
151
|
|
|
* |
|
152
|
|
|
* @var OrderKeyword[] |
|
153
|
|
|
*/ |
|
154
|
|
|
public $group; |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Conditions used for filtering the result set. |
|
158
|
|
|
* |
|
159
|
|
|
* @var Condition[] |
|
160
|
|
|
*/ |
|
161
|
|
|
public $having; |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Specifies the order of the rows in the result set. |
|
165
|
|
|
* |
|
166
|
|
|
* @var OrderKeyword[] |
|
167
|
|
|
*/ |
|
168
|
|
|
public $order; |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Conditions used for limiting the size of the result set. |
|
172
|
|
|
* |
|
173
|
|
|
* @var Limit |
|
174
|
|
|
*/ |
|
175
|
|
|
public $limit; |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Procedure that should process the data in the result set. |
|
179
|
|
|
* |
|
180
|
|
|
* @var FunctionCall |
|
181
|
|
|
*/ |
|
182
|
|
|
public $procedure; |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Destination of this result set. |
|
186
|
|
|
* |
|
187
|
|
|
* @var IntoKeyword |
|
188
|
|
|
*/ |
|
189
|
|
|
public $into; |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Joins. |
|
193
|
|
|
* |
|
194
|
|
|
* @var JoinKeyword[] |
|
195
|
|
|
*/ |
|
196
|
|
|
public $join; |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Unions. |
|
200
|
|
|
* |
|
201
|
|
|
* @var SelectStatement[] |
|
202
|
|
|
*/ |
|
203
|
|
|
public $union = array(); |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* The end options of this query. |
|
207
|
|
|
* |
|
208
|
|
|
* @var OptionsArray |
|
209
|
|
|
* |
|
210
|
|
|
* @see static::$END_OPTIONS |
|
211
|
|
|
*/ |
|
212
|
|
|
public $end_options; |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Gets the clauses of this statement. |
|
216
|
|
|
* |
|
217
|
|
|
* @return array |
|
218
|
|
|
*/ |
|
219
|
95 |
|
public function getClauses() |
|
220
|
|
|
{ |
|
221
|
|
|
// This is a cheap fix for `SELECT` statements that contain `UNION`. |
|
222
|
|
|
// The `ORDER BY` and `LIMIT` clauses should be at the end of the |
|
223
|
|
|
// statement. |
|
224
|
95 |
|
if (!empty($this->union)) { |
|
225
|
2 |
|
$clauses = static::$CLAUSES; |
|
226
|
2 |
|
unset($clauses['ORDER BY']); |
|
227
|
2 |
|
unset($clauses['LIMIT']); |
|
228
|
2 |
|
$clauses['ORDER BY'] = array('ORDER BY', 3); |
|
229
|
2 |
|
$clauses['LIMIT'] = array('LIMIT', 3); |
|
230
|
|
|
|
|
231
|
2 |
|
return $clauses; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
95 |
|
return static::$CLAUSES; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|