Skip to content

Commit

Permalink
Fixes striping of inline comments #11717
Browse files Browse the repository at this point in the history
Signed-off-by: Raghuram Vadapalli <raghuram.vadapalli@research.iiit.ac.in>
  • Loading branch information
Achilles-96 committed Apr 5, 2017
1 parent 6305be5 commit e52cabf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ composer.lock
coverage.xml
*~
.php_cs.cache
*sw[op]
8 changes: 5 additions & 3 deletions src/Utils/BufferedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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+
Expand Down Expand Up @@ -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;
}

Expand All @@ -259,20 +259,23 @@ 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] === '-')
&& ($this->query[$i + 1] === '-')
&& (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] === '/')
&& ($this->query[$i + 1] === '*')
&& ($this->query[$i + 2] !== '!')
) {
$this->status = static::STATUS_COMMENT_C;
$this->current .= $this->query[$i];
continue;
}

Expand All @@ -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.
Expand Down
26 changes: 25 additions & 1 deletion tests/Utils/BufferedQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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" .
Expand All @@ -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" .
Expand Down Expand Up @@ -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" .
Expand All @@ -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" .
Expand Down Expand Up @@ -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" .
Expand All @@ -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" .
Expand Down

0 comments on commit e52cabf

Please sign in to comment.