private function Schema::createPrimaryKey in Drupal driver for SQL Server and SQL Azure 8.2
Same name and namespace in other branches
- 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::createPrimaryKey()
- 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::createPrimaryKey()
Create a Primary Key for the table, does not drop any prior primary keys neither it takes care of cleaning technical primary column. Only call this if you are sure the table does not currently hold a primary key.
Parameters
string $table:
mixed $fields:
int $limit:
4 calls to Schema::createPrimaryKey()
- Schema::addPrimaryKey in drivers/
lib/ Drupal/ Driver/ Database/ sqlsrv/ Schema.php - Override DatabaseSchema::addPrimaryKey().
- Schema::compressPrimaryKeyIndex in drivers/
lib/ Drupal/ Driver/ Database/ sqlsrv/ Schema.php - Sometimes the size of a table's primary key index needs to be reduced to allow for Primary XML Indexes.
- Schema::createTable in drivers/
lib/ Drupal/ Driver/ Database/ sqlsrv/ Schema.php - {@Inheritdoc}
- Schema::recreatePrimaryKey in drivers/
lib/ Drupal/ Driver/ Database/ sqlsrv/ Schema.php - Drops the current primary key and creates a new one. If the previous primary key was an internal primary key, it tries to cleant it up.
File
- drivers/
lib/ Drupal/ Driver/ Database/ sqlsrv/ Schema.php, line 322 - Definition of Drupal\Driver\Database\sqlsrv\Schema
Class
Namespace
Drupal\Driver\Database\sqlsrvCode
private function createPrimaryKey($table, $fields, $limit = 900) {
// To be on the safe side, on the most restrictive use case the limit
// for a primary key clustered index is of 128 bytes (usually 900).
// @see http://blogs.msdn.com/b/jgalla/archive/2005/08/18/453189.aspx
// If that is going to be exceeded, use a computed column.
$csv_fields = $this
->createKeySql($fields, false);
$real_table = $this->connection
->prefixTable($table);
$size = $this->connection
->Scheme()
->calculateClusteredIndexRowSizeBytes($real_table, $this
->createKeySql($fields, true));
$result = [];
$index = false;
// Add support for nullable columns in a primary key.
$nullable = false;
$field_specs = $this->connection
->schema()
->getColumnIntrospection($table, $fields);
foreach ($field_specs as $field) {
if ($field['is_nullable'] == true) {
$nullable = true;
break;
}
}
if ($nullable || $size >= $limit) {
// Use a computed column instead, and create a custom index.
$result[] = "{$this->COMPUTED_PK_COLUMN_NAME} AS (CONVERT(VARCHAR(32), HASHBYTES('MD5', CONCAT('', {$csv_fields})), 2)) PERSISTED NOT NULL";
$result[] = "CONSTRAINT {$real_table}_pkey PRIMARY KEY CLUSTERED ({$this->COMPUTED_PK_COLUMN_NAME})";
$index = true;
}
else {
$result[] = "CONSTRAINT {$real_table}_pkey PRIMARY KEY CLUSTERED ({$csv_fields})";
}
$add = implode(' ', $result);
$this->connection
->query_direct("ALTER TABLE [{$real_table}] ADD {$add}");
// If we relied on a computed column for the Primary Key,
// at least index the fields with a regular index.
if ($index) {
$this
->addIndex($table, $this->COMPUTED_PK_COLUMN_INDEX, $fields);
}
// Invalidate current introspection.
$this
->getTableIntrospectionInvalidate($table);
}