From b9e3b496faf3053e1b71520038f7221ef2d667c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 16 Nov 2021 11:47:42 +0100 Subject: [PATCH] Fix MSSQL CLOB column type to support Unicode --- .../DBAL/Platforms/SQLServer2005Platform.php | 2 +- .../DBAL/Platforms/SQLServerPlatform.php | 18 +++++++++- .../AbstractSQLServerPlatformTestCase.php | 33 +++++++++++++------ 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/lib/Doctrine/DBAL/Platforms/SQLServer2005Platform.php b/lib/Doctrine/DBAL/Platforms/SQLServer2005Platform.php index 6f02bb8ff51..4d83ec74f2a 100644 --- a/lib/Doctrine/DBAL/Platforms/SQLServer2005Platform.php +++ b/lib/Doctrine/DBAL/Platforms/SQLServer2005Platform.php @@ -33,7 +33,7 @@ public function supportsLimitOffset() */ public function getClobTypeDeclarationSQL(array $column) { - return 'VARCHAR(MAX)'; + return 'NVARCHAR(MAX)'; } /** diff --git a/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php b/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php index 2b958018603..85b7cd5e40c 100644 --- a/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php @@ -10,6 +10,8 @@ use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\TableDiff; +use Doctrine\DBAL\Types\Type; +use Doctrine\DBAL\Types\Types; use InvalidArgumentException; use function array_merge; @@ -1238,7 +1240,7 @@ public function getBinaryMaxLength() */ public function getClobTypeDeclarationSQL(array $column) { - return 'VARCHAR(MAX)'; + return 'NVARCHAR(MAX)'; } /** @@ -1532,6 +1534,20 @@ protected function initializeDoctrineTypeMappings() ]; } + /** + * {@inheritdoc} + */ + public function isCommentedDoctrineType(Type $doctrineType) + { + if ($doctrineType->getName() === Types::TEXT) { + // We require a commented text type in order to distinguish between text and string + // as both (have to) map to the same native type. + return true; + } + + return parent::isCommentedDoctrineType($doctrineType); + } + /** * {@inheritDoc} */ diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php index 26e75423ab4..daa8e969b5f 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php @@ -11,6 +11,7 @@ use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\TransactionIsolationLevel; use Doctrine\DBAL\Types\Type; +use Doctrine\DBAL\Types\Types; use function sprintf; @@ -143,9 +144,9 @@ public function testGeneratesTypeDeclarationsForStrings(): void 'NVARCHAR(255)', $this->platform->getVarcharTypeDeclarationSQL([]) ); - self::assertSame('VARCHAR(MAX)', $this->platform->getClobTypeDeclarationSQL([])); + self::assertSame('NVARCHAR(MAX)', $this->platform->getClobTypeDeclarationSQL([])); self::assertSame( - 'VARCHAR(MAX)', + 'NVARCHAR(MAX)', $this->platform->getClobTypeDeclarationSQL(['length' => 5, 'fixed' => true]) ); } @@ -741,7 +742,7 @@ public function getAlterTableColumnCommentsSQL(): array public function getCreateTableColumnTypeCommentsSQL(): array { return [ - 'CREATE TABLE test (id INT NOT NULL, data VARCHAR(MAX) NOT NULL, PRIMARY KEY (id))', + 'CREATE TABLE test (id INT NOT NULL, data NVARCHAR(MAX) NOT NULL, PRIMARY KEY (id))', "EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:array)', " . "N'SCHEMA', 'dbo', N'TABLE', 'test', N'COLUMN', data", ]; @@ -780,8 +781,8 @@ public function testGeneratesCreateTableSQLWithColumnComments(): void . 'comment INT NOT NULL, ' . '[comment_quoted] INT NOT NULL, ' . '[create] INT NOT NULL, ' - . 'commented_type VARCHAR(MAX) NOT NULL, ' - . 'commented_type_with_comment VARCHAR(MAX) NOT NULL, ' + . 'commented_type NVARCHAR(MAX) NOT NULL, ' + . 'commented_type_with_comment NVARCHAR(MAX) NOT NULL, ' . 'comment_with_string_literal_char NVARCHAR(255) NOT NULL, ' . 'PRIMARY KEY (id))', "EXEC sp_addextendedproperty N'MS_Description', " @@ -988,14 +989,14 @@ public function testGeneratesAlterTableSQLWithColumnComments(): void 'ALTER TABLE mytable ADD added_comment INT NOT NULL', 'ALTER TABLE mytable ADD [added_comment_quoted] INT NOT NULL', 'ALTER TABLE mytable ADD [select] INT NOT NULL', - 'ALTER TABLE mytable ADD added_commented_type VARCHAR(MAX) NOT NULL', - 'ALTER TABLE mytable ADD added_commented_type_with_comment VARCHAR(MAX) NOT NULL', + 'ALTER TABLE mytable ADD added_commented_type NVARCHAR(MAX) NOT NULL', + 'ALTER TABLE mytable ADD added_commented_type_with_comment NVARCHAR(MAX) NOT NULL', 'ALTER TABLE mytable ADD added_comment_with_string_literal_char NVARCHAR(255) NOT NULL', 'ALTER TABLE mytable DROP COLUMN comment_integer_0', 'ALTER TABLE mytable ALTER COLUMN comment_null NVARCHAR(255) NOT NULL', - 'ALTER TABLE mytable ALTER COLUMN comment_empty_string VARCHAR(MAX) NOT NULL', - 'ALTER TABLE mytable ALTER COLUMN [comment_quoted] VARCHAR(MAX) NOT NULL', - 'ALTER TABLE mytable ALTER COLUMN [create] VARCHAR(MAX) NOT NULL', + 'ALTER TABLE mytable ALTER COLUMN comment_empty_string NVARCHAR(MAX) NOT NULL', + 'ALTER TABLE mytable ALTER COLUMN [comment_quoted] NVARCHAR(MAX) NOT NULL', + 'ALTER TABLE mytable ALTER COLUMN [create] NVARCHAR(MAX) NOT NULL', 'ALTER TABLE mytable ALTER COLUMN commented_type INT NOT NULL', // Added columns. @@ -1122,6 +1123,18 @@ public function testInitializesDoctrineTypeMappings(): void self::assertSame('guid', $this->platform->getDoctrineTypeMapping('uniqueidentifier')); } + /** + * {@inheritDoc} + */ + public function getIsCommentedDoctrineType(): array + { + $data = parent::getIsCommentedDoctrineType(); + + $data[Types::TEXT] = [Type::getType(Types::TEXT), true]; + + return $data; + } + protected function getBinaryMaxLength(): int { return 8000;