From fc25e9023c7748a7a2e957c823699900257ce4e1 Mon Sep 17 00:00:00 2001 From: Deven Bansod Date: Mon, 30 Dec 2019 00:51:48 -0500 Subject: [PATCH] 275: Prevent overwrite of offset in Limit clause by parenthesis --- src/Components/Limit.php | 5 +++++ tests/Builder/SelectStatementTest.php | 15 +++++++++++++-- tests/Parser/SelectStatementTest.php | 3 ++- tests/data/parser/parseSelectWithParenthesis.in | 1 + tests/data/parser/parseSelectWithParenthesis.out | 1 + 5 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/data/parser/parseSelectWithParenthesis.in create mode 100644 tests/data/parser/parseSelectWithParenthesis.out diff --git a/src/Components/Limit.php b/src/Components/Limit.php index 17423e23b..a3903b50c 100644 --- a/src/Components/Limit.php +++ b/src/Components/Limit.php @@ -95,6 +95,11 @@ public static function parse(Parser $parser, TokensList $list, array $options = continue; } + // Skip if not a number + if (($token->type !== Token::TYPE_NUMBER)) { + break; + } + if ($offset) { $ret->offset = $token->value; $offset = false; diff --git a/tests/Builder/SelectStatementTest.php b/tests/Builder/SelectStatementTest.php index bec4b536f..376ba07ba 100644 --- a/tests/Builder/SelectStatementTest.php +++ b/tests/Builder/SelectStatementTest.php @@ -91,7 +91,7 @@ public function testBuilderIntoOptions() ); } - public function testBuildGroupBy() + public function testBuilderGroupBy() { $query = 'SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country'; $parser = new Parser($query); @@ -103,7 +103,7 @@ public function testBuildGroupBy() ); } - public function testBuildIndexHint() + public function testBuilderIndexHint() { $query = 'SELECT * FROM address FORCE INDEX (idx_fk_city_id) IGNORE KEY FOR GROUP BY (a, b,c) WHERE city_id<0'; $parser = new Parser($query); @@ -114,4 +114,15 @@ public function testBuildIndexHint() $stmt->build() ); } + + public function testBuilderSurroundedByParanthesisWithLimit() { + $query = '(SELECT first_name FROM `actor` LIMIT 1, 2)'; + $parser = new Parser($query); + $stmt = $parser->statements[0]; + + $this->assertEquals( + 'SELECT first_name FROM `actor` LIMIT 1, 2', + $stmt->build() + ); + } } diff --git a/tests/Parser/SelectStatementTest.php b/tests/Parser/SelectStatementTest.php index b19b3cfe4..f4e1e906a 100644 --- a/tests/Parser/SelectStatementTest.php +++ b/tests/Parser/SelectStatementTest.php @@ -84,7 +84,8 @@ public function selectProvider() array('parser/parseSelectIndexHintErr1'), array('parser/parseSelectIndexHintErr2'), array('parser/parseSelectIndexHintErr3'), - array('parser/parseSelectIndexHintErr4') + array('parser/parseSelectIndexHintErr4'), + array('parser/parseSelectWithParenthesis') ); } } diff --git a/tests/data/parser/parseSelectWithParenthesis.in b/tests/data/parser/parseSelectWithParenthesis.in new file mode 100644 index 000000000..3c47d72e9 --- /dev/null +++ b/tests/data/parser/parseSelectWithParenthesis.in @@ -0,0 +1 @@ +(SELECT first_name FROM `actor` LIMIT 1, 2) \ No newline at end of file diff --git a/tests/data/parser/parseSelectWithParenthesis.out b/tests/data/parser/parseSelectWithParenthesis.out new file mode 100644 index 000000000..79e984d21 --- /dev/null +++ b/tests/data/parser/parseSelectWithParenthesis.out @@ -0,0 +1 @@ +a:4:{s:5:"query";s:43:"(SELECT first_name FROM `actor` LIMIT 1, 2)";s:5:"lexer";O:26:"PhpMyAdmin\SqlParser\Lexer":8:{s:3:"str";s:43:"(SELECT first_name FROM `actor` LIMIT 1, 2)";s:3:"len";i:43;s:4:"last";i:43;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:1:"(";s:5:"value";s:1:"(";s:7:"keyword";N;s:4:"type";i:2;s:5:"flags";i:16;s:8:"position";i:0;}i:1;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:6:"SELECT";s:5:"value";s:6:"SELECT";s:7:"keyword";s:6:"SELECT";s:4:"type";i:1;s:5:"flags";i:3;s:8:"position";i:1;}i:2;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:7;}i:3;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:10:"first_name";s:5:"value";s:10:"first_name";s:7:"keyword";N;s:4:"type";i:0;s:5:"flags";i:0;s:8:"position";i:8;}i:4;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:5;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:4:"FROM";s:5:"value";s:4:"FROM";s:7:"keyword";s:4:"FROM";s:4:"type";i:1;s:5:"flags";i:3;s:8:"position";i:19;}i:6;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:23;}i:7;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:7:"`actor`";s:5:"value";s:5:"actor";s:7:"keyword";N;s:4:"type";i:8;s:5:"flags";i:2;s:8:"position";i:24;}i:8;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:31;}i:9;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:5:"LIMIT";s:5:"value";s:5:"LIMIT";s:7:"keyword";s:5:"LIMIT";s:4:"type";i:1;s:5:"flags";i:3;s:8:"position";i:32;}i:10;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:37;}i:11;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:"1";s:5:"value";i:1;s:7:"keyword";N;s:4:"type";i:6;s:5:"flags";i:0;s:8:"position";i:38;}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:39;}i:13;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:40;}i:14;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:"2";s:5:"value";i:2;s:7:"keyword";N;s:4:"type";i:6;s:5:"flags";i:0;s:8:"position";i:41;}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:2;s:5:"flags";i:16;s:8:"position";i:42;}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:47:"PhpMyAdmin\SqlParser\Statements\SelectStatement":17:{s:4:"expr";a:1:{i:0;O:42:"PhpMyAdmin\SqlParser\Components\Expression":7:{s:8:"database";N;s:5:"table";N;s:6:"column";s:10:"first_name";s:4:"expr";s:10:"first_name";s:5:"alias";N;s:8:"function";N;s:8:"subquery";N;}}s:4:"from";a:1:{i:0;O:42:"PhpMyAdmin\SqlParser\Components\Expression":7:{s:8:"database";N;s:5:"table";s:5:"actor";s:6:"column";N;s:4:"expr";s:7:"`actor`";s:5:"alias";N;s:8:"function";N;s:8:"subquery";N;}}s:11:"index_hints";N;s:9:"partition";N;s:5:"where";N;s:5:"group";N;s:6:"having";N;s:5:"order";N;s:5:"limit";O:37:"PhpMyAdmin\SqlParser\Components\Limit":2:{s:6:"offset";i:1;s:8:"rowCount";i:2;}s:9:"procedure";N;s:4:"into";N;s:4:"join";N;s:5:"union";a:0:{}s:11:"end_options";N;s:7:"options";O:44:"PhpMyAdmin\SqlParser\Components\OptionsArray":1:{s:7:"options";a:0:{}}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:{}}} \ No newline at end of file