db_add_column

Хочешь помочь с переводом? Это очень просто и быстро. Лишь зарегистрируйся, и можешь тут же начать переводить.

./update.php, строка 52

Версии
5 – 6
db_add_column(&$ret, $table, $column, $type, $attributes = array())

Add a column to a database using syntax appropriate for PostgreSQL. Save result of SQL commands in $ret array.

Note: when you add a column with NOT NULL and you are not sure if there are already rows in the table, you MUST also add DEFAULT. Otherwise PostgreSQL won't work when the table is not empty, and db_add_column() will fail. To have an empty string as the default, you must use: 'default' => '''' in the $attributes array. If NOT NULL and DEFAULT are set the PostgreSQL version will set values of the added column in old rows to the DEFAULT value.

Параметры

$ret Array to which results will be added.

$table Name of the table, without {}

$column Name of the column

$type Type of column

$attributes Additional optional attributes. Recognized attributes: not null => TRUE|FALSE default => NULL|FALSE|value (the value must be enclosed in '' marks)

Возвращаемое значение

nothing, but modifies $ret parameter.

▾ 19 функции вызывают db_add_column()

hook_update_N in developer/hooks/install.php
Производит обновление таблиц БД, необходимое для единичного обновления модуля.
system_update_1001 in modules/system/system.install
system_update_1003 in modules/system/system.install
system_update_1006 in modules/system/system.install
system_update_129 in modules/system/system.install
system_update_133 in modules/system/system.install
system_update_139 in modules/system/system.install
system_update_145 in modules/system/system.install
system_update_146 in modules/system/system.install
system_update_148 in modules/system/system.install
system_update_149 in modules/system/system.install
system_update_153 in modules/system/system.install
system_update_154 in modules/system/system.install
system_update_158 in modules/system/system.install
system_update_181 in modules/system/system.install
update_fix_schema_version in ./update.php
If the schema version for Drupal core is stored in the variables table (4.6.x and earlier) move it to the schema_version column of the system table.
update_fix_sessions in ./update.php
System update 130 changes the sessions table, which breaks the update script's ability to use session variables. This changes the table appropriately.
update_fix_system_table in ./update.php
update_fix_watchdog in ./update.php
System update 142 changes the watchdog table, which breaks the update script's ability to use logging. This changes the table appropriately.

Код

<?php
function db_add_column(&$ret, $table, $column, $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 ."} ADD $column $type");
  if ($default) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column SET $default"); }
  if ($not_null) {
    if ($default) { $ret[] = update_sql("UPDATE {". $table ."} SET $column = $default_val"); }
    $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column SET NOT NULL");
  }
}
?>
Войдите или зарегистрируйтесь, чтобы получить возможность отправлять комментарии

Вход в систему