Permalink
Browse files

Fix modify_column() issues (#2020)

  • Loading branch information...
1 parent 2eaeee5 commit 7ade8b7944d10f4fd1583789309cf003a3eac6f7 @narfbg narfbg committed Nov 22, 2012
@@ -642,7 +642,7 @@ public function modify_column($table = '', $field = array())
show_error('Field information is required.');
}
- $sqls = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
+ $sqls = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->_process_fields());
$this->_reset();
if ($sqls === FALSE)
{
@@ -108,12 +108,8 @@ protected function _alter_table($alter_type, $table, $field)
}
else
{
- $sqls[] = $sql.' CHANGE '.$this->_process_column($field[$i]);
- if ( ! empty($field[$i]['new_name']))
- {
- $sqls[] = $sql.' RENAME COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
- .' AS '.$this->db->escape_identifiers($field[$i]['name']);
- }
+ $alter_type = empty($field[$i]['new_name']) ? ' MODIFY ' : ' CHANGE ';
+ $sqls[] = $sql.$alter_type$this->_process_column($field[$i]);
}
}
@@ -139,7 +135,7 @@ protected function _process_column($field)
}
return $this->db->escape_identifiers($field['name'])
- .(empty($field['new_name']) ? '' : $this->db->escape_identifiers($field['new_name']))
+ .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
.' '.$field['type'].$field['length']
.$field['unsigned']
.$field['null']
@@ -137,7 +137,7 @@ protected function _alter_table($alter_type, $table, $field)
$sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
$sqls = array();
- for ($i = 0, $c = count($field), $sql .= $alter_type.' '; $i < $c; $i++)
+ for ($i = 0, $c = count($field); $i < $c; $i++)
{
if ($field[$i]['_literal'] !== FALSE)
{
@@ -146,13 +146,14 @@ protected function _alter_table($alter_type, $table, $field)
if (isset($field[$i]['type']))
{
- $sqls[] = $sql.' TYPE '.$field[$i]['type'].$field[$i]['length'];
+ $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identififers($field[$i]['name'])
+ .' TYPE '.$field[$i]['type'].$field[$i]['length'];
}
if ( ! empty($field[$i]['default']))
{
- $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name'])
- .' SET '.$field[$i]['default'];
+ $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
+ .' SET DEFAULT '.$field[$i]['default'];
}
if (isset($field[$i]['null']))
@@ -165,7 +166,7 @@ protected function _alter_table($alter_type, $table, $field)
if ( ! empty($field[$i]['new_name']))
{
- $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name'])
+ $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
.' TO '.$this->db->escape_identifiers($field[$i]['new_name']);
}
}
@@ -157,7 +157,7 @@ protected function _process_column($field)
}
return $this->db->escape_identifiers($field['name'])
- .(empty($field['new_name']) ? '' : $this->db->escape_identifiers($field['new_name']))
+ .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
.' '.$field['type'].$field['length']
.$field['unsigned']
.$field['null']
@@ -157,7 +157,7 @@ protected function _process_column($field)
}
return $this->db->escape_identifiers($field['name'])
- .(empty($field['new_name']) ? '' : $this->db->escape_identifiers($field['new_name']))
+ .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
.' '.$field['type'].$field['length']
.$field['unsigned']
.$field['null']
@@ -105,7 +105,7 @@ protected function _alter_table($alter_type, $table, $field)
$sql .= ' '.$alter_type.' ';
$sql .= (count($field) === 1)
- ? $fields[0]
+ ? $field[0]
: '('.implode(',', $field).')';
// RENAME COLUMN must be executed after MODIFY
@@ -108,12 +108,8 @@ protected function _alter_table($alter_type, $table, $field)
}
else
{
- $sqls[] = $sql.' CHANGE '.$this->_process_column($field[$i]);
- if ( ! empty($field[$i]['new_name']))
- {
- $sqls[] = $sql.' RENAME COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
- .' AS '.$this->db->escape_identifiers($field[$i]['name']);
- }
+ $alter_type = empty($field[$i]['new_name']) ? ' MODIFY ' : ' CHANGE ';
+ $sqls[] = $sql.$alter_type.$this->_process_column($field[$i]);
}
}
@@ -139,7 +135,7 @@ protected function _process_column($field)
}
return $this->db->escape_identifiers($field['name'])
- .(empty($field['new_name']) ? '' : $this->db->escape_identifiers($field['new_name']))
+ .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
.' '.$field['type'].$field['length']
.$field['unsigned']
.$field['null']
@@ -123,7 +123,7 @@ protected function _alter_table($alter_type, $table, $field)
$sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
$sqls = array();
- for ($i = 0, $c = count($field), $sql .= $alter_type.' '; $i < $c; $i++)
+ for ($i = 0, $c = count($field); $i < $c; $i++)
{
if ($field[$i]['_literal'] !== FALSE)
{
@@ -132,13 +132,14 @@ protected function _alter_table($alter_type, $table, $field)
if (isset($field[$i]['type']))
{
- $sqls[] = $sql.' TYPE '.$field[$i]['type'].$field[$i]['length'];
+ $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
+ .' TYPE '.$field[$i]['type'].$field[$i]['length'];
}
if ( ! empty($field[$i]['default']))
{
- $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name'])
- .' SET '.$field[$i]['default'];
+ $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
+ .' SET DEFAULT '.$field[$i]['default'];
}
if (isset($field[$i]['null']))
@@ -151,7 +152,7 @@ protected function _alter_table($alter_type, $table, $field)
if ( ! empty($field[$i]['new_name']))
{
- $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name'])
+ $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
.' TO '.$this->db->escape_identifiers($field[$i]['new_name']);
}
}
@@ -171,7 +171,7 @@ protected function _process_column($field)
}
return $this->db->escape_identifiers($field['name'])
- .(empty($field['new_name']) ? '' : $this->db->escape_identifiers($field['new_name']))
+ .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
.' '.$field['type'].$field['length']
.$field['unsigned']
.$field['null']
@@ -105,7 +105,7 @@ protected function _alter_table($alter_type, $table, $field)
$sql .= ' '.$alter_type.' ';
$sql .= (count($field) === 1)
- ? $fields[0]
+ ? $field[0]
: '('.implode(',', $field).')';
// RENAME COLUMN must be executed after MODIFY
@@ -103,7 +103,7 @@ protected function _alter_table($alter_type, $table, $field)
$sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
$sqls = array();
- for ($i = 0, $c = count($field), $sql .= $alter_type.' '; $i < $c; $i++)
+ for ($i = 0, $c = count($field); $i < $c; $i++)
{
if ($field[$i]['_literal'] !== FALSE)
{
@@ -112,24 +112,25 @@ protected function _alter_table($alter_type, $table, $field)
if (version_compare($this->db->version(), '8', '>=') && isset($field[$i]['type']))
{
- $sqls[] = $sql.' TYPE '.$field[$i]['type'].$field[$i]['length'];
+ $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
+ .' TYPE '.$field[$i]['type'].$field[$i]['length'];
}
if ( ! empty($field[$i]['default']))
{
- $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name'])
- .' SET '.$field[$i]['default'];
+ $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
+ .' SET DEFAULT '.$field[$i]['default'];
}
if (isset($field[$i]['null']))
{
- $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name'])
+ $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
.($field[$i]['null'] === TRUE ? ' DROP NOT NULL' : ' SET NOT NULL');
}
if ( ! empty($field[$i]['new_name']))
{
- $sqls[] = $sql.' RENAME '.$this->db->escape_identifiers($field[$i]['name'])
+ $sqls[] = $sql.' RENAME COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
.' TO '.$this->db->escape_identifiers($field[$i]['new_name']);
}
}
@@ -96,7 +96,7 @@ protected function _alter_table($alter_type, $table, $field)
$sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
$sqls = array();
- for ($i = 0, $c = count($field), $sql .= $alter_type.' '; $i < $c; $i++)
+ for ($i = 0, $c = count($field); $i < $c; $i++)
{
if ($field[$i]['_literal'] !== FALSE)
{
@@ -105,24 +105,25 @@ protected function _alter_table($alter_type, $table, $field)
if (version_compare($this->db->version(), '8', '>=') && isset($field[$i]['type']))
{
- $sqls[] = $sql.' TYPE '.$field[$i]['type'].$field[$i]['length'];
+ $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
+ .' TYPE '.$field[$i]['type'].$field[$i]['length'];
}
if ( ! empty($field[$i]['default']))
{
- $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name'])
- .' SET '.$field[$i]['default'];
+ $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
+ .' SET DEFAULT '.$field[$i]['default'];
}
if (isset($field[$i]['null']))
{
- $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name'])
+ $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
.($field[$i]['null'] === TRUE ? ' DROP NOT NULL' : ' SET NOT NULL');
}
if ( ! empty($field[$i]['new_name']))
{
- $sqls[] = $sql.' RENAME '.$this->db->escape_identifiers($field[$i]['name'])
+ $sqls[] = $sql.' RENAME COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
.' TO '.$this->db->escape_identifiers($field[$i]['new_name']);
}
}

0 comments on commit 7ade8b7

Please sign in to comment.