Skip to content

Commit f4ff382

Browse files
committed
Fixed parsing SQL comment at the end of query.
Fixes #156 Signed-off-by: Michal Čihař <michal@cihar.com>
1 parent 60daa81 commit f4ff382

File tree

6 files changed

+35
-16
lines changed

6 files changed

+35
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [Unreleased]
44

5+
* Fixed parsing SQL comment at the end of query.
6+
57
## [4.1.7] - 2017-06-06
68

79
* Fixed setting combination SQL Modes.

src/Context.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public static function isWhitespace($str)
307307
*
308308
* @return int the appropriate flag for the comment type
309309
*/
310-
public static function isComment($str)
310+
public static function isComment($str, $end=false)
311311
{
312312
$len = strlen($str);
313313
if ($str[0] === '#') {
@@ -321,6 +321,8 @@ public static function isComment($str)
321321
&& ($str[1] === '-') && (static::isWhitespace($str[2]))
322322
) {
323323
return Token::FLAG_COMMENT_SQL;
324+
} elseif (($len == 2) && $end && ($str[0] === '-') && ($str[1] === '-')) {
325+
return Token::FLAG_COMMENT_SQL;
324326
}
325327

326328
return null;

src/Lexer.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -602,23 +602,27 @@ public function parseComment()
602602
// SQL style comments. (-- comment\n)
603603
if (++$this->last < $this->len) {
604604
$token .= $this->str[$this->last];
605-
if (Context::isComment($token)) {
606-
// Checking if this comment did not end already (```--\n```).
607-
if ($this->str[$this->last] !== "\n") {
608-
while (
609-
++$this->last < $this->len
610-
&& $this->str[$this->last] !== "\n"
611-
) {
612-
$token .= $this->str[$this->last];
613-
}
614-
}
615-
// Include trailing \n as whitespace token
616-
if ($this->last < $this->len) {
617-
--$this->last;
605+
$end = false;
606+
} else {
607+
--$this->last;
608+
$end = true;
609+
}
610+
if (Context::isComment($token, $end)) {
611+
// Checking if this comment did not end already (```--\n```).
612+
if ($this->str[$this->last] !== "\n") {
613+
while (
614+
++$this->last < $this->len
615+
&& $this->str[$this->last] !== "\n"
616+
) {
617+
$token .= $this->str[$this->last];
618618
}
619-
620-
return new Token($token, Token::TYPE_COMMENT, Token::FLAG_COMMENT_SQL);
621619
}
620+
// Include trailing \n as whitespace token
621+
if ($this->last < $this->len) {
622+
--$this->last;
623+
}
624+
625+
return new Token($token, Token::TYPE_COMMENT, Token::FLAG_COMMENT_SQL);
622626
}
623627

624628
$this->last = $iBak;

tests/Lexer/LexerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function testLexProvider()
6363
array('lexer/lexUtf8'),
6464
array('lexer/lexBool'),
6565
array('lexer/lexComment'),
66+
array('lexer/lexCommentEnd'),
6667
array('lexer/lexDelimiter'),
6768
array('lexer/lexDelimiter2'),
6869
array('lexer/lexDelimiterErr1'),

tests/data/lexer/lexCommentEnd.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT 1
2+
-- comment
3+
--

tests/data/lexer/lexCommentEnd.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
a:4:{s:5:"query";s:22:"SELECT 1
2+
-- comment
3+
--";s:5:"lexer";O:26:"PhpMyAdmin\SqlParser\Lexer":8:{s:3:"str";s:22:"SELECT 1
4+
-- comment
5+
--";s:3:"len";i:22;s:4:"last";i:23;s:4:"list";O:31:"PhpMyAdmin\SqlParser\TokensList":3:{s:6:"tokens";a:8:{i:0;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:0;}i:1;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:6;}i:2;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:7;}i:3;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:"
6+
";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:8;}i:4;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:10:"-- comment";s:5:"value";s:10:"-- comment";s:7:"keyword";N;s:4:"type";i:4;s:5:"flags";i:4;s:8:"position";i:9;}i:5;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:"
7+
";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:19;}i:6;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:2:"--";s:5:"value";s:2:"--";s:7:"keyword";N;s:4:"type";i:4;s:5:"flags";i:4;s:8:"position";i:20;}i:7;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:8;s:3:"idx";i:0;}s:9:"delimiter";s:1:";";s:12:"delimiterLen";i:1;s:6:"strict";b:0;s:6:"errors";a:0:{}}s:6:"parser";N;s:6:"errors";a:2:{s:5:"lexer";a:0:{}s:6:"parser";a:0:{}}}

0 commit comments

Comments
 (0)