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
|
|
|
* Type of target (OUTFILE or SYMBOL). |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $type; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The destination, which can be a table or a file. |
36
|
|
|
* |
37
|
|
|
* @var string|Expression |
38
|
|
|
*/ |
39
|
|
|
public $dest; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The name of the columns. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
public $columns; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The values to be selected into (SELECT .. INTO @var1) |
50
|
|
|
* |
51
|
|
|
* @var ExpressionArray |
52
|
|
|
*/ |
53
|
|
|
public $values; |
54
|
|
|
|
55
|
|
|
/** |
56
|
15 |
|
* @param Parser $parser The parser that serves as context. |
57
|
|
|
* @param TokensList $list The list of tokens that are being parsed. |
58
|
15 |
|
* @param array $options Parameters for parsing. |
59
|
|
|
* |
60
|
|
|
* @return IntoKeyword |
61
|
|
|
*/ |
62
|
|
|
public static function parse(Parser $parser, TokensList $list, array $options = array()) |
63
|
|
|
{ |
64
|
|
|
$ret = new IntoKeyword(); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The state of the parser. |
68
|
|
|
* |
69
|
|
|
* Below are the states of the parser. |
70
|
|
|
* |
71
|
|
|
* 0 -----------------------[ name ]----------------------> 1 |
72
|
|
|
* 0 ---------------------[ OUTFILE ]---------------------> 2 |
73
|
|
|
* |
74
|
15 |
|
* 1 ------------------------[ ( ]------------------------> (END) |
75
|
|
|
* |
76
|
15 |
|
* 2 ---------------------[ filename ]--------------------> 1 |
77
|
|
|
* |
78
|
|
|
* @var int $state |
79
|
|
|
*/ |
80
|
|
|
$state = 0; |
81
|
|
|
|
82
|
15 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
83
|
|
|
/** |
84
|
|
|
* Token parsed at this moment. |
85
|
15 |
|
* |
86
|
1 |
|
* @var Token $token |
87
|
|
|
*/ |
88
|
|
|
$token = $list->tokens[$list->idx]; |
89
|
|
|
|
90
|
15 |
|
// End of statement. |
91
|
13 |
|
if ($token->type === Token::TYPE_DELIMITER) { |
92
|
|
|
break; |
93
|
|
|
} |
94
|
15 |
|
|
95
|
9 |
|
// Skipping whitespaces and comments. |
96
|
6 |
|
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) { |
97
|
6 |
|
continue; |
98
|
6 |
|
} |
99
|
|
|
|
100
|
|
|
if (($token->type === Token::TYPE_KEYWORD) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) { |
101
|
|
|
if (($state === 0) && ($token->value === 'OUTFILE')) { |
102
|
3 |
|
$ret->type = 'OUTFILE'; |
103
|
|
|
$state = 2; |
104
|
|
|
continue; |
105
|
14 |
|
} |
106
|
9 |
|
|
107
|
9 |
|
// No other keyword is expected. |
108
|
9 |
|
break; |
109
|
|
|
} |
110
|
9 |
|
|
111
|
9 |
|
if ($state === 0) { |
112
|
|
|
if ((isset($options['fromInsert']) |
113
|
9 |
|
&& $options['fromInsert']) |
114
|
9 |
|
|| (isset($options['fromReplace']) |
115
|
14 |
|
&& $options['fromReplace']) |
116
|
6 |
|
) { |
117
|
6 |
|
$ret->dest = Expression::parse( |
118
|
6 |
|
$parser, |
119
|
6 |
|
$list, |
120
|
6 |
|
array( |
121
|
5 |
|
'parseField' => 'table', |
122
|
5 |
|
'breakOnAlias' => true, |
123
|
5 |
|
) |
124
|
5 |
|
); |
125
|
|
|
} else { |
126
|
9 |
|
$ret->values = ExpressionArray::parse($parser, $list); |
|
|
|
|
127
|
|
|
} |
128
|
15 |
|
$state = 1; |
129
|
15 |
|
} elseif ($state === 1) { |
130
|
|
|
if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) { |
131
|
|
|
$ret->columns = ArrayObj::parse($parser, $list)->values; |
132
|
|
|
++$list->idx; |
133
|
|
|
} |
134
|
|
|
break; |
135
|
|
|
} elseif ($state === 2) { |
136
|
|
|
$ret->dest = $token->value; |
137
|
|
|
++$list->idx; |
138
|
3 |
|
break; |
139
|
|
|
} |
140
|
3 |
|
} |
141
|
2 |
|
|
142
|
2 |
|
--$list->idx; |
143
|
2 |
|
return $ret; |
144
|
|
|
} |
145
|
1 |
|
|
146
|
|
|
/** |
147
|
|
|
* @param IntoKeyword $component The component to be built. |
148
|
|
|
* @param array $options Parameters for building. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public static function build($component, array $options = array()) |
153
|
|
|
{ |
154
|
|
|
if ($component->dest instanceof Expression) { |
155
|
|
|
$columns = !empty($component->columns) ? |
156
|
|
|
'(`' . implode('`, `', $component->columns) . '`)' : ''; |
157
|
|
|
return $component->dest . $columns; |
158
|
|
|
} elseif (isset($component->values)) { |
159
|
|
|
return ExpressionArray::build($component->values); |
|
|
|
|
160
|
|
|
} else { |
161
|
|
|
return 'OUTFILE "' . $component->dest . '"'; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
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..