You are here

protected function DatabaseSchema_sqlsrv::introspectPrimaryKey in Drupal driver for SQL Server and SQL Azure 7

1 call to DatabaseSchema_sqlsrv::introspectPrimaryKey()
DatabaseSchema_sqlsrv::changeField in sqlsrv/schema.inc
Override DatabaseSchema::changeField().

File

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

Class

DatabaseSchema_sqlsrv

Code

protected function introspectPrimaryKey($table, $field) {

  // Fetch the list of columns participating to the primary key.
  $result = $this->connection
    ->query('SELECT i.name, ic.is_descending_key, c.name column_name
      FROM sys.columns c
       INNER JOIN sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
       INNER JOIN sys.indexes i ON i.object_id = ic.object_id AND i.index_id = ic.index_id
      WHERE i.object_id = OBJECT_ID(:table) AND i.is_primary_key = 1 ORDER BY ic.key_ordinal', array(
    ':table' => $this->connection
      ->prefixTables('{' . $table . '}'),
  ));
  $columns = array();
  $valid = FALSE;
  foreach ($result as $column) {
    if ($column->column_name == $field) {
      $valid = TRUE;
    }
    $columns[] = '[' . $column->column_name . ']' . ($column->is_descending_key ? ' DESC' : '');
  }
  if ($valid) {
    return 'ADD CONSTRAINT [' . $column->name . '] PRIMARY KEY CLUSTERED (' . implode(', ', $columns) . ')';
  }
}