From e52cabfaa0bd7d51fa3545c45f8d7fd804d379ba Mon Sep 17 00:00:00 2001 From: Raghuram Date: Wed, 5 Apr 2017 22:00:53 +0530 Subject: [PATCH] Fixes striping of inline comments #11717 Signed-off-by: Raghuram Vadapalli --- .gitignore | 1 + src/Utils/BufferedQuery.php | 8 +++++--- tests/Utils/BufferedQueryTest.php | 26 +++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 95d490ae9..31b4affdf 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ composer.lock coverage.xml *~ .php_cs.cache +*sw[op] diff --git a/src/Utils/BufferedQuery.php b/src/Utils/BufferedQuery.php index acd11e0a6..b333fa340 100644 --- a/src/Utils/BufferedQuery.php +++ b/src/Utils/BufferedQuery.php @@ -15,8 +15,6 @@ * that are being buffered. After each statement has been extracted, a lexer or * a parser may be used. * - * All comments are skipped, with one exception: MySQL commands inside `/*!`. - * * @category Lexer * * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ @@ -228,12 +226,14 @@ public function extract($end = false) if ($this->query[$i] === "\n") { $this->status = 0; } + $this->current .= $this->query[$i]; continue; } elseif ($this->status === static::STATUS_COMMENT_C) { // C-like comments end in */. if (($this->query[$i - 1] === '*') && ($this->query[$i] === '/')) { $this->status = 0; } + $this->current .= $this->query[$i]; continue; } @@ -259,6 +259,7 @@ public function extract($end = false) */ if ($this->query[$i] === '#') { $this->status = static::STATUS_COMMENT_BASH; + $this->current .= $this->query[$i]; continue; } elseif (($i + 2 < $len) && ($this->query[$i] === '-') @@ -266,6 +267,7 @@ public function extract($end = false) && (Context::isWhitespace($this->query[$i + 2])) ) { $this->status = static::STATUS_COMMENT_SQL; + $this->current .= $this->query[$i]; continue; } elseif (($i + 2 < $len) && ($this->query[$i] === '/') @@ -273,6 +275,7 @@ public function extract($end = false) && ($this->query[$i + 2] !== '!') ) { $this->status = static::STATUS_COMMENT_C; + $this->current .= $this->query[$i]; continue; } @@ -299,7 +302,6 @@ public function extract($end = false) && (($this->query[$i + 7] === 'E') || ($this->query[$i + 7] === 'e')) && (($this->query[$i + 8] === 'R') || ($this->query[$i + 8] === 'r')) && (Context::isWhitespace($this->query[$i + 9])) - && (trim($this->current) === '') ) { // Saving the current index to be able to revert any parsing // done in this block. diff --git a/tests/Utils/BufferedQueryTest.php b/tests/Utils/BufferedQueryTest.php index 9ef539d0b..182c1ab5f 100644 --- a/tests/Utils/BufferedQueryTest.php +++ b/tests/Utils/BufferedQueryTest.php @@ -185,6 +185,7 @@ public function testExtractProvider() 'SET time_zone = "+00:00"', + '# Bash-like comment sytanx.' . "\n" . 'CREATE DEFINER=`root`@`localhost` PROCEDURE `film_in_stock` (IN `p_film_id` INT, IN `p_store_id` INT, OUT `p_film_count` INT) READS SQL DATA' . "\n" . 'BEGIN' . "\n" . ' SELECT inventory_id' . "\n" . @@ -196,6 +197,13 @@ public function testExtractProvider() ' SELECT FOUND_ROWS() INTO p_film_count;' . "\n" . 'END', + '-- --------------------------------------------------------' . "\n" . + '' . "\n" . + '--' . "\n" . + '-- Table structure for `actor`' . "\n" . + '--' . "\n" . + '' . "\n" . + '/* C-like comment syntax. */' . "\n" . 'CREATE TABLE IF NOT EXISTS `actor` (' . "\n" . '`actor_id` SMALLINT(5) UNSIGNED NOT NULL,' . "\n" . '`first_name` VARCHAR(45) NOT NULL,' . "\n" . @@ -231,8 +239,9 @@ public function testExtractProvider() 'SET time_zone = "+00:00"', - 'DELIMITER $$', + '/* a comment */ DELIMITER $$', + '# Bash-like comment sytanx.' . "\n" . 'CREATE DEFINER=`root`@`localhost` PROCEDURE `film_in_stock` (IN `p_film_id` INT, IN `p_store_id` INT, OUT `p_film_count` INT) READS SQL DATA' . "\n" . 'BEGIN' . "\n" . ' SELECT inventory_id' . "\n" . @@ -246,6 +255,13 @@ public function testExtractProvider() 'DELIMITER ;', + '-- --------------------------------------------------------' . "\n" . + '' . "\n" . + '--' . "\n" . + '-- Table structure for `actor`' . "\n" . + '--' . "\n" . + '' . "\n" . + '/* C-like comment syntax. */' . "\n" . 'CREATE TABLE IF NOT EXISTS `actor` (' . "\n" . '`actor_id` SMALLINT(5) UNSIGNED NOT NULL,' . "\n" . '`first_name` VARCHAR(45) NOT NULL,' . "\n" . @@ -281,6 +297,7 @@ public function testExtractProvider() 'SET time_zone = "+00:00";', + '# Bash-like comment sytanx.' . "\n" . 'CREATE DEFINER=`root`@`localhost` PROCEDURE `film_in_stock` (IN `p_film_id` INT, IN `p_store_id` INT, OUT `p_film_count` INT) READS SQL DATA' . "\n" . 'BEGIN' . "\n" . ' SELECT inventory_id' . "\n" . @@ -292,6 +309,13 @@ public function testExtractProvider() ' SELECT FOUND_ROWS() INTO p_film_count;' . "\n" . 'END$$', + '-- --------------------------------------------------------' . "\n" . + '' . "\n" . + '--' . "\n" . + '-- Table structure for `actor`' . "\n" . + '--' . "\n" . + '' . "\n" . + '/* C-like comment syntax. */' . "\n" . 'CREATE TABLE IF NOT EXISTS `actor` (' . "\n" . '`actor_id` SMALLINT(5) UNSIGNED NOT NULL,' . "\n" . '`first_name` VARCHAR(45) NOT NULL,' . "\n" .