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

Escape identifiers that need it #501

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
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -925,16 +925,6 @@ parameters:
count: 2
path: tests/Components/PartitionDefinitionTest.php

-
message: "#^Call to method PHPUnit\\\\Framework\\\\Assert\\:\\:assertArrayHasKey\\(\\) with 2 and PhpMyAdmin\\\\SqlParser\\\\TokensList will always evaluate to false\\.$#"
count: 1
path: tests/Lexer/TokensListTest.php

-
message: "#^Call to method PHPUnit\\\\Framework\\\\Assert\\:\\:assertArrayHasKey\\(\\) with 112 and PhpMyAdmin\\\\SqlParser\\\\UtfString will always evaluate to false\\.$#"
count: 1
path: tests/Misc/UtfStringTest.php

-
message: "#^Cannot call method has\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
count: 1
Expand Down
2 changes: 1 addition & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.14.1@b9d355e0829c397b9b3b47d0c0ed042a8a70284d">
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<file src="src/Components/AlterOperation.php">
<InvalidPropertyAssignmentValue>
<code><![CDATA[[
Expand Down
12 changes: 11 additions & 1 deletion src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function intval;
use function is_int;
use function is_numeric;
use function preg_match;
use function str_replace;
use function str_starts_with;
use function strlen;
Expand Down Expand Up @@ -675,7 +676,11 @@ private static function getModeFromString(string $mode): int
*/
public static function escape(string $str, string $quote = '`')
{
if ((static::$mode & self::SQL_MODE_NO_ENCLOSING_QUOTES) && (! static::isKeyword($str, true))) {
if (
(static::$mode & self::SQL_MODE_NO_ENCLOSING_QUOTES) && ! (
static::isKeyword($str, true) || self::doesIdentifierRequireQuoting($str)
)
) {
return $str;
}

Expand Down Expand Up @@ -727,4 +732,9 @@ public static function hasMode(int|null $flag = null): bool

return (self::$mode & $flag) === $flag;
}

private static function doesIdentifierRequireQuoting(string $identifier): bool
{
return preg_match('/^[$]|^\d+$|[^0-9a-zA-Z$_\x80-\xffff]/', $identifier) === 1;
williamdes marked this conversation as resolved.
Show resolved Hide resolved
}
}
4 changes: 4 additions & 0 deletions tests/Lexer/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ public function testEscape(): void
{
Context::setMode(Context::SQL_MODE_NO_ENCLOSING_QUOTES);
$this->assertEquals('test', Context::escape('test'));
$this->assertEquals('`123`', Context::escape('123'));
$this->assertEquals('`$test`', Context::escape('$test'));
$this->assertEquals('`te st`', Context::escape('te st'));
$this->assertEquals('`te.st`', Context::escape('te.st'));

Context::setMode(Context::SQL_MODE_ANSI_QUOTES);
$this->assertEquals('"test"', Context::escape('test'));
Expand Down