diff --git a/src/Database/Schema/SqlserverSchema.php b/src/Database/Schema/SqlserverSchema.php index a2669282c96..d9067de98cf 100644 --- a/src/Database/Schema/SqlserverSchema.php +++ b/src/Database/Schema/SqlserverSchema.php @@ -46,11 +46,7 @@ public function describeColumnSql($tableName, $config) AC.column_id AS [column_id], AC.name AS [name], TY.name AS [type], - IIF ( - TY.name = \'nchar\' OR TY.name = \'nvarchar\' OR TY.name = \'ntext\', - AC.max_length / 2, - AC.max_length - ) AS [char_length], + AC.max_length AS [char_length], AC.precision AS [precision], AC.scale AS [scale], AC.is_identity AS [autoincrement], @@ -117,7 +113,11 @@ protected function _convertColumn($col, $length = null, $precision = null, $scal if ($col === 'real' || $col === 'float') { return ['type' => TableSchema::TYPE_FLOAT, 'length' => null]; } - + // SqlServer schema reflection returns double length for unicode + // columns because internally it uses UTF16/UCS2 + if ($col === 'nvarchar' || $col === 'nchar' || $col === 'ntext') { + $length = $length / 2; + } if (strpos($col, 'varchar') !== false && $length < 0) { return ['type' => TableSchema::TYPE_TEXT, 'length' => null]; } diff --git a/tests/TestCase/Database/Schema/SqlserverSchemaTest.php b/tests/TestCase/Database/Schema/SqlserverSchemaTest.php index 2a00e62b91d..b5867677ba2 100644 --- a/tests/TestCase/Database/Schema/SqlserverSchemaTest.php +++ b/tests/TestCase/Database/Schema/SqlserverSchemaTest.php @@ -198,7 +198,8 @@ public static function convertColumnProvider() 50, null, null, - ['type' => 'string', 'length' => 50] + // Sqlserver returns double lenghts for unicode columns + ['type' => 'string', 'length' => 25] ], [ 'CHAR', @@ -212,7 +213,8 @@ public static function convertColumnProvider() 10, null, null, - ['type' => 'string', 'fixed' => true, 'length' => 10] + // SQLServer returns double length for unicode columns. + ['type' => 'string', 'fixed' => true, 'length' => 5] ], [ 'UNIQUEIDENTIFIER',