From 53db3081bd9d0c84059bbc651abe865ca643f6f1 Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 19 Feb 2017 17:37:25 +0000 Subject: [PATCH 1/2] Fix for issue 233 comments not parsing when they have a quote Moved concat comments up the splitting order so the comment doesn't get parsed like the rest of the query --- src/PHPSQLParser/lexer/PHPSQLLexer.php | 7 ++- tests/cases/parser/issue233Test.php | 59 +++++++++++++++++++++++ tests/expected/parser/issue233.serialized | 1 + 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/cases/parser/issue233Test.php create mode 100644 tests/expected/parser/issue233.serialized diff --git a/src/PHPSQLParser/lexer/PHPSQLLexer.php b/src/PHPSQLParser/lexer/PHPSQLLexer.php index 4264b2a7..559aacc7 100644 --- a/src/PHPSQLParser/lexer/PHPSQLLexer.php +++ b/src/PHPSQLParser/lexer/PHPSQLLexer.php @@ -119,11 +119,11 @@ public function split($sql) { $tokens[] = $token; } + $tokens = $this->concatComments($tokens); $tokens = $this->concatEscapeSequences($tokens); $tokens = $this->balanceBackticks($tokens); $tokens = $this->concatColReferences($tokens); $tokens = $this->balanceParenthesis($tokens); - $tokens = $this->concatComments($tokens); $tokens = $this->concatUserDefinedVariables($tokens); $tokens = $this->concatScientificNotations($tokens); $tokens = $this->concatNegativeNumbers($tokens); @@ -272,6 +272,11 @@ protected function concatComments($tokens) { $inline = true; } + if (($comment === false) && (substr($token, 0, 1) === "#")) { + $comment = $i; + $inline = true; + } + if (($comment === false) && ($token === "/*")) { $comment = $i; $inline = false; diff --git a/tests/cases/parser/issue233Test.php b/tests/cases/parser/issue233Test.php new file mode 100644 index 00000000..2f46cd90 --- /dev/null +++ b/tests/cases/parser/issue233Test.php @@ -0,0 +1,59 @@ + + * @copyright 2010-2014 Justin Swanhart and André Rothe + * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) + * @version SVN: $Id$ + * + */ +namespace PHPSQLParser\Test\Parser; +use PHPUnit_Framework_TestCase; +use PHPSQLParser\PHPSQLParser; + +class issue233Test extends PHPUnit_Framework_TestCase +{ + public function testIssue233() + { + $sql="#Check parser doesn't break with single quotes + CREATE TABLE moomoo (cow VARCHAR(20));"; + + $parser = new PHPSQLParser($sql); + + $p = $parser->parsed; + $expected = getExpectedValue(dirname(__FILE__), 'issue233.serialized'); + $this->assertEquals($expected, $p, 'comment with single quote'); + } +} + diff --git a/tests/expected/parser/issue233.serialized b/tests/expected/parser/issue233.serialized new file mode 100644 index 00000000..f9f19702 --- /dev/null +++ b/tests/expected/parser/issue233.serialized @@ -0,0 +1 @@ +a:2:{s:6:"CREATE";a:4:{s:9:"expr_type";s:5:"table";s:10:"not-exists";b:0;s:9:"base_expr";s:5:"TABLE";s:8:"sub_tree";a:1:{i:0;a:2:{s:9:"expr_type";s:8:"reserved";s:9:"base_expr";s:5:"TABLE";}}}s:5:"TABLE";a:5:{s:9:"base_expr";s:6:"moomoo";s:4:"name";s:6:"moomoo";s:9:"no_quotes";a:2:{s:5:"delim";b:0;s:5:"parts";a:1:{i:0;s:6:"moomoo";}}s:10:"create-def";a:3:{s:9:"expr_type";s:18:"bracket_expression";s:9:"base_expr";s:18:" (cow VARCHAR(20))";s:8:"sub_tree";a:1:{i:0;a:3:{s:9:"expr_type";s:10:"column-def";s:9:"base_expr";s:15:"cow VARCHAR(20)";s:8:"sub_tree";a:2:{i:0;a:3:{s:9:"expr_type";s:6:"colref";s:9:"base_expr";s:3:"cow";s:9:"no_quotes";a:2:{s:5:"delim";b:0;s:5:"parts";a:1:{i:0;s:3:"cow";}}}i:1;a:7:{s:9:"expr_type";s:11:"column-type";s:9:"base_expr";s:11:"VARCHAR(20)";s:8:"sub_tree";a:2:{i:0;a:3:{s:9:"expr_type";s:9:"data-type";s:9:"base_expr";s:7:"VARCHAR";s:6:"length";s:2:"20";}i:1;a:3:{s:9:"expr_type";s:18:"bracket_expression";s:9:"base_expr";s:4:"(20)";s:8:"sub_tree";a:1:{i:0;a:2:{s:9:"expr_type";s:5:"const";s:9:"base_expr";s:2:"20";}}}}s:6:"unique";b:0;s:8:"nullable";b:1;s:8:"auto_inc";b:0;s:7:"primary";b:0;}}}}}s:7:"options";b:0;}} \ No newline at end of file From 5d607e4b310c4e128499a05cc737f0866f2c2361 Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 19 Feb 2017 19:18:28 +0000 Subject: [PATCH 2/2] Removed legacy comments from test --- tests/cases/parser/issue233Test.php | 40 +---------------------------- 1 file changed, 1 insertion(+), 39 deletions(-) diff --git a/tests/cases/parser/issue233Test.php b/tests/cases/parser/issue233Test.php index 2f46cd90..f82c0867 100644 --- a/tests/cases/parser/issue233Test.php +++ b/tests/cases/parser/issue233Test.php @@ -1,43 +1,5 @@ - * @copyright 2010-2014 Justin Swanhart and André Rothe - * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) - * @version SVN: $Id$ - * - */ + namespace PHPSQLParser\Test\Parser; use PHPUnit_Framework_TestCase; use PHPSQLParser\PHPSQLParser;