You are here

protected function Schema::findPrimaryKeyColumns in Drupal driver for SQL Server and SQL Azure 3.0.x

Finds the primary key columns of a table, from the database.

Parameters

string $table: The name of the table.

Return value

string[]|false A simple array with the names of the columns composing the table's primary key, or FALSE if the table does not exist.

Throws

\RuntimeException If the driver does not override this method.

Overrides Schema::findPrimaryKeyColumns

4 calls to Schema::findPrimaryKeyColumns()
Schema::addField in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Add a new field to a table.
Schema::changeField in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Change a field definition.
Schema::dropField in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Should this be in a Transaction?
Schema::introspectIndexSchema in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Finds the columns for the primary key, unique keys and indexes of a table.

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php, line 455

Class

Schema

Namespace

Drupal\Driver\Database\sqlsrv

Code

protected function findPrimaryKeyColumns($table) {
  if (!$this
    ->tableExists($table)) {
    return FALSE;
  }

  // Use already prefixed table name.
  $prefixInfo = $this
    ->getPrefixInfo($table, TRUE);
  $query = "SELECT column_name FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC " . "INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU " . "ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND " . "TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME AND " . "KU.table_name=:table AND column_name != '__pk' AND column_name != '__pkc' " . "ORDER BY KU.ORDINAL_POSITION";
  $result = $this->connection
    ->query($query, [
    ':table' => $prefixInfo['table'],
  ])
    ->fetchAllAssoc('column_name');
  return array_keys($result);
}