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
|
<?php
/**
* Parses a reference to a CASE expression.
*/
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
/**
* Parses a reference to a CASE expression.
*/
class CaseExpression extends Component
{
/**
* The value to be compared.
*
* @var Expression
*/
public $value;
/**
* The conditions in WHEN clauses.
*
* @var array
*/
public $conditions = [];
/**
* The results matching with the WHEN clauses.
*
* @var array
*/
public $results = [];
/**
* The values to be compared against.
*
* @var array
*/
public $compare_values = [];
/**
* The result in ELSE section of expr.
*
* @var Expression
*/
public $else_result;
/**
* The alias of this CASE statement.
*
* @var string
*/
public $alias;
/**
* The sub-expression.
*
* @var string
*/
public $expr = '';
public function __construct()
{
}
/**
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array $options parameters for parsing
*
* @return CaseExpression
*/
public static function parse(Parser $parser, TokensList $list, array $options = [])
{
$ret = new static();
/**
* State of parser.
*
* @var int
*/
$state = 0;
/**
* Syntax type (type 0 or type 1).
*
* @var int
*/
$type = 0;
++$list->idx; // Skip 'CASE'
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*
* @var Token
*/
$token = $list->tokens[$list->idx];
// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE)
|| ($token->type === Token::TYPE_COMMENT)
) {
continue;
}
if ($state === 0) {
if ($token->type === Token::TYPE_KEYWORD) {
switch ($token->keyword) {
case 'WHEN':
++$list->idx; // Skip 'WHEN'
$new_condition = Condition::parse($parser, $list);
$type = 1;
$state = 1;
$ret->conditions[] = $new_condition;
break;
case 'ELSE':
++$list->idx; // Skip 'ELSE'
$ret->else_result = Expression::parse($parser, $list);
$state = 0; // last clause of CASE expression
break;
case 'END':
$state = 3; // end of CASE expression
++$list->idx;
break 2;
default:
$parser->error('Unexpected keyword.', $token);
break 2;
}
} else {
$ret->value = Expression::parse($parser, $list);
$type = 0;
$state = 1;
}
} elseif ($state === 1) {
if ($type === 0) {
if ($token->type === Token::TYPE_KEYWORD) {
switch ($token->keyword) {
case 'WHEN':
++$list->idx; // Skip 'WHEN'
$new_value = Expression::parse($parser, $list);
$state = 2;
$ret->compare_values[] = $new_value;
break;
case 'ELSE':
++$list->idx; // Skip 'ELSE'
$ret->else_result = Expression::parse($parser, $list);
$state = 0; // last clause of CASE expression
break;
case 'END':
$state = 3; // end of CASE expression
++$list->idx;
break 2;
default:
$parser->error('Unexpected keyword.', $token);
break 2;
}
}
} else {
if ($token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'THEN'
) {
++$list->idx; // Skip 'THEN'
$new_result = Expression::parse($parser, $list);
$state = 0;
$ret->results[] = $new_result;
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error('Unexpected keyword.', $token);
break;
}
}
} elseif ($state === 2) {
if ($type === 0) {
if ($token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'THEN'
) {
++$list->idx; // Skip 'THEN'
$new_result = Expression::parse($parser, $list);
$ret->results[] = $new_result;
$state = 1;
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error('Unexpected keyword.', $token);
break;
}
}
}
}
if ($state !== 3) {
$parser->error(
'Unexpected end of CASE expression',
$list->tokens[$list->idx - 1]
);
} else {
// Parse for alias of CASE expression
$asFound = false;
for (; $list->idx < $list->count; ++$list->idx) {
$token = $list->tokens[$list->idx];
// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
break;
}
// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE)
|| ($token->type === Token::TYPE_COMMENT)
) {
continue;
}
// Handle optional AS keyword before alias
if ($token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'AS') {
if ($asFound || ! empty($ret->alias)) {
$parser->error('Potential duplicate alias of CASE expression.', $token);
break;
}
$asFound = true;
continue;
}
if ($asFound
&& $token->type === Token::TYPE_KEYWORD
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED || $token->flags & Token::FLAG_KEYWORD_FUNCTION)) {
$parser->error('An alias expected after AS but got ' . $token->value, $token);
$asFound = false;
break;
}
if ($asFound
|| $token->type === Token::TYPE_STRING
|| ($token->type === Token::TYPE_SYMBOL && ! $token->flags & Token::FLAG_SYMBOL_VARIABLE)
|| $token->type === Token::TYPE_NONE
) {
// An alias is expected (the keyword `AS` was previously found).
if (! empty($ret->alias)) {
$parser->error('An alias was previously found.', $token);
break;
}
$ret->alias = $token->value;
$asFound = false;
continue;
}
break;
}
if ($asFound) {
$parser->error('An alias was expected after AS.', $list->tokens[$list->idx - 1]);
}
$ret->expr = self::build($ret);
}
--$list->idx;
return $ret;
}
/**
* @param CaseExpression $component the component to be built
* @param array $options parameters for building
*
* @return string
*/
public static function build($component, array $options = [])
{
$ret = 'CASE ';
if (isset($component->value)) {
// Syntax type 0
$ret .= $component->value . ' ';
$val_cnt = count($component->compare_values);
$res_cnt = count($component->results);
for ($i = 0; $i < $val_cnt && $i < $res_cnt; ++$i) {
$ret .= 'WHEN ' . $component->compare_values[$i] . ' ';
$ret .= 'THEN ' . $component->results[$i] . ' ';
}
} else {
// Syntax type 1
$val_cnt = count($component->conditions);
$res_cnt = count($component->results);
for ($i = 0; $i < $val_cnt && $i < $res_cnt; ++$i) {
$ret .= 'WHEN ' . Condition::build($component->conditions[$i]) . ' ';
$ret .= 'THEN ' . $component->results[$i] . ' ';
}
}
if (isset($component->else_result)) {
$ret .= 'ELSE ' . $component->else_result . ' ';
}
$ret .= 'END';
if ($component->alias) {
$ret .= ' AS ' . Context::escape($component->alias);
}
return $ret;
}
}
|