You are here

public function DatabaseSchema_sqlsrv::fieldSetDefault 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::fieldSetDefault()
  2. 7.2 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::fieldSetDefault()

Override DatabaseSchema::fieldSetDefault().

@status complete

Overrides DatabaseSchema::fieldSetDefault

File

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

Class

DatabaseSchema_sqlsrv

Code

public function fieldSetDefault($table, $field, $default) {
  if (!$this
    ->fieldExists($table, $field)) {
    throw new DatabaseSchemaObjectDoesNotExistException(t("Cannot set default value of field %table.%field: field doesn't exist.", array(
      '%table' => $table,
      '%field' => $field,
    )));
  }
  if ($default === NULL) {
    $default = 'NULL';
  }
  elseif (is_string($default)) {
    $default = "'" . addslashes($spec['default']) . "'";
  }

  // Try to remove any existing default first.
  try {
    $this
      ->fieldSetNoDefault($table, $field);
  } catch (Exception $e) {
  }

  // Create the new default.
  $this->connection
    ->query('ALTER TABLE [{' . $table . '}] ADD CONSTRAINT {' . $table . '}_' . $field . '_df DEFAULT ' . $default . ' FOR [' . $field . ']');
}