class Schema extends Schema (View source)

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

Properties

protected Connection $connection

The database connection.

from  Schema
protected int $placeholder

The placeholder counter.

from  Schema
protected string $defaultSchema

Definition of prefixInfo array structure.

from  Schema
protected string $uniqueIdentifier

A unique identifier for this query object.

from  Schema
protected array $tableInformation

A cache of information about blob columns and sequences of tables.

protected int $maxIdentifierLength

The maximum allowed length for index, primary key and constraint names.

protected string $tempNamespaceName

PostgreSQL's temporary namespace name.

Methods

__construct($connection)

No description

from  Schema
__clone()

Implements the magic __clone function.

from  Schema
uniqueIdentifier()

Returns a unique identifier for this object.

from  Schema
The
nextPlaceholder()

Returns the next placeholder ID for the query.

from  Schema
array
getPrefixInfo(string $table = 'default', bool $add_prefix = TRUE)

Get information about the table name and schema from the prefix.

from  Schema
prefixNonTable($table)

Create names for indexes, primary keys and constraints.

from  Schema
buildTableNameCondition($table_name, $operator = '=', $add_prefix = TRUE)

Build a condition to match a table name against a standard information_schema.

from  Schema
true
tableExists($table)

Check if a table exists.

array
findTables(string $table_expression)

Finds all tables that are like the specified base table name.

true
fieldExists(string $table, string $column)

Check if a column exists in the given table.

array
getFieldTypeMap()

Returns a mapping of Drupal schema field names to DB-native field types.

renameTable($table, $new_name)

Rename a table.

true
dropTable($table)

Drop a table.

addField($table, $field, $spec, $new_keys = [])

Add a new field to a table.

true
dropField($table, $field)

Drop a field.

true
indexExists($table, $name)

Checks if an index exists in the given table.

addPrimaryKey($table, $fields)

Add a primary key.

true
dropPrimaryKey($table)

Drop the primary key.

string[]|false
findPrimaryKeyColumns(string $table)

Finds the primary key columns of a table, from the database.

addUniqueKey($table, $name, $fields)

Add a unique key.

true
dropUniqueKey($table, $name)

Drop a unique key.

addIndex($table, $name, $fields, array $spec)

Add an index.

true
dropIndex($table, $name)

Drop an index.

array
introspectIndexSchema(string $table)

Finds the columns for the primary key, unique keys and indexes of a table.

changeField($table, $field, $field_new, $spec, $new_keys = [])

Change a field definition.

createTable($name, $table)

Create a new table from a Drupal table definition.

from  Schema
An
fieldNames($fields)

Return an array of field names from an array of key/index column specifiers.

from  Schema
The
prepareComment($comment, $length = NULL)

Prepare a table or column comment for database query.

from  Schema
string|int|float
escapeDefaultValue(mixed $value)

Return an escaped version of its parameter to be used as a default value on a column.

from  Schema
ensureNotNullPrimaryKey(array $primary_key, array $fields)

Ensures that all the primary key fields are correctly defined.

from  Schema
string
ensureIdentifiersLength(string $table_identifier_part, string $column_identifier_part, string $tag, string $separator = '__')

Make sure to limit identifiers according to PostgreSQL compiled in length.

mixed|object
queryTableInformation(string $table)

Fetch the list of blobs and sequences used on a table.

string
getTempNamespaceName()

Gets PostgreSQL's temporary namespace name.

resetTableInformation($table)

Resets information about table blobs, sequences and serial fields.

array
queryFieldInformation(string $table, string $field, string $constraint_type = 'c')

Fetches the list of constraints used on a field.

array
createTableSql(string $name, array $table)

Generate SQL to create a new table from a Drupal schema definition.

createFieldSql($name, $spec)

Create an SQL string for a field to be used in table creation or alteration.

processField($field)

Set database-engine specific properties for a field.

_createKeySql($fields)

No description

createPrimaryKeySql($fields)

Create the SQL expression for primary keys.

bool
constraintExists(string $table, string $name)

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

_createIndexSql($table, $name, $fields)

No description

_createKeys($table, $new_keys)

No description

getComment($table, $column = NULL)

Retrieve a table or column comment.

string
hashBase64($data)

Calculates a base-64 encoded, PostgreSQL-safe sha-256 hash per PostgreSQL documentation: 4.1. Lexical Structure.

bool
extensionExists(string $name)

Determines whether the PostgreSQL extension is created.

Details

__construct($connection)

No description

Parameters

$connection

__clone()

Implements the magic __clone function.

uniqueIdentifier()

Returns a unique identifier for this object.

The nextPlaceholder()

Returns the next placeholder ID for the query.

Return Value

The

next available placeholder ID as an integer.

protected array getPrefixInfo(string $table = 'default', bool $add_prefix = TRUE)

Get information about the table name and schema from the prefix.

Parameters

string $table

Name of table to look prefix up for. Defaults to 'default' because that's default key for prefix.

bool $add_prefix

Boolean that indicates whether the given table name should be prefixed.

Return Value

array

A keyed array with information about the schema, table name and prefix.

prefixNonTable($table)

Create names for indexes, primary keys and constraints.

This prevents using } around non-table names like indexes and keys.

Parameters

$table

protected Condition buildTableNameCondition($table_name, $operator = '=', $add_prefix = TRUE)

Build a condition to match a table name against a standard information_schema.

The information_schema is a SQL standard that provides information about the database server and the databases, schemas, tables, columns and users within it. This makes information_schema a useful tool to use across the drupal database drivers and is used by a few different functions. The function below describes the conditions to be meet when querying information_schema.tables for drupal tables or information associated with drupal tables. Even though this is the standard method, not all databases follow standards and so this method should be overwritten by a database driver if the database provider uses alternate methods. Because information_schema.tables is used in a few different functions, a database driver will only need to override this function to make all the others work. For example see core/includes/databases/mysql/schema.inc.

Parameters

$table_name

The name of the table in question.

$operator

The operator to apply on the 'table' part of the condition.

$add_prefix

Boolean to indicate whether the table name needs to be prefixed.

Return Value

Condition

A Condition object.

true tableExists($table)

Check if a table exists.

Parameters

$table

The name of the table in drupal (no prefixing).

Return Value

true

if the given table exists, otherwise FALSE.

array findTables(string $table_expression)

Finds all tables that are like the specified base table name.

Parameters

string $table_expression

An SQL expression, for example "cache_%" (without the quotes).

Return Value

array

Both the keys and the values are the matching tables.

true fieldExists(string $table, string $column)

Check if a column exists in the given table.

Parameters

string $table

The name of the table in drupal (no prefixing).

string $column

The name of the column.

Return Value

true

if the given column exists, otherwise FALSE.

array getFieldTypeMap()

Returns a mapping of Drupal schema field names to DB-native field types.

Because different field types do not map 1:1 between databases, Drupal has its own normalized field type names. This function returns a driver-specific mapping table from Drupal names to the native names for each database.

Return Value

array

An array of Schema API field types to driver-specific field types.

renameTable($table, $new_name)

Rename a table.

Parameters

$table

The table to be renamed.

$new_name

The new name for the table.

Exceptions

SchemaObjectDoesNotExistException
SchemaObjectExistsException

true dropTable($table)

Drop a table.

Parameters

$table

The table to be dropped.

Return Value

true

if the table was successfully dropped, FALSE if there was no table by that name to begin with.

addField($table, $field, $spec, $new_keys = [])

Add a new field to a table.

Parameters

$table

Name of the table to be altered.

$field

Name of the field to be added.

$spec

The field specification array, as taken from a schema definition. The specification may also contain the key 'initial', the newly created field will be set to the value of the key in all rows. This is most useful for creating NOT NULL columns with no default value in existing tables. Alternatively, the 'initial_form_field' key may be used, which will auto-populate the new field with values from the specified field.

$new_keys

Exceptions

SchemaObjectDoesNotExistException
SchemaObjectExistsException

true dropField($table, $field)

Drop a field.

Parameters

$table

The table to be altered.

$field

The field to be dropped.

Return Value

true

if the field was successfully dropped, FALSE if there was no field by that name to begin with.

true indexExists($table, $name)

Checks if an index exists in the given table.

Parameters

$table

The name of the table in drupal (no prefixing).

$name

The name of the index in drupal (no prefixing).

Return Value

true

if the given index exists, otherwise FALSE.

addPrimaryKey($table, $fields)

Add a primary key.

Parameters

$table

The table to be altered.

$fields

Fields for the primary key.

Exceptions

SchemaObjectDoesNotExistException
SchemaObjectExistsException

true dropPrimaryKey($table)

Drop the primary key.

Parameters

$table

The table to be altered.

Return Value

true

if the primary key was successfully dropped, FALSE if there was no primary key on this table to begin with.

protected string[]|false findPrimaryKeyColumns(string $table)

Finds the primary key columns of a table, from the database.

Parameters

string $table

The name of the table.

Return Value

string[]|false

A simple array with the names of the columns composing the table's primary key, or FALSE if the table does not exist.

Exceptions

RuntimeException

addUniqueKey($table, $name, $fields)

Add a unique key.

Parameters

$table

The table to be altered.

$name

The name of the key.

$fields

An array of field names.

Exceptions

SchemaObjectDoesNotExistException
SchemaObjectExistsException

true dropUniqueKey($table, $name)

Drop a unique key.

Parameters

$table

The table to be altered.

$name

The name of the key.

Return Value

true

if the key was successfully dropped, FALSE if there was no key by that name to begin with.

addIndex($table, $name, $fields, array $spec)

Add an index.

Parameters

$table

The table to be altered.

$name

The name of the index.

$fields

An array of field names or field information; if field information is passed, it's an array whose first element is the field name and whose second is the maximum length in the index. For example, the following will use the full length of the foo field, but limit the bar field to 4 characters: @code $fields = ['foo', ['bar', 4]]; @endcode

array $spec

The table specification for the table to be altered. This is used in order to be able to ensure that the index length is not too long. This schema definition can usually be obtained through hook_schema(), or in case the table was created by the Entity API, through the schema handler listed in the entity class definition. For reference, see SqlContentEntityStorageSchema::getDedicatedTableSchema() and SqlContentEntityStorageSchema::getSharedTableFieldSchema().

In order to prevent human error, it is recommended to pass in the complete table specification. However, in the edge case of the complete table specification not being available, we can pass in a partial table definition containing only the fields that apply to the index: @code $spec = [ // Example partial specification for a table: 'fields' => [ 'example_field' => [ 'description' => 'An example field', 'type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => '', ], ], 'indexes' => [ 'table_example_field' => ['example_field'], ], ]; @endcode Note that the above is a partial table definition and that we would usually pass a complete table definition as obtained through hook_schema() instead.

Exceptions

SchemaObjectDoesNotExistException
SchemaObjectExistsException

true dropIndex($table, $name)

Drop an index.

Parameters

$table

The table to be altered.

$name

The name of the index.

Return Value

true

if the index was successfully dropped, FALSE if there was no index by that name to begin with.

protected array introspectIndexSchema(string $table)

Finds the columns for the primary key, unique keys and indexes of a table.

Parameters

string $table

The name of the table.

Return Value

array

A schema array with the following keys: 'primary key', 'unique keys' and 'indexes', and values as arrays of database columns.

Exceptions

SchemaObjectDoesNotExistException
RuntimeException

changeField($table, $field, $field_new, $spec, $new_keys = [])

Change a field definition.

IMPORTANT NOTE: To maintain database portability, you have to explicitly recreate all indices and primary keys that are using the changed field.

That means that you have to drop all affected keys and indexes with Schema::dropPrimaryKey(), Schema::dropUniqueKey(), or Schema::dropIndex() before calling ::changeField(). To recreate the keys and indices, pass the key definitions as the optional $keys_new argument directly to ::changeField().

For example, suppose you have:

Parameters

$table

Name of the table.

$field

Name of the field to change.

$field_new

New name for the field (set to the same as $field if you don't want to change the name).

$spec

The field specification for the new field.

$new_keys

Exceptions

SchemaObjectDoesNotExistException
SchemaObjectExistsException

createTable($name, $table)

Create a new table from a Drupal table definition.

Parameters

$name

The name of the table to create.

$table

A Schema API table definition array.

Exceptions

SchemaObjectExistsException

An fieldNames($fields)

Return an array of field names from an array of key/index column specifiers.

This is usually an identity function but if a key/index uses a column prefix specification, this function extracts just the name.

Parameters

$fields

An array of key/index column specifiers.

Return Value

An

array of field names.

The prepareComment($comment, $length = NULL)

Prepare a table or column comment for database query.

Parameters

$comment

The comment string to prepare.

$length

Optional upper limit on the returned string length.

Return Value

The

prepared comment.

protected string|int|float escapeDefaultValue(mixed $value)

Return an escaped version of its parameter to be used as a default value on a column.

Parameters

mixed $value

The value to be escaped (int, float, null or string).

Return Value

string|int|float

The escaped value.

protected ensureNotNullPrimaryKey(array $primary_key, array $fields)

Ensures that all the primary key fields are correctly defined.

Parameters

array $primary_key

An array containing the fields that will form the primary key of a table.

array $fields

An array containing the field specifications of the table, as per the schema data structure format.

Exceptions

SchemaException

protected string ensureIdentifiersLength(string $table_identifier_part, string $column_identifier_part, string $tag, string $separator = '__')

Make sure to limit identifiers according to PostgreSQL compiled in length.

PostgreSQL allows in standard configuration identifiers no longer than 63 chars for table/relation names, indexes, primary keys, and constraints. So we map all identifiers that are too long to drupal_base64hash_tag, where tag is one of:

  • idx for indexes
  • key for constraints
  • pkey for primary keys
  • seq for sequences

Parameters

string $table_identifier_part

The first argument used to build the identifier string. This usually refers to a table/relation name.

string $column_identifier_part

The second argument used to build the identifier string. This usually refers to one or more column names.

string $tag

The identifier tag. It can be one of 'idx', 'key', 'pkey' or 'seq'.

string $separator

(optional) The separator used to glue together the aforementioned identifier parts. Defaults to '__'.

Return Value

string

The index/constraint/pkey identifier.

mixed|object queryTableInformation(string $table)

Fetch the list of blobs and sequences used on a table.

We introspect the database to collect the information required by insert and update queries.

Parameters

string $table

The non-prefixed name of the table.

Return Value

mixed|object

An object with two member variables:

  • 'blob_fields' that lists all the blob fields in the table.
  • 'sequences' that lists the sequences used in that table.

Exceptions

Exception

protected string getTempNamespaceName()

Gets PostgreSQL's temporary namespace name.

Return Value

string

PostgreSQL's temporary namespace name.

protected resetTableInformation($table)

Resets information about table blobs, sequences and serial fields.

Parameters

$table

The non-prefixed name of the table.

array queryFieldInformation(string $table, string $field, string $constraint_type = 'c')

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:

Return Value

array

An array containing all the constraint names for the field.

Exceptions

Exception

protected array createTableSql(string $name, array $table)

Generate SQL to create a new table from a Drupal schema definition.

Parameters

string $name

The name of the table to create.

array $table

A Schema API table definition array.

Return Value

array

An array of SQL statements to create the table.

protected createFieldSql($name, $spec)

Create an SQL string for a field to be used in table creation or alteration.

Parameters

$name

Name of the field.

$spec

The field specification, as per the schema data structure format.

protected processField($field)

Set database-engine specific properties for a field.

Parameters

$field

A field description array, as specified in the schema documentation.

protected _createKeySql($fields)

No description

Parameters

$fields

protected createPrimaryKeySql($fields)

Create the SQL expression for primary keys.

Postgresql does not support key length. It does support fillfactor, but that requires a separate database lookup for each column in the key. The key length defined in the schema is ignored.

Parameters

$fields

bool constraintExists(string $table, string $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.

protected _createIndexSql($table, $name, $fields)

No description

Parameters

$table
$name
$fields

protected _createKeys($table, $new_keys)

No description

Parameters

$table
$new_keys

getComment($table, $column = NULL)

Retrieve a table or column comment.

Parameters

$table
$column

protected string hashBase64($data)

Calculates a base-64 encoded, PostgreSQL-safe sha-256 hash per PostgreSQL documentation: 4.1. Lexical Structure.

Parameters

$data

String to be hashed.

Return Value

string

A base-64 encoded sha-256 hash, with + and / replaced with _ and any = padding characters removed.

bool extensionExists(string $name)

internal  
 

Determines whether the PostgreSQL extension is created.

Parameters

string $name

The name of the extension.

Return Value

bool

Return TRUE when the extension is created, FALSE otherwise.