You are here

protected function Schema::dropFieldRelatedObjects 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::dropFieldRelatedObjects()
  2. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::dropFieldRelatedObjects()

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

@status complete

2 calls to Schema::dropFieldRelatedObjects()
Schema::changeField in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Override DatabaseSchema::changeField().
Schema::dropField in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Override DatabaseSchema::dropField().

File

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

Class

Schema

Namespace

Drupal\Driver\Database\sqlsrv

Code

protected function dropFieldRelatedObjects($table, $field) {

  // Fetch the list of indexes referencing this column.
  $indexes = $this->connection
    ->query('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('DROP INDEX [' . $index->name . '] ON [{' . $table . '}]');
  }

  // Fetch the list of check constraints referencing this column.
  $constraints = $this->connection
    ->query('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('ALTER TABLE [{' . $table . '}] DROP CONSTRAINT [' . $constraint->name . ']');
  }

  // Fetch the list of default constraints referencing this column.
  $constraints = $this->connection
    ->query('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('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);
  }
}