You are here

protected function Schema::createIndexSql in Drupal driver for SQL Server and SQL Azure 8

Same name and namespace in other branches
  1. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::createIndexSql()
  2. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::createIndexSql()

Returns the SQL needed (incomplete) to create and index. Supports XML indexes.

Parameters

string $table: Table to create the index on.

string $name: Name of the index.

array $fields: Fields to be included in the Index.

Return value

string

1 call to Schema::createIndexSql()
Schema::addIndex in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Override DatabaseSchema::addIndex().

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php, line 778
Definition of Drupal\Driver\Database\sqlsrv\Schema

Class

Schema

Namespace

Drupal\Driver\Database\sqlsrv

Code

protected function createIndexSql($table, $name, $fields, &$xml_field) {

  // Get information about current columns.
  $info = $this
    ->queryColumnInformation($table);

  // Flatten $fields array if neccesary.
  $fields = $this
    ->createKeySql($fields, TRUE);

  // Look if an XML column is present in the fields list.
  $xml_field = NULL;
  foreach ($fields as $field) {
    if (isset($info['columns'][$field]['type']) && $info['columns'][$field]['type'] == 'xml') {
      $xml_field = $field;
      break;
    }
  }

  // XML indexes can only have 1 column.
  if (!empty($xml_field) && isset($fields[1])) {
    throw new \Exception("Cannot include an XML field on a multiple column index.");
  }

  // No more than one XML index per table.
  if ($xml_field && $this
    ->tableHasXmlIndex($table)) {
    throw new \Exception("Only one primary clustered XML index is allowed per table.");
  }
  if (empty($xml_field)) {

    // TODO: As we are already doing with primary keys, when a user requests
    // an index that is too big for SQL Server (> 900 bytes) this could be dependant
    // on a computed hash column.
    $fields_csv = implode(', ', $fields);
    return "CREATE INDEX {$name}_idx ON [{{$table}}] ({$fields_csv})";
  }
  else {
    return "CREATE PRIMARY XML INDEX {$name}_idx ON [{{$table}}] ({$xml_field})";
  }
}