Skip to content

Commit f732ea4

Browse files
Merge pull request #661 from M393/mariadb-comment-command
Parse MariaDB comment command
2 parents 11ffb89 + 282edb5 commit f732ea4

11 files changed

Lines changed: 425 additions & 21 deletions

‎phpstan-baseline.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2472,7 +2472,7 @@ parameters:
24722472

24732473
-
24742474
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#"
2475-
count: 30
2475+
count: 34
24762476
path: tests/Lexer/IsMethodsTest.php
24772477

24782478
-

‎psalm-baseline.xml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@
811811
<code>$token</code>
812812
<code>$token</code>
813813
</PossiblyNullArgument>
814-
<PossiblyNullOperand occurrences="24">
814+
<PossiblyNullOperand occurrences="25">
815815
<code>$this-&gt;delimiter</code>
816816
<code>$this-&gt;str[$this-&gt;last++]</code>
817817
<code>$this-&gt;str[$this-&gt;last]</code>
@@ -836,6 +836,7 @@
836836
<code>$this-&gt;str[++$this-&gt;last]</code>
837837
<code>$this-&gt;str[++$this-&gt;last]</code>
838838
<code>$this-&gt;str[++$this-&gt;last]</code>
839+
<code>$this-&gt;str[++$this-&gt;last]</code>
839840
</PossiblyNullOperand>
840841
<PossiblyNullPropertyAssignmentValue occurrences="1">
841842
<code>null</code>

‎src/Context.php‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,19 @@ public static function isComment($str, $end = false)
443443
return Token::FLAG_COMMENT_BASH;
444444
}
445445

446-
// If comment is opening C style (/*), warning, it could be a MySQL command (/*!)
446+
// If comment is opening C style (/*), warning, it could be
447+
// - a MySQL command (/*!)
448+
// - a MariaDB command (/*M!)
447449
if (($len > 1) && ($str[0] === '/') && ($str[1] === '*')) {
448-
return ($len > 2) && ($str[2] === '!') ?
449-
Token::FLAG_COMMENT_MYSQL_CMD : Token::FLAG_COMMENT_C;
450+
if ($len > 2 && $str[2] === '!') {
451+
return Token::FLAG_COMMENT_MYSQL_CMD;
452+
}
453+
454+
if ($len > 3 && $str[2] === 'M' && $str[3] === '!') {
455+
return Token::FLAG_COMMENT_MARIADB_CMD;
456+
}
457+
458+
return Token::FLAG_COMMENT_C;
450459
}
451460

452461
// If comment is closing C style (*/), warning, it could conflicts with wildcard and a real opening C style.

‎src/Lexer.php‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,10 +693,21 @@ public function parseComment()
693693
return new Token($token, Token::TYPE_COMMENT, $flags);
694694
}
695695

696-
// Checking if this is a MySQL-specific command.
697-
if ($this->last + 1 < $this->len && $this->str[$this->last + 1] === '!') {
698-
$flags |= Token::FLAG_COMMENT_MYSQL_CMD;
696+
// Checking if this is a MySQL (/*!) or MariaDB (/*M!) specific command.
697+
if (
698+
$this->last + 1 < $this->len &&
699+
($this->str[$this->last + 1] === '!' ||
700+
($this->str[$this->last + 1] === 'M' &&
701+
$this->last + 2 < $this->len &&
702+
$this->str[$this->last + 2] === '!'))
703+
) {
699704
$token .= $this->str[++$this->last];
705+
if ($this->str[$this->last] === '!') {
706+
$flags |= Token::FLAG_COMMENT_MYSQL_CMD;
707+
} else {
708+
$flags |= Token::FLAG_COMMENT_MARIADB_CMD;
709+
$token .= $this->str[++$this->last];
710+
}
700711

701712
while (
702713
++$this->last < $this->len

‎src/Token.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class Token
152152
public const FLAG_COMMENT_C = 2;
153153
public const FLAG_COMMENT_SQL = 4;
154154
public const FLAG_COMMENT_MYSQL_CMD = 8;
155+
public const FLAG_COMMENT_MARIADB_CMD = 16;
155156

156157
// Operators related flags.
157158
public const FLAG_OPERATOR_ARITHMETIC = 1;

‎tests/Lexer/IsMethodsTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public function testIsComment(): void
6161
{
6262
$this->assertEquals(Token::FLAG_COMMENT_BASH, Context::isComment('#'));
6363
$this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('/*'));
64+
$this->assertEquals(Token::FLAG_COMMENT_MYSQL_CMD, Context::isComment('/*!'));
65+
$this->assertEquals(Token::FLAG_COMMENT_MYSQL_CMD, Context::isComment('/*!50000'));
66+
$this->assertEquals(Token::FLAG_COMMENT_MARIADB_CMD, Context::isComment('/*M!'));
67+
$this->assertEquals(Token::FLAG_COMMENT_MARIADB_CMD, Context::isComment('/*M!100300'));
6468
$this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('*/'));
6569
$this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment('-- '));
6670
$this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment("--\t"));

‎tests/Parser/CreateStatementTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public static function createProvider(): array
6464
['parser/parseCreateTableErr5'],
6565
['parser/parseCreateTableSelect'],
6666
['parser/parseCreateTableAsSelect'],
67+
['parser/parseCreateTableColumnCompressed'],
6768
['parser/parseCreateTableLike'],
6869
['parser/parseCreateTableSpatial'],
6970
['parser/parseCreateTableSRID'],

‎tests/data/lexer/lexComment.in‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# comment
2-
SELECT /*!50000 STRAIGHT_JOIN */ col1 FROM table1, table2 /* select query */
2+
SELECT /*!50000 STRAIGHT_JOIN */ col1 FROM table1, table2; /* select query */
33
-- comment
4-
-- comment 2
4+
-- comment 2
5+
SELECT /*M!100300 1, */ 2;

‎tests/data/lexer/lexComment.out‎

Lines changed: 128 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"query": "# comment\nSELECT /*!50000 STRAIGHT_JOIN */ col1 FROM table1, table2 /* select query */\n-- comment\n-- comment 2",
2+
"query": "# comment\nSELECT /*!50000 STRAIGHT_JOIN */ col1 FROM table1, table2; /* select query */\n-- comment\n-- comment 2\nSELECT /*M!100300 1, */ 2;",
33
"lexer": {
44
"@type": "PhpMyAdmin\\SqlParser\\Lexer",
5-
"str": "# comment\nSELECT /*!50000 STRAIGHT_JOIN */ col1 FROM table1, table2 /* select query */\n-- comment\n-- comment 2",
6-
"len": 110,
7-
"last": 111,
5+
"str": "# comment\nSELECT /*!50000 STRAIGHT_JOIN */ col1 FROM table1, table2; /* select query */\n-- comment\n-- comment 2\nSELECT /*M!100300 1, */ 2;",
6+
"len": 138,
7+
"last": 138,
88
"list": {
99
"@type": "PhpMyAdmin\\SqlParser\\TokensList",
1010
"tokens": [
@@ -170,14 +170,23 @@
170170
"flags": 0,
171171
"position": 61
172172
},
173+
{
174+
"@type": "PhpMyAdmin\\SqlParser\\Token",
175+
"token": ";",
176+
"value": ";",
177+
"keyword": null,
178+
"type": 9,
179+
"flags": 0,
180+
"position": 67
181+
},
173182
{
174183
"@type": "PhpMyAdmin\\SqlParser\\Token",
175184
"token": " ",
176185
"value": " ",
177186
"keyword": null,
178187
"type": 3,
179188
"flags": 0,
180-
"position": 67
189+
"position": 68
181190
},
182191
{
183192
"@type": "PhpMyAdmin\\SqlParser\\Token",
@@ -186,7 +195,7 @@
186195
"keyword": null,
187196
"type": 4,
188197
"flags": 2,
189-
"position": 68
198+
"position": 69
190199
},
191200
{
192201
"@type": "PhpMyAdmin\\SqlParser\\Token",
@@ -195,7 +204,7 @@
195204
"keyword": null,
196205
"type": 3,
197206
"flags": 0,
198-
"position": 86
207+
"position": 87
199208
},
200209
{
201210
"@type": "PhpMyAdmin\\SqlParser\\Token",
@@ -204,7 +213,7 @@
204213
"keyword": null,
205214
"type": 4,
206215
"flags": 4,
207-
"position": 87
216+
"position": 88
208217
},
209218
{
210219
"@type": "PhpMyAdmin\\SqlParser\\Token",
@@ -213,7 +222,7 @@
213222
"keyword": null,
214223
"type": 3,
215224
"flags": 0,
216-
"position": 97
225+
"position": 98
217226
},
218227
{
219228
"@type": "PhpMyAdmin\\SqlParser\\Token",
@@ -222,7 +231,115 @@
222231
"keyword": null,
223232
"type": 4,
224233
"flags": 4,
225-
"position": 98
234+
"position": 99
235+
},
236+
{
237+
"@type": "PhpMyAdmin\\SqlParser\\Token",
238+
"token": "\n",
239+
"value": " ",
240+
"keyword": null,
241+
"type": 3,
242+
"flags": 0,
243+
"position": 111
244+
},
245+
{
246+
"@type": "PhpMyAdmin\\SqlParser\\Token",
247+
"token": "SELECT",
248+
"value": "SELECT",
249+
"keyword": "SELECT",
250+
"type": 1,
251+
"flags": 3,
252+
"position": 112
253+
},
254+
{
255+
"@type": "PhpMyAdmin\\SqlParser\\Token",
256+
"token": " ",
257+
"value": " ",
258+
"keyword": null,
259+
"type": 3,
260+
"flags": 0,
261+
"position": 118
262+
},
263+
{
264+
"@type": "PhpMyAdmin\\SqlParser\\Token",
265+
"token": "/*M!100300",
266+
"value": "/*M!100300",
267+
"keyword": null,
268+
"type": 4,
269+
"flags": 18,
270+
"position": 119
271+
},
272+
{
273+
"@type": "PhpMyAdmin\\SqlParser\\Token",
274+
"token": " ",
275+
"value": " ",
276+
"keyword": null,
277+
"type": 3,
278+
"flags": 0,
279+
"position": 129
280+
},
281+
{
282+
"@type": "PhpMyAdmin\\SqlParser\\Token",
283+
"token": "1",
284+
"value": 1,
285+
"keyword": null,
286+
"type": 6,
287+
"flags": 0,
288+
"position": 130
289+
},
290+
{
291+
"@type": "PhpMyAdmin\\SqlParser\\Token",
292+
"token": ",",
293+
"value": ",",
294+
"keyword": null,
295+
"type": 2,
296+
"flags": 16,
297+
"position": 131
298+
},
299+
{
300+
"@type": "PhpMyAdmin\\SqlParser\\Token",
301+
"token": " ",
302+
"value": " ",
303+
"keyword": null,
304+
"type": 3,
305+
"flags": 0,
306+
"position": 132
307+
},
308+
{
309+
"@type": "PhpMyAdmin\\SqlParser\\Token",
310+
"token": "*/",
311+
"value": "*/",
312+
"keyword": null,
313+
"type": 4,
314+
"flags": 2,
315+
"position": 133
316+
},
317+
{
318+
"@type": "PhpMyAdmin\\SqlParser\\Token",
319+
"token": " ",
320+
"value": " ",
321+
"keyword": null,
322+
"type": 3,
323+
"flags": 0,
324+
"position": 135
325+
},
326+
{
327+
"@type": "PhpMyAdmin\\SqlParser\\Token",
328+
"token": "2",
329+
"value": 2,
330+
"keyword": null,
331+
"type": 6,
332+
"flags": 0,
333+
"position": 136
334+
},
335+
{
336+
"@type": "PhpMyAdmin\\SqlParser\\Token",
337+
"token": ";",
338+
"value": ";",
339+
"keyword": null,
340+
"type": 9,
341+
"flags": 0,
342+
"position": 137
226343
},
227344
{
228345
"@type": "PhpMyAdmin\\SqlParser\\Token",
@@ -234,7 +351,7 @@
234351
"position": null
235352
}
236353
],
237-
"count": 25,
354+
"count": 38,
238355
"idx": 0
239356
},
240357
"delimiter": ";",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE `t` (`c1` longtext /*M!100301 COMPRESSED*/);

0 commit comments

Comments
 (0)