You are here

public function DatabaseSchema_sqlsrv::createTable in Drupal driver for SQL Server and SQL Azure 7.2

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

{@Inheritdoc}

Overrides DatabaseSchema::createTable

File

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

Class

DatabaseSchema_sqlsrv

Code

public function createTable($name, $table) {
  if ($this
    ->tableExists($name, FALSE)) {
    throw new DatabaseSchemaObjectExistsException(t('Table %name already exists.', array(
      '%name' => $name,
    )));
  }

  // Reset caches after calling tableExists() otherwise it's results get cached again before
  // the table is created.
  $this
    ->queryColumnInformationInvalidate($name);
  fastcache::cache_clear_all('*', 'tableExists', TRUE);

  // Build the table and its unique keys in a transaction, and fail the whole
  // creation in case of an error.
  $transaction = $this->connection
    ->startTransaction(NULL, DatabaseTransactionSettings::GetDDLCompatibleDefaults());

  // Create the table with a default technical primary key.
  // $this->createTableSql already prefixes the table name, and we must inhibit prefixing at the query level
  // because field default _context_menu_block_active_values definitions can contain string literals with braces.
  $this->connection
    ->query_direct($this
    ->createTableSql($name, $table), array(), array(
    'prefix_tables' => FALSE,
  ));

  // If the spec had a primary key, set it now after all fields have been created.
  // We are creating the keys after creating the table so that createPrimaryKey
  // is able to introspect column definition from the database to calculate index sizes
  // This adds quite quite some overhead, but is only noticeable during table creation.
  if (isset($table['primary key']) && is_array($table['primary key'])) {
    $this
      ->createPrimaryKey($name, $table['primary key']);
  }
  else {
    $this
      ->createTechnicalPrimaryColumn($name);
  }

  // Now all the unique keys.
  if (isset($table['unique keys']) && is_array($table['unique keys'])) {
    foreach ($table['unique keys'] as $key_name => $key) {
      $this
        ->addUniqueKey($name, $key_name, $key);
    }
  }

  // Commit changes until now.
  $transaction
    ->commit();

  // 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
          ->addIndex($name, $key_name, $key);
      } catch (Exception $e) {

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

  // Invalidate introspection cache.
  $this
    ->queryColumnInformationInvalidate($name);
}