Skip to content

Commit 53db308

Browse files
committed
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
1 parent 45227af commit 53db308

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

src/PHPSQLParser/lexer/PHPSQLLexer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ public function split($sql) {
119119
$tokens[] = $token;
120120
}
121121

122+
$tokens = $this->concatComments($tokens);
122123
$tokens = $this->concatEscapeSequences($tokens);
123124
$tokens = $this->balanceBackticks($tokens);
124125
$tokens = $this->concatColReferences($tokens);
125126
$tokens = $this->balanceParenthesis($tokens);
126-
$tokens = $this->concatComments($tokens);
127127
$tokens = $this->concatUserDefinedVariables($tokens);
128128
$tokens = $this->concatScientificNotations($tokens);
129129
$tokens = $this->concatNegativeNumbers($tokens);
@@ -272,6 +272,11 @@ protected function concatComments($tokens) {
272272
$inline = true;
273273
}
274274

275+
if (($comment === false) && (substr($token, 0, 1) === "#")) {
276+
$comment = $i;
277+
$inline = true;
278+
}
279+
275280
if (($comment === false) && ($token === "/*")) {
276281
$comment = $i;
277282
$inline = false;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* issue95.php
4+
*
5+
* Test case for PHPSQLParser.
6+
*
7+
* PHP version 5
8+
*
9+
* LICENSE:
10+
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
11+
* All rights reserved.
12+
*
13+
* Redistribution and use in source and binary forms, with or without
14+
* modification, are permitted provided that the following conditions
15+
* are met:
16+
* 1. Redistributions of source code must retain the above copyright
17+
* notice, this list of conditions and the following disclaimer.
18+
* 2. Redistributions in binary form must reproduce the above copyright
19+
* notice, this list of conditions and the following disclaimer in the
20+
* documentation and/or other materials provided with the distribution.
21+
* 3. The name of the author may not be used to endorse or promote products
22+
* derived from this software without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*
35+
* @author André Rothe <andre.rothe@phosco.info>
36+
* @copyright 2010-2014 Justin Swanhart and André Rothe
37+
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
38+
* @version SVN: $Id$
39+
*
40+
*/
41+
namespace PHPSQLParser\Test\Parser;
42+
use PHPUnit_Framework_TestCase;
43+
use PHPSQLParser\PHPSQLParser;
44+
45+
class issue233Test extends PHPUnit_Framework_TestCase
46+
{
47+
public function testIssue233()
48+
{
49+
$sql="#Check parser doesn't break with single quotes
50+
CREATE TABLE moomoo (cow VARCHAR(20));";
51+
52+
$parser = new PHPSQLParser($sql);
53+
54+
$p = $parser->parsed;
55+
$expected = getExpectedValue(dirname(__FILE__), 'issue233.serialized');
56+
$this->assertEquals($expected, $p, 'comment with single quote');
57+
}
58+
}
59+
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)