Skip to content

Commit

Permalink
Attempt to fix double length issue in PHP.
Browse files Browse the repository at this point in the history
The SQL that was in use did not work well in older versions of
SQLServer.
  • Loading branch information
markstory committed Oct 23, 2017
1 parent f6a27e2 commit 51bf75e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/Database/Schema/SqlserverSchema.php
Expand Up @@ -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],
Expand Down Expand Up @@ -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];
}
Expand Down
6 changes: 4 additions & 2 deletions tests/TestCase/Database/Schema/SqlserverSchemaTest.php
Expand Up @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit 51bf75e

Please sign in to comment.