You are here

public function Schema::createTable in Drupal driver for SQL Server and SQL Azure 8

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

{@Inheritdoc}

Overrides Schema::createTable

File

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

Class

Schema

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function createTable($name, $table) {
  if ($this
    ->tableExists($name, FALSE)) {
    throw new SchemaObjectExistsException(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.

  /** @var Transaction $transaction */
  $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.
        // Is there a better way to do this?
        if ($this
          ->tableExists('watchdog')) {
          watchdog_exception('database', $e);
        }
      }
    }
  }
}