class Schema extends Schema (View source)

SQLite 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

Override DatabaseSchema::$defaultSchema.

protected string $uniqueIdentifier

A unique identifier for this query object.

from  Schema

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, $specification, $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.

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

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

createIndexSql($tablename, $schema)

Build the SQL expression for indexes.

createColumnsSql($tablename, $schema)

Build the SQL expression for creating columns.

createKeySql($fields)

Build the SQL expression for keys.

processField($field)

Set database-engine specific properties for a field.

createFieldSql($name, $spec)

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

alterTable($table, $old_schema, $new_schema, array $mapping = [])

Create a table with a new schema containing the old content.

An
introspectSchema($table)

Find out the schema of a table.

mapKeyDefinition(array $key_definition, array $mapping)

Utility method: rename columns in an index definition according to a new mapping.

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, $specification, $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.

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

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 createIndexSql($tablename, $schema)

Build the SQL expression for indexes.

Parameters

$tablename
$schema

protected createColumnsSql($tablename, $schema)

Build the SQL expression for creating columns.

Parameters

$tablename
$schema

protected createKeySql($fields)

Build the SQL expression for keys.

Parameters

$fields

protected processField($field)

Set database-engine specific properties for a field.

Parameters

$field

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

protected createFieldSql($name, $spec)

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

Before passing a field out of a schema definition into this function it has to be processed by self::processField().

Parameters

$name

Name of the field.

$spec

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

protected alterTable($table, $old_schema, $new_schema, array $mapping = [])

Create a table with a new schema containing the old content.

As SQLite does not support ALTER TABLE (with a few exceptions) it is necessary to create a new table and copy over the old content.

Parameters

$table

Name of the table to be altered.

$old_schema

The old schema array for the table.

$new_schema

The new schema array for the table.

array $mapping

An optional mapping between the fields of the old specification and the fields of the new specification. An associative array, whose keys are the fields of the new table, and values can take two possible forms:

  • a simple string, which is interpreted as the name of a field of the old table,
  • an associative array with two keys 'expression' and 'arguments', that will be used as an expression field.

protected An introspectSchema($table)

Find out the schema of a table.

This function uses introspection methods provided by the database to create a schema array. This is useful, for example, during update when the old schema is not available.

Parameters

$table

Name of the table.

Return Value

An

array representing the schema.

Exceptions

Exception

protected mapKeyDefinition(array $key_definition, array $mapping)

Utility method: rename columns in an index definition according to a new mapping.

Parameters

array $key_definition

The key definition.

array $mapping

The new mapping.