Skip to content

Commit

Permalink
Added check for quoted keyword to fix bug 317
Browse files Browse the repository at this point in the history
  • Loading branch information
elliot2extreme authored and williamdes committed Nov 4, 2020
1 parent 550ce69 commit 52f90e0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/Components/AlterOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,27 +277,29 @@ public static function parse(Parser $parser, TokensList $list, array $options =
} elseif (($token->value === ',') && ($brackets === 0)) {
break;
}
} elseif (! empty(Parser::$STATEMENT_PARSERS[$token->value])) {
// We have reached the end of ALTER operation and suddenly found
// a start to new statement, but have not find a delimiter between them
} elseif (! self::checkIfTokenQuotedSymbol($token)) {
if (! empty(Parser::$STATEMENT_PARSERS[$token->value])) {
// We have reached the end of ALTER operation and suddenly found
// a start to new statement, but have not find a delimiter between them

if (! ($token->value === 'SET' && $list->tokens[$list->idx - 1]->value === 'CHARACTER')) {
if (! ($token->value === 'SET' && $list->tokens[$list->idx - 1]->value === 'CHARACTER')) {
$parser->error(
'A new statement was found, but no delimiter between it and the previous one.',
$token
);
break;
}
} elseif ((array_key_exists($array_key, self::$DB_OPTIONS)
|| array_key_exists($array_key, self::$TABLE_OPTIONS))
&& ! self::checkIfColumnDefinitionKeyword($array_key)
) {
// This alter operation has finished, which means a comma was missing before start of new alter operation
$parser->error(
'A new statement was found, but no delimiter between it and the previous one.',
'Missing comma before start of a new alter operation.',
$token
);
break;
}
} elseif ((array_key_exists($array_key, self::$DB_OPTIONS)
|| array_key_exists($array_key, self::$TABLE_OPTIONS))
&& ! self::checkIfColumnDefinitionKeyword($array_key)
) {
// This alter operation has finished, which means a comma was missing before start of new alter operation
$parser->error(
'Missing comma before start of a new alter operation.',
$token
);
break;
}
$ret->unknown[] = $token;
}
Expand Down Expand Up @@ -356,4 +358,14 @@ private static function checkIfColumnDefinitionKeyword($tokenValue)
// both table as well as a specific column in the table
return in_array($tokenValue, $common_options);
}

/**
* Check if token is symbol and quoted with backtick
*
* @param Token $token token to check
* @return bool
*/
private static function checkIfTokenQuotedSymbol($token) {
return $token->type === Token::TYPE_SYMBOL && $token->flags === Token::FLAG_SYMBOL_BACKTICK;
}
}
1 change: 1 addition & 0 deletions tests/Misc/BugsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function bugProvider()
array('bugs/gh9'),
array('bugs/gh14'),
array('bugs/gh16'),
array('bugs/gh317'),
array('bugs/pma11800'),
array('bugs/pma11836'),
array('bugs/pma11843'),
Expand Down
1 change: 1 addition & 0 deletions tests/data/bugs/gh317.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `demo` ADD KEY `IDX_REPAIR` (`REPAIR`);
1 change: 1 addition & 0 deletions tests/data/bugs/gh317.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a:4:{s:5:"query";s:51:"ALTER TABLE `demo` ADD KEY `IDX_REPAIR` (`REPAIR`);";s:5:"lexer";O:26:"PhpMyAdmin\SqlParser\Lexer":8:{s:3:"str";s:51:"ALTER TABLE `demo` ADD KEY `IDX_REPAIR` (`REPAIR`);";s:3:"len";i:51;s:4:"last";i:51;s:4:"list";O:31:"PhpMyAdmin\SqlParser\TokensList":3:{s:6:"tokens";a:17:{i:0;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:5:"ALTER";s:5:"value";s:5:"ALTER";s:7:"keyword";s:5:"ALTER";s:4:"type";i:1;s:5:"flags";i:3;s:8:"position";i:0;}i:1;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:5;}i:2;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:5:"TABLE";s:5:"value";s:5:"TABLE";s:7:"keyword";s:5:"TABLE";s:4:"type";i:1;s:5:"flags";i:3;s:8:"position";i:6;}i:3;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:11;}i:4;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:6:"`demo`";s:5:"value";s:4:"demo";s:7:"keyword";N;s:4:"type";i:8;s:5:"flags";i:2;s:8:"position";i:12;}i:5;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:18;}i:6;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:3:"ADD";s:5:"value";s:3:"ADD";s:7:"keyword";s:3:"ADD";s:4:"type";i:1;s:5:"flags";i:3;s:8:"position";i:19;}i:7;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:22;}i:8;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:3:"KEY";s:5:"value";s:3:"KEY";s:7:"keyword";s:3:"KEY";s:4:"type";i:1;s:5:"flags";i:19;s:8:"position";i:23;}i:9;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:26;}i:10;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:12:"`IDX_REPAIR`";s:5:"value";s:10:"IDX_REPAIR";s:7:"keyword";N;s:4:"type";i:8;s:5:"flags";i:2;s:8:"position";i:27;}i:11;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:39;}i:12;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:"(";s:5:"value";s:1:"(";s:7:"keyword";N;s:4:"type";i:2;s:5:"flags";i:16;s:8:"position";i:40;}i:13;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:8:"`REPAIR`";s:5:"value";s:6:"REPAIR";s:7:"keyword";N;s:4:"type";i:8;s:5:"flags";i:2;s:8:"position";i:41;}i:14;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:")";s:5:"value";s:1:")";s:7:"keyword";N;s:4:"type";i:2;s:5:"flags";i:16;s:8:"position";i:49;}i:15;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:";";s:5:"value";s:1:";";s:7:"keyword";N;s:4:"type";i:9;s:5:"flags";i:0;s:8:"position";i:50;}i:16;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";N;s:5:"value";N;s:7:"keyword";N;s:4:"type";i:9;s:5:"flags";i:0;s:8:"position";N;}}s:5:"count";i:17;s:3:"idx";i:17;}s:9:"delimiter";s:1:";";s:12:"delimiterLen";i:1;s:6:"strict";b:0;s:6:"errors";a:0:{}}s:6:"parser";O:27:"PhpMyAdmin\SqlParser\Parser":5:{s:4:"list";r:7;s:10:"statements";a:1:{i:0;O:46:"PhpMyAdmin\SqlParser\Statements\AlterStatement":5:{s:5:"table";O:42:"PhpMyAdmin\SqlParser\Components\Expression":7:{s:8:"database";N;s:5:"table";s:4:"demo";s:6:"column";N;s:4:"expr";s:6:"`demo`";s:5:"alias";N;s:8:"function";N;s:8:"subquery";N;}s:7:"altered";a:1:{i:0;O:46:"PhpMyAdmin\SqlParser\Components\AlterOperation":3:{s:7:"options";O:44:"PhpMyAdmin\SqlParser\Components\OptionsArray":1:{s:7:"options";a:2:{i:1;s:3:"ADD";i:2;s:3:"KEY";}}s:5:"field";O:42:"PhpMyAdmin\SqlParser\Components\Expression":7:{s:8:"database";N;s:5:"table";N;s:6:"column";s:10:"IDX_REPAIR";s:4:"expr";s:12:"`IDX_REPAIR`";s:5:"alias";N;s:8:"function";N;s:8:"subquery";N;}s:7:"unknown";a:3:{i:0;r:93;i:1;r:100;i:2;r:107;}}}s:7:"options";O:44:"PhpMyAdmin\SqlParser\Components\OptionsArray":1:{s:7:"options";a:1:{i:3;s:5:"TABLE";}}s:5:"first";i:0;s:4:"last";i:15;}}s:8:"brackets";i:0;s:6:"strict";b:0;s:6:"errors";a:0:{}}s:6:"errors";a:2:{s:5:"lexer";a:0:{}s:6:"parser";a:0:{}}}

0 comments on commit 52f90e0

Please sign in to comment.