Skip to content

Fix #226 - Ignore end of comment string at the end of queries with MySQL-specific commands #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2023
Merged
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
3 changes: 2 additions & 1 deletion src/Components/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =

// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
if ($isExpr) {
// If the token is a closing C comment from a MySQL command, it must be ignored.
if ($isExpr && $token->token !== '*/') {
$ret->expr .= $token->token;
}

Expand Down
59 changes: 59 additions & 0 deletions tests/Components/ExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,63 @@ public function testBuild(): void
'1 + 2 AS `three`, 1 + 3 AS `four`'
);
}

/**
* @return string[][]
*/
public static function mysqlCommandsProvider(): array
{
return [
[
'/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;',
'SET @OLD_CHARACTER_SET_CLIENT = @@CHARACTER_SET_CLIENT',
],
[
'/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;',
'SET @OLD_CHARACTER_SET_RESULTS = @@CHARACTER_SET_RESULTS',
],
[
'/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;',
'SET @OLD_COLLATION_CONNECTION = @@COLLATION_CONNECTION',
],
[
'/*!40101 SET NAMES utf8 */;',
'SET NAMES utf8',
],
[
'/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;',
'SET @OLD_TIME_ZONE = @@TIME_ZONE',
],
[
"/*!40103 SET TIME_ZONE='+00:00' */;",
"SET TIME_ZONE = '+00:00'",
],
[
'/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;',
'SET @OLD_UNIQUE_CHECKS = @@UNIQUE_CHECKS, UNIQUE_CHECKS = 0',
],
[
'/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;',
'SET @OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS = 0',
],
[
"/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;",
"SET @OLD_SQL_MODE = @@SQL_MODE, SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO'",
],
[
'/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;',
'SET @OLD_SQL_NOTES = @@SQL_NOTES, SQL_NOTES = 0',
],
];
}

/**
* @dataProvider mysqlCommandsProvider
*/
public function testMysqlCommands(string $expr, string $expected): void
{
$parser = new Parser($expr, true);
$parser->parse();
self::assertSame($expected, $parser->statements[0]->build());
}
}
20 changes: 10 additions & 10 deletions tests/Parser/ParserLongExportsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ public function testMysqldump(): void
SQL;

$expectedSql = [
'SET @OLD_CHARACTER_SET_CLIENT = @@CHARACTER_SET_CLIENT */',
'SET @OLD_CHARACTER_SET_RESULTS = @@CHARACTER_SET_RESULTS */',
'SET @OLD_COLLATION_CONNECTION = @@COLLATION_CONNECTION */',
'SET @OLD_CHARACTER_SET_CLIENT = @@CHARACTER_SET_CLIENT',
'SET @OLD_CHARACTER_SET_RESULTS = @@CHARACTER_SET_RESULTS',
'SET @OLD_COLLATION_CONNECTION = @@COLLATION_CONNECTION',
'SET NAMES utf8',
'SET @OLD_TIME_ZONE = @@TIME_ZONE */',
'SET @OLD_TIME_ZONE = @@TIME_ZONE',
"SET TIME_ZONE = '+00:00'",
'SET @OLD_UNIQUE_CHECKS = @@UNIQUE_CHECKS, UNIQUE_CHECKS = 0 */',
'SET @OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS = 0 */',
'SET @OLD_UNIQUE_CHECKS = @@UNIQUE_CHECKS, UNIQUE_CHECKS = 0',
'SET @OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS = 0',
"SET @OLD_SQL_MODE = @@SQL_MODE, SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO'",
'SET @OLD_SQL_NOTES = @@SQL_NOTES, SQL_NOTES = 0 */',
'SET @OLD_SQL_NOTES = @@SQL_NOTES, SQL_NOTES = 0',
];

$parser = new Parser($sql, true);
Expand Down Expand Up @@ -85,9 +85,9 @@ public function testParsephpMyAdminDump(): void
'SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"',
'SET AUTOCOMMIT = 0',
'SET time_zone = "+00:00"',
'SET @OLD_CHARACTER_SET_CLIENT = @@CHARACTER_SET_CLIENT */',
'SET @OLD_CHARACTER_SET_RESULTS = @@CHARACTER_SET_RESULTS */',
'SET @OLD_COLLATION_CONNECTION = @@COLLATION_CONNECTION */',
'SET @OLD_CHARACTER_SET_CLIENT = @@CHARACTER_SET_CLIENT',
'SET @OLD_CHARACTER_SET_RESULTS = @@CHARACTER_SET_RESULTS',
'SET @OLD_COLLATION_CONNECTION = @@COLLATION_CONNECTION',
'SET NAMES utf8mb4',
], $collectedSetStatements);

Expand Down
6 changes: 3 additions & 3 deletions tests/data/parser/parsephpMyAdminExport1.out
Original file line number Diff line number Diff line change
Expand Up @@ -4641,7 +4641,7 @@
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\SetOperation",
"column": "@OLD_CHARACTER_SET_CLIENT",
"value": "@@CHARACTER_SET_CLIENT *\/"
"value": "@@CHARACTER_SET_CLIENT"
}
],
"first": 44,
Expand All @@ -4658,7 +4658,7 @@
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\SetOperation",
"column": "@OLD_CHARACTER_SET_RESULTS",
"value": "@@CHARACTER_SET_RESULTS *\/"
"value": "@@CHARACTER_SET_RESULTS"
}
],
"first": 55,
Expand All @@ -4675,7 +4675,7 @@
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\SetOperation",
"column": "@OLD_COLLATION_CONNECTION",
"value": "@@COLLATION_CONNECTION *\/"
"value": "@@COLLATION_CONNECTION"
}
],
"first": 66,
Expand Down