function Schema::constraintExists

Same name in other branches
  1. 9 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::constraintExists()
  2. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php \Drupal\Core\Database\Driver\pgsql\Schema::constraintExists()
  3. 10 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\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/modules/pgsql/src/Driver/Database/pgsql/Schema.php
Schema::addPrimaryKey in core/modules/pgsql/src/Driver/Database/pgsql/Schema.php
Schema::addUniqueKey in core/modules/pgsql/src/Driver/Database/pgsql/Schema.php
Schema::dropPrimaryKey in core/modules/pgsql/src/Driver/Database/pgsql/Schema.php
Schema::dropUniqueKey in core/modules/pgsql/src/Driver/Database/pgsql/Schema.php

File

core/modules/pgsql/src/Driver/Database/pgsql/Schema.php, line 750

Class

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

Namespace

Drupal\pgsql\Driver\Database\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();
}

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