You are here

protected function DatabaseSchema_sqlsrv::createIndexSql in Drupal driver for SQL Server and SQL Azure 7.2

Same name and namespace in other branches
  1. 7.3 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::createIndexSql()
  2. 7 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::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 DatabaseSchema_sqlsrv::createIndexSql()
DatabaseSchema_sqlsrv::addIndex in sqlsrv/schema.inc
Override DatabaseSchema::addIndex().

File

sqlsrv/schema.inc, line 769
Database schema code for Microsoft SQL Server database servers.

Class

DatabaseSchema_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})";
  }
}