Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2472,7 +2472,7 @@ parameters:

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

-
Expand Down
3 changes: 2 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@
<code>$token</code>
<code>$token</code>
</PossiblyNullArgument>
<PossiblyNullOperand occurrences="24">
<PossiblyNullOperand occurrences="25">
<code>$this-&gt;delimiter</code>
<code>$this-&gt;str[$this-&gt;last++]</code>
<code>$this-&gt;str[$this-&gt;last]</code>
Expand All @@ -836,6 +836,7 @@
<code>$this-&gt;str[++$this-&gt;last]</code>
<code>$this-&gt;str[++$this-&gt;last]</code>
<code>$this-&gt;str[++$this-&gt;last]</code>
<code>$this-&gt;str[++$this-&gt;last]</code>
</PossiblyNullOperand>
<PossiblyNullPropertyAssignmentValue occurrences="1">
<code>null</code>
Expand Down
15 changes: 12 additions & 3 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,19 @@ public static function isComment($str, $end = false)
return Token::FLAG_COMMENT_BASH;
}

// If comment is opening C style (/*), warning, it could be a MySQL command (/*!)
// If comment is opening C style (/*), warning, it could be
// - a MySQL command (/*!)
// - a MariaDB command (/*M!)
if (($len > 1) && ($str[0] === '/') && ($str[1] === '*')) {
return ($len > 2) && ($str[2] === '!') ?
Token::FLAG_COMMENT_MYSQL_CMD : Token::FLAG_COMMENT_C;
if ($len > 2 && $str[2] === '!') {
return Token::FLAG_COMMENT_MYSQL_CMD;
}

if ($len > 3 && $str[2] === 'M' && $str[3] === '!') {
return Token::FLAG_COMMENT_MARIADB_CMD;
}

return Token::FLAG_COMMENT_C;
}

// If comment is closing C style (*/), warning, it could conflicts with wildcard and a real opening C style.
Expand Down
17 changes: 14 additions & 3 deletions src/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,10 +693,21 @@ public function parseComment()
return new Token($token, Token::TYPE_COMMENT, $flags);
}

// Checking if this is a MySQL-specific command.
if ($this->last + 1 < $this->len && $this->str[$this->last + 1] === '!') {
$flags |= Token::FLAG_COMMENT_MYSQL_CMD;
// Checking if this is a MySQL (/*!) or MariaDB (/*M!) specific command.
if (
$this->last + 1 < $this->len &&
($this->str[$this->last + 1] === '!' ||
($this->str[$this->last + 1] === 'M' &&
$this->last + 2 < $this->len &&
$this->str[$this->last + 2] === '!'))
) {
$token .= $this->str[++$this->last];
if ($this->str[$this->last] === '!') {
$flags |= Token::FLAG_COMMENT_MYSQL_CMD;
} else {
$flags |= Token::FLAG_COMMENT_MARIADB_CMD;
$token .= $this->str[++$this->last];
}

while (
++$this->last < $this->len
Expand Down
1 change: 1 addition & 0 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class Token
public const FLAG_COMMENT_C = 2;
public const FLAG_COMMENT_SQL = 4;
public const FLAG_COMMENT_MYSQL_CMD = 8;
public const FLAG_COMMENT_MARIADB_CMD = 16;

// Operators related flags.
public const FLAG_OPERATOR_ARITHMETIC = 1;
Expand Down
4 changes: 4 additions & 0 deletions tests/Lexer/IsMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function testIsComment(): void
{
$this->assertEquals(Token::FLAG_COMMENT_BASH, Context::isComment('#'));
$this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('/*'));
$this->assertEquals(Token::FLAG_COMMENT_MYSQL_CMD, Context::isComment('/*!'));
$this->assertEquals(Token::FLAG_COMMENT_MYSQL_CMD, Context::isComment('/*!50000'));
$this->assertEquals(Token::FLAG_COMMENT_MARIADB_CMD, Context::isComment('/*M!'));
$this->assertEquals(Token::FLAG_COMMENT_MARIADB_CMD, Context::isComment('/*M!100300'));
$this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('*/'));
$this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment('-- '));
$this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment("--\t"));
Expand Down
1 change: 1 addition & 0 deletions tests/Parser/CreateStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static function createProvider(): array
['parser/parseCreateTableErr5'],
['parser/parseCreateTableSelect'],
['parser/parseCreateTableAsSelect'],
['parser/parseCreateTableColumnCompressed'],
['parser/parseCreateTableLike'],
['parser/parseCreateTableSpatial'],
['parser/parseCreateTableSRID'],
Expand Down
5 changes: 3 additions & 2 deletions tests/data/lexer/lexComment.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# comment
SELECT /*!50000 STRAIGHT_JOIN */ col1 FROM table1, table2 /* select query */
SELECT /*!50000 STRAIGHT_JOIN */ col1 FROM table1, table2; /* select query */
-- comment
-- comment 2
-- comment 2
SELECT /*M!100300 1, */ 2;
139 changes: 128 additions & 11 deletions tests/data/lexer/lexComment.out
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"query": "# comment\nSELECT /*!50000 STRAIGHT_JOIN */ col1 FROM table1, table2 /* select query */\n-- comment\n-- comment 2",
"query": "# comment\nSELECT /*!50000 STRAIGHT_JOIN */ col1 FROM table1, table2; /* select query */\n-- comment\n-- comment 2\nSELECT /*M!100300 1, */ 2;",
"lexer": {
"@type": "PhpMyAdmin\\SqlParser\\Lexer",
"str": "# comment\nSELECT /*!50000 STRAIGHT_JOIN */ col1 FROM table1, table2 /* select query */\n-- comment\n-- comment 2",
"len": 110,
"last": 111,
"str": "# comment\nSELECT /*!50000 STRAIGHT_JOIN */ col1 FROM table1, table2; /* select query */\n-- comment\n-- comment 2\nSELECT /*M!100300 1, */ 2;",
"len": 138,
"last": 138,
"list": {
"@type": "PhpMyAdmin\\SqlParser\\TokensList",
"tokens": [
Expand Down Expand Up @@ -170,14 +170,23 @@
"flags": 0,
"position": 61
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": ";",
"value": ";",
"keyword": null,
"type": 9,
"flags": 0,
"position": 67
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 67
"position": 68
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
Expand All @@ -186,7 +195,7 @@
"keyword": null,
"type": 4,
"flags": 2,
"position": 68
"position": 69
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
Expand All @@ -195,7 +204,7 @@
"keyword": null,
"type": 3,
"flags": 0,
"position": 86
"position": 87
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
Expand All @@ -204,7 +213,7 @@
"keyword": null,
"type": 4,
"flags": 4,
"position": 87
"position": 88
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
Expand All @@ -213,7 +222,7 @@
"keyword": null,
"type": 3,
"flags": 0,
"position": 97
"position": 98
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
Expand All @@ -222,7 +231,115 @@
"keyword": null,
"type": 4,
"flags": 4,
"position": 98
"position": 99
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "\n",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 111
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "SELECT",
"value": "SELECT",
"keyword": "SELECT",
"type": 1,
"flags": 3,
"position": 112
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 118
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "/*M!100300",
"value": "/*M!100300",
"keyword": null,
"type": 4,
"flags": 18,
"position": 119
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 129
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "1",
"value": 1,
"keyword": null,
"type": 6,
"flags": 0,
"position": 130
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": ",",
"value": ",",
"keyword": null,
"type": 2,
"flags": 16,
"position": 131
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 132
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "*/",
"value": "*/",
"keyword": null,
"type": 4,
"flags": 2,
"position": 133
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 135
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "2",
"value": 2,
"keyword": null,
"type": 6,
"flags": 0,
"position": 136
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": ";",
"value": ";",
"keyword": null,
"type": 9,
"flags": 0,
"position": 137
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
Expand All @@ -234,7 +351,7 @@
"position": null
}
],
"count": 25,
"count": 38,
"idx": 0
},
"delimiter": ";",
Expand Down
1 change: 1 addition & 0 deletions tests/data/parser/parseCreateTableColumnCompressed.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE `t` (`c1` longtext /*M!100301 COMPRESSED*/);
Loading