You are here

protected function Schema::introspectIndexSchema in Drupal 10

Same name in this branch
  1. 10 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::introspectIndexSchema()
  2. 10 core/modules/sqlite/src/Driver/Database/sqlite/Schema.php \Drupal\sqlite\Driver\Database\sqlite\Schema::introspectIndexSchema()
  3. 10 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::introspectIndexSchema()
  4. 10 core/modules/mysql/src/Driver/Database/mysql/Schema.php \Drupal\mysql\Driver\Database\mysql\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

core/modules/pgsql/src/Driver/Database/pgsql/Schema.php, line 853

Class

Schema
PostgreSQL implementation of \Drupal\Core\Database\Schema.

Namespace

Drupal\pgsql\Driver\Database\pgsql

Code

protected function introspectIndexSchema($table) {
  if (!$this
    ->tableExists($table)) {
    throw new SchemaObjectDoesNotExistException("The table {$table} doesn't exist.");
  }
  $index_schema = [
    'primary key' => [],
    'unique keys' => [],
    'indexes' => [],
  ];

  // Get the schema and tablename for the table without identifier quotes.
  $full_name = str_replace('"', '', $this->connection
    ->prefixTables('{' . $table . '}'));
  $result = $this->connection
    ->query("SELECT i.relname AS index_name, a.attname AS column_name FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND a.attnum = ANY(ix.indkey) AND t.relkind = 'r' AND t.relname = :table_name ORDER BY index_name ASC, column_name ASC", [
    ':table_name' => $full_name,
  ])
    ->fetchAll();
  foreach ($result as $row) {
    if (preg_match('/_pkey$/', $row->index_name)) {
      $index_schema['primary key'][] = $row->column_name;
    }
    elseif (preg_match('/_key$/', $row->index_name)) {
      $index_schema['unique keys'][$row->index_name][] = $row->column_name;
    }
    elseif (preg_match('/_idx$/', $row->index_name)) {
      $index_schema['indexes'][$row->index_name][] = $row->column_name;
    }
  }
  return $index_schema;
}