5 * Core systems for the database layer.
7 * Classes required for basic functioning of the database system should be
8 * placed in this file. All utility functions should also be placed in this
9 * file only, as they cannot auto-load the way classes can.
12 use Drupal\Core\Database\Database;
13 use Drupal\Core\Database\Query\Condition;
14 use Drupal\Core\Site\Settings;
17 * @addtogroup database
22 * Executes an arbitrary query string against the active database.
24 * Use this function for SELECT queries if it is just a simple query string.
25 * If the caller or other modules need to change the query, use db_select()
28 * Do not use this function for INSERT, UPDATE, or DELETE queries. Those should
29 * be handled via db_insert(), db_update() and db_delete() respectively.
31 * @param string|\Drupal\Core\Database\StatementInterface $query
32 * The prepared statement query to run. Although it will accept both named and
33 * unnamed placeholders, named placeholders are strongly preferred as they are
34 * more self-documenting. If the argument corresponding to a placeholder is
35 * an array of values to be expanded (for example, with an IN query), the
36 * placeholder should be named with a trailing bracket like :example[].
38 * An array of values to substitute into the query. If the query uses named
39 * placeholders, this is an associative array in any order. If the query uses
40 * unnamed placeholders (?), this is an indexed array and the order must match
41 * the order of placeholders in the query string.
42 * @param array $options
43 * An array of options to control how the query operates.
45 * @return \Drupal\Core\Database\StatementInterface
46 * A prepared statement object, already executed.
48 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
49 * a database connection injected into your service from the container and
50 * call query() on it. For example,
51 * $injected_database->query($query, $args, $options);
53 * @see \Drupal\Core\Database\Connection::query()
54 * @see \Drupal\Core\Database\Connection::defaultOptions()
56 function db_query($query, array $args = [], array $options = []) {
57 if (empty($options['target'])) {
58 $options['target'] = 'default';
61 return Database::getConnection($options['target'])->query($query, $args, $options);
65 * Executes a query against the active database, restricted to a range.
67 * @param string $query
68 * The prepared statement query to run. Although it will accept both named and
69 * unnamed placeholders, named placeholders are strongly preferred as they are
70 * more self-documenting.
72 * The first record from the result set to return.
74 * The number of records to return from the result set.
76 * An array of values to substitute into the query. If the query uses named
77 * placeholders, this is an associative array in any order. If the query uses
78 * unnamed placeholders (?), this is an indexed array and the order must match
79 * the order of placeholders in the query string.
80 * @param array $options
81 * An array of options to control how the query operates.
83 * @return \Drupal\Core\Database\StatementInterface
84 * A prepared statement object, already executed.
86 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
87 * a database connection injected into your service from the container and
88 * call queryRange() on it. For example,
89 * $injected_database->queryRange($query, $from, $count, $args, $options);
91 * @see \Drupal\Core\Database\Connection::queryRange()
92 * @see \Drupal\Core\Database\Connection::defaultOptions()
94 function db_query_range($query, $from, $count, array $args = [], array $options = []) {
95 if (empty($options['target'])) {
96 $options['target'] = 'default';
99 return Database::getConnection($options['target'])->queryRange($query, $from, $count, $args, $options);
103 * Executes a SELECT query string and saves the result set to a temporary table.
105 * The execution of the query string happens against the active database.
107 * @param string $query
108 * The prepared SELECT statement query to run. Although it will accept both
109 * named and unnamed placeholders, named placeholders are strongly preferred
110 * as they are more self-documenting.
112 * An array of values to substitute into the query. If the query uses named
113 * placeholders, this is an associative array in any order. If the query uses
114 * unnamed placeholders (?), this is an indexed array and the order must match
115 * the order of placeholders in the query string.
116 * @param array $options
117 * An array of options to control how the query operates.
120 * The name of the temporary table.
122 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
123 * a database connection injected into your service from the container and
124 * call queryTemporary() on it. For example,
125 * $injected_database->queryTemporary($query, $args, $options);
127 * @see \Drupal\Core\Database\Connection::queryTemporary()
128 * @see \Drupal\Core\Database\Connection::defaultOptions()
130 function db_query_temporary($query, array $args = [], array $options = []) {
131 if (empty($options['target'])) {
132 $options['target'] = 'default';
135 return Database::getConnection($options['target'])->queryTemporary($query, $args, $options);
139 * Returns a new InsertQuery object for the active database.
141 * @param string $table
142 * The table into which to insert.
143 * @param array $options
144 * An array of options to control how the query operates.
146 * @return \Drupal\Core\Database\Query\Insert
147 * A new Insert object for this connection.
149 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
150 * a database connection injected into your service from the container and
151 * call insert() on it. For example,
152 * $injected_database->insert($table, $options);
154 * @see \Drupal\Core\Database\Connection::insert()
155 * @see \Drupal\Core\Database\Connection::defaultOptions()
157 function db_insert($table, array $options = []) {
158 if (empty($options['target']) || $options['target'] == 'replica') {
159 $options['target'] = 'default';
161 return Database::getConnection($options['target'])->insert($table, $options);
165 * Returns a new MergeQuery object for the active database.
167 * @param string $table
168 * Name of the table to associate with this query.
169 * @param array $options
170 * An array of options to control how the query operates.
172 * @return \Drupal\Core\Database\Query\Merge
173 * A new Merge object for this connection.
175 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
176 * a database connection injected into your service from the container and
177 * call merge() on it. For example,
178 * $injected_database->merge($table, $options);
180 * @see \Drupal\Core\Database\Connection::merge()
181 * @see \Drupal\Core\Database\Connection::defaultOptions()
183 function db_merge($table, array $options = []) {
184 if (empty($options['target']) || $options['target'] == 'replica') {
185 $options['target'] = 'default';
187 return Database::getConnection($options['target'])->merge($table, $options);
191 * Returns a new UpdateQuery object for the active database.
193 * @param string $table
194 * The table to update.
195 * @param array $options
196 * An array of options to control how the query operates.
198 * @return \Drupal\Core\Database\Query\Update
199 * A new Update object for this connection.
201 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
202 * a database connection injected into your service from the container and
203 * call update() on it. For example,
204 * $injected_database->update($table, $options);
206 * @see \Drupal\Core\Database\Connection::update()
207 * @see \Drupal\Core\Database\Connection::defaultOptions()
209 function db_update($table, array $options = []) {
210 if (empty($options['target']) || $options['target'] == 'replica') {
211 $options['target'] = 'default';
213 return Database::getConnection($options['target'])->update($table, $options);
217 * Returns a new DeleteQuery object for the active database.
219 * @param string $table
220 * The table from which to delete.
221 * @param array $options
222 * An array of options to control how the query operates.
224 * @return \Drupal\Core\Database\Query\Delete
225 * A new Delete object for this connection.
227 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
228 * a database connection injected into your service from the container and
229 * call delete() on it. For example,
230 * $injected_database->delete($table, $options);
232 * @see \Drupal\Core\Database\Connection::delete()
233 * @see \Drupal\Core\Database\Connection::defaultOptions()
235 function db_delete($table, array $options = []) {
236 if (empty($options['target']) || $options['target'] == 'replica') {
237 $options['target'] = 'default';
239 return Database::getConnection($options['target'])->delete($table, $options);
243 * Returns a new TruncateQuery object for the active database.
245 * @param string $table
246 * The table from which to truncate.
247 * @param array $options
248 * An array of options to control how the query operates.
250 * @return \Drupal\Core\Database\Query\Truncate
251 * A new Truncate object for this connection.
253 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
254 * a database connection injected into your service from the container and
255 * call truncate() on it. For example,
256 * $injected_database->truncate($table, $options);
258 * @see \Drupal\Core\Database\Connection::truncate()
259 * @see \Drupal\Core\Database\Connection::defaultOptions()
261 function db_truncate($table, array $options = []) {
262 if (empty($options['target']) || $options['target'] == 'replica') {
263 $options['target'] = 'default';
265 return Database::getConnection($options['target'])->truncate($table, $options);
269 * Returns a new SelectQuery object for the active database.
271 * @param string|\Drupal\Core\Database\Query\SelectInterface $table
272 * The base table for this query. May be a string or another SelectInterface
273 * object. If a SelectInterface object is passed, it will be used as a
275 * @param string $alias
276 * (optional) The alias for the base table of this query.
277 * @param array $options
278 * (optional) An array of options to control how the query operates.
280 * @return \Drupal\Core\Database\Query\Select
281 * A new Select object for this connection.
283 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
284 * a database connection injected into your service from the container and
285 * call select() on it. For example,
286 * $injected_database->select($table, $alias, $options);
288 * @see \Drupal\Core\Database\Connection::select()
289 * @see \Drupal\Core\Database\Connection::defaultOptions()
291 function db_select($table, $alias = NULL, array $options = []) {
292 if (empty($options['target'])) {
293 $options['target'] = 'default';
295 return Database::getConnection($options['target'])->select($table, $alias, $options);
299 * Returns a new transaction object for the active database.
301 * @param string $name
302 * Optional name of the transaction.
303 * @param array $options
304 * An array of options to control how the transaction operates:
305 * - target: The database target name.
307 * @return \Drupal\Core\Database\Transaction
308 * A new Transaction object for this connection.
310 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
311 * a database connection injected into your service from the container and
312 * call startTransaction() on it. For example,
313 * $injected_database->startTransaction($name);
315 * @see \Drupal\Core\Database\Connection::startTransaction()
316 * @see \Drupal\Core\Database\Connection::defaultOptions()
318 function db_transaction($name = NULL, array $options = []) {
319 if (empty($options['target'])) {
320 $options['target'] = 'default';
322 return Database::getConnection($options['target'])->startTransaction($name);
326 * Sets a new active database.
329 * The key in the $databases array to set as the default database.
331 * @return string|null
332 * The key of the formerly active database.
334 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Use
335 * \Drupal\Core\Database\Database::setActiveConnection().
337 function db_set_active($key = 'default') {
338 return Database::setActiveConnection($key);
342 * Restricts a dynamic table name to safe characters.
344 * Only keeps alphanumeric and underscores.
347 * The table name to escape.
350 * The escaped table name as a string.
352 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
353 * a database connection injected into your service from the container and
354 * call escapeTable() on it. For example,
355 * $injected_database->escapeTable($table);
357 * @see \Drupal\Core\Database\Connection::escapeTable()
359 function db_escape_table($table) {
360 return Database::getConnection()->escapeTable($table);
364 * Restricts a dynamic column or constraint name to safe characters.
366 * Only keeps alphanumeric and underscores.
368 * @param string $field
369 * The field name to escape.
372 * The escaped field name as a string.
374 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
375 * a database connection injected into your service from the container and
376 * call escapeTable() on it. For example,
377 * $injected_database->escapeTable($table);
379 * @see \Drupal\Core\Database\Connection::escapeField()
381 function db_escape_field($field) {
382 return Database::getConnection()->escapeField($field);
386 * Escapes characters that work as wildcard characters in a LIKE pattern.
388 * The wildcard characters "%" and "_" as well as backslash are prefixed with
389 * a backslash. Use this to do a search for a verbatim string without any
392 * You must use a query builder like db_select() in order to use db_like() on
393 * all supported database systems. Using db_like() with db_query() or
394 * db_query_range() is not supported.
396 * For example, the following does a case-insensitive query for all rows whose
397 * name starts with $prefix:
399 * $result = db_select('person', 'p')
401 * ->condition('name', db_like($prefix) . '%', 'LIKE')
406 * Backslash is defined as escape character for LIKE patterns in
407 * DatabaseCondition::mapConditionOperator().
409 * @param string $string
410 * The string to escape.
413 * The escaped string.
415 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
416 * a database connection injected into your service from the container and
417 * call escapeLike() on it. For example,
418 * $injected_database->escapeLike($string);
420 * @see \Drupal\Core\Database\Connection::escapeLike()
422 function db_like($string) {
423 return Database::getConnection()->escapeLike($string);
427 * Retrieves the name of the currently active database driver.
430 * The name of the currently active database driver.
432 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
433 * a database connection injected into your service from the container and
434 * call driver() on it. For example, $injected_database->driver($string);
436 * @see \Drupal\Core\Database\Connection::driver()
438 function db_driver() {
439 return Database::getConnection()->driver();
443 * Closes the active database connection.
445 * @param array $options
446 * An array of options to control which connection is closed. Only the target
447 * key has any meaning in this case.
449 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Use
450 * \Drupal\Core\Database\Database::closeConnection($target).
452 * @see \Drupal\Core\Database\Database::closeConnection()
454 function db_close(array $options = []) {
455 if (empty($options['target'])) {
456 $options['target'] = NULL;
458 Database::closeConnection($options['target']);
462 * Retrieves a unique id.
464 * Use this function if for some reason you can't use a serial field. Using a
465 * serial field is preferred, and InsertQuery::execute() returns the value of
466 * the last ID inserted.
468 * @param int $existing_id
469 * After a database import, it might be that the sequences table is behind, so
470 * by passing in a minimum ID, it can be assured that we never issue the same
474 * An integer number larger than any number returned before for this sequence.
476 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
477 * a database connection injected into your service from the container and
478 * call nextId() on it. For example, $injected_database->nextId($existing_id);
480 * @see \Drupal\Core\Database\Connection::nextId()
482 function db_next_id($existing_id = 0) {
483 return Database::getConnection()->nextId($existing_id);
487 * Returns a new DatabaseCondition, set to "OR" all conditions together.
489 * @return \Drupal\Core\Database\Query\Condition
490 * A new Condition object, set to "OR" all conditions together.
492 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
493 * a \Drupal\Core\Database\Query\Condition object, specifying an OR
494 * conjunction: new Condition('OR');
496 * @see \Drupal\Core\Database\Query\Condition
499 return new Condition('OR');
503 * Returns a new DatabaseCondition, set to "AND" all conditions together.
505 * @return \Drupal\Core\Database\Query\Condition
506 * A new Condition object, set to "AND" all conditions together.
508 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
509 * a \Drupal\Core\Database\Query\Condition object, specifying an AND
510 * conjunction: new Condition('AND');
512 * @see \Drupal\Core\Database\Query\Condition
515 return new Condition('AND');
519 * Returns a new DatabaseCondition, set to "XOR" all conditions together.
521 * @return \Drupal\Core\Database\Query\Condition
522 * A new Condition object, set to "XOR" all conditions together.
524 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
525 * a \Drupal\Core\Database\Query\Condition object, specifying an XOR
526 * conjunction: new Condition('XOR');
528 * @see \Drupal\Core\Database\Query\Condition
531 return new Condition('XOR');
535 * Returns a new DatabaseCondition, set to the specified conjunction.
537 * Internal API function call. The db_and(), db_or(), and db_xor()
538 * functions are preferred.
540 * @param string $conjunction
541 * The conjunction to use for query conditions (AND, OR or XOR).
543 * @return \Drupal\Core\Database\Query\Condition
544 * A new Condition object, set to the specified conjunction.
546 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
547 * a \Drupal\Core\Database\Query\Condition object, specifying the desired
548 * conjunction: new Condition($conjunctin);
550 * @see \Drupal\Core\Database\Query\Condition
552 function db_condition($conjunction) {
553 return new Condition($conjunction);
557 * @} End of "addtogroup database".
562 * @addtogroup schemaapi
567 * Creates a new table from a Drupal table definition.
569 * @param string $name
570 * The name of the table to create.
571 * @param array $table
572 * A Schema API table definition array.
574 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
575 * a database connection injected into your service from the container, get
576 * its schema driver, and call createTable() on it. For example,
577 * $injected_database->schema()->createTable($name, $table);
579 * @see \Drupal\Core\Database\Schema::createTable()
581 function db_create_table($name, $table) {
582 return Database::getConnection()->schema()->createTable($name, $table);
586 * Returns an array of field names from an array of key/index column specifiers.
588 * This is usually an identity function but if a key/index uses a column prefix
589 * specification, this function extracts just the name.
591 * @param array $fields
592 * An array of key/index column specifiers.
595 * An array of field names.
597 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
598 * a database connection injected into your service from the container, get
599 * its schema driver, and call fieldNames() on it. For example,
600 * $injected_database->schema()->fieldNames($fields);
602 * @see \Drupal\Core\Database\Schema::fieldNames()
604 function db_field_names($fields) {
605 return Database::getConnection()->schema()->fieldNames($fields);
609 * Checks if an index exists in the given table.
611 * @param string $table
612 * The name of the table in drupal (no prefixing).
613 * @param string $name
614 * The name of the index in drupal (no prefixing).
617 * TRUE if the given index exists, otherwise FALSE.
619 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
620 * a database connection injected into your service from the container, get
621 * its schema driver, and call indexExists() on it. For example,
622 * $injected_database->schema()->indexExists($table, $name);
624 * @see \Drupal\Core\Database\Schema::indexExists()
626 function db_index_exists($table, $name) {
627 return Database::getConnection()->schema()->indexExists($table, $name);
631 * Checks if a table exists.
633 * @param string $table
634 * The name of the table in drupal (no prefixing).
637 * TRUE if the given table exists, otherwise FALSE.
639 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
640 * a database connection injected into your service from the container, get
641 * its schema driver, and call tableExists() on it. For example,
642 * $injected_database->schema()->tableExists($table);
644 * @see \Drupal\Core\Database\Schema::tableExists()
646 function db_table_exists($table) {
647 return Database::getConnection()->schema()->tableExists($table);
651 * Checks if a column exists in the given table.
654 * The name of the table in drupal (no prefixing).
656 * The name of the field.
659 * TRUE if the given column exists, otherwise FALSE.
661 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
662 * a database connection injected into your service from the container, get
663 * its schema driver, and call fieldExists() on it. For example,
664 * $injected_database->schema()->fieldExists($table, $field);
666 * @see \Drupal\Core\Database\Schema::fieldExists()
668 function db_field_exists($table, $field) {
669 return Database::getConnection()->schema()->fieldExists($table, $field);
673 * Finds all tables that are like the specified base table name.
675 * @param string $table_expression
676 * An SQL expression, for example "simpletest%" (without the quotes).
679 * Array, both the keys and the values are the matching tables.
681 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
682 * a database connection injected into your service from the container, get
683 * its schema driver, and call findTables() on it. For example,
684 * $injected_database->schema()->findTables($table_expression);
686 * @see \Drupal\Core\Database\Schema::findTables()
688 function db_find_tables($table_expression) {
689 return Database::getConnection()->schema()->findTables($table_expression);
696 * The current name of the table to be renamed.
698 * The new name for the table.
700 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
701 * a database connection injected into your service from the container, get
702 * its schema driver, and call renameTable() on it. For example,
703 * $injected_database->schema()->renameTable($table, $new_name);
705 * @see \Drupal\Core\Database\Schema::renameTable()
707 function db_rename_table($table, $new_name) {
708 return Database::getConnection()->schema()->renameTable($table, $new_name);
715 * The table to be dropped.
717 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
718 * a database connection injected into your service from the container, get
719 * its schema driver, and call dropTable() on it. For example,
720 * $injected_database->schema()->dropTable($table);
722 * @see \Drupal\Core\Database\Schema::dropTable()
724 function db_drop_table($table) {
725 return Database::getConnection()->schema()->dropTable($table);
729 * Adds a new field to a table.
732 * Name of the table to be altered.
734 * Name of the field to be added.
736 * The field specification array, as taken from a schema definition. The
737 * specification may also contain the key 'initial'; the newly-created field
738 * will be set to the value of the key in all rows. This is most useful for
739 * creating NOT NULL columns with no default value in existing tables.
740 * @param array $keys_new
741 * (optional) Keys and indexes specification to be created on the table along
742 * with adding the field. The format is the same as a table specification, but
743 * without the 'fields' element. If you are adding a type 'serial' field, you
744 * MUST specify at least one key or index including it in this array. See
745 * db_change_field() for more explanation why.
747 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
748 * a database connection injected into your service from the container, get
749 * its schema driver, and call addField() on it. For example,
750 * $injected_database->schema()->addField($table, $field, $spec, $keys_new);
752 * @see \Drupal\Core\Database\Schema::addField()
753 * @see db_change_field()
755 function db_add_field($table, $field, $spec, $keys_new = []) {
756 return Database::getConnection()->schema()->addField($table, $field, $spec, $keys_new);
763 * The table to be altered.
765 * The field to be dropped.
768 * TRUE if the field was successfully dropped, FALSE if there was no field by
769 * that name to begin with.
771 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
772 * a database connection injected into your service from the container, get
773 * its schema driver, and call dropField() on it. For example,
774 * $injected_database->schema()->dropField($table, $field);
776 * @see \Drupal\Core\Database\Schema::dropField()
778 function db_drop_field($table, $field) {
779 return Database::getConnection()->schema()->dropField($table, $field);
783 * Sets the default value for a field.
786 * The table to be altered.
788 * The field to be altered.
790 * Default value to be set. NULL for 'default NULL'.
792 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
793 * a database connection injected into your service from the container, get
794 * its schema driver, and call fieldSetDefault() on it. For example,
795 * $injected_database->schema()->fieldSetDefault($table, $field, $default);
797 * @see \Drupal\Core\Database\Schema::fieldSetDefault()
799 function db_field_set_default($table, $field, $default) {
800 return Database::getConnection()->schema()->fieldSetDefault($table, $field, $default);
804 * Sets a field to have no default value.
807 * The table to be altered.
809 * The field to be altered.
811 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
812 * a database connection injected into your service from the container, get
813 * its schema driver, and call fieldSetNoDefault() on it. For example,
814 * $injected_database->schema()->fieldSetNoDefault($table, $field);
816 * @see \Drupal\Core\Database\Schema::fieldSetNoDefault()
818 function db_field_set_no_default($table, $field) {
819 return Database::getConnection()->schema()->fieldSetNoDefault($table, $field);
823 * Adds a primary key to a database table.
826 * Name of the table to be altered.
828 * Array of fields for the primary key.
830 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
831 * a database connection injected into your service from the container, get
832 * its schema driver, and call addPrimaryKey() on it. For example,
833 * $injected_database->schema()->addPrimaryKey($table, $fields);
835 * @see \Drupal\Core\Database\Schema::addPrimaryKey()
837 function db_add_primary_key($table, $fields) {
838 return Database::getConnection()->schema()->addPrimaryKey($table, $fields);
842 * Drops the primary key of a database table.
845 * Name of the table to be altered.
848 * TRUE if the primary key was successfully dropped, FALSE if there was no
849 * primary key on this table to begin with.
851 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
852 * a database connection injected into your service from the container, get
853 * its schema driver, and call dropPrimaryKey() on it. For example,
854 * $injected_database->schema()->dropPrimaryKey($table);
856 * @see \Drupal\Core\Database\Schema::dropPrimaryKey()
858 function db_drop_primary_key($table) {
859 return Database::getConnection()->schema()->dropPrimaryKey($table);
866 * The table to be altered.
868 * The name of the key.
869 * @param array $fields
870 * An array of field names.
872 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
873 * a database connection injected into your service from the container, get
874 * its schema driver, and call addUniqueKey() on it. For example,
875 * $injected_database->schema()->addUniqueKey($table, $name, $fields);
877 * @see \Drupal\Core\Database\Schema::addUniqueKey()
879 function db_add_unique_key($table, $name, $fields) {
880 return Database::getConnection()->schema()->addUniqueKey($table, $name, $fields);
884 * Drops a unique key.
887 * The table to be altered.
889 * The name of the key.
892 * TRUE if the key was successfully dropped, FALSE if there was no key by
893 * that name to begin with.
895 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
896 * a database connection injected into your service from the container, get
897 * its schema driver, and call dropUniqueKey() on it. For example,
898 * $injected_database->schema()->dropUniqueKey($table, $name);
900 * @see \Drupal\Core\Database\Schema::dropUniqueKey()
902 function db_drop_unique_key($table, $name) {
903 return Database::getConnection()->schema()->dropUniqueKey($table, $name);
910 * The table to be altered.
912 * The name of the index.
913 * @param array $fields
914 * An array of field names.
916 * The table specification of the table to be altered, as taken from a schema
917 * definition. See \Drupal\Core\Database\Schema::addIndex() for how to obtain
918 * this specification.
920 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
921 * a database connection injected into your service from the container, get
922 * its schema driver, and call addIndex() on it. For example,
923 * $injected_database->schema()->addIndex($table, $name, $fields, $spec);
927 * @see \Drupal\Core\Database\Schema::addIndex()
929 function db_add_index($table, $name, $fields, array $spec) {
930 return Database::getConnection()->schema()->addIndex($table, $name, $fields, $spec);
937 * The table to be altered.
939 * The name of the index.
942 * TRUE if the index was successfully dropped, FALSE if there was no index
943 * by that name to begin with.
945 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
946 * a database connection injected into your service from the container, get
947 * its schema driver, and call dropIndex() on it. For example,
948 * $injected_database->schema()->dropIndex($table, $name);
950 * @see \Drupal\Core\Database\Schema::dropIndex()
952 function db_drop_index($table, $name) {
953 return Database::getConnection()->schema()->dropIndex($table, $name);
957 * Changes a field definition.
959 * IMPORTANT NOTE: To maintain database portability, you have to explicitly
960 * recreate all indices and primary keys that are using the changed field.
962 * That means that you have to drop all affected keys and indexes with
963 * db_drop_{primary_key,unique_key,index}() before calling db_change_field().
964 * To recreate the keys and indices, pass the key definitions as the optional
965 * $keys_new argument directly to db_change_field().
967 * For example, suppose you have:
969 * $schema['foo'] = array(
971 * 'bar' => array('type' => 'int', 'not null' => TRUE)
973 * 'primary key' => array('bar')
976 * and you want to change foo.bar to be type serial, leaving it as the primary
977 * key. The correct sequence is:
979 * db_drop_primary_key('foo');
980 * db_change_field('foo', 'bar', 'bar',
981 * array('type' => 'serial', 'not null' => TRUE),
982 * array('primary key' => array('bar')));
985 * The reasons for this are due to the different database engines:
987 * On PostgreSQL, changing a field definition involves adding a new field and
988 * dropping an old one which causes any indices, primary keys and sequences
989 * (from serial-type fields) that use the changed field to be dropped.
991 * On MySQL, all type 'serial' fields must be part of at least one key or index
992 * as soon as they are created. You cannot use
993 * db_add_{primary_key,unique_key,index}() for this purpose because the ALTER
994 * TABLE command will fail to add the column without a key or index
995 * specification. The solution is to use the optional $keys_new argument to
996 * create the key or index at the same time as field.
998 * You could use db_add_{primary_key,unique_key,index}() in all cases unless you
999 * are converting a field to be type serial. You can use the $keys_new argument
1003 * Name of the table.
1005 * Name of the field to change.
1007 * New name for the field (set to the same as $field if you don't want to
1010 * The field specification for the new field.
1011 * @param array $keys_new
1012 * (optional) Keys and indexes specification to be created on the table along
1013 * with changing the field. The format is the same as a table specification
1014 * but without the 'fields' element.
1016 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
1017 * a database connection injected into your service from the container, get
1018 * its schema driver, and call changeField() on it. For example,
1019 * $injected_database->schema()->changeField($table, $field, $field_new, $spec, $keys_new);
1021 * @see \Drupal\Core\Database\Schema::changeField()
1023 function db_change_field($table, $field, $field_new, $spec, $keys_new = []) {
1024 return Database::getConnection()->schema()->changeField($table, $field, $field_new, $spec, $keys_new);
1028 * @} End of "addtogroup schemaapi".
1032 * Sets a session variable specifying the lag time for ignoring a replica
1033 * server (A replica server is traditionally referred to as
1034 * a "slave" in database server documentation).
1035 * @see https://www.drupal.org/node/2275877
1037 function db_ignore_replica() {
1038 $connection_info = Database::getConnectionInfo();
1039 // Only set ignore_replica_server if there are replica servers being used,
1040 // which is assumed if there are more than one.
1041 if (count($connection_info) > 1) {
1042 // Five minutes is long enough to allow the replica to break and resume
1043 // interrupted replication without causing problems on the Drupal site from
1045 $duration = Settings::get('maximum_replication_lag', 300);
1046 // Set session variable with amount of time to delay before using replica.
1047 $_SESSION['ignore_replica_server'] = REQUEST_TIME + $duration;