You are here

public function Schema::createTable in Drupal driver for SQL Server and SQL Azure 3.0.x

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. 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::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

\Drupal\Core\Database\SchemaObjectExistsException If the specified table already exists.

Overrides Schema::createTable

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php, line 998

Class

Schema

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function createTable($name, $table) {

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

  // 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 (!empty($table['primary key']) && is_array($table['primary key'])) {
    $this
      ->ensureNotNullPrimaryKey($table['primary key'], $table['fields']);
    $this
      ->createPrimaryKey($name, $table['primary key']);
  }

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

        // Log the exception but do not rollback the transaction.
        if ($this
          ->tableExists('watchdog')) {
          watchdog_exception('database', $e);
        }
      }
    }
  }
}