Skip to content

Commit 52f90e0

Browse files
elliot2extremewilliamdes
authored andcommitted
Added check for quoted keyword to fix bug 317
1 parent 550ce69 commit 52f90e0

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

src/Components/AlterOperation.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -277,27 +277,29 @@ public static function parse(Parser $parser, TokensList $list, array $options =
277277
} elseif (($token->value === ',') && ($brackets === 0)) {
278278
break;
279279
}
280-
} elseif (! empty(Parser::$STATEMENT_PARSERS[$token->value])) {
281-
// We have reached the end of ALTER operation and suddenly found
282-
// a start to new statement, but have not find a delimiter between them
280+
} elseif (! self::checkIfTokenQuotedSymbol($token)) {
281+
if (! empty(Parser::$STATEMENT_PARSERS[$token->value])) {
282+
// We have reached the end of ALTER operation and suddenly found
283+
// a start to new statement, but have not find a delimiter between them
283284

284-
if (! ($token->value === 'SET' && $list->tokens[$list->idx - 1]->value === 'CHARACTER')) {
285+
if (! ($token->value === 'SET' && $list->tokens[$list->idx - 1]->value === 'CHARACTER')) {
286+
$parser->error(
287+
'A new statement was found, but no delimiter between it and the previous one.',
288+
$token
289+
);
290+
break;
291+
}
292+
} elseif ((array_key_exists($array_key, self::$DB_OPTIONS)
293+
|| array_key_exists($array_key, self::$TABLE_OPTIONS))
294+
&& ! self::checkIfColumnDefinitionKeyword($array_key)
295+
) {
296+
// This alter operation has finished, which means a comma was missing before start of new alter operation
285297
$parser->error(
286-
'A new statement was found, but no delimiter between it and the previous one.',
298+
'Missing comma before start of a new alter operation.',
287299
$token
288300
);
289301
break;
290302
}
291-
} elseif ((array_key_exists($array_key, self::$DB_OPTIONS)
292-
|| array_key_exists($array_key, self::$TABLE_OPTIONS))
293-
&& ! self::checkIfColumnDefinitionKeyword($array_key)
294-
) {
295-
// This alter operation has finished, which means a comma was missing before start of new alter operation
296-
$parser->error(
297-
'Missing comma before start of a new alter operation.',
298-
$token
299-
);
300-
break;
301303
}
302304
$ret->unknown[] = $token;
303305
}
@@ -356,4 +358,14 @@ private static function checkIfColumnDefinitionKeyword($tokenValue)
356358
// both table as well as a specific column in the table
357359
return in_array($tokenValue, $common_options);
358360
}
361+
362+
/**
363+
* Check if token is symbol and quoted with backtick
364+
*
365+
* @param Token $token token to check
366+
* @return bool
367+
*/
368+
private static function checkIfTokenQuotedSymbol($token) {
369+
return $token->type === Token::TYPE_SYMBOL && $token->flags === Token::FLAG_SYMBOL_BACKTICK;
370+
}
359371
}

tests/Misc/BugsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function bugProvider()
2222
array('bugs/gh9'),
2323
array('bugs/gh14'),
2424
array('bugs/gh16'),
25+
array('bugs/gh317'),
2526
array('bugs/pma11800'),
2627
array('bugs/pma11836'),
2728
array('bugs/pma11843'),

tests/data/bugs/gh317.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE `demo` ADD KEY `IDX_REPAIR` (`REPAIR`);

tests/data/bugs/gh317.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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 commit comments

Comments
 (0)