You are here

public function Schema::fieldSetDefault in Drupal driver for SQL Server and SQL Azure 8.2

Same name and namespace in other branches
  1. 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::fieldSetDefault()
  2. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::fieldSetDefault()

Override DatabaseSchema::fieldSetDefault().

@status complete

Overrides Schema::fieldSetDefault

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php, line 1012
Definition of Drupal\Driver\Database\sqlsrv\Schema

Class

Schema

Namespace

Drupal\Driver\Database\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,
    )));
  }
  $transaction = $this->connection
    ->startTransaction(null, DatabaseTransactionSettings::GetDDLCompatibleDefaults());
  $real_table = $this->connection
    ->prefixTable($table);
  $constraint_name = "{$real_table}_{$field}_df";
  $spec = $this
    ->getTableIntrospection($table);
  $default_expression = $this->connection
    ->Scheme()
    ->DefaultValueExpression($spec['columns'][$field]['sqlsrv_type'], $default);

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

  // Create the new default.
  $this->connection
    ->query_direct("ALTER TABLE [{$real_table}] ADD CONSTRAINT [{$constraint_name}] DEFAULT {$default_expression} FOR [{$field}]", [], array(
    'prefix_tables' => false,
  ));
  $transaction
    ->commit();
}