You are here

public function DatabaseSchema_sqlsrv::addPrimaryKey in Drupal driver for SQL Server and SQL Azure 7.3

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

Override DatabaseSchema::addPrimaryKey().

@status tested

Overrides DatabaseSchema::addPrimaryKey

1 call to DatabaseSchema_sqlsrv::addPrimaryKey()
DatabaseSchema_sqlsrv::recreateTableKeys in sqlsrv/schema.inc
Re-create keys associated to a table.

File

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

Class

DatabaseSchema_sqlsrv

Code

public function addPrimaryKey($table, $fields) {
  if (!$this
    ->tableExists($table, TRUE)) {
    throw new DatabaseSchemaObjectDoesNotExistException(t("Cannot add primary key to table %table: table doesn't exist.", array(
      '%table' => $table,
    )));
  }
  if ($primary_key_name = $this
    ->primaryKeyName($table)) {
    if ($this
      ->isTechnicalPrimaryKey($primary_key_name)) {

      // Destroy the existing technical primary key.
      $this->connection
        ->query_direct('ALTER TABLE [{' . $table . '}] DROP CONSTRAINT [' . $primary_key_name . ']');
      $this
        ->cleanUpTechnicalPrimaryColumn($table);
    }
    else {
      throw new DatabaseSchemaObjectExistsException(t("Cannot add primary key to table %table: primary key already exists.", array(
        '%table' => $table,
      )));
    }
  }

  // The size limit of the primary key depends on the
  // cohexistance with an XML field.
  if ($this
    ->tableHasXmlIndex($table)) {
    $this
      ->createPrimaryKey($table, $fields, 128);
  }
  else {
    $this
      ->createPrimaryKey($table, $fields);
  }
  return TRUE;
}