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 MSSQL CLOB column type to support Unicode #4987

Closed
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Platforms/SQLServer2005Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function supportsLimitOffset()
*/
public function getClobTypeDeclarationSQL(array $column)
{
return 'VARCHAR(MAX)';
return 'NVARCHAR(MAX)';
}

/**
Expand Down
18 changes: 17 additions & 1 deletion lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1238,7 +1240,7 @@ public function getBinaryMaxLength()
*/
public function getClobTypeDeclarationSQL(array $column)
{
return 'VARCHAR(MAX)';
return 'NVARCHAR(MAX)';
}

/**
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

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

Only TEXT maps to VARCHAR(MAX). Is it possible to identify this during the schema introspection? See

case 'varchar':
// TEXT type is returned as VARCHAR(MAX) with a length of -1
if ($length === -1) {
$dbType = 'text';
}
break;

}

return parent::isCommentedDoctrineType($doctrineType);
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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])
);
}
Expand Down Expand Up @@ -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",
];
Expand Down Expand Up @@ -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', "
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down