You are here

function content_db_add_column in Content Construction Kit (CCK) 5

Add a column to a database table.

Parameters

$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 (with or without '', it won't be added)

1 call to content_db_add_column()
content_alter_db_field in ./content_admin.inc
Perform adds, alters, and drops as needed to synchronize the database with new field definitions.

File

./content_admin.inc, line 1347
Administrative interface for content type creation.

Code

function content_db_add_column($table, $column, $type, $attributes = array()) {
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      $mappings = array(
        'int' => 'integer',
        'mediumint' => 'integer',
        'bigint' => 'integer',
        'tinyint' => 'smallint',
        'float' => 'float',
        'varchar' => 'varchar',
        'text' => 'text',
        'mediumtext' => 'text',
        'longtext' => 'text',
      );
      if (isset($mappings[$type])) {
        $type = $mappings[$type];
      }
      else {
        watchdog('database', t('No PostgreSQL mapping found for %type data type.', array(
          '%type' => $type,
        )), WATCHDOG_WARNING);
      }
      if ($type != 'varchar') {
        unset($attributes['length']);
      }
      break;
  }
  if (array_key_exists('not null', $attributes) && $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']}";
    }
  }
  if (array_key_exists('length', $attributes)) {
    $type .= '(' . $attributes['length'] . ')';
  }
  if (array_key_exists('unsigned', $attributes) && $attributes['unsigned']) {
    switch ($GLOBALS['db_type']) {
      case 'pgsql':
        $type = str_replace('integer', 'int_unsigned', $type);
        break;
      default:
        $type .= ' unsigned';
        break;
    }
  }
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_query("ALTER TABLE {" . $table . "} ADD {$column} {$type}");
      if ($default) {
        db_query("ALTER TABLE {" . $table . "} ALTER {$column} SET {$default}");
      }
      if ($not_null) {
        if ($default) {
          db_query("UPDATE {" . $table . "} SET {$column} = {$default_val}");
        }
        db_query("ALTER TABLE {" . $table . "} ALTER {$column} SET NOT NULL");
      }
      break;
    case 'mysql':
    case 'mysqli':

      // MySQL allows no DEFAULT value for text (and blob) columns
      if (in_array($type, array(
        'text',
        'mediumtext',
        'longtext',
      ))) {
        $default = '';

        // We also allow NULL values to account for CCK's per field INSERTs
        $not_null = '';
      }
      db_query('ALTER TABLE {' . $table . '} ADD COLUMN ' . $column . ' ' . $type . ' ' . $not_null . ' ' . $default);
      break;
  }
}