1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* `INTO` keyword parser. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace PhpMyAdmin\SqlParser\Components; |
9
|
|
|
|
10
|
|
|
use PhpMyAdmin\SqlParser\Component; |
11
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
12
|
|
|
use PhpMyAdmin\SqlParser\Token; |
13
|
|
|
use PhpMyAdmin\SqlParser\TokensList; |
14
|
|
|
use function implode; |
15
|
|
|
use function trim; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* `INTO` keyword parser. |
19
|
|
|
*/ |
20
|
|
|
class IntoKeyword extends Component |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* FIELDS/COLUMNS Options for `SELECT...INTO` statements. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
public static $FIELDS_OPTIONS = [ |
28
|
|
|
'TERMINATED BY' => [ |
29
|
|
|
1, |
30
|
|
|
'expr', |
31
|
|
|
], |
32
|
|
|
'OPTIONALLY' => 2, |
33
|
|
|
'ENCLOSED BY' => [ |
34
|
|
|
3, |
35
|
|
|
'expr', |
36
|
|
|
], |
37
|
|
|
'ESCAPED BY' => [ |
38
|
|
|
4, |
39
|
|
|
'expr', |
40
|
|
|
], |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* LINES Options for `SELECT...INTO` statements. |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
public static $LINES_OPTIONS = [ |
49
|
|
|
'STARTING BY' => [ |
50
|
|
|
1, |
51
|
|
|
'expr', |
52
|
|
|
], |
53
|
|
|
'TERMINATED BY' => [ |
54
|
|
|
2, |
55
|
|
|
'expr', |
56
|
|
|
], |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Type of target (OUTFILE or SYMBOL). |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
public $type; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The destination, which can be a table or a file. |
68
|
|
|
* |
69
|
|
|
* @var string|Expression |
70
|
|
|
*/ |
71
|
|
|
public $dest; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The name of the columns. |
75
|
|
|
* |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
public $columns; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The values to be selected into (SELECT .. INTO @var1). |
82
|
|
|
* |
83
|
|
|
* @var Expression[] |
84
|
|
|
*/ |
85
|
|
|
public $values; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Options for FIELDS/COLUMNS keyword. |
89
|
|
|
* |
90
|
|
|
* @see static::$FIELDS_OPTIONS |
91
|
|
|
* |
92
|
|
|
* @var OptionsArray |
93
|
|
|
*/ |
94
|
|
|
public $fields_options; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Whether to use `FIELDS` or `COLUMNS` while building. |
98
|
|
|
* |
99
|
|
|
* @var bool |
100
|
|
|
*/ |
101
|
|
|
public $fields_keyword; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Options for OPTIONS keyword. |
105
|
|
|
* |
106
|
|
|
* @see static::$LINES_OPTIONS |
107
|
|
|
* |
108
|
|
|
* @var OptionsArray |
109
|
|
|
*/ |
110
|
|
|
public $lines_options; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $type type of destination (may be OUTFILE) |
114
|
|
|
* @param string|Expression $dest actual destination |
115
|
|
|
* @param array $columns column list of destination |
116
|
|
|
* @param array $values selected fields |
117
|
|
|
* @param OptionsArray $fields_options options for FIELDS/COLUMNS keyword |
118
|
|
|
* @param bool $fields_keyword options for OPTIONS keyword |
119
|
|
|
*/ |
120
|
160 |
|
public function __construct( |
121
|
|
|
$type = null, |
122
|
|
|
$dest = null, |
123
|
|
|
$columns = null, |
124
|
|
|
$values = null, |
125
|
|
|
$fields_options = null, |
126
|
|
|
$fields_keyword = null |
127
|
|
|
) { |
128
|
160 |
|
$this->type = $type; |
129
|
160 |
|
$this->dest = $dest; |
130
|
160 |
|
$this->columns = $columns; |
131
|
160 |
|
$this->values = $values; |
132
|
160 |
|
$this->fields_options = $fields_options; |
133
|
160 |
|
$this->fields_keyword = $fields_keyword; |
134
|
160 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Parser $parser the parser that serves as context |
138
|
|
|
* @param TokensList $list the list of tokens that are being parsed |
139
|
|
|
* @param array $options parameters for parsing |
140
|
|
|
* |
141
|
|
|
* @return IntoKeyword |
142
|
|
|
*/ |
143
|
160 |
|
public static function parse(Parser $parser, TokensList $list, array $options = []) |
144
|
|
|
{ |
145
|
160 |
|
$ret = new static(); |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* The state of the parser. |
149
|
|
|
* |
150
|
|
|
* Below are the states of the parser. |
151
|
|
|
* |
152
|
|
|
* 0 -----------------------[ name ]----------------------> 1 |
153
|
|
|
* 0 ---------------------[ OUTFILE ]---------------------> 2 |
154
|
|
|
* |
155
|
|
|
* 1 ------------------------[ ( ]------------------------> (END) |
156
|
|
|
* |
157
|
|
|
* 2 ---------------------[ filename ]--------------------> 1 |
158
|
|
|
* |
159
|
|
|
* @var int |
160
|
|
|
*/ |
161
|
160 |
|
$state = 0; |
162
|
|
|
|
163
|
160 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
164
|
|
|
/** |
165
|
|
|
* Token parsed at this moment. |
166
|
|
|
* |
167
|
|
|
* @var Token |
168
|
|
|
*/ |
169
|
160 |
|
$token = $list->tokens[$list->idx]; |
170
|
|
|
|
171
|
|
|
// End of statement. |
172
|
160 |
|
if ($token->type === Token::TYPE_DELIMITER) { |
173
|
28 |
|
break; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// Skipping whitespaces and comments. |
177
|
160 |
|
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) { |
178
|
148 |
|
continue; |
179
|
|
|
} |
180
|
|
|
|
181
|
160 |
|
if (($token->type === Token::TYPE_KEYWORD) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) { |
182
|
72 |
|
if (($state === 0) && ($token->keyword === 'OUTFILE')) { |
183
|
40 |
|
$ret->type = 'OUTFILE'; |
184
|
40 |
|
$state = 2; |
185
|
40 |
|
continue; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// No other keyword is expected except for $state = 4, which expects `LINES` |
189
|
52 |
|
if ($state !== 4) { |
190
|
48 |
|
break; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
156 |
|
if ($state === 0) { |
195
|
120 |
|
if ((isset($options['fromInsert']) |
196
|
60 |
|
&& $options['fromInsert']) |
197
|
60 |
|
|| (isset($options['fromReplace']) |
198
|
120 |
|
&& $options['fromReplace']) |
199
|
|
|
) { |
200
|
112 |
|
$ret->dest = Expression::parse( |
201
|
112 |
|
$parser, |
202
|
112 |
|
$list, |
203
|
|
|
[ |
204
|
112 |
|
'parseField' => 'table', |
205
|
|
|
'breakOnAlias' => true, |
206
|
|
|
] |
207
|
|
|
); |
208
|
|
|
} else { |
209
|
8 |
|
$ret->values = ExpressionArray::parse($parser, $list); |
210
|
|
|
} |
211
|
|
|
|
212
|
120 |
|
$state = 1; |
213
|
120 |
|
} elseif ($state === 1) { |
214
|
84 |
|
if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) { |
215
|
76 |
|
$ret->columns = ArrayObj::parse($parser, $list)->values; |
216
|
76 |
|
++$list->idx; |
217
|
|
|
} |
218
|
|
|
|
219
|
84 |
|
break; |
220
|
36 |
|
} elseif ($state === 2) { |
221
|
36 |
|
$ret->dest = $token->value; |
222
|
|
|
|
223
|
36 |
|
$state = 3; |
224
|
16 |
|
} elseif ($state === 3) { |
225
|
16 |
|
$ret->parseFileOptions($parser, $list, $token->value); |
226
|
16 |
|
$state = 4; |
227
|
16 |
|
} elseif ($state === 4) { |
228
|
16 |
|
if ($token->type === Token::TYPE_KEYWORD && $token->keyword !== 'LINES') { |
229
|
4 |
|
break; |
230
|
|
|
} |
231
|
|
|
|
232
|
12 |
|
$ret->parseFileOptions($parser, $list, $token->value); |
233
|
12 |
|
$state = 5; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
160 |
|
--$list->idx; |
238
|
|
|
|
239
|
160 |
|
return $ret; |
240
|
|
|
} |
241
|
|
|
|
242
|
16 |
|
public function parseFileOptions(Parser $parser, TokensList $list, $keyword = 'FIELDS') |
243
|
|
|
{ |
244
|
16 |
|
++$list->idx; |
245
|
|
|
|
246
|
16 |
|
if ($keyword === 'FIELDS' || $keyword === 'COLUMNS') { |
247
|
|
|
// parse field options |
248
|
16 |
|
$this->fields_options = OptionsArray::parse( |
249
|
16 |
|
$parser, |
250
|
8 |
|
$list, |
251
|
16 |
|
static::$FIELDS_OPTIONS |
252
|
|
|
); |
253
|
|
|
|
254
|
16 |
|
$this->fields_keyword = ($keyword === 'FIELDS'); |
255
|
|
|
} else { |
256
|
|
|
// parse line options |
257
|
12 |
|
$this->lines_options = OptionsArray::parse( |
258
|
12 |
|
$parser, |
259
|
6 |
|
$list, |
260
|
12 |
|
static::$LINES_OPTIONS |
261
|
|
|
); |
262
|
|
|
} |
263
|
16 |
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param IntoKeyword $component the component to be built |
267
|
|
|
* @param array $options parameters for building |
268
|
|
|
* |
269
|
|
|
* @return string |
270
|
|
|
*/ |
271
|
36 |
|
public static function build($component, array $options = []) |
272
|
|
|
{ |
273
|
36 |
|
if ($component->dest instanceof Expression) { |
274
|
20 |
|
$columns = ! empty($component->columns) ? '(`' . implode('`, `', $component->columns) . '`)' : ''; |
275
|
|
|
|
276
|
20 |
|
return $component->dest . $columns; |
277
|
16 |
|
} elseif (isset($component->values)) { |
278
|
8 |
|
return ExpressionArray::build($component->values); |
279
|
|
|
} |
280
|
|
|
|
281
|
8 |
|
$ret = 'OUTFILE "' . $component->dest . '"'; |
282
|
|
|
|
283
|
8 |
|
$fields_options_str = OptionsArray::build($component->fields_options); |
284
|
8 |
|
if (trim($fields_options_str) !== '') { |
285
|
4 |
|
$ret .= $component->fields_keyword ? ' FIELDS' : ' COLUMNS'; |
286
|
4 |
|
$ret .= ' ' . $fields_options_str; |
287
|
|
|
} |
288
|
|
|
|
289
|
8 |
|
$lines_options_str = OptionsArray::build($component->lines_options, ['expr' => true]); |
290
|
8 |
|
if (trim($lines_options_str) !== '') { |
291
|
4 |
|
$ret .= ' LINES ' . $lines_options_str; |
292
|
|
|
} |
293
|
|
|
|
294
|
8 |
|
return $ret; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|