You are here

protected function Schema::createTechnicalPrimaryColumn 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::createTechnicalPrimaryColumn()
  2. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::createTechnicalPrimaryColumn()

Make sure that this table has a technical primary key.

Parameters

string $table:

3 calls to Schema::createTechnicalPrimaryColumn()
Schema::addUniqueKey in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Override DatabaseSchema::addUniqueKey().
Schema::createTable in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
{@Inheritdoc}
Schema::dropPrimaryKey in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Override DatabaseSchema::dropPrimaryKey().

File

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

Class

Schema

Namespace

Drupal\Driver\Database\sqlsrv

Code

protected function createTechnicalPrimaryColumn($table) {
  $real_table = $this->connection
    ->prefixTable($table);

  // Make sure the column exists.
  if (!$this
    ->fieldExists($table, $this->TECHNICAL_PK_COLUMN_NAME)) {
    $this->connection
      ->query_direct("ALTER TABLE {$real_table} ADD {$this->TECHNICAL_PK_COLUMN_NAME} UNIQUEIDENTIFIER DEFAULT NEWID() NOT NULL");
  }

  // Make sure the index exists.
  $index = $this
    ->getTechnicalPrimaryKeyIndexName($table);
  if (!$this->connection
    ->Scheme()
    ->IndexExists($real_table, $index)) {

    // Wether or not the TKP is a PRIMARY KEY depends
    // on the existence of OTHER primary keys.
    if (empty($this
      ->introspectPrimaryKeyFields($table))) {
      $this->connection
        ->query_direct("ALTER TABLE {$real_table} ADD CONSTRAINT {$index} PRIMARY KEY CLUSTERED ({$this->TECHNICAL_PK_COLUMN_NAME})");
    }
    else {
      $this->connection
        ->query_direct("CREATE UNIQUE INDEX {$index} ON [{$real_table}] ({$this->TECHNICAL_PK_COLUMN_NAME})");
    }
  }
}