Skip to content

Commit

Permalink
BaseDriver: fix parsing of SQL with trailing comment without EOL
Browse files Browse the repository at this point in the history
[closes #102]
  • Loading branch information
JanTvrdik committed Jun 23, 2019
1 parent 82da016 commit 85b4101
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Drivers/BaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function loadFile($path)
$parseOffset = 0;
$queries = 0;

$space = "(?:\\s|/\\*.*\\*/|(?:#|-- )[^\\n]*\\n|--\\n)";
$space = "(?:\\s|/\\*.*\\*/|(?:#|-- )[^\\n]*(?:\\n|\\z)|--(?:\\n|\\z))";
$spacesRe = "~\\G{$space}*\\z~";
$delimiter = ';';
$delimiterRe = "~\\G{$space}*DELIMITER\\s+(\\S+)~i";
Expand Down Expand Up @@ -106,6 +106,10 @@ public function loadFile($path)
$endRe = isset($endReTable[$found]) ? $endReTable[$found] : '(' . (preg_match('~^-- |^#~', $found) ? "\n" : preg_quote($found) . "|\\\\.") . '|\z)s';
while (preg_match($endRe, $content, $match, PREG_OFFSET_CAPTURE, $parseOffset)) { //! respect sql_mode NO_BACKSLASH_ESCAPES
$s = $match[0][0];
if (strlen($s) === 0) {
break 3;
}

$parseOffset = $match[0][1] + strlen($s);
if ($s[0] !== '\\') {
continue 2;
Expand Down
19 changes: 18 additions & 1 deletion tests/cases/unit/BaseDriverTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,24 @@ class BaseDriverTest extends Tester\TestCase
"\nCREATE TRIGGER `users_bu` BEFORE UPDATE ON `users` FOR EACH ROW BEGIN SELECT 1; END; ",
"\nSELECT 2",
]
]
],
[
'-- ', [],
],
[
"--\n", [],
],
[
"SELECT 1;\n--", [
'SELECT 1'
],
],
[
"SELECT 1;\n--\nSELECT 2;", [
'SELECT 1',
"\n--\nSELECT 2",
],
],
];
}
}
Expand Down

0 comments on commit 85b4101

Please sign in to comment.