function DatabaseSchema_pgsql::constraintExists

Helper function: check if a constraint (PK, FK, UK) exists.

Parameters

$table: The name of the table.

$name: The name of the constraint (typically 'pkey' or '[constraint]_key').

4 calls to DatabaseSchema_pgsql::constraintExists()
DatabaseSchema_pgsql::addPrimaryKey in includes/database/pgsql/schema.inc
Add a primary key.
DatabaseSchema_pgsql::addUniqueKey in includes/database/pgsql/schema.inc
Add a unique key.
DatabaseSchema_pgsql::dropPrimaryKey in includes/database/pgsql/schema.inc
Drop the primary key.
DatabaseSchema_pgsql::dropUniqueKey in includes/database/pgsql/schema.inc
Drop a unique key.

File

includes/database/pgsql/schema.inc, line 627

Class

DatabaseSchema_pgsql

Code

protected function constraintExists($table, $name) {
  // ensureIdentifiersLength() expects three parameters, thus we split our
  // constraint name in a proper name and a suffix.
  if ($name == 'pkey') {
    $suffix = $name;
    $name = '';
  }
  else {
    $pos = strrpos($name, '_');
    $suffix = substr($name, $pos + 1);
    $name = substr($name, 0, $pos);
  }
  $constraint_name = $this->ensureIdentifiersLength($table, $name, $suffix);
  return (bool) $this->connection
    ->query("SELECT 1 FROM pg_constraint WHERE conname = '{$constraint_name}'")
    ->fetchField();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.