class Schema extends Schema (View source)

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

Constants

COMMENT_MAX_TABLE

Maximum length of a table comment in MySQL.

COMMENT_MAX_COLUMN

Maximum length of a column comment in MySQL.

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 $mysqlStringTypes

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 and database name from the prefix.

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.

true
tableExists($table)

Check if a table exists.

array
findTables(string $table_expression)

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

from  Schema
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, $keys_new = [])

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, $keys_new = [])

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.

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
An
createTableSql($name, $table)

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

createFieldSql(string $name, array $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.

createKeysSql($spec)

No description

array
getNormalizedIndexes(array $spec)

Gets normalized indexes from a table specification.

shortenIndex(array $index)

Helper function for normalizeIndexes().

createKeySql($fields)

No description

getComment($table, $column = NULL)

Retrieve a table or column comment.

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 and database name 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.

MySQL uses databases like schemas rather than catalogs so when we build a condition to query the information_schema.tables, we set the default database as the schema unless specified otherwise, and exclude table_catalog from the condition criteria.

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, $keys_new = [])

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.

$keys_new

(optional) Keys and indexes specification to be created on the table along with adding the field. The format is the same as a table specification but without the 'fields' element. If you are adding a type 'serial' field, you MUST specify at least one key or index including it in this array. See ::changeField() for more explanation why.

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, $keys_new = [])

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.

$keys_new

(optional) Keys and indexes specification to be created on the table along with changing the field. The format is the same as a table specification but without the 'fields' element.

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 An createTableSql($name, $table)

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

Parameters

$name

The name of the table to create.

$table

A Schema API table definition array.

Return Value

An

array of SQL statements to create the table.

protected createFieldSql(string $name, array $spec)

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

Parameters

string $name

Name of the field.

array $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 createKeysSql($spec)

No description

Parameters

$spec

protected array getNormalizedIndexes(array $spec)

Gets normalized indexes from a table specification.

Shortens indexes to 191 characters if they apply to utf8mb4-encoded fields, in order to comply with the InnoDB index limitation of 756 bytes.

Parameters

array $spec

The table specification.

Return Value

array

List of shortened indexes.

Exceptions

SchemaException

protected shortenIndex(array $index)

Helper function for normalizeIndexes().

Shortens an index to 191 characters.

Parameters

array $index

The index array to be used in createKeySql.

See also

\Drupal\Core\Database\Driver\mysql\Drupal\Core\Database\Driver\mysql\Schema::createKeySql()
\Drupal\Core\Database\Driver\mysql\Drupal\Core\Database\Driver\mysql\Schema::normalizeIndexes()

protected createKeySql($fields)

No description

Parameters

$fields

getComment($table, $column = NULL)

Retrieve a table or column comment.

Parameters

$table
$column