Skip to content

Commit 41935eb

Browse files
authored
Merge pull request #235 from dbould/fix-issue233
Fix for issue 233 comments not parsing when they have a quote
2 parents 06e0611 + 5d607e4 commit 41935eb

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/PHPSQLParser/lexer/PHPSQLLexer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ public function split($sql) {
8787

8888
$tokens = preg_split($this->splitters->getSplittersRegexPattern(), $sql, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
8989

90+
$tokens = $this->concatComments($tokens);
9091
$tokens = $this->concatEscapeSequences($tokens);
9192
$tokens = $this->balanceBackticks($tokens);
9293
$tokens = $this->concatColReferences($tokens);
9394
$tokens = $this->balanceParenthesis($tokens);
94-
$tokens = $this->concatComments($tokens);
9595
$tokens = $this->concatUserDefinedVariables($tokens);
9696
$tokens = $this->concatScientificNotations($tokens);
9797
$tokens = $this->concatNegativeNumbers($tokens);
@@ -240,6 +240,11 @@ protected function concatComments($tokens) {
240240
$inline = true;
241241
}
242242

243+
if (($comment === false) && (substr($token, 0, 1) === "#")) {
244+
$comment = $i;
245+
$inline = true;
246+
}
247+
243248
if (($comment === false) && ($token === "/*")) {
244249
$comment = $i;
245250
$inline = false;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace PHPSQLParser\Test\Parser;
4+
use PHPUnit_Framework_TestCase;
5+
use PHPSQLParser\PHPSQLParser;
6+
7+
class issue233Test extends PHPUnit_Framework_TestCase
8+
{
9+
public function testIssue233()
10+
{
11+
$sql="#Check parser doesn't break with single quotes
12+
CREATE TABLE moomoo (cow VARCHAR(20));";
13+
14+
$parser = new PHPSQLParser($sql);
15+
16+
$p = $parser->parsed;
17+
$expected = getExpectedValue(dirname(__FILE__), 'issue233.serialized');
18+
$this->assertEquals($expected, $p, 'comment with single quote');
19+
}
20+
}
21+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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;}}

0 commit comments

Comments
 (0)