private function Schema::createPrimaryKey in Drupal driver for SQL Server and SQL Azure 3.0.x
Same name and namespace in other branches
- 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::createPrimaryKey()
- 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::createPrimaryKey()
Create primary key.
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: Table name.
mixed $fields: Array of fields.
int $limit: Size limit.
4 calls to Schema::createPrimaryKey()
- Schema::addPrimaryKey in drivers/
lib/ Drupal/ Driver/ Database/ sqlsrv/ Schema.php - Add a primary key.
- Schema::compressPrimaryKeyIndex in drivers/
lib/ Drupal/ Driver/ Database/ sqlsrv/ Schema.php - Compress Primary key Index.
- Schema::createTable in drivers/
lib/ Drupal/ Driver/ Database/ sqlsrv/ Schema.php - Create a new table from a Drupal table definition.
- Schema::recreatePrimaryKey in drivers/
lib/ Drupal/ Driver/ Database/ sqlsrv/ Schema.php - Recreate primary key.
File
- drivers/
lib/ Drupal/ Driver/ Database/ sqlsrv/ Schema.php, line 1286
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 https://web.archive.org/web/20140510074940/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);
$size = $this
->calculateClusteredIndexRowSizeBytes($table, $this
->createKeySql($fields, TRUE));
$result = [];
$index = FALSE;
// Add support for nullable columns in a primary key.
$nullable = FALSE;
$field_specs = $this
->loadFieldsSpec($fields, $table);
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[] = self::COMPUTED_PK_COLUMN_NAME . " AS (CONVERT(VARCHAR(32), HASHBYTES('MD5', CONCAT('',{$csv_fields})), 2)) PERSISTED NOT NULL";
$result[] = "CONSTRAINT {{$table}}_pkey PRIMARY KEY CLUSTERED (" . self::COMPUTED_PK_COLUMN_NAME . ")";
$index = TRUE;
}
else {
$result[] = "CONSTRAINT {{$table}}_pkey PRIMARY KEY CLUSTERED ({$csv_fields})";
}
$this->connection
->queryDirect('ALTER TABLE [{' . $table . '}] ADD ' . implode(' ', $result));
$this
->resetColumnInformation($table);
// 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, self::COMPUTED_PK_COLUMN_INDEX, $fields);
}
}