Skip to content

Commit 1a18d1c

Browse files
committed
Add Support for CASE ... END AS alias
1 parent 20ed994 commit 1a18d1c

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/Components/CaseExpression.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace PhpMyAdmin\SqlParser\Components;
88

99
use PhpMyAdmin\SqlParser\Component;
10+
use PhpMyAdmin\SqlParser\Context;
1011
use PhpMyAdmin\SqlParser\Parser;
1112
use PhpMyAdmin\SqlParser\Token;
1213
use PhpMyAdmin\SqlParser\TokensList;
@@ -55,6 +56,13 @@ class CaseExpression extends Component
5556
*/
5657
public $else_result;
5758

59+
/**
60+
* The alias of this CASE statement
61+
*
62+
* @var string
63+
*/
64+
public $alias;
65+
5866
/**
5967
* The sub-expression.
6068
*
@@ -94,6 +102,13 @@ public static function parse(Parser $parser, TokensList $list, array $options =
94102
*/
95103
$type = 0;
96104

105+
/**
106+
* Whether an alias is expected
107+
*
108+
* @bool
109+
*/
110+
$alias = false;
111+
97112
++$list->idx; // Skip 'CASE'
98113

99114
for (; $list->idx < $list->count; ++$list->idx) {
@@ -201,6 +216,56 @@ public static function parse(Parser $parser, TokensList $list, array $options =
201216
$list->tokens[$list->idx - 1]
202217
);
203218
} else {
219+
220+
// Parse alias for CASE statement
221+
for (; $list->idx < $list->count; ++$list->idx) {
222+
$token = $list->tokens[$list->idx];
223+
224+
// Skipping whitespaces and comments.
225+
if (($token->type === Token::TYPE_WHITESPACE)
226+
|| ($token->type === Token::TYPE_COMMENT)
227+
) {
228+
continue;
229+
}
230+
231+
//
232+
if($token->type === Token::TYPE_KEYWORD
233+
&& $token->keyword === 'AS'){
234+
235+
if ($alias) {
236+
$parser->error(
237+
'An alias was expected.',
238+
$token
239+
);
240+
break;
241+
}
242+
$alias = true;
243+
continue;
244+
}
245+
246+
if ($alias){
247+
248+
// An alias is expected (the keyword `AS` was previously found).
249+
if (!empty($ret->alias)) {
250+
$parser->error('An alias was previously found.', $token);
251+
break;
252+
}
253+
$ret->alias = $token->value;
254+
$alias = false;
255+
256+
continue;
257+
}
258+
259+
break;
260+
}
261+
if ($alias) {
262+
$parser->error(
263+
'An alias was expected.',
264+
$list->tokens[$list->idx - 1]
265+
);
266+
}
267+
268+
204269
$ret->expr = self::build($ret);
205270
}
206271

@@ -241,6 +306,10 @@ public static function build($component, array $options = array())
241306
}
242307
$ret .= 'END';
243308

309+
if ($component->alias) {
310+
$ret .= ' AS ' . Context::escape($component->alias);
311+
}
312+
244313
return $ret;
245314
}
246315
}

0 commit comments

Comments
 (0)