function Schema::queryFieldInformation

Same name and namespace in other branches
  1. 9 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::queryFieldInformation()
  2. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php \Drupal\Core\Database\Driver\pgsql\Schema::queryFieldInformation()
  3. 10 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::queryFieldInformation()

Fetches the list of constraints used on a field.

We introspect the database to collect the information required by field alteration.

Parameters

string $table: The non-prefixed name of the table.

string $field: The name of the field.

string $constraint_type: (optional) The type of the constraint. This can be one of the following:

  • c: check constraint;
  • f: foreign key constraint;
  • p: primary key constraint;
  • u: unique constraint;
  • t: constraint trigger;
  • x: exclusion constraint.

Defaults to 'c' for a CHECK constraint. @see https://www.postgresql.org/docs/current/catalog-pg-constraint.html

Return value

array An array containing all the constraint names for the field.

Throws

\Exception Exception thrown when the query for the table information fails.

1 call to Schema::queryFieldInformation()
Schema::changeField in core/modules/pgsql/src/Driver/Database/pgsql/Schema.php
Change a field definition.

File

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

Class

Schema
PostgreSQL implementation of <a href="/api/drupal/core%21lib%21Drupal%21Core%21Database%21Schema.php/class/Schema/11.x" title="Provides a base implementation for Database Schema." class="local">\Drupal\Core\Database\Schema</a>.

Namespace

Drupal\pgsql\Driver\Database\pgsql

Code

public function queryFieldInformation($table, $field, $constraint_type = 'c') {
    assert(in_array($constraint_type, [
        'c',
        'f',
        'p',
        'u',
        't',
        'x',
    ]));
    $prefixInfo = $this->getPrefixInfo($table, TRUE);
    // Split the key into schema and table for querying.
    $schema = $prefixInfo['schema'];
    $table_name = $prefixInfo['table'];
    $this->connection
        ->addSavepoint();
    try {
        $checks = $this->connection
            ->query("SELECT conname FROM pg_class cl INNER JOIN pg_constraint co ON co.conrelid = cl.oid INNER JOIN pg_attribute attr ON attr.attrelid = cl.oid AND attr.attnum = ANY (co.conkey) INNER JOIN pg_namespace ns ON cl.relnamespace = ns.oid WHERE co.contype = :constraint_type AND ns.nspname = :schema AND cl.relname = :table AND attr.attname = :column", [
            ':constraint_type' => $constraint_type,
            ':schema' => $schema,
            ':table' => $table_name,
            ':column' => $field,
        ]);
    } catch (\Exception $e) {
        $this->connection
            ->rollbackSavepoint();
        throw $e;
    }
    $this->connection
        ->releaseSavepoint();
    $field_information = $checks->fetchCol();
    return $field_information;
}

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