You are here

protected function Schema::createIndexSql 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::createIndexSql()
  2. 4.0.x src/Driver/Database/sqlsrv/Schema.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Schema::createIndexSql()
  3. 4.1.x src/Driver/Database/sqlsrv/Schema.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Schema::createIndexSql()

Returns the SQL needed to create an index.

Supports XML indexes. Incomplete.

Parameters

string $table: Table to create the index on.

string $name: Name of the index.

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

mixed $xml_field: The xml field.

Return value

string SQL string.

1 call to Schema::createIndexSql()
Schema::addIndex in src/Driver/Database/sqlsrv/Schema.php
Add an index.

File

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

Class

Schema

Namespace

Drupal\sqlsrv\Driver\Database\sqlsrv

Code

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

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

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

  // 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)) {
    $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})";
  }
}