diff options
author | Brad Mostert <mostertb@users.noreply.github.com> | 2018-10-13 14:41:15 +0200 |
---|---|---|
committer | Brad Mostert <mostertb@users.noreply.github.com> | 2018-10-13 14:41:15 +0200 |
commit | f2127a175835f25e4936bb08698a812841a3915d (patch) | |
tree | f2c593e86affbae74db946730c6e2faee7b5f853 | |
parent | 1a18d1c5bad164cb0d2e4584578a76273af7feaf (diff) | |
download | sql-parser-f2127a175835f25e4936bb08698a812841a3915d.zip sql-parser-f2127a175835f25e4936bb08698a812841a3915d.tar.gz sql-parser-f2127a175835f25e4936bb08698a812841a3915d.tar.bz2 |
Handle CASE alias without AS
-rw-r--r-- | src/Components/CaseExpression.php | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/src/Components/CaseExpression.php b/src/Components/CaseExpression.php index a256f89..364b7d7 100644 --- a/src/Components/CaseExpression.php +++ b/src/Components/CaseExpression.php @@ -102,13 +102,6 @@ class CaseExpression extends Component */ $type = 0; - /** - * Whether an alias is expected - * - * @bool - */ - $alias = false; - ++$list->idx; // Skip 'CASE' for (; $list->idx < $list->count; ++$list->idx) { @@ -217,7 +210,8 @@ class CaseExpression extends Component ); } 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]; @@ -228,22 +222,26 @@ class CaseExpression extends Component 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)) { @@ -251,16 +249,16 @@ class CaseExpression extends Component 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] ); } |