Browse Source

Attempt to fix double length issue in PHP.

The SQL that was in use did not work well in older versions of
SQLServer.
Mark Story 8 years ago
parent
commit
51bf75e89b

+ 6 - 6
src/Database/Schema/SqlserverSchema.php

@@ -46,11 +46,7 @@ class SqlserverSchema extends BaseSchema
             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 @@ class SqlserverSchema extends BaseSchema
         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];
         }

+ 4 - 2
tests/TestCase/Database/Schema/SqlserverSchemaTest.php

@@ -198,7 +198,8 @@ SQL;
                 50,
                 null,
                 null,
-                ['type' => 'string', 'length' => 50]
+                // Sqlserver returns double lenghts for unicode columns
+                ['type' => 'string', 'length' => 25]
             ],
             [
                 'CHAR',
@@ -212,7 +213,8 @@ SQL;
                 10,
                 null,
                 null,
-                ['type' => 'string', 'fixed' => true, 'length' => 10]
+                // SQLServer returns double length for unicode columns.
+                ['type' => 'string', 'fixed' => true, 'length' => 5]
             ],
             [
                 'UNIQUEIDENTIFIER',