summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaghuram <raghuram.vadapalli@research.iiit.ac.in>2017-04-05 22:00:53 +0530
committerRaghuram <raghuram.vadapalli@research.iiit.ac.in>2017-04-05 22:02:55 +0530
commite52cabfaa0bd7d51fa3545c45f8d7fd804d379ba (patch)
tree8acd43fa8b0079cc753e4c7effbccb6370d5e7db
parent6305be565414a4471133c251791edc4b31e97a4f (diff)
downloadsql-parser-e52cabfaa0bd7d51fa3545c45f8d7fd804d379ba.zip
sql-parser-e52cabfaa0bd7d51fa3545c45f8d7fd804d379ba.tar.gz
sql-parser-e52cabfaa0bd7d51fa3545c45f8d7fd804d379ba.tar.bz2
Fixes striping of inline comments #11717
Signed-off-by: Raghuram Vadapalli <raghuram.vadapalli@research.iiit.ac.in>
-rw-r--r--.gitignore1
-rw-r--r--src/Utils/BufferedQuery.php8
-rw-r--r--tests/Utils/BufferedQueryTest.php26
3 files changed, 31 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index 95d490a..31b4aff 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 acd11e0..b333fa3 100644
--- a/src/Utils/BufferedQuery.php
+++ b/src/Utils/BufferedQuery.php
@@ -15,8 +15,6 @@ use PhpMyAdmin\SqlParser\Context;
* 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 @@ class BufferedQuery
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 @@ class BufferedQuery
*/
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 @@ class BufferedQuery
&& (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 @@ class BufferedQuery
&& ($this->query[$i + 2] !== '!')
) {
$this->status = static::STATUS_COMMENT_C;
+ $this->current .= $this->query[$i];
continue;
}
@@ -299,7 +302,6 @@ class BufferedQuery
&& (($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 9ef539d..182c1ab 100644
--- a/tests/Utils/BufferedQueryTest.php
+++ b/tests/Utils/BufferedQueryTest.php
@@ -185,6 +185,7 @@ class BufferedQueryTest extends TestCase
'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 @@ class BufferedQueryTest extends TestCase
' 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 @@ class BufferedQueryTest extends TestCase
'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 @@ class BufferedQueryTest extends TestCase
'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 @@ class BufferedQueryTest extends TestCase
'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 @@ class BufferedQueryTest extends TestCase
' 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" .