Skip to content

Commit e52cabf

Browse files
committed
Fixes striping of inline comments #11717
Signed-off-by: Raghuram Vadapalli <raghuram.vadapalli@research.iiit.ac.in>
1 parent 6305be5 commit e52cabf

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ composer.lock
55
coverage.xml
66
*~
77
.php_cs.cache
8+
*sw[op]

src/Utils/BufferedQuery.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
* that are being buffered. After each statement has been extracted, a lexer or
1616
* a parser may be used.
1717
*
18-
* All comments are skipped, with one exception: MySQL commands inside `/*!`.
19-
*
2018
* @category Lexer
2119
*
2220
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
@@ -228,12 +226,14 @@ public function extract($end = false)
228226
if ($this->query[$i] === "\n") {
229227
$this->status = 0;
230228
}
229+
$this->current .= $this->query[$i];
231230
continue;
232231
} elseif ($this->status === static::STATUS_COMMENT_C) {
233232
// C-like comments end in */.
234233
if (($this->query[$i - 1] === '*') && ($this->query[$i] === '/')) {
235234
$this->status = 0;
236235
}
236+
$this->current .= $this->query[$i];
237237
continue;
238238
}
239239

@@ -259,20 +259,23 @@ public function extract($end = false)
259259
*/
260260
if ($this->query[$i] === '#') {
261261
$this->status = static::STATUS_COMMENT_BASH;
262+
$this->current .= $this->query[$i];
262263
continue;
263264
} elseif (($i + 2 < $len)
264265
&& ($this->query[$i] === '-')
265266
&& ($this->query[$i + 1] === '-')
266267
&& (Context::isWhitespace($this->query[$i + 2]))
267268
) {
268269
$this->status = static::STATUS_COMMENT_SQL;
270+
$this->current .= $this->query[$i];
269271
continue;
270272
} elseif (($i + 2 < $len)
271273
&& ($this->query[$i] === '/')
272274
&& ($this->query[$i + 1] === '*')
273275
&& ($this->query[$i + 2] !== '!')
274276
) {
275277
$this->status = static::STATUS_COMMENT_C;
278+
$this->current .= $this->query[$i];
276279
continue;
277280
}
278281

@@ -299,7 +302,6 @@ public function extract($end = false)
299302
&& (($this->query[$i + 7] === 'E') || ($this->query[$i + 7] === 'e'))
300303
&& (($this->query[$i + 8] === 'R') || ($this->query[$i + 8] === 'r'))
301304
&& (Context::isWhitespace($this->query[$i + 9]))
302-
&& (trim($this->current) === '')
303305
) {
304306
// Saving the current index to be able to revert any parsing
305307
// done in this block.

tests/Utils/BufferedQueryTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ public function testExtractProvider()
185185

186186
'SET time_zone = "+00:00"',
187187

188+
'# Bash-like comment sytanx.' . "\n" .
188189
'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" .
189190
'BEGIN' . "\n" .
190191
' SELECT inventory_id' . "\n" .
@@ -196,6 +197,13 @@ public function testExtractProvider()
196197
' SELECT FOUND_ROWS() INTO p_film_count;' . "\n" .
197198
'END',
198199

200+
'-- --------------------------------------------------------' . "\n" .
201+
'' . "\n" .
202+
'--' . "\n" .
203+
'-- Table structure for `actor`' . "\n" .
204+
'--' . "\n" .
205+
'' . "\n" .
206+
'/* C-like comment syntax. */' . "\n" .
199207
'CREATE TABLE IF NOT EXISTS `actor` (' . "\n" .
200208
'`actor_id` SMALLINT(5) UNSIGNED NOT NULL,' . "\n" .
201209
'`first_name` VARCHAR(45) NOT NULL,' . "\n" .
@@ -231,8 +239,9 @@ public function testExtractProvider()
231239

232240
'SET time_zone = "+00:00"',
233241

234-
'DELIMITER $$',
242+
'/* a comment */ DELIMITER $$',
235243

244+
'# Bash-like comment sytanx.' . "\n" .
236245
'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" .
237246
'BEGIN' . "\n" .
238247
' SELECT inventory_id' . "\n" .
@@ -246,6 +255,13 @@ public function testExtractProvider()
246255

247256
'DELIMITER ;',
248257

258+
'-- --------------------------------------------------------' . "\n" .
259+
'' . "\n" .
260+
'--' . "\n" .
261+
'-- Table structure for `actor`' . "\n" .
262+
'--' . "\n" .
263+
'' . "\n" .
264+
'/* C-like comment syntax. */' . "\n" .
249265
'CREATE TABLE IF NOT EXISTS `actor` (' . "\n" .
250266
'`actor_id` SMALLINT(5) UNSIGNED NOT NULL,' . "\n" .
251267
'`first_name` VARCHAR(45) NOT NULL,' . "\n" .
@@ -281,6 +297,7 @@ public function testExtractProvider()
281297

282298
'SET time_zone = "+00:00";',
283299

300+
'# Bash-like comment sytanx.' . "\n" .
284301
'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" .
285302
'BEGIN' . "\n" .
286303
' SELECT inventory_id' . "\n" .
@@ -292,6 +309,13 @@ public function testExtractProvider()
292309
' SELECT FOUND_ROWS() INTO p_film_count;' . "\n" .
293310
'END$$',
294311

312+
'-- --------------------------------------------------------' . "\n" .
313+
'' . "\n" .
314+
'--' . "\n" .
315+
'-- Table structure for `actor`' . "\n" .
316+
'--' . "\n" .
317+
'' . "\n" .
318+
'/* C-like comment syntax. */' . "\n" .
295319
'CREATE TABLE IF NOT EXISTS `actor` (' . "\n" .
296320
'`actor_id` SMALLINT(5) UNSIGNED NOT NULL,' . "\n" .
297321
'`first_name` VARCHAR(45) NOT NULL,' . "\n" .

0 commit comments

Comments
 (0)