Skip to content
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

Fix quotes in single-line comments breaking parameters #3497

Closed
wants to merge 5 commits into from
Closed
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
8 changes: 5 additions & 3 deletions lib/Doctrine/DBAL/SQLParserUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use const PREG_OFFSET_CAPTURE;
use function array_fill;
use function array_filter;
use function array_key_exists;
use function array_merge;
use function array_slice;
Expand Down Expand Up @@ -250,11 +251,12 @@ private static function getUnquotedStatementFragments($statement)
self::ESCAPED_DOUBLE_QUOTED_TEXT . '|' .
self::ESCAPED_BACKTICK_QUOTED_TEXT . '|' .
self::ESCAPED_BRACKET_QUOTED_TEXT;
$expression = sprintf('/((.+(?i:ARRAY)\\[.+\\])|([^\'"`\\[]+))(?:%s)?/s', $literal);

$expression = sprintf('/(?:[\s]*--.*?\n)|((.+(?i:ARRAY)\\[.+\\])|([^-\'"`\\[]+))(?:%s)?/s', $literal);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an issue (#3472) caused by making this regular expression more complex (#3277). I don't know of a proper solution but it seems like making it even more complex may make things even worse.

preg_match_all($expression, $statement, $fragments, PREG_OFFSET_CAPTURE);

return $fragments[1];
return array_filter($fragments[1], static function (string $match) : bool {
return $match !== '';
});
}

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/Doctrine/Tests/DBAL/SQLParserUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,52 @@ public function dataGetPlaceholderPositions()
['SELECT data.age AS age, data.id AS id, data.name AS name, data.id AS id FROM test_data data WHERE (data.description LIKE :condition_0 ESCAPE "\\\\") AND (data.description LIKE :condition_1 ESCAPE \'\\\\\') ORDER BY id ASC', false, [121 => 'condition_0', 174 => 'condition_1']],
['SELECT data.age AS age, data.id AS id, data.name AS name, data.id AS id FROM test_data data WHERE (data.description LIKE :condition_0 ESCAPE `\\\\`) AND (data.description LIKE :condition_1 ESCAPE `\\\\`) ORDER BY id ASC', false, [121 => 'condition_0', 174 => 'condition_1']],
['SELECT data.age AS age, data.id AS id, data.name AS name, data.id AS id FROM test_data data WHERE (data.description LIKE :condition_0 ESCAPE \'\\\\\') AND (data.description LIKE :condition_1 ESCAPE `\\\\`) ORDER BY id ASC', false, [121 => 'condition_0', 174 => 'condition_1']],
// Named parameter containing comment(s) with odd number of single quotes
[
<<<'SQLDATA'
SELECT * FROM foo WHERE
bar=:a_param1
-- Oops! Perfectly normal comment but it's got an odd number of apostrophes
OR bar=':not_a_param1'
OR bar=:a_param2
OR bar=:a_param3
OR bar=':not_a_param2'
SQLDATA
,
false,
[
29 => 'a_param1',
145 => 'a_param2',
162 => 'a_param3',
],
],
// Check comments in strings don't reverse it either
[
<<<'SQLDATA'
INSERT INTO foo VALUES
('
-- Not a comment
morozov marked this conversation as resolved.
Show resolved Hide resolved
Hello, world!
',
:a_param1
':not_a_param1')
SQLDATA
,
false,
[ 61 => 'a_param1' ],
],
// And with comment on same line as quote
[
<<<'SQLDATA'
INSERT INTO foo VALUES
('
-- Not a comment Hello, world! ', :a_param1
':not_a_param1')
SQLDATA
,
false,
[ 60 => 'a_param1' ],
],

morozov marked this conversation as resolved.
Show resolved Hide resolved
];
}
Expand Down