Skip to content

Commit

Permalink
Fix issues with SQLServer + boolean columns.
Browse files Browse the repository at this point in the history
SQLServer should not have lengths applied to BIT column types.
Remove any length that could have been provided.

Fixes #2439
  • Loading branch information
markstory committed Apr 2, 2012
1 parent ee7a224 commit 4e67698
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/Cake/Model/Datasource/Database/Sqlserver.php
Expand Up @@ -656,12 +656,15 @@ public function insertMulti($table, $fields, $values) {
/**
* Generate a database-native column schema string
*
* @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]),
* @param array $column An array structured like the
* following: array('name'=>'value', 'type'=>'value'[, options]),
* where options can be 'default', 'length', or 'key'.
* @return string
*/
public function buildColumn($column) {
$result = preg_replace('/(int|integer)\([0-9]+\)/i', '$1', parent::buildColumn($column));
$result = parent::buildColumn($column);
$result = preg_replace('/(int|integer)\([0-9]+\)/i', '$1', $result);
$result = preg_replace('/(bit)\([0-9]+\)/i', '$1', $result);
if (strpos($result, 'DEFAULT NULL') !== false) {
if (isset($column['default']) && $column['default'] === '') {
$result = str_replace('DEFAULT NULL', "DEFAULT ''", $result);
Expand Down
10 changes: 10 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/SqlserverTest.php
Expand Up @@ -543,6 +543,16 @@ public function testBuildColumn() {
$result = $this->db->buildColumn($column);
$expected = '[body] nvarchar(MAX)';
$this->assertEquals($expected, $result);

$column = array(
'name' => 'checked',
'type' => 'boolean',
'length' => 10,
'default' => '1'
);
$result = $this->db->buildColumn($column);
$expected = "[checked] bit DEFAULT '1'";
$this->assertEquals($expected, $result);
}

/**
Expand Down

0 comments on commit 4e67698

Please sign in to comment.