You are here

public function DatabaseSchema_sqlsrv::addField in Drupal driver for SQL Server and SQL Azure 7

Same name and namespace in other branches
  1. 7.3 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::addField()
  2. 7.2 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::addField()

Override DatabaseSchema::addField().

@status complete

Overrides DatabaseSchema::addField

1 call to DatabaseSchema_sqlsrv::addField()
DatabaseSchema_sqlsrv::changeField in sqlsrv/schema.inc
Override DatabaseSchema::changeField().

File

sqlsrv/schema.inc, line 451
Database schema code for Microsoft SQL Server database servers.

Class

DatabaseSchema_sqlsrv

Code

public function addField($table, $field, $spec, $new_keys = array()) {
  if (!$this
    ->tableExists($table)) {
    throw new DatabaseSchemaObjectDoesNotExistException(t("Cannot add field %table.%field: table doesn't exist.", array(
      '%field' => $field,
      '%table' => $table,
    )));
  }
  if ($this
    ->fieldExists($table, $field)) {
    throw new DatabaseSchemaObjectExistsException(t("Cannot add field %table.%field: field already exists.", array(
      '%field' => $field,
      '%table' => $table,
    )));
  }

  // If the field is declared NOT NULL, we have to first create it NULL insert
  // the initial data then switch to NOT NULL.
  if (!empty($spec['not null']) && !isset($spec['default'])) {
    $fixnull = TRUE;
    $spec['not null'] = FALSE;
  }

  // Create the field.
  $query = 'ALTER TABLE {' . $table . '} ADD ';
  $query .= $this
    ->createFieldSql($table, $field, $this
    ->processField($spec));
  $this->connection
    ->query($query);

  // Reset the blob cache.
  $this->additionalColumnInformation = NULL;

  // Load the initial data.
  if (isset($spec['initial'])) {
    $this->connection
      ->update($table)
      ->fields(array(
      $field => $spec['initial'],
    ))
      ->execute();
  }

  // Switch to NOT NULL now.
  if (!empty($fixnull)) {
    $spec['not null'] = TRUE;
    $this->connection
      ->query('ALTER TABLE {' . $table . '} ALTER COLUMN ' . $this
      ->createFieldSql($table, $field, $this
      ->processField($spec), TRUE));
  }

  // Add the new keys.
  if (isset($new_keys)) {
    $this
      ->recreateTableKeys($table, $new_keys);
  }
}