Pull merge.
[yaffs-website] / web / core / includes / database.inc
1 <?php
2
3 /**
4  * @file
5  * Core systems for the database layer.
6  *
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.
10  */
11
12 use Drupal\Core\Database\Database;
13 use Drupal\Core\Database\Query\Condition;
14 use Drupal\Core\Site\Settings;
15
16 /**
17  * @addtogroup database
18  * @{
19  */
20
21 /**
22  * Executes an arbitrary query string against the active database.
23  *
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()
26  * instead.
27  *
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.
30  *
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[].
37  * @param array $args
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.
44  *
45  * @return \Drupal\Core\Database\StatementInterface
46  *   A prepared statement object, already executed.
47  *
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);
52  *
53  * @see \Drupal\Core\Database\Connection::query()
54  * @see \Drupal\Core\Database\Connection::defaultOptions()
55  */
56 function db_query($query, array $args = [], array $options = []) {
57   if (empty($options['target'])) {
58     $options['target'] = 'default';
59   }
60
61   return Database::getConnection($options['target'])->query($query, $args, $options);
62 }
63
64 /**
65  * Executes a query against the active database, restricted to a range.
66  *
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.
71  * @param $from
72  *   The first record from the result set to return.
73  * @param $count
74  *   The number of records to return from the result set.
75  * @param array $args
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.
82  *
83  * @return \Drupal\Core\Database\StatementInterface
84  *   A prepared statement object, already executed.
85  *
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);
90  *
91  * @see \Drupal\Core\Database\Connection::queryRange()
92  * @see \Drupal\Core\Database\Connection::defaultOptions()
93  */
94 function db_query_range($query, $from, $count, array $args = [], array $options = []) {
95   if (empty($options['target'])) {
96     $options['target'] = 'default';
97   }
98
99   return Database::getConnection($options['target'])->queryRange($query, $from, $count, $args, $options);
100 }
101
102 /**
103  * Executes a SELECT query string and saves the result set to a temporary table.
104  *
105  * The execution of the query string happens against the active database.
106  *
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.
111  * @param array $args
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.
118  *
119  * @return
120  *   The name of the temporary table.
121  *
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);
126  *
127  * @see \Drupal\Core\Database\Connection::queryTemporary()
128  * @see \Drupal\Core\Database\Connection::defaultOptions()
129  */
130 function db_query_temporary($query, array $args = [], array $options = []) {
131   if (empty($options['target'])) {
132     $options['target'] = 'default';
133   }
134
135   return Database::getConnection($options['target'])->queryTemporary($query, $args, $options);
136 }
137
138 /**
139  * Returns a new InsertQuery object for the active database.
140  *
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.
145  *
146  * @return \Drupal\Core\Database\Query\Insert
147  *   A new Insert object for this connection.
148  *
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);
153  *
154  * @see \Drupal\Core\Database\Connection::insert()
155  * @see \Drupal\Core\Database\Connection::defaultOptions()
156  */
157 function db_insert($table, array $options = []) {
158   if (empty($options['target']) || $options['target'] == 'replica') {
159     $options['target'] = 'default';
160   }
161   return Database::getConnection($options['target'])->insert($table, $options);
162 }
163
164 /**
165  * Returns a new MergeQuery object for the active database.
166  *
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.
171  *
172  * @return \Drupal\Core\Database\Query\Merge
173  *   A new Merge object for this connection.
174  *
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);
179  *
180  * @see \Drupal\Core\Database\Connection::merge()
181  * @see \Drupal\Core\Database\Connection::defaultOptions()
182  */
183 function db_merge($table, array $options = []) {
184   if (empty($options['target']) || $options['target'] == 'replica') {
185     $options['target'] = 'default';
186   }
187   return Database::getConnection($options['target'])->merge($table, $options);
188 }
189
190 /**
191  * Returns a new UpdateQuery object for the active database.
192  *
193  * @param string $table
194  *   The table to update.
195  * @param array $options
196  *   An array of options to control how the query operates.
197  *
198  * @return \Drupal\Core\Database\Query\Update
199  *   A new Update object for this connection.
200  *
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);
205  *
206  * @see \Drupal\Core\Database\Connection::update()
207  * @see \Drupal\Core\Database\Connection::defaultOptions()
208  */
209 function db_update($table, array $options = []) {
210   if (empty($options['target']) || $options['target'] == 'replica') {
211     $options['target'] = 'default';
212   }
213   return Database::getConnection($options['target'])->update($table, $options);
214 }
215
216 /**
217  * Returns a new DeleteQuery object for the active database.
218  *
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.
223  *
224  * @return \Drupal\Core\Database\Query\Delete
225  *   A new Delete object for this connection.
226  *
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);
231  *
232  * @see \Drupal\Core\Database\Connection::delete()
233  * @see \Drupal\Core\Database\Connection::defaultOptions()
234  */
235 function db_delete($table, array $options = []) {
236   if (empty($options['target']) || $options['target'] == 'replica') {
237     $options['target'] = 'default';
238   }
239   return Database::getConnection($options['target'])->delete($table, $options);
240 }
241
242 /**
243  * Returns a new TruncateQuery object for the active database.
244  *
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.
249  *
250  * @return \Drupal\Core\Database\Query\Truncate
251  *   A new Truncate object for this connection.
252  *
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);
257  *
258  * @see \Drupal\Core\Database\Connection::truncate()
259  * @see \Drupal\Core\Database\Connection::defaultOptions()
260  */
261 function db_truncate($table, array $options = []) {
262   if (empty($options['target']) || $options['target'] == 'replica') {
263     $options['target'] = 'default';
264   }
265   return Database::getConnection($options['target'])->truncate($table, $options);
266 }
267
268 /**
269  * Returns a new SelectQuery object for the active database.
270  *
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
274  *   subselect.
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.
279  *
280  * @return \Drupal\Core\Database\Query\Select
281  *   A new Select object for this connection.
282  *
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);
287  *
288  * @see \Drupal\Core\Database\Connection::select()
289  * @see \Drupal\Core\Database\Connection::defaultOptions()
290  */
291 function db_select($table, $alias = NULL, array $options = []) {
292   if (empty($options['target'])) {
293     $options['target'] = 'default';
294   }
295   return Database::getConnection($options['target'])->select($table, $alias, $options);
296 }
297
298 /**
299  * Returns a new transaction object for the active database.
300  *
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.
306  *
307  * @return \Drupal\Core\Database\Transaction
308  *   A new Transaction object for this connection.
309  *
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);
314  *
315  * @see \Drupal\Core\Database\Connection::startTransaction()
316  * @see \Drupal\Core\Database\Connection::defaultOptions()
317  */
318 function db_transaction($name = NULL, array $options = []) {
319   if (empty($options['target'])) {
320     $options['target'] = 'default';
321   }
322   return Database::getConnection($options['target'])->startTransaction($name);
323 }
324
325 /**
326  * Sets a new active database.
327  *
328  * @param $key
329  *   The key in the $databases array to set as the default database.
330  *
331  * @return string|null
332  *   The key of the formerly active database.
333  *
334  * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Use
335  * \Drupal\Core\Database\Database::setActiveConnection().
336  */
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);
340 }
341
342 /**
343  * Restricts a dynamic table name to safe characters.
344  *
345  * Only keeps alphanumeric and underscores.
346  *
347  * @param $table
348  *   The table name to escape.
349  *
350  * @return string
351  *   The escaped table name as a string.
352  *
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);
357  *
358  * @see \Drupal\Core\Database\Connection::escapeTable()
359  */
360 function db_escape_table($table) {
361   return Database::getConnection()->escapeTable($table);
362 }
363
364 /**
365  * Restricts a dynamic column or constraint name to safe characters.
366  *
367  * Only keeps alphanumeric and underscores.
368  *
369  * @param string $field
370  *   The field name to escape.
371  *
372  * @return string
373  *   The escaped field name as a string.
374  *
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);
379  *
380  * @see \Drupal\Core\Database\Connection::escapeField()
381  */
382 function db_escape_field($field) {
383   return Database::getConnection()->escapeField($field);
384 }
385
386 /**
387  * Escapes characters that work as wildcard characters in a LIKE pattern.
388  *
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
391  * wildcard behavior.
392  *
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.
396  *
397  * For example, the following does a case-insensitive query for all rows whose
398  * name starts with $prefix:
399  * @code
400  * $result = db_select('person', 'p')
401  *   ->fields('p')
402  *   ->condition('name', db_like($prefix) . '%', 'LIKE')
403  *   ->execute()
404  *   ->fetchAll();
405  * @endcode
406  *
407  * Backslash is defined as escape character for LIKE patterns in
408  * DatabaseCondition::mapConditionOperator().
409  *
410  * @param string $string
411  *   The string to escape.
412  *
413  * @return string
414  *   The escaped string.
415  *
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);
420  *
421  * @see \Drupal\Core\Database\Connection::escapeLike()
422  */
423 function db_like($string) {
424   return Database::getConnection()->escapeLike($string);
425 }
426
427 /**
428  * Retrieves the name of the currently active database driver.
429  *
430  * @return string
431  *   The name of the currently active database driver.
432  *
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);
436  *
437  * @see \Drupal\Core\Database\Connection::driver()
438  */
439 function db_driver() {
440   return Database::getConnection()->driver();
441 }
442
443 /**
444  * Closes the active database connection.
445  *
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.
449  *
450  * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Use
451  *   \Drupal\Core\Database\Database::closeConnection($target).
452  *
453  * @see \Drupal\Core\Database\Database::closeConnection()
454  */
455 function db_close(array $options = []) {
456   if (empty($options['target'])) {
457     $options['target'] = NULL;
458   }
459   Database::closeConnection($options['target']);
460 }
461
462 /**
463  * Retrieves a unique id.
464  *
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.
468  *
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
472  *   ID.
473  *
474  * @return int
475  *   An integer number larger than any number returned before for this sequence.
476  *
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);
480  *
481  * @see \Drupal\Core\Database\Connection::nextId()
482  */
483 function db_next_id($existing_id = 0) {
484   return Database::getConnection()->nextId($existing_id);
485 }
486
487 /**
488  * Returns a new DatabaseCondition, set to "OR" all conditions together.
489  *
490  * @return \Drupal\Core\Database\Query\Condition
491  *   A new Condition object, set to "OR" all conditions together.
492  *
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');
496  *
497  * @see \Drupal\Core\Database\Query\Condition
498  */
499 function db_or() {
500   return new Condition('OR');
501 }
502
503 /**
504  * Returns a new DatabaseCondition, set to "AND" all conditions together.
505  *
506  * @return \Drupal\Core\Database\Query\Condition
507  *   A new Condition object, set to "AND" all conditions together.
508  *
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');
512  *
513  * @see \Drupal\Core\Database\Query\Condition
514  */
515 function db_and() {
516   return new Condition('AND');
517 }
518
519 /**
520  * Returns a new DatabaseCondition, set to "XOR" all conditions together.
521  *
522  * @return \Drupal\Core\Database\Query\Condition
523  *   A new Condition object, set to "XOR" all conditions together.
524  *
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');
528  *
529  * @see \Drupal\Core\Database\Query\Condition
530  */
531 function db_xor() {
532   return new Condition('XOR');
533 }
534
535 /**
536  * Returns a new DatabaseCondition, set to the specified conjunction.
537  *
538  * Internal API function call.  The db_and(), db_or(), and db_xor()
539  * functions are preferred.
540  *
541  * @param string $conjunction
542  *   The conjunction to use for query conditions (AND, OR or XOR).
543  *
544  * @return \Drupal\Core\Database\Query\Condition
545  *   A new Condition object, set to the specified conjunction.
546  *
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);
550  *
551  * @see \Drupal\Core\Database\Query\Condition
552  */
553 function db_condition($conjunction) {
554   return new Condition($conjunction);
555 }
556
557 /**
558  * @} End of "addtogroup database".
559  */
560
561
562 /**
563  * @addtogroup schemaapi
564  * @{
565  */
566
567 /**
568  * Creates a new table from a Drupal table definition.
569  *
570  * @param string $name
571  *   The name of the table to create.
572  * @param array $table
573  *   A Schema API table definition array.
574  *
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);
579  *
580  * @see \Drupal\Core\Database\Schema::createTable()
581  */
582 function db_create_table($name, $table) {
583   return Database::getConnection()->schema()->createTable($name, $table);
584 }
585
586 /**
587  * Returns an array of field names from an array of key/index column specifiers.
588  *
589  * This is usually an identity function but if a key/index uses a column prefix
590  * specification, this function extracts just the name.
591  *
592  * @param array $fields
593  *   An array of key/index column specifiers.
594  *
595  * @return array
596  *   An array of field names.
597  *
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);
602  *
603  * @see \Drupal\Core\Database\Schema::fieldNames()
604  */
605 function db_field_names($fields) {
606   return Database::getConnection()->schema()->fieldNames($fields);
607 }
608
609 /**
610  * Checks if an index exists in the given table.
611  *
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).
616  *
617  * @return bool
618  *   TRUE if the given index exists, otherwise FALSE.
619  *
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);
624  *
625  * @see \Drupal\Core\Database\Schema::indexExists()
626  */
627 function db_index_exists($table, $name) {
628   return Database::getConnection()->schema()->indexExists($table, $name);
629 }
630
631 /**
632  * Checks if a table exists.
633  *
634  * @param string $table
635  *   The name of the table in drupal (no prefixing).
636  *
637  * @return bool
638  *   TRUE if the given table exists, otherwise FALSE.
639  *
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);
644  *
645  * @see \Drupal\Core\Database\Schema::tableExists()
646  */
647 function db_table_exists($table) {
648   @trigger_error(
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.',
650     E_USER_DEPRECATED
651   );
652
653   return Database::getConnection()->schema()->tableExists($table);
654 }
655
656 /**
657  * Checks if a column exists in the given table.
658  *
659  * @param $table
660  *   The name of the table in drupal (no prefixing).
661  * @param $field
662  *   The name of the field.
663  *
664  * @return bool
665  *   TRUE if the given column exists, otherwise FALSE.
666  *
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);
671  *
672  * @see \Drupal\Core\Database\Schema::fieldExists()
673  */
674 function db_field_exists($table, $field) {
675   return Database::getConnection()->schema()->fieldExists($table, $field);
676 }
677
678 /**
679  * Finds all tables that are like the specified base table name.
680  *
681  * @param string $table_expression
682  *   An SQL expression, for example "simpletest%" (without the quotes).
683  *
684  * @return array
685  *   Array, both the keys and the values are the matching tables.
686  *
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);
691  *
692  * @see \Drupal\Core\Database\Schema::findTables()
693  */
694 function db_find_tables($table_expression) {
695   return Database::getConnection()->schema()->findTables($table_expression);
696 }
697
698 /**
699  * Renames a table.
700  *
701  * @param $table
702  *   The current name of the table to be renamed.
703  * @param $new_name
704  *   The new name for the table.
705  *
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);
710  *
711  * @see \Drupal\Core\Database\Schema::renameTable()
712  */
713 function db_rename_table($table, $new_name) {
714   return Database::getConnection()->schema()->renameTable($table, $new_name);
715 }
716
717 /**
718  * Drops a table.
719  *
720  * @param $table
721  *   The table to be dropped.
722  *
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);
727  *
728  * @see \Drupal\Core\Database\Schema::dropTable()
729  */
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);
733 }
734
735 /**
736  * Adds a new field to a table.
737  *
738  * @param $table
739  *   Name of the table to be altered.
740  * @param $field
741  *   Name of the field to be added.
742  * @param array $spec
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.
753  *
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);
758  *
759  * @see \Drupal\Core\Database\Schema::addField()
760  * @see db_change_field()
761  */
762 function db_add_field($table, $field, $spec, $keys_new = []) {
763   return Database::getConnection()->schema()->addField($table, $field, $spec, $keys_new);
764 }
765
766 /**
767  * Drops a field.
768  *
769  * @param $table
770  *   The table to be altered.
771  * @param $field
772  *   The field to be dropped.
773  *
774  * @return bool
775  *   TRUE if the field was successfully dropped, FALSE if there was no field by
776  *   that name to begin with.
777  *
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);
782  *
783  * @see \Drupal\Core\Database\Schema::dropField()
784  */
785 function db_drop_field($table, $field) {
786   return Database::getConnection()->schema()->dropField($table, $field);
787 }
788
789 /**
790  * Sets the default value for a field.
791  *
792  * @param $table
793  *   The table to be altered.
794  * @param $field
795  *   The field to be altered.
796  * @param $default
797  *   Default value to be set. NULL for 'default NULL'.
798  *
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);
803  *
804  * @see \Drupal\Core\Database\Schema::fieldSetDefault()
805  */
806 function db_field_set_default($table, $field, $default) {
807   return Database::getConnection()->schema()->fieldSetDefault($table, $field, $default);
808 }
809
810 /**
811  * Sets a field to have no default value.
812  *
813  * @param $table
814  *   The table to be altered.
815  * @param $field
816  *   The field to be altered.
817  *
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);
822  *
823  * @see \Drupal\Core\Database\Schema::fieldSetNoDefault()
824  */
825 function db_field_set_no_default($table, $field) {
826   return Database::getConnection()->schema()->fieldSetNoDefault($table, $field);
827 }
828
829 /**
830  * Adds a primary key to a database table.
831  *
832  * @param $table
833  *   Name of the table to be altered.
834  * @param $fields
835  *   Array of fields for the primary key.
836  *
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);
841  *
842  * @see \Drupal\Core\Database\Schema::addPrimaryKey()
843  */
844 function db_add_primary_key($table, $fields) {
845   return Database::getConnection()->schema()->addPrimaryKey($table, $fields);
846 }
847
848 /**
849  * Drops the primary key of a database table.
850  *
851  * @param $table
852  *   Name of the table to be altered.
853  *
854  * @return bool
855  *   TRUE if the primary key was successfully dropped, FALSE if there was no
856  *   primary key on this table to begin with.
857  *
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);
862  *
863  * @see \Drupal\Core\Database\Schema::dropPrimaryKey()
864  */
865 function db_drop_primary_key($table) {
866   return Database::getConnection()->schema()->dropPrimaryKey($table);
867 }
868
869 /**
870  * Adds a unique key.
871  *
872  * @param $table
873  *   The table to be altered.
874  * @param $name
875  *   The name of the key.
876  * @param array $fields
877  *   An array of field names.
878  *
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);
883  *
884  * @see \Drupal\Core\Database\Schema::addUniqueKey()
885  */
886 function db_add_unique_key($table, $name, $fields) {
887   return Database::getConnection()->schema()->addUniqueKey($table, $name, $fields);
888 }
889
890 /**
891  * Drops a unique key.
892  *
893  * @param $table
894  *   The table to be altered.
895  * @param $name
896  *   The name of the key.
897  *
898  * @return bool
899  *   TRUE if the key was successfully dropped, FALSE if there was no key by
900  *   that name to begin with.
901  *
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);
906  *
907  * @see \Drupal\Core\Database\Schema::dropUniqueKey()
908  */
909 function db_drop_unique_key($table, $name) {
910   return Database::getConnection()->schema()->dropUniqueKey($table, $name);
911 }
912
913 /**
914  * Adds an index.
915  *
916  * @param $table
917  *   The table to be altered.
918  * @param $name
919  *   The name of the index.
920  * @param array $fields
921  *   An array of field names.
922  * @param array $spec
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.
926  *
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);
931  *
932  * @see hook_schema()
933  * @see schemaapi
934  * @see \Drupal\Core\Database\Schema::addIndex()
935  */
936 function db_add_index($table, $name, $fields, array $spec) {
937   return Database::getConnection()->schema()->addIndex($table, $name, $fields, $spec);
938 }
939
940 /**
941  * Drops an index.
942  *
943  * @param $table
944  *   The table to be altered.
945  * @param $name
946  *   The name of the index.
947  *
948  * @return bool
949  *   TRUE if the index was successfully dropped, FALSE if there was no index
950  *   by that name to begin with.
951  *
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);
956  *
957  * @see \Drupal\Core\Database\Schema::dropIndex()
958  */
959 function db_drop_index($table, $name) {
960   return Database::getConnection()->schema()->dropIndex($table, $name);
961 }
962
963 /**
964  * Changes a field definition.
965  *
966  * IMPORTANT NOTE: To maintain database portability, you have to explicitly
967  * recreate all indices and primary keys that are using the changed field.
968  *
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().
973  *
974  * For example, suppose you have:
975  * @code
976  * $schema['foo'] = array(
977  *   'fields' => array(
978  *     'bar' => array('type' => 'int', 'not null' => TRUE)
979  *   ),
980  *   'primary key' => array('bar')
981  * );
982  * @endcode
983  * and you want to change foo.bar to be type serial, leaving it as the primary
984  * key. The correct sequence is:
985  * @code
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')));
990  * @endcode
991  *
992  * The reasons for this are due to the different database engines:
993  *
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.
997  *
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.
1004  *
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
1007  * in all cases.
1008  *
1009  * @param $table
1010  *   Name of the table.
1011  * @param $field
1012  *   Name of the field to change.
1013  * @param $field_new
1014  *   New name for the field (set to the same as $field if you don't want to
1015  *   change the name).
1016  * @param $spec
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.
1022  *
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);
1027  *
1028  * @see \Drupal\Core\Database\Schema::changeField()
1029  */
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);
1032 }
1033
1034 /**
1035  * @} End of "addtogroup schemaapi".
1036  */
1037
1038 /**
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
1043  */
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
1051     // the old data.
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;
1055   }
1056 }