3 namespace Drupal\Core\Database;
5 use Drupal\Core\Database\Query\Condition;
6 use Drupal\Core\Database\Query\PlaceholderInterface;
9 * Provides a base implementation for Database Schema.
11 abstract class Schema implements PlaceholderInterface {
14 * The database connection.
16 * @var \Drupal\Core\Database\Connection
18 protected $connection;
21 * The placeholder counter.
23 protected $placeholder = 0;
26 * Definition of prefixInfo array structure.
28 * Rather than redefining DatabaseSchema::getPrefixInfo() for each driver,
29 * by defining the defaultSchema variable only MySQL has to re-write the
32 * @see DatabaseSchema::getPrefixInfo()
34 protected $defaultSchema = 'public';
37 * A unique identifier for this query object.
39 protected $uniqueIdentifier;
41 public function __construct($connection) {
42 $this->uniqueIdentifier = uniqid('', TRUE);
43 $this->connection = $connection;
47 * Implements the magic __clone function.
49 public function __clone() {
50 $this->uniqueIdentifier = uniqid('', TRUE);
56 public function uniqueIdentifier() {
57 return $this->uniqueIdentifier;
63 public function nextPlaceholder() {
64 return $this->placeholder++;
68 * Get information about the table name and schema from the prefix.
71 * Name of table to look prefix up for. Defaults to 'default' because that's
72 * default key for prefix.
74 * Boolean that indicates whether the given table name should be prefixed.
77 * A keyed array with information about the schema, table name and prefix.
79 protected function getPrefixInfo($table = 'default', $add_prefix = TRUE) {
81 'schema' => $this->defaultSchema,
82 'prefix' => $this->connection->tablePrefix($table),
85 $table = $info['prefix'] . $table;
87 // If the prefix contains a period in it, then that means the prefix also
88 // contains a schema reference in which case we will change the schema key
89 // to the value before the period in the prefix. Everything after the dot
90 // will be prefixed onto the front of the table.
91 if (($pos = strpos($table, '.')) !== FALSE) {
92 // Grab everything before the period.
93 $info['schema'] = substr($table, 0, $pos);
94 // Grab everything after the dot.
95 $info['table'] = substr($table, ++$pos);
98 $info['table'] = $table;
104 * Create names for indexes, primary keys and constraints.
106 * This prevents using {} around non-table names like indexes and keys.
108 public function prefixNonTable($table) {
109 $args = func_get_args();
110 $info = $this->getPrefixInfo($table);
111 $args[0] = $info['table'];
112 return implode('_', $args);
116 * Build a condition to match a table name against a standard information_schema.
118 * The information_schema is a SQL standard that provides information about the
119 * database server and the databases, schemas, tables, columns and users within
120 * it. This makes information_schema a useful tool to use across the drupal
121 * database drivers and is used by a few different functions. The function below
122 * describes the conditions to be meet when querying information_schema.tables
123 * for drupal tables or information associated with drupal tables. Even though
124 * this is the standard method, not all databases follow standards and so this
125 * method should be overwritten by a database driver if the database provider
126 * uses alternate methods. Because information_schema.tables is used in a few
127 * different functions, a database driver will only need to override this function
128 * to make all the others work. For example see
129 * core/includes/databases/mysql/schema.inc.
132 * The name of the table in question.
134 * The operator to apply on the 'table' part of the condition.
136 * Boolean to indicate whether the table name needs to be prefixed.
138 * @return \Drupal\Core\Database\Query\Condition
139 * A Condition object.
141 protected function buildTableNameCondition($table_name, $operator = '=', $add_prefix = TRUE) {
142 $info = $this->connection->getConnectionOptions();
144 // Retrieve the table name and schema
145 $table_info = $this->getPrefixInfo($table_name, $add_prefix);
147 $condition = new Condition('AND');
148 $condition->condition('table_catalog', $info['database']);
149 $condition->condition('table_schema', $table_info['schema']);
150 $condition->condition('table_name', $table_info['table'], $operator);
155 * Check if a table exists.
158 * The name of the table in drupal (no prefixing).
161 * TRUE if the given table exists, otherwise FALSE.
163 public function tableExists($table) {
164 $condition = $this->buildTableNameCondition($table);
165 $condition->compile($this->connection, $this);
166 // Normally, we would heartily discourage the use of string
167 // concatenation for conditionals like this however, we
168 // couldn't use db_select() here because it would prefix
169 // information_schema.tables and the query would fail.
170 // Don't use {} around information_schema.tables table.
171 return (bool) $this->connection->query("SELECT 1 FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchField();
175 * Finds all tables that are like the specified base table name.
177 * @param string $table_expression
178 * An SQL expression, for example "cache_%" (without the quotes).
181 * Both the keys and the values are the matching tables.
183 public function findTables($table_expression) {
184 // Load all the tables up front in order to take into account per-table
185 // prefixes. The actual matching is done at the bottom of the method.
186 $condition = $this->buildTableNameCondition('%', 'LIKE');
187 $condition->compile($this->connection, $this);
189 $individually_prefixed_tables = $this->connection->getUnprefixedTablesMap();
190 $default_prefix = $this->connection->tablePrefix();
191 $default_prefix_length = strlen($default_prefix);
193 // Normally, we would heartily discourage the use of string
194 // concatenation for conditionals like this however, we
195 // couldn't use db_select() here because it would prefix
196 // information_schema.tables and the query would fail.
197 // Don't use {} around information_schema.tables table.
198 $results = $this->connection->query("SELECT table_name FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments());
199 foreach ($results as $table) {
200 // Take into account tables that have an individual prefix.
201 if (isset($individually_prefixed_tables[$table->table_name])) {
202 $prefix_length = strlen($this->connection->tablePrefix($individually_prefixed_tables[$table->table_name]));
204 elseif ($default_prefix && substr($table->table_name, 0, $default_prefix_length) !== $default_prefix) {
205 // This table name does not start the default prefix, which means that
206 // it is not managed by Drupal so it should be excluded from the result.
210 $prefix_length = $default_prefix_length;
213 // Remove the prefix from the returned tables.
214 $unprefixed_table_name = substr($table->table_name, $prefix_length);
216 // The pattern can match a table which is the same as the prefix. That
217 // will become an empty string when we remove the prefix, which will
218 // probably surprise the caller, besides not being a prefixed table. So
220 if (!empty($unprefixed_table_name)) {
221 $tables[$unprefixed_table_name] = $unprefixed_table_name;
225 // Convert the table expression from its SQL LIKE syntax to a regular
226 // expression and escape the delimiter that will be used for matching.
227 $table_expression = str_replace(['%', '_'], ['.*?', '.'], preg_quote($table_expression, '/'));
228 $tables = preg_grep('/^' . $table_expression . '$/i', $tables);
234 * Check if a column exists in the given table.
237 * The name of the table in drupal (no prefixing).
239 * The name of the column.
242 * TRUE if the given column exists, otherwise FALSE.
244 public function fieldExists($table, $column) {
245 $condition = $this->buildTableNameCondition($table);
246 $condition->condition('column_name', $column);
247 $condition->compile($this->connection, $this);
248 // Normally, we would heartily discourage the use of string
249 // concatenation for conditionals like this however, we
250 // couldn't use db_select() here because it would prefix
251 // information_schema.tables and the query would fail.
252 // Don't use {} around information_schema.columns table.
253 return (bool) $this->connection->query("SELECT 1 FROM information_schema.columns WHERE " . (string) $condition, $condition->arguments())->fetchField();
257 * Returns a mapping of Drupal schema field names to DB-native field types.
259 * Because different field types do not map 1:1 between databases, Drupal has
260 * its own normalized field type names. This function returns a driver-specific
261 * mapping table from Drupal names to the native names for each database.
264 * An array of Schema API field types to driver-specific field types.
266 abstract public function getFieldTypeMap();
272 * The table to be renamed.
274 * The new name for the table.
276 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
277 * If the specified table doesn't exist.
278 * @throws \Drupal\Core\Database\SchemaObjectExistsException
279 * If a table with the specified new name already exists.
281 abstract public function renameTable($table, $new_name);
287 * The table to be dropped.
290 * TRUE if the table was successfully dropped, FALSE if there was no table
291 * by that name to begin with.
293 abstract public function dropTable($table);
296 * Add a new field to a table.
299 * Name of the table to be altered.
301 * Name of the field to be added.
303 * The field specification array, as taken from a schema definition.
304 * The specification may also contain the key 'initial', the newly
305 * created field will be set to the value of the key in all rows.
306 * This is most useful for creating NOT NULL columns with no default
307 * value in existing tables.
308 * Alternatively, the 'initial_form_field' key may be used, which will
309 * auto-populate the new field with values from the specified field.
311 * (optional) Keys and indexes specification to be created on the
312 * table along with adding the field. The format is the same as a
313 * table specification but without the 'fields' element. If you are
314 * adding a type 'serial' field, you MUST specify at least one key
315 * or index including it in this array. See db_change_field() for more
318 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
319 * If the specified table doesn't exist.
320 * @throws \Drupal\Core\Database\SchemaObjectExistsException
321 * If the specified table already has a field by that name.
323 abstract public function addField($table, $field, $spec, $keys_new = []);
329 * The table to be altered.
331 * The field to be dropped.
334 * TRUE if the field was successfully dropped, FALSE if there was no field
335 * by that name to begin with.
337 abstract public function dropField($table, $field);
340 * Set the default value for a field.
343 * The table to be altered.
345 * The field to be altered.
347 * Default value to be set. NULL for 'default NULL'.
349 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
350 * If the specified table or field doesn't exist.
352 abstract public function fieldSetDefault($table, $field, $default);
355 * Set a field to have no default value.
358 * The table to be altered.
360 * The field to be altered.
362 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
363 * If the specified table or field doesn't exist.
365 abstract public function fieldSetNoDefault($table, $field);
368 * Checks if an index exists in the given table.
371 * The name of the table in drupal (no prefixing).
373 * The name of the index in drupal (no prefixing).
376 * TRUE if the given index exists, otherwise FALSE.
378 abstract public function indexExists($table, $name);
384 * The table to be altered.
386 * Fields for the primary key.
388 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
389 * If the specified table doesn't exist.
390 * @throws \Drupal\Core\Database\SchemaObjectExistsException
391 * If the specified table already has a primary key.
393 abstract public function addPrimaryKey($table, $fields);
396 * Drop the primary key.
399 * The table to be altered.
402 * TRUE if the primary key was successfully dropped, FALSE if there was no
403 * primary key on this table to begin with.
405 abstract public function dropPrimaryKey($table);
411 * The table to be altered.
413 * The name of the key.
415 * An array of field names.
417 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
418 * If the specified table doesn't exist.
419 * @throws \Drupal\Core\Database\SchemaObjectExistsException
420 * If the specified table already has a key by that name.
422 abstract public function addUniqueKey($table, $name, $fields);
428 * The table to be altered.
430 * The name of the key.
433 * TRUE if the key was successfully dropped, FALSE if there was no key by
434 * that name to begin with.
436 abstract public function dropUniqueKey($table, $name);
442 * The table to be altered.
444 * The name of the index.
446 * An array of field names or field information; if field information is
447 * passed, it's an array whose first element is the field name and whose
448 * second is the maximum length in the index. For example, the following
449 * will use the full length of the `foo` field, but limit the `bar` field to
452 * $fields = ['foo', ['bar', 4]];
455 * The table specification for the table to be altered. This is used in
456 * order to be able to ensure that the index length is not too long.
457 * This schema definition can usually be obtained through hook_schema(), or
458 * in case the table was created by the Entity API, through the schema
459 * handler listed in the entity class definition. For reference, see
460 * SqlContentEntityStorageSchema::getDedicatedTableSchema() and
461 * SqlContentEntityStorageSchema::getSharedTableFieldSchema().
463 * In order to prevent human error, it is recommended to pass in the
464 * complete table specification. However, in the edge case of the complete
465 * table specification not being available, we can pass in a partial table
466 * definition containing only the fields that apply to the index:
469 * // Example partial specification for a table:
471 * 'example_field' => [
472 * 'description' => 'An example field',
473 * 'type' => 'varchar',
475 * 'not null' => TRUE,
480 * 'table_example_field' => ['example_field'],
484 * Note that the above is a partial table definition and that we would
485 * usually pass a complete table definition as obtained through
486 * hook_schema() instead.
491 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
492 * If the specified table doesn't exist.
493 * @throws \Drupal\Core\Database\SchemaObjectExistsException
494 * If the specified table already has an index by that name.
496 * @todo remove the $spec argument whenever schema introspection is added.
498 abstract public function addIndex($table, $name, $fields, array $spec);
504 * The table to be altered.
506 * The name of the index.
509 * TRUE if the index was successfully dropped, FALSE if there was no index
510 * by that name to begin with.
512 abstract public function dropIndex($table, $name);
515 * Change a field definition.
517 * IMPORTANT NOTE: To maintain database portability, you have to explicitly
518 * recreate all indices and primary keys that are using the changed field.
520 * That means that you have to drop all affected keys and indexes with
521 * db_drop_{primary_key,unique_key,index}() before calling db_change_field().
522 * To recreate the keys and indices, pass the key definitions as the
523 * optional $keys_new argument directly to db_change_field().
525 * For example, suppose you have:
527 * $schema['foo'] = array(
529 * 'bar' => array('type' => 'int', 'not null' => TRUE)
531 * 'primary key' => array('bar')
534 * and you want to change foo.bar to be type serial, leaving it as the
535 * primary key. The correct sequence is:
537 * db_drop_primary_key('foo');
538 * db_change_field('foo', 'bar', 'bar',
539 * array('type' => 'serial', 'not null' => TRUE),
540 * array('primary key' => array('bar')));
543 * The reasons for this are due to the different database engines:
545 * On PostgreSQL, changing a field definition involves adding a new field
546 * and dropping an old one which* causes any indices, primary keys and
547 * sequences (from serial-type fields) that use the changed field to be dropped.
549 * On MySQL, all type 'serial' fields must be part of at least one key
550 * or index as soon as they are created. You cannot use
551 * db_add_{primary_key,unique_key,index}() for this purpose because
552 * the ALTER TABLE command will fail to add the column without a key
553 * or index specification. The solution is to use the optional
554 * $keys_new argument to create the key or index at the same time as
557 * You could use db_add_{primary_key,unique_key,index}() in all cases
558 * unless you are converting a field to be type serial. You can use
559 * the $keys_new argument in all cases.
564 * Name of the field to change.
566 * New name for the field (set to the same as $field if you don't want to change the name).
568 * The field specification for the new field.
570 * (optional) Keys and indexes specification to be created on the
571 * table along with changing the field. The format is the same as a
572 * table specification but without the 'fields' element.
574 * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
575 * If the specified table or source field doesn't exist.
576 * @throws \Drupal\Core\Database\SchemaObjectExistsException
577 * If the specified destination field already exists.
579 abstract public function changeField($table, $field, $field_new, $spec, $keys_new = []);
582 * Create a new table from a Drupal table definition.
585 * The name of the table to create.
587 * A Schema API table definition array.
589 * @throws \Drupal\Core\Database\SchemaObjectExistsException
590 * If the specified table already exists.
592 public function createTable($name, $table) {
593 if ($this->tableExists($name)) {
594 throw new SchemaObjectExistsException(t('Table @name already exists.', ['@name' => $name]));
596 $statements = $this->createTableSql($name, $table);
597 foreach ($statements as $statement) {
598 $this->connection->query($statement);
603 * Return an array of field names from an array of key/index column specifiers.
605 * This is usually an identity function but if a key/index uses a column prefix
606 * specification, this function extracts just the name.
609 * An array of key/index column specifiers.
612 * An array of field names.
614 public function fieldNames($fields) {
616 foreach ($fields as $field) {
617 if (is_array($field)) {
618 $return[] = $field[0];
628 * Prepare a table or column comment for database query.
631 * The comment string to prepare.
633 * Optional upper limit on the returned string length.
636 * The prepared comment.
638 public function prepareComment($comment, $length = NULL) {
639 // Remove semicolons to avoid triggering multi-statement check.
640 $comment = strtr($comment, [';' => '.']);
641 return $this->connection->quote($comment);
645 * Return an escaped version of its parameter to be used as a default value
648 * @param mixed $value
649 * The value to be escaped (int, float, null or string).
651 * @return string|int|float
654 protected function escapeDefaultValue($value) {
655 if (is_null($value)) {
658 return is_string($value) ? $this->connection->quote($value) : $value;