Skip to content

Commit

Permalink
[doctrineGH-4643] Fix SQLServerPlatform::quoteIdentifier()
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed May 14, 2021
1 parent b728198 commit bd18ec4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ protected function getReservedKeywordsClass()
*/
public function quoteSingleIdentifier($str)
{
return '[' . str_replace(']', '][', $str) . ']';
return '[' . str_replace(']', ']]', $str) . ']';
}

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/Platform/QuotingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand All @@ -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<string,array{0:string}>
*/
public static function identifierProvider(): iterable
{
return [
'[' => ['['],
']' => [']'],
'"' => ['"'],
'`' => ['`'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down

0 comments on commit bd18ec4

Please sign in to comment.