You are here

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

Create a new table from a Drupal table definition.

Parameters

$name: The name of the table to create.

$table: A Schema API table definition array.

Throws

DatabaseSchemaObjectExistsException If the specified table already exists.

Overrides DatabaseSchema::createTable

File

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

Class

DatabaseSchema_sqlsrv

Code

public function createTable($name, $table) {

  // Reset the additional column information because the schema changed.
  $this->additionalColumnInformation = NULL;
  if ($this
    ->tableExists($name)) {
    throw new DatabaseSchemaObjectExistsException(t('Table %name already exists.', array(
      '%name' => $name,
    )));
  }

  // Build the table and its unique keys in a transaction, and fail the whole
  // creation in case of an error.
  $transaction = $this->connection
    ->startTransaction();
  try {
    $this->connection
      ->query($this
      ->createTableSql($name, $table));
    if (isset($table['unique keys']) && is_array($table['unique keys'])) {
      foreach ($table['unique keys'] as $key_name => $key) {
        $this
          ->addUniqueKey($name, $key_name, $key);
      }
    }
  } catch (Exception $e) {
    $transaction
      ->rollback();
    throw $e;
  }

  // Everything went well, commit the transaction.
  unset($transaction);

  // Create the indexes but ignore any error during the creation. We do that
  // do avoid pulling the carpet under modules that try to implement indexes
  // with invalid data types (long columns), before we come up with a better
  // solution.
  if (isset($table['indexes']) && is_array($table['indexes'])) {
    foreach ($table['indexes'] as $key_name => $key) {
      try {
        $this->connection
          ->query($this
          ->createIndexSql($name, $key_name, $key));
      } catch (Exception $e) {

        // Log the exception but do not rollback the transaction.
        watchdog_exception('database', $e);
      }
    }
  }
}