You are here

public function Schema::createTable 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::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 134
Definition of Drupal\Driver\Database\sqlsrv\Schema

Class

Schema

Namespace

Drupal\Driver\Database\sqlsrv

Code

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

  // Add table comment.
  if (!empty($table['description'])) {
    if ($this
      ->tableExists($name)) {
      $comment = $this
        ->prepareComment($table['description'], Scheme::COMMENT_MAX_BYTES);
      $this
        ->CommentCreateOrUpdate($comment, $name);
    }
  }

  // Add column comments.
  foreach ($table['fields'] as $field_name => $field) {
    if (!empty($field['description'])) {
      $comment = $this
        ->prepareComment($table['description'], Scheme::COMMENT_MAX_BYTES);
      $this
        ->CommentCreateOrUpdate($comment, $name, $field_name);
    }
  }

  // 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
    ->getTableIntrospectionInvalidate($name);
}