Skip to content

Commit

Permalink
Handle CASE alias without AS
Browse files Browse the repository at this point in the history
  • Loading branch information
mostertb committed Oct 13, 2018
1 parent 1a18d1c commit f2127a1
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/Components/CaseExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,6 @@ public static function parse(Parser $parser, TokensList $list, array $options =
*/
$type = 0;

/**
* Whether an alias is expected
*
* @bool
*/
$alias = false;

++$list->idx; // Skip 'CASE'

for (; $list->idx < $list->count; ++$list->idx) {
Expand Down Expand Up @@ -217,7 +210,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
);
} else {

// Parse alias for CASE statement
// Parse for alias of CASE expression
$asFound = false;
for (; $list->idx < $list->count; ++$list->idx) {
$token = $list->tokens[$list->idx];

Expand All @@ -228,39 +222,43 @@ public static function parse(Parser $parser, TokensList $list, array $options =
continue;
}

//
// Handle optional AS keyword before alias
if($token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'AS'){

if ($alias) {
if ($asFound || !empty($ret->alias)) {
$parser->error(
'An alias was expected.',
'Potential duplicate alias of CASE expression.',
$token
);
break;
}
$alias = true;
$asFound = true;
continue;
}

if ($alias){
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;
$alias = false;
$asFound = false;

continue;
}

break;
}
if ($alias) {
if ($asFound) {
$parser->error(
'An alias was expected.',
'An alias was expected after AS.',
$list->tokens[$list->idx - 1]
);
}
Expand Down

0 comments on commit f2127a1

Please sign in to comment.