You are here

protected function Schema::dropFieldRelatedObjects in Drupal driver for SQL Server and SQL Azure 4.0.x

Same name and namespace in other branches
  1. 4.2.x src/Driver/Database/sqlsrv/Schema.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Schema::dropFieldRelatedObjects()
  2. 3.1.x src/Driver/Database/sqlsrv/Schema.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Schema::dropFieldRelatedObjects()
  3. 4.1.x src/Driver/Database/sqlsrv/Schema.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Schema::dropFieldRelatedObjects()

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

Parameters

mixed $table: Table name.

mixed $field: Field name.

2 calls to Schema::dropFieldRelatedObjects()
Schema::changeField in src/Driver/Database/sqlsrv/Schema.php
Change a field definition.
Schema::dropField in src/Driver/Database/sqlsrv/Schema.php
Should this be in a Transaction?

File

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

Class

Schema

Namespace

Drupal\sqlsrv\Driver\Database\sqlsrv

Code

protected function dropFieldRelatedObjects($table, $field) {
  $prefixInfo = $this
    ->getPrefixInfo($table, TRUE);

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

  // Fetch the list of check constraints referencing this column.
  $sql = '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';
  $constraints = $this->connection
    ->query($sql, [
    ':table' => $prefixInfo['table'],
    ':name' => $field,
  ]);
  foreach ($constraints as $constraint) {
    $this
      ->dropConstraint($table, $constraint->name, FALSE);
  }

  // Fetch the list of default constraints referencing this column.
  $sql = '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';
  $constraints = $this->connection
    ->query($sql, [
    ':table' => $prefixInfo['table'],
    ':name' => $field,
  ]);
  foreach ($constraints as $constraint) {
    $this
      ->dropConstraint($table, $constraint->name, FALSE);
  }

  // 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);
  if (isset($data['columns'][self::COMPUTED_PK_COLUMN_NAME]['dependencies'][$field])) {
    $this
      ->cleanUpPrimaryKey($table);
  }
}