You are here

public function Schema::constraintExists in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php \Drupal\Core\Database\Driver\pgsql\Schema::constraintExists()

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.

5 calls to Schema::constraintExists()
Schema::addField in core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
Add a new field to a table.
Schema::addPrimaryKey in core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
Add a primary key.
Schema::addUniqueKey in core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
Add a unique key.
Schema::dropPrimaryKey in core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
Drop the primary key.
Schema::dropUniqueKey in core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
Drop a unique key.

File

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

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