W3cubDocs

/Drupal 8

public function 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 595

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();
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Database!Driver!pgsql!Schema.php/function/Schema::constraintExists/8.1.x