You are here

public function Schema::introspectIndexSchema in Drupal driver for SQL Server and SQL Azure 4.2.x

Same name and namespace in other branches
  1. 3.1.x src/Driver/Database/sqlsrv/Schema.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Schema::introspectIndexSchema()
  2. 4.0.x src/Driver/Database/sqlsrv/Schema.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Schema::introspectIndexSchema()
  3. 4.1.x src/Driver/Database/sqlsrv/Schema.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Schema::introspectIndexSchema()

Finds the columns for the primary key, unique keys and indexes of a table.

Parameters

string $table: The name of the table.

Return value

array A schema array with the following keys: 'primary key', 'unique keys' and 'indexes', and values as arrays of database columns.

Throws

\Drupal\Core\Database\SchemaObjectDoesNotExistException If the specified table doesn't exist.

\RuntimeException If the driver does not implement this method.

Overrides Schema::introspectIndexSchema

File

src/Driver/Database/sqlsrv/Schema.php, line 548

Class

Schema

Namespace

Drupal\sqlsrv\Driver\Database\sqlsrv

Code

public function introspectIndexSchema($table) {
  if (!$this
    ->tableExists($table)) {
    throw new SchemaObjectDoesNotExistException("The table {$table} doesn't exist.");
  }
  $index_schema = [
    'primary key' => $this
      ->findPrimaryKeyColumns($table),
    'unique keys' => [],
    'indexes' => [],
  ];
  $column_information = $this
    ->queryColumnInformation($table);
  foreach ($column_information['indexes'] as $key => $values) {
    if ($values['is_primary_key'] !== 1 && $values['data_space_id'] == 1 && $values['is_unique'] == 0) {
      foreach ($values['columns'] as $num => $stats) {
        $index_schema['indexes'][substr($key, 0, -4)][] = $stats['name'];
      }
    }
  }
  foreach ($column_information['columns'] as $name => $spec) {
    if (substr($name, 0, 9) == '__unique_' && $column_information['indexes'][substr($name, 9) . '_unique']['is_unique'] == 1) {
      $definition = $spec['definition'];
      $matches = [];
      preg_match_all("/CONVERT\\(\\[varbinary\\]\\(max\\),\\[([a-zA-Z0-9_]*)\\]/", $definition, $matches);
      foreach ($matches[1] as $match) {
        if ($match != '__pk') {
          $index_schema['unique keys'][substr($name, 9)][] = $match;
        }
      }
    }
  }
  return $index_schema;
}