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 @trigger_error('db_set_active() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\Database::setActiveConnection() instead. See https://www.drupal.org/node/2944084.', E_USER_DEPRECATED);
339 return Database::setActiveConnection($key);
343 * Restricts a dynamic table name to safe characters.
345 * Only keeps alphanumeric and underscores.
348 * The table name to escape.
351 * The escaped table name as a string.
353 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
354 * a database connection injected into your service from the container and
355 * call escapeTable() on it. For example,
356 * $injected_database->escapeTable($table);
358 * @see \Drupal\Core\Database\Connection::escapeTable()
360 function db_escape_table($table) {
361 return Database::getConnection()->escapeTable($table);
365 * Restricts a dynamic column or constraint name to safe characters.
367 * Only keeps alphanumeric and underscores.
369 * @param string $field
370 * The field name to escape.
373 * The escaped field name as a string.
375 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
376 * a database connection injected into your service from the container and
377 * call escapeTable() on it. For example,
378 * $injected_database->escapeTable($table);
380 * @see \Drupal\Core\Database\Connection::escapeField()
382 function db_escape_field($field) {
383 return Database::getConnection()->escapeField($field);
387 * Escapes characters that work as wildcard characters in a LIKE pattern.
389 * The wildcard characters "%" and "_" as well as backslash are prefixed with
390 * a backslash. Use this to do a search for a verbatim string without any
393 * You must use a query builder like db_select() in order to use db_like() on
394 * all supported database systems. Using db_like() with db_query() or
395 * db_query_range() is not supported.
397 * For example, the following does a case-insensitive query for all rows whose
398 * name starts with $prefix:
400 * $result = db_select('person', 'p')
402 * ->condition('name', db_like($prefix) . '%', 'LIKE')
407 * Backslash is defined as escape character for LIKE patterns in
408 * DatabaseCondition::mapConditionOperator().
410 * @param string $string
411 * The string to escape.
414 * The escaped string.
416 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
417 * a database connection injected into your service from the container and
418 * call escapeLike() on it. For example,
419 * $injected_database->escapeLike($string);
421 * @see \Drupal\Core\Database\Connection::escapeLike()
423 function db_like($string) {
424 return Database::getConnection()->escapeLike($string);
428 * Retrieves the name of the currently active database driver.
431 * The name of the currently active database driver.
433 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
434 * a database connection injected into your service from the container and
435 * call driver() on it. For example, $injected_database->driver($string);
437 * @see \Drupal\Core\Database\Connection::driver()
439 function db_driver() {
440 return Database::getConnection()->driver();
444 * Closes the active database connection.
446 * @param array $options
447 * An array of options to control which connection is closed. Only the target
448 * key has any meaning in this case.
450 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Use
451 * \Drupal\Core\Database\Database::closeConnection($target).
453 * @see \Drupal\Core\Database\Database::closeConnection()
455 function db_close(array $options = []) {
456 if (empty($options['target'])) {
457 $options['target'] = NULL;
459 Database::closeConnection($options['target']);
463 * Retrieves a unique id.
465 * Use this function if for some reason you can't use a serial field. Using a
466 * serial field is preferred, and InsertQuery::execute() returns the value of
467 * the last ID inserted.
469 * @param int $existing_id
470 * After a database import, it might be that the sequences table is behind, so
471 * by passing in a minimum ID, it can be assured that we never issue the same
475 * An integer number larger than any number returned before for this sequence.
477 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
478 * a database connection injected into your service from the container and
479 * call nextId() on it. For example, $injected_database->nextId($existing_id);
481 * @see \Drupal\Core\Database\Connection::nextId()
483 function db_next_id($existing_id = 0) {
484 return Database::getConnection()->nextId($existing_id);
488 * Returns a new DatabaseCondition, set to "OR" all conditions together.
490 * @return \Drupal\Core\Database\Query\Condition
491 * A new Condition object, set to "OR" all conditions together.
493 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
494 * a \Drupal\Core\Database\Query\Condition object, specifying an OR
495 * conjunction: new Condition('OR');
497 * @see \Drupal\Core\Database\Query\Condition
500 return new Condition('OR');
504 * Returns a new DatabaseCondition, set to "AND" all conditions together.
506 * @return \Drupal\Core\Database\Query\Condition
507 * A new Condition object, set to "AND" all conditions together.
509 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
510 * a \Drupal\Core\Database\Query\Condition object, specifying an AND
511 * conjunction: new Condition('AND');
513 * @see \Drupal\Core\Database\Query\Condition
516 return new Condition('AND');
520 * Returns a new DatabaseCondition, set to "XOR" all conditions together.
522 * @return \Drupal\Core\Database\Query\Condition
523 * A new Condition object, set to "XOR" all conditions together.
525 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
526 * a \Drupal\Core\Database\Query\Condition object, specifying an XOR
527 * conjunction: new Condition('XOR');
529 * @see \Drupal\Core\Database\Query\Condition
532 return new Condition('XOR');
536 * Returns a new DatabaseCondition, set to the specified conjunction.
538 * Internal API function call. The db_and(), db_or(), and db_xor()
539 * functions are preferred.
541 * @param string $conjunction
542 * The conjunction to use for query conditions (AND, OR or XOR).
544 * @return \Drupal\Core\Database\Query\Condition
545 * A new Condition object, set to the specified conjunction.
547 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
548 * a \Drupal\Core\Database\Query\Condition object, specifying the desired
549 * conjunction: new Condition($conjunctin);
551 * @see \Drupal\Core\Database\Query\Condition
553 function db_condition($conjunction) {
554 return new Condition($conjunction);
558 * @} End of "addtogroup database".
563 * @addtogroup schemaapi
568 * Creates a new table from a Drupal table definition.
570 * @param string $name
571 * The name of the table to create.
572 * @param array $table
573 * A Schema API table definition array.
575 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
576 * a database connection injected into your service from the container, get
577 * its schema driver, and call createTable() on it. For example,
578 * $injected_database->schema()->createTable($name, $table);
580 * @see \Drupal\Core\Database\Schema::createTable()
582 function db_create_table($name, $table) {
583 return Database::getConnection()->schema()->createTable($name, $table);
587 * Returns an array of field names from an array of key/index column specifiers.
589 * This is usually an identity function but if a key/index uses a column prefix
590 * specification, this function extracts just the name.
592 * @param array $fields
593 * An array of key/index column specifiers.
596 * An array of field names.
598 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
599 * a database connection injected into your service from the container, get
600 * its schema driver, and call fieldNames() on it. For example,
601 * $injected_database->schema()->fieldNames($fields);
603 * @see \Drupal\Core\Database\Schema::fieldNames()
605 function db_field_names($fields) {
606 return Database::getConnection()->schema()->fieldNames($fields);
610 * Checks if an index exists in the given table.
612 * @param string $table
613 * The name of the table in drupal (no prefixing).
614 * @param string $name
615 * The name of the index in drupal (no prefixing).
618 * TRUE if the given index exists, otherwise FALSE.
620 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
621 * a database connection injected into your service from the container, get
622 * its schema driver, and call indexExists() on it. For example,
623 * $injected_database->schema()->indexExists($table, $name);
625 * @see \Drupal\Core\Database\Schema::indexExists()
627 function db_index_exists($table, $name) {
628 return Database::getConnection()->schema()->indexExists($table, $name);
632 * Checks if a table exists.
634 * @param string $table
635 * The name of the table in drupal (no prefixing).
638 * TRUE if the given table exists, otherwise FALSE.
640 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
641 * a database connection injected into your service from the container, get
642 * its schema driver, and call tableExists() on it. For example,
643 * $injected_database->schema()->tableExists($table);
645 * @see \Drupal\Core\Database\Schema::tableExists()
647 function db_table_exists($table) {
649 'db_table_exists() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use $injected_database->schema()->tableExists($table) instead. See https://www.drupal.org/node/2947929.',
653 return Database::getConnection()->schema()->tableExists($table);
657 * Checks if a column exists in the given table.
660 * The name of the table in drupal (no prefixing).
662 * The name of the field.
665 * TRUE if the given column exists, otherwise FALSE.
667 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
668 * a database connection injected into your service from the container, get
669 * its schema driver, and call fieldExists() on it. For example,
670 * $injected_database->schema()->fieldExists($table, $field);
672 * @see \Drupal\Core\Database\Schema::fieldExists()
674 function db_field_exists($table, $field) {
675 return Database::getConnection()->schema()->fieldExists($table, $field);
679 * Finds all tables that are like the specified base table name.
681 * @param string $table_expression
682 * An SQL expression, for example "simpletest%" (without the quotes).
685 * Array, both the keys and the values are the matching tables.
687 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
688 * a database connection injected into your service from the container, get
689 * its schema driver, and call findTables() on it. For example,
690 * $injected_database->schema()->findTables($table_expression);
692 * @see \Drupal\Core\Database\Schema::findTables()
694 function db_find_tables($table_expression) {
695 return Database::getConnection()->schema()->findTables($table_expression);
702 * The current name of the table to be renamed.
704 * The new name for the table.
706 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
707 * a database connection injected into your service from the container, get
708 * its schema driver, and call renameTable() on it. For example,
709 * $injected_database->schema()->renameTable($table, $new_name);
711 * @see \Drupal\Core\Database\Schema::renameTable()
713 function db_rename_table($table, $new_name) {
714 return Database::getConnection()->schema()->renameTable($table, $new_name);
721 * The table to be dropped.
723 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
724 * a database connection injected into your service from the container, get
725 * its schema driver, and call dropTable() on it. For example,
726 * $injected_database->schema()->dropTable($table);
728 * @see \Drupal\Core\Database\Schema::dropTable()
730 function db_drop_table($table) {
731 @trigger_error('db_drop_table() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\Database::getConnection()->schema()->dropTable() instead. See https://www.drupal.org/node/2987737', E_USER_DEPRECATED);
732 return Database::getConnection()->schema()->dropTable($table);
736 * Adds a new field to a table.
739 * Name of the table to be altered.
741 * Name of the field to be added.
743 * The field specification array, as taken from a schema definition. The
744 * specification may also contain the key 'initial'; the newly-created field
745 * will be set to the value of the key in all rows. This is most useful for
746 * creating NOT NULL columns with no default value in existing tables.
747 * @param array $keys_new
748 * (optional) Keys and indexes specification to be created on the table along
749 * with adding the field. The format is the same as a table specification, but
750 * without the 'fields' element. If you are adding a type 'serial' field, you
751 * MUST specify at least one key or index including it in this array. See
752 * db_change_field() for more explanation why.
754 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
755 * a database connection injected into your service from the container, get
756 * its schema driver, and call addField() on it. For example,
757 * $injected_database->schema()->addField($table, $field, $spec, $keys_new);
759 * @see \Drupal\Core\Database\Schema::addField()
760 * @see db_change_field()
762 function db_add_field($table, $field, $spec, $keys_new = []) {
763 return Database::getConnection()->schema()->addField($table, $field, $spec, $keys_new);
770 * The table to be altered.
772 * The field to be dropped.
775 * TRUE if the field was successfully dropped, FALSE if there was no field by
776 * that name to begin with.
778 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
779 * a database connection injected into your service from the container, get
780 * its schema driver, and call dropField() on it. For example,
781 * $injected_database->schema()->dropField($table, $field);
783 * @see \Drupal\Core\Database\Schema::dropField()
785 function db_drop_field($table, $field) {
786 return Database::getConnection()->schema()->dropField($table, $field);
790 * Sets the default value for a field.
793 * The table to be altered.
795 * The field to be altered.
797 * Default value to be set. NULL for 'default NULL'.
799 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
800 * a database connection injected into your service from the container, get
801 * its schema driver, and call fieldSetDefault() on it. For example,
802 * $injected_database->schema()->fieldSetDefault($table, $field, $default);
804 * @see \Drupal\Core\Database\Schema::fieldSetDefault()
806 function db_field_set_default($table, $field, $default) {
807 return Database::getConnection()->schema()->fieldSetDefault($table, $field, $default);
811 * Sets a field to have no default value.
814 * The table to be altered.
816 * The field to be altered.
818 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
819 * a database connection injected into your service from the container, get
820 * its schema driver, and call fieldSetNoDefault() on it. For example,
821 * $injected_database->schema()->fieldSetNoDefault($table, $field);
823 * @see \Drupal\Core\Database\Schema::fieldSetNoDefault()
825 function db_field_set_no_default($table, $field) {
826 return Database::getConnection()->schema()->fieldSetNoDefault($table, $field);
830 * Adds a primary key to a database table.
833 * Name of the table to be altered.
835 * Array of fields for the primary key.
837 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
838 * a database connection injected into your service from the container, get
839 * its schema driver, and call addPrimaryKey() on it. For example,
840 * $injected_database->schema()->addPrimaryKey($table, $fields);
842 * @see \Drupal\Core\Database\Schema::addPrimaryKey()
844 function db_add_primary_key($table, $fields) {
845 return Database::getConnection()->schema()->addPrimaryKey($table, $fields);
849 * Drops the primary key of a database table.
852 * Name of the table to be altered.
855 * TRUE if the primary key was successfully dropped, FALSE if there was no
856 * primary key on this table to begin with.
858 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
859 * a database connection injected into your service from the container, get
860 * its schema driver, and call dropPrimaryKey() on it. For example,
861 * $injected_database->schema()->dropPrimaryKey($table);
863 * @see \Drupal\Core\Database\Schema::dropPrimaryKey()
865 function db_drop_primary_key($table) {
866 return Database::getConnection()->schema()->dropPrimaryKey($table);
873 * The table to be altered.
875 * The name of the key.
876 * @param array $fields
877 * An array of field names.
879 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
880 * a database connection injected into your service from the container, get
881 * its schema driver, and call addUniqueKey() on it. For example,
882 * $injected_database->schema()->addUniqueKey($table, $name, $fields);
884 * @see \Drupal\Core\Database\Schema::addUniqueKey()
886 function db_add_unique_key($table, $name, $fields) {
887 return Database::getConnection()->schema()->addUniqueKey($table, $name, $fields);
891 * Drops a unique key.
894 * The table to be altered.
896 * The name of the key.
899 * TRUE if the key was successfully dropped, FALSE if there was no key by
900 * that name to begin with.
902 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
903 * a database connection injected into your service from the container, get
904 * its schema driver, and call dropUniqueKey() on it. For example,
905 * $injected_database->schema()->dropUniqueKey($table, $name);
907 * @see \Drupal\Core\Database\Schema::dropUniqueKey()
909 function db_drop_unique_key($table, $name) {
910 return Database::getConnection()->schema()->dropUniqueKey($table, $name);
917 * The table to be altered.
919 * The name of the index.
920 * @param array $fields
921 * An array of field names.
923 * The table specification of the table to be altered, as taken from a schema
924 * definition. See \Drupal\Core\Database\Schema::addIndex() for how to obtain
925 * this specification.
927 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
928 * a database connection injected into your service from the container, get
929 * its schema driver, and call addIndex() on it. For example,
930 * $injected_database->schema()->addIndex($table, $name, $fields, $spec);
934 * @see \Drupal\Core\Database\Schema::addIndex()
936 function db_add_index($table, $name, $fields, array $spec) {
937 return Database::getConnection()->schema()->addIndex($table, $name, $fields, $spec);
944 * The table to be altered.
946 * The name of the index.
949 * TRUE if the index was successfully dropped, FALSE if there was no index
950 * by that name to begin with.
952 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
953 * a database connection injected into your service from the container, get
954 * its schema driver, and call dropIndex() on it. For example,
955 * $injected_database->schema()->dropIndex($table, $name);
957 * @see \Drupal\Core\Database\Schema::dropIndex()
959 function db_drop_index($table, $name) {
960 return Database::getConnection()->schema()->dropIndex($table, $name);
964 * Changes a field definition.
966 * IMPORTANT NOTE: To maintain database portability, you have to explicitly
967 * recreate all indices and primary keys that are using the changed field.
969 * That means that you have to drop all affected keys and indexes with
970 * db_drop_{primary_key,unique_key,index}() before calling db_change_field().
971 * To recreate the keys and indices, pass the key definitions as the optional
972 * $keys_new argument directly to db_change_field().
974 * For example, suppose you have:
976 * $schema['foo'] = array(
978 * 'bar' => array('type' => 'int', 'not null' => TRUE)
980 * 'primary key' => array('bar')
983 * and you want to change foo.bar to be type serial, leaving it as the primary
984 * key. The correct sequence is:
986 * db_drop_primary_key('foo');
987 * db_change_field('foo', 'bar', 'bar',
988 * array('type' => 'serial', 'not null' => TRUE),
989 * array('primary key' => array('bar')));
992 * The reasons for this are due to the different database engines:
994 * On PostgreSQL, changing a field definition involves adding a new field and
995 * dropping an old one which causes any indices, primary keys and sequences
996 * (from serial-type fields) that use the changed field to be dropped.
998 * On MySQL, all type 'serial' fields must be part of at least one key or index
999 * as soon as they are created. You cannot use
1000 * db_add_{primary_key,unique_key,index}() for this purpose because the ALTER
1001 * TABLE command will fail to add the column without a key or index
1002 * specification. The solution is to use the optional $keys_new argument to
1003 * create the key or index at the same time as field.
1005 * You could use db_add_{primary_key,unique_key,index}() in all cases unless you
1006 * are converting a field to be type serial. You can use the $keys_new argument
1010 * Name of the table.
1012 * Name of the field to change.
1014 * New name for the field (set to the same as $field if you don't want to
1017 * The field specification for the new field.
1018 * @param array $keys_new
1019 * (optional) Keys and indexes specification to be created on the table along
1020 * with changing the field. The format is the same as a table specification
1021 * but without the 'fields' element.
1023 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
1024 * a database connection injected into your service from the container, get
1025 * its schema driver, and call changeField() on it. For example,
1026 * $injected_database->schema()->changeField($table, $field, $field_new, $spec, $keys_new);
1028 * @see \Drupal\Core\Database\Schema::changeField()
1030 function db_change_field($table, $field, $field_new, $spec, $keys_new = []) {
1031 return Database::getConnection()->schema()->changeField($table, $field, $field_new, $spec, $keys_new);
1035 * @} End of "addtogroup schemaapi".
1039 * Sets a session variable specifying the lag time for ignoring a replica
1040 * server (A replica server is traditionally referred to as
1041 * a "slave" in database server documentation).
1042 * @see https://www.drupal.org/node/2275877
1044 function db_ignore_replica() {
1045 $connection_info = Database::getConnectionInfo();
1046 // Only set ignore_replica_server if there are replica servers being used,
1047 // which is assumed if there are more than one.
1048 if (count($connection_info) > 1) {
1049 // Five minutes is long enough to allow the replica to break and resume
1050 // interrupted replication without causing problems on the Drupal site from
1052 $duration = Settings::get('maximum_replication_lag', 300);
1053 // Set session variable with amount of time to delay before using replica.
1054 $_SESSION['ignore_replica_server'] = REQUEST_TIME + $duration;