From 402890bc7b74534f2150bba7dc40562bf436b085 Mon Sep 17 00:00:00 2001 From: Dan Ungureanu Date: Tue, 6 Oct 2015 23:04:34 +0300 Subject: [PATCH] Handle backslashes separately for BufferedQuery. --- src/Utils/BufferedQuery.php | 16 ++++++++++++++-- tests/Utils/BufferedQueryTest.php | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Utils/BufferedQuery.php b/src/Utils/BufferedQuery.php index 1da0b81df..7a2e5f437 100644 --- a/src/Utils/BufferedQuery.php +++ b/src/Utils/BufferedQuery.php @@ -186,19 +186,31 @@ public function extract($end = false) $loopLen = $end ? $len : $len - 16; for (; $i < $loopLen; ++$i) { + /** + * Handling backslash. + * + * Even if the next character is a special character that should be + * treated differently, because of the preceding backslash, it will + * be ignored. + */ + if ($this->query[$i] === '\\') { + $this->current .= $this->query[$i] . $this->query[++$i]; + continue; + } + /* * Handling special parses statuses. */ if ($this->status === static::STATUS_STRING_SINGLE_QUOTES) { // Single-quoted strings like 'foo'. - if (($this->query[$i - 1] != '\\') && ($this->query[$i] === '\'')) { + if ($this->query[$i] === '\'') { $this->status = 0; } $this->current .= $this->query[$i]; continue; } elseif ($this->status === static::STATUS_STRING_DOUBLE_QUOTES) { // Double-quoted strings like "bar". - if (($this->query[$i - 1] != '\\') && ($this->query[$i] === '"')) { + if ($this->query[$i] === '"') { $this->status = 0; } $this->current .= $this->query[$i]; diff --git a/tests/Utils/BufferedQueryTest.php b/tests/Utils/BufferedQueryTest.php index 185e9d27e..a6bf3ed2a 100644 --- a/tests/Utils/BufferedQueryTest.php +++ b/tests/Utils/BufferedQueryTest.php @@ -100,6 +100,20 @@ public function testExtractProvider() return array( + array( + 'SELECT """""""";' . + 'SELECT """\\\\"""', + 8, + array( + 'parse_delimiter' => true, + 'add_delimiter' => true, + ), + array( + 'SELECT """""""";', + 'SELECT """\\\\"""' + ) + ), + array( 'DELIMITER A_VERY_LONG_DEL' . "\n" . 'SELECT 1 A_VERY_LONG_DEL' . "\n" .