Schema
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
Get information about the table and database name from the prefix.
Build a condition to match a table name against a standard information_schema.
Check if a table exists.
Finds all tables that are like the specified base table name.
Check if a column exists in the given table.
Returns a mapping of Drupal schema field names to DB-native field types.
Rename a table.
Drop a table.
Add a new field to a table.
Drop a field.
Checks if an index exists in the given table.
Add a primary key.
Drop the primary key.
Finds the primary key columns of a table, from the database.
Add a unique key.
Drop a unique key.
Add an index.
Drop an index.
Finds the columns for the primary key, unique keys and indexes of a table.
Change a field definition.
Return an array of field names from an array of key/index column specifiers.
Prepare a table or column comment for database query.
Return an escaped version of its parameter to be used as a default value on a column.
Ensures that all the primary key fields are correctly defined.
Generate SQL to create a new table from a Drupal schema definition.
Create an SQL string for a field to be used in table creation or alteration.
Set database-engine specific properties for a field.
No description
Gets normalized indexes from a table specification.
Helper function for normalizeIndexes().
No description
Retrieve a table or column comment.
Details
in
Schema at line 46
__construct($connection)
No description
in
Schema at line 54
__clone()
Implements the magic __clone function.
in
Schema at line 61
uniqueIdentifier()
Returns a unique identifier for this object.
in
Schema at line 68
The
nextPlaceholder()
Returns the next placeholder ID for the query.
protected 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.
This prevents using } around non-table names like indexes and keys.
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.
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.
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.
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.
protected 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.
protected 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.
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:
createTable($name, $table)
Create a new table from a Drupal table definition.
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.
The
prepareComment($comment, $length = NULL)
Prepare a table or column comment for database query.
protected string|int|float
escapeDefaultValue(mixed $value)
Return an escaped version of its parameter to be used as a default value on a column.
protected
ensureNotNullPrimaryKey(array $primary_key, array $fields)
Ensures that all the primary key fields are correctly defined.
protected An
createTableSql($name, $table)
Generate SQL to create a new table from a Drupal schema definition.
protected
createFieldSql(string $name, array $spec)
Create an SQL string for a field to be used in table creation or alteration.
protected
processField($field)
Set database-engine specific properties for a field.
protected
createKeysSql($spec)
No description
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.
protected
shortenIndex(array $index)
Helper function for normalizeIndexes().
Shortens an index to 191 characters.
protected
createKeySql($fields)
No description
getComment($table, $column = NULL)
Retrieve a table or column comment.