Schema::constraintExists

public Schema::constraintExists($table, $name)

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

Parameters

string $table: The name of the table.

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

Return value

bool TRUE if the constraint exists, FALSE otherwise.

File

core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php, line 600

Class

Schema
PostgreSQL implementation of \Drupal\Core\Database\Schema.

Namespace

Drupal\Core\Database\Driver\pgsql

Code

public function constraintExists($table, $name) {
  // ::ensureIdentifiersLength() expects three parameters, although not
  // explicitly stated in its signature, 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 + 2);
    $name = substr($name, 0, $pos);
  }
  $constraint_name = $this->ensureIdentifiersLength($table, $name, $suffix);
  // Remove leading and trailing quotes because the index name is in a WHERE
  // clause and not used as an identifier.
  $constraint_name = str_replace('"', '', $constraint_name);
  return (bool) $this->connection->query("SELECT 1 FROM pg_constraint WHERE conname = '$constraint_name'")->fetchField();
}
doc_Drupal
2016-10-29 09:39:47
Comments
Leave a Comment

Please login to continue.