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