db_change_column
./update.php, строка 106
- Версии
- 5 – 6
db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array())
Change a column definition using syntax appropriate for PostgreSQL.
Save result of SQL commands in $ret
array.
Remember that changing a column definition involves adding a new column and dropping an old one. This means that any indices, primary keys and sequences from serial-type columns are dropped and might need to be recreated.
Параметры
$ret
Array to which results will be added.
$table
Name of the table, without {}
$column
Name of the column to change
$column_new
New name for the column (set to the same as $column
if you don't want to change the name)
$type
Type of column
$attributes
Additional optional attributes. Recognized attributes:
not null => TRUE
|FALSE
default => NULL
|FALSE
|value (with or without ''
, it won't be added)
Возвращаемое значение
nothing, but modifies $ret
parameter.
Код
<?php
function db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array()) {
if (array_key_exists('not null', $attributes) and $attributes['not null']) {
$not_null = 'NOT NULL';
}
if (array_key_exists('default', $attributes)) {
if (is_null($attributes['default'])) {
$default_val = 'NULL';
$default = 'default NULL';
}
elseif ($attributes['default'] === FALSE) {
$default = '';
}
else {
$default_val = "$attributes[default]";
$default = "default $attributes[default]";
}
}
$ret[] = update_sql("ALTER TABLE {". $table ."} RENAME $column TO ". $column ."_old");
$ret[] = update_sql("ALTER TABLE {". $table ."} ADD $column_new $type");
$ret[] = update_sql("UPDATE {". $table ."} SET $column_new = ". $column ."_old");
if ($default) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET $default"); }
if ($not_null) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET NOT NULL"); }
$ret[] = update_sql("ALTER TABLE {". $table ."} DROP ". $column ."_old");
}
?>
Войдите или зарегистрируйтесь, чтобы получить возможность отправлять комментарии