Skip to content

Commit

Permalink
Adding better support for nvarchar(MAX) in SQLServer
Browse files Browse the repository at this point in the history
Newer versions of SQLServer perfer to use nvarchar(max) for
text columns.
  • Loading branch information
markstory committed Jun 22, 2011
1 parent 936d068 commit 0c1472e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
42 changes: 28 additions & 14 deletions lib/Cake/Model/Datasource/Database/Sqlserver.php
Expand Up @@ -213,10 +213,10 @@ public function describe($model) {
foreach ($cols as $column) {
$field = $column->Field;
$fields[$field] = array(
'type' => $this->column($column->Type),
'type' => $this->column($column),
'null' => ($column->Null === 'YES' ? true : false),
'default' => preg_replace("/^[(]{1,2}'?([^')]*)?'?[)]{1,2}$/", "$1", $column->Default),
'length' => intval($column->Length),
'length' => $this->length($column),
'key' => ($column->Key == '1') ? 'primary' : false
);

Expand Down Expand Up @@ -385,22 +385,16 @@ public function limit($limit, $offset = null) {
/**
* Converts database-layer column types to basic types
*
* @param string $real Real database-layer column type (i.e. "varchar(255)")
* @param mixed $real Either the string value of the fields type.
* or the Result object from Sqlserver::describe()
* @return string Abstract column type (i.e. "string")
*/
public function column($real) {
if (is_array($real)) {
$col = $real['name'];

if (isset($real['limit'])) {
$col .= '(' . $real['limit'] . ')';
}
return $col;
}
$col = str_replace(')', '', $real);
$limit = null;
if (strpos($col, '(') !== false) {
list($col, $limit) = explode('(', $col);
$col = $real;
if (is_object($real) && isset($real->Field)) {
$limit = $real->Length;
$col = $real->Type;
}

if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
Expand All @@ -412,6 +406,9 @@ public function column($real) {
if (strpos($col, 'int') !== false) {
return 'integer';
}
if (strpos($col, 'char') !== false && $limit === -1) {
return 'text';
}
if (strpos($col, 'char') !== false) {
return 'string';
}
Expand All @@ -427,6 +424,23 @@ public function column($real) {
return 'text';
}

/**
* Handle SQLServer specific length properties.
* SQLServer handles text types as nvarchar/varchar with a length of -1.
*
* @param mixed $length Either the length as a string, or a Column descriptor object.
* @return mixed null|integer with length of column.
*/
public function length($length) {
if (is_object($length) && isset($length->Length)) {
if ($length->Length === -1 && strpos($length->Type, 'char') !== false) {
return null;
}
return $length->Length;
}
return parent::length($length);
}

/**
* Builds a map of the columns contained in a result
*
Expand Down
14 changes: 14 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/SqlserverTest.php
Expand Up @@ -394,6 +394,14 @@ public function testDescribe() {
'Length' => '4',
'Null' => 'NO',
'Type' => 'integer'
),
(object) array(
'Default' => '',
'Field' => 'body',
'Key' => 0,
'Length' => -1,
'Null' => 'YES',
'Type' => 'nvarchar'
)
));
$this->db->executeResultsStack = array($SqlserverTableDescription);
Expand All @@ -405,6 +413,12 @@ public function testDescribe() {
'null' => false,
'default' => '0',
'length' => 4
),
'body' => array(
'type' => 'text',
'null' => true,
'default' => null,
'length' => null
)
);
$this->assertEqual($expected, $result);
Expand Down

0 comments on commit 0c1472e

Please sign in to comment.