You are here

protected function DatabaseSchema_sqlsrv::dropFieldRelatedObjects in Drupal driver for SQL Server and SQL Azure 7.3

Same name and namespace in other branches
  1. 7 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::dropFieldRelatedObjects()
  2. 7.2 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::dropFieldRelatedObjects()

Drop the related objects of a column (indexes, constraints, etc.).

@status complete

2 calls to DatabaseSchema_sqlsrv::dropFieldRelatedObjects()
DatabaseSchema_sqlsrv::changeField in sqlsrv/schema.inc
Override DatabaseSchema::changeField().
DatabaseSchema_sqlsrv::dropField in sqlsrv/schema.inc
Override DatabaseSchema::dropField().

File

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

Class

DatabaseSchema_sqlsrv

Code

protected function dropFieldRelatedObjects($table, $field) {

  // Fetch the list of indexes referencing this column.
  $indexes = $this->connection
    ->query_direct('SELECT DISTINCT i.name FROM sys.columns c INNER JOIN sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id INNER JOIN sys.indexes i ON i.object_id = ic.object_id AND i.index_id = ic.index_id WHERE i.is_primary_key = 0 AND i.is_unique_constraint = 0 AND c.object_id = OBJECT_ID(:table) AND c.name = :name', array(
    ':table' => $this->connection
      ->prefixTables('{' . $table . '}'),
    ':name' => $field,
  ));
  foreach ($indexes as $index) {
    $this->connection
      ->query_direct('DROP INDEX [' . $index->name . '] ON [{' . $table . '}]');
  }

  // Fetch the list of check constraints referencing this column.
  $constraints = $this->connection
    ->query_direct('SELECT DISTINCT cc.name FROM sys.columns c INNER JOIN sys.check_constraints cc ON cc.parent_object_id = c.object_id AND cc.parent_column_id = c.column_id WHERE c.object_id = OBJECT_ID(:table) AND c.name = :name', array(
    ':table' => $this->connection
      ->prefixTables('{' . $table . '}'),
    ':name' => $field,
  ));
  foreach ($constraints as $constraint) {
    $this->connection
      ->query_direct('ALTER TABLE [{' . $table . '}] DROP CONSTRAINT [' . $constraint->name . ']');
  }

  // Fetch the list of default constraints referencing this column.
  $constraints = $this->connection
    ->query_direct('SELECT DISTINCT dc.name FROM sys.columns c INNER JOIN sys.default_constraints dc ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id WHERE c.object_id = OBJECT_ID(:table) AND c.name = :name', array(
    ':table' => $this->connection
      ->prefixTables('{' . $table . '}'),
    ':name' => $field,
  ));
  foreach ($constraints as $constraint) {
    $this->connection
      ->query_direct('ALTER TABLE [{' . $table . '}] DROP CONSTRAINT [' . $constraint->name . ']');
  }

  // Drop any indexes on related computed columns when we have some.
  if ($this
    ->uniqueKeyExists($table, $field)) {
    $this
      ->dropUniqueKey($table, $field);
  }

  // If this column is part of a computed primary key, drop the key.
  $data = $this
    ->queryColumnInformation($table, TRUE);
  if (isset($data['columns'][$this->COMPUTED_PK_COLUMN_NAME]['dependencies'][$field])) {
    $this
      ->cleanUpPrimaryKey($table);
  }
}