You are here

protected function DbDumpCommand::getTableIndexes in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Command/DbDumpCommand.php \Drupal\Core\Command\DbDumpCommand::getTableIndexes()

Adds primary key, unique keys, and index information to the schema.

Parameters

\Drupal\Core\Database\Connection $connection: The database connection to use.

string $table: The table to find indexes for.

array &$definition: The schema definition to modify.

1 call to DbDumpCommand::getTableIndexes()
DbDumpCommand::getTableSchema in core/lib/Drupal/Core/Command/DbDumpCommand.php
Returns a schema array for a given table.

File

core/lib/Drupal/Core/Command/DbDumpCommand.php, line 226
Contains \Drupal\Core\Command\DbDumpCommand.

Class

DbDumpCommand
Provides a command to dump the current database to a script.

Namespace

Drupal\Core\Command

Code

protected function getTableIndexes(Connection $connection, $table, &$definition) {

  // Note, this query doesn't support ordering, so that is worked around
  // below by keying the array on Seq_in_index.
  $query = $connection
    ->query("SHOW INDEX FROM {" . $table . "}");
  while (($row = $query
    ->fetchAssoc()) !== FALSE) {
    $index_name = $row['Key_name'];
    $column = $row['Column_name'];

    // Key the arrays by the index sequence for proper ordering (start at 0).
    $order = $row['Seq_in_index'] - 1;

    // If specified, add length to the index.
    if ($row['Sub_part']) {
      $column = [
        $column,
        $row['Sub_part'],
      ];
    }
    if ($index_name === 'PRIMARY') {
      $definition['primary key'][$order] = $column;
    }
    elseif ($row['Non_unique'] == 0) {
      $definition['unique keys'][$index_name][$order] = $column;
    }
    else {
      $definition['indexes'][$index_name][$order] = $column;
    }
  }
}