From 0eabc72241e5177753a09119c5111a78e3c09ec9 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Wed, 12 May 2021 17:24:55 -0700 Subject: [PATCH] [GH-4643] Fix SQLServerPlatform::quoteIdentifier() --- .../DBAL/Platforms/SQLServerPlatform.php | 2 +- .../DBAL/Functional/Platform/QuotingTest.php | 40 +++++++++++++++++++ .../AbstractSQLServerPlatformTestCase.php | 4 +- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php b/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php index b4ef0fa1094..2b958018603 100644 --- a/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php @@ -1610,7 +1610,7 @@ protected function getReservedKeywordsClass() */ public function quoteSingleIdentifier($str) { - return '[' . str_replace(']', '][', $str) . ']'; + return '[' . str_replace(']', ']]', $str) . ']'; } /** diff --git a/tests/Doctrine/Tests/DBAL/Functional/Platform/QuotingTest.php b/tests/Doctrine/Tests/DBAL/Functional/Platform/QuotingTest.php index fee5b33f0c6..4ec44dd306b 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Platform/QuotingTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Platform/QuotingTest.php @@ -2,8 +2,11 @@ namespace Doctrine\Tests\DBAL\Functional\Platform; +use Doctrine\DBAL\Platforms\OraclePlatform; use Doctrine\Tests\DbalFunctionalTestCase; +use function key; + class QuotingTest extends DbalFunctionalTestCase { /** @@ -29,4 +32,41 @@ public static function stringLiteralProvider(): iterable 'single-quote' => ["'"], ]; } + + /** + * @dataProvider identifierProvider + */ + public function testQuoteIdentifier(string $identifier): void + { + $platform = $this->connection->getDatabasePlatform(); + + /** + * @link https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements008.htm + */ + if ($platform instanceof OraclePlatform && identifier === '"') { + self::markTestSkipped('Oracle does not support double quotes in identifiers'); + } + + $query = $platform->getDummySelectSQL( + 'NULL AS ' . $platform->quoteIdentifier($identifier) + ); + + $row = $this->connection->fetchAssociative($query); + + self::assertNotFalse($row); + self::assertSame($identifier, key($row)); + } + + /** + * @return iterable + */ + public static function identifierProvider(): iterable + { + return [ + '[' => ['['], + ']' => [']'], + '"' => ['"'], + '`' => ['`'], + ]; + } } diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php index 3c7068d6fa7..26e75423ab4 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php @@ -545,14 +545,14 @@ public function testModifyLimitSubquerySimple(): void public function testQuoteIdentifier(): void { - self::assertEquals('[fo][o]', $this->platform->quoteIdentifier('fo]o')); + self::assertEquals('[fo]]o]', $this->platform->quoteIdentifier('fo]o')); self::assertEquals('[test]', $this->platform->quoteIdentifier('test')); self::assertEquals('[test].[test]', $this->platform->quoteIdentifier('test.test')); } public function testQuoteSingleIdentifier(): void { - self::assertEquals('[fo][o]', $this->platform->quoteSingleIdentifier('fo]o')); + self::assertEquals('[fo]]o]', $this->platform->quoteSingleIdentifier('fo]o')); self::assertEquals('[test]', $this->platform->quoteSingleIdentifier('test')); self::assertEquals('[test.test]', $this->platform->quoteSingleIdentifier('test.test')); }