/**
* ALTER TABLE
*
* @param string $alter_type ALTER type
* @param string $table Table name
* @param mixed $field Column definition
* @return string|string[]
*/
protected function _alter_table($alter_type, $table, $field)
{
if ($alter_type === 'DROP') {
return parent::_alter_table($alter_type, $table, $field);
} elseif ($alter_type === 'CHANGE') {
$alter_type = 'MODIFY';
}
$sql = 'ALTER TABLE ' . $this->db->escape_identifiers($table);
$sqls = array();
for ($i = 0, $c = count($field); $i < $c; $i++) {
if ($field[$i]['_literal'] !== FALSE) {
$field[$i] = "\n\t" . $field[$i]['_literal'];
} else {
$field[$i]['_literal'] = "\n\t" . $this->_process_column($field[$i]);
if ($alter_type === 'MODIFY' && !empty($field[$i]['new_name'])) {
$sqls[] = $sql . ' RENAME COLUMN ' . $this->db->escape_identifiers($field[$i]['name']) . ' ' . $this->db->escape_identifiers($field[$i]['new_name']);
}
}
}
$sql .= ' ' . $alter_type . ' ';
$sql .= count($field) === 1 ? $field[0] : '(' . implode(',', $field) . ')';
// RENAME COLUMN must be executed after MODIFY
array_unshift($sqls, $sql);
return $sql;
}