Skip to content

Commit

Permalink
Fix issue #862
Browse files Browse the repository at this point in the history
  • Loading branch information
narfbg committed May 26, 2012
1 parent ae3d10c commit 650f2a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 54 deletions.
39 changes: 12 additions & 27 deletions system/database/drivers/mssql/mssql_forge.php
Expand Up @@ -48,32 +48,27 @@ class CI_DB_mssql_forge extends CI_DB_forge {
*/ */
protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
{ {
$sql = 'CREATE TABLE '; $sql = ($if_not_exists === TRUE)
? "IF NOT EXISTS (SELECT * FROM sysobjects WHERE ID = object_id(N'".$table."') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)\n"
: '';


if ($if_not_exists === TRUE) $sql .= 'CREATE TABLE '.$this->db->escape_identifiers($table).' (';
{
$sql .= 'IF NOT EXISTS ';
}


$sql .= $this->db->escape_identifiers($table).' (';
$current_field_count = 0; $current_field_count = 0;

foreach ($fields as $field => $attributes) foreach ($fields as $field => $attributes)
{ {
// Numeric field names aren't allowed in databases, so if the key is // Numeric field names aren't allowed in databases, so if the key is
// numeric, we know it was assigned by PHP and the developer manually // numeric, we know it was assigned by PHP and the developer manually
// entered the field information, so we'll simply add it to the list // entered the field information, so we'll simply add it to the list
if (is_numeric($field)) if (is_numeric($field))
{ {
$sql .= "\n\t$attributes"; $sql .= "\n\t".$attributes;
} }
else else
{ {
$attributes = array_change_key_case($attributes, CASE_UPPER); $attributes = array_change_key_case($attributes, CASE_UPPER);


$sql .= "\n\t".$this->db->protect_identifiers($field); $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE'];

$sql .= ' '.$attributes['TYPE'];


if (array_key_exists('CONSTRAINT', $attributes)) if (array_key_exists('CONSTRAINT', $attributes))
{ {
Expand Down Expand Up @@ -115,7 +110,7 @@ protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_
if (count($primary_keys) > 0) if (count($primary_keys) > 0)
{ {
$primary_keys = $this->db->protect_identifiers($primary_keys); $primary_keys = $this->db->protect_identifiers($primary_keys);
$sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")"; $sql .= ",\n\tPRIMARY KEY (".implode(', ', $primary_keys).')';
} }


if (is_array($keys) && count($keys) > 0) if (is_array($keys) && count($keys) > 0)
Expand All @@ -131,13 +126,11 @@ protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_
$key = array($this->db->protect_identifiers($key)); $key = array($this->db->protect_identifiers($key));
} }


$sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")"; $sql .= ",\n\tFOREIGN KEY (".implode(', ', $key).')';
} }
} }


$sql .= "\n)"; return $sql."\n)";

return $sql;
} }


// -------------------------------------------------------------------- // --------------------------------------------------------------------
Expand Down Expand Up @@ -167,29 +160,21 @@ protected function _alter_table($alter_type, $table, $column_name, $column_defin
return $sql; return $sql;
} }


$sql .= " $column_definition"; $sql .= " ".$column_definition;


if ($default_value != '') if ($default_value != '')
{ {
$sql .= " DEFAULT \"$default_value\""; $sql .= " DEFAULT '".$default_value."'";
} }


if ($null === NULL) $sql .= ($null === NULL) ? ' NULL' : ' NOT NULL';
{
$sql .= ' NULL';
}
else
{
$sql .= ' NOT NULL';
}


if ($after_field != '') if ($after_field != '')
{ {
return $sql.' AFTER '.$this->db->protect_identifiers($after_field); return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
} }


return $sql; return $sql;

} }


} }
Expand Down
39 changes: 12 additions & 27 deletions system/database/drivers/sqlsrv/sqlsrv_forge.php
Expand Up @@ -48,32 +48,27 @@ class CI_DB_sqlsrv_forge extends CI_DB_forge {
*/ */
protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
{ {
$sql = 'CREATE TABLE '; $sql = ($if_not_exists === TRUE)
? "IF NOT EXISTS (SELECT * FROM sysobjects WHERE ID = object_id(N'".$table."') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)\n"
: '';


if ($if_not_exists === TRUE) $sql .= 'CREATE TABLE '.$this->db->escape_identifiers($table).' (';
{
$sql .= 'IF NOT EXISTS ';
}


$sql .= $this->db->escape_identifiers($table).' (';
$current_field_count = 0; $current_field_count = 0;

foreach ($fields as $field => $attributes) foreach ($fields as $field => $attributes)
{ {
// Numeric field names aren't allowed in databases, so if the key is // Numeric field names aren't allowed in databases, so if the key is
// numeric, we know it was assigned by PHP and the developer manually // numeric, we know it was assigned by PHP and the developer manually
// entered the field information, so we'll simply add it to the list // entered the field information, so we'll simply add it to the list
if (is_numeric($field)) if (is_numeric($field))
{ {
$sql .= "\n\t$attributes"; $sql .= "\n\t".$attributes;
} }
else else
{ {
$attributes = array_change_key_case($attributes, CASE_UPPER); $attributes = array_change_key_case($attributes, CASE_UPPER);


$sql .= "\n\t".$this->db->protect_identifiers($field); $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE'];

$sql .= ' '.$attributes['TYPE'];


if (array_key_exists('CONSTRAINT', $attributes)) if (array_key_exists('CONSTRAINT', $attributes))
{ {
Expand Down Expand Up @@ -115,7 +110,7 @@ protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_
if (count($primary_keys) > 0) if (count($primary_keys) > 0)
{ {
$primary_keys = $this->db->protect_identifiers($primary_keys); $primary_keys = $this->db->protect_identifiers($primary_keys);
$sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")"; $sql .= ",\n\tPRIMARY KEY (".implode(', ', $primary_keys).')';
} }


if (is_array($keys) && count($keys) > 0) if (is_array($keys) && count($keys) > 0)
Expand All @@ -131,13 +126,11 @@ protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_
$key = array($this->db->protect_identifiers($key)); $key = array($this->db->protect_identifiers($key));
} }


$sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")"; $sql .= ",\n\tFOREIGN KEY (".implode(', ', $key).')';
} }
} }


$sql .= "\n)"; return $sql."\n)";

return $sql;
} }


// -------------------------------------------------------------------- // --------------------------------------------------------------------
Expand Down Expand Up @@ -167,29 +160,21 @@ protected function _alter_table($alter_type, $table, $column_name, $column_defin
return $sql; return $sql;
} }


$sql .= " $column_definition"; $sql .= ' '.$column_definition;


if ($default_value != '') if ($default_value != '')
{ {
$sql .= " DEFAULT \"$default_value\""; $sql .= " DEFAULT '".$default_value."'";
} }


if ($null === NULL) $sql .= ($null === NULL) ? ' NULL' : ' NOT NULL';
{
$sql .= ' NULL';
}
else
{
$sql .= ' NOT NULL';
}


if ($after_field != '') if ($after_field != '')
{ {
return $sql.' AFTER '.$this->db->protect_identifiers($after_field); return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
} }


return $sql; return $sql;

} }


} }
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelog.rst
Expand Up @@ -220,6 +220,7 @@ Bug fixes for 3.0
- Fixed a bug (#121) - ``CI_DB_result::row()`` returned an array when there's no actual result to be returned. - Fixed a bug (#121) - ``CI_DB_result::row()`` returned an array when there's no actual result to be returned.
- Fixed a bug (#319) - SQLSRV's affected_rows() method failed due to a scrollable cursor being created for write-type queries. - Fixed a bug (#319) - SQLSRV's affected_rows() method failed due to a scrollable cursor being created for write-type queries.
- Fixed a bug (#356) - PostgreSQL driver didn't have an _update_batch() method, which resulted in fatal error being triggered when update_batch() is used with it. - Fixed a bug (#356) - PostgreSQL driver didn't have an _update_batch() method, which resulted in fatal error being triggered when update_batch() is used with it.
- Fixed a bug (#862) - create_table() failed on SQLSRV/MSSQL when used with 'IF NOT EXISTS'.


Version 2.1.1 Version 2.1.1
============= =============
Expand Down

0 comments on commit 650f2a2

Please sign in to comment.