Skip to content

Commit

Permalink
Merge pull request #235 from dbould/fix-issue233
Browse files Browse the repository at this point in the history
Fix for issue 233 comments not parsing when they have a quote
  • Loading branch information
greenlion committed May 16, 2017
2 parents 06e0611 + 5d607e4 commit 41935eb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/PHPSQLParser/lexer/PHPSQLLexer.php
Expand Up @@ -87,11 +87,11 @@ public function split($sql) {

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

$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);
Expand Down Expand Up @@ -240,6 +240,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;
Expand Down
21 changes: 21 additions & 0 deletions tests/cases/parser/issue233Test.php
@@ -0,0 +1,21 @@
<?php

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');
}
}

1 change: 1 addition & 0 deletions 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;}}

0 comments on commit 41935eb

Please sign in to comment.