public function DatabaseSchema_sqlsrv::addPrimaryKey in Drupal driver for SQL Server and SQL Azure 7
Same name and namespace in other branches
- 7.3 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::addPrimaryKey()
- 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 781 - Database schema code for Microsoft SQL Server database servers.
Class
Code
public function addPrimaryKey($table, $fields) {
if (!$this
->tableExists($table)) {
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('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,
)));
}
}
$this->connection
->query('ALTER TABLE [{' . $table . '}] ADD CONSTRAINT {' . $table . '_pkey} PRIMARY KEY (' . $this
->createKeySql($fields) . ')');
return TRUE;
}