database.inc 35.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * @file
 * Core systems for the database layer.
 *
 * Classes required for basic functioning of the database system should be
 * placed in this file.  All utility functions should also be placed in this
 * file only, as they cannot auto-load the way classes can.
 */

12 13 14 15
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Site\Settings;

16
/**
17
 * @addtogroup database
18 19 20 21 22 23 24 25 26 27 28 29 30
 * @{
 */

/**
 * Executes an arbitrary query string against the active database.
 *
 * Use this function for SELECT queries if it is just a simple query string.
 * If the caller or other modules need to change the query, use db_select()
 * instead.
 *
 * Do not use this function for INSERT, UPDATE, or DELETE queries. Those should
 * be handled via db_insert(), db_update() and db_delete() respectively.
 *
31
 * @param string|\Drupal\Core\Database\StatementInterface $query
32 33
 *   The prepared statement query to run. Although it will accept both named and
 *   unnamed placeholders, named placeholders are strongly preferred as they are
34 35 36
 *   more self-documenting. If the argument corresponding to a placeholder is
 *   an array of values to be expanded, e.g. for an IN query, the placeholder
 *   should be named with a trailing bracket like :example[]
37
 * @param array $args
38 39 40 41
 *   An array of values to substitute into the query. If the query uses named
 *   placeholders, this is an associative array in any order. If the query uses
 *   unnamed placeholders (?), this is an indexed array and the order must match
 *   the order of placeholders in the query string.
42
 * @param array $options
43 44
 *   An array of options to control how the query operates.
 *
45
 * @return \Drupal\Core\Database\StatementInterface
46 47
 *   A prepared statement object, already executed.
 *
48 49 50 51 52 53
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call query() on it. E.g.
 *   $injected_database->query($query, $args, $options);
 *
 * @see \Drupal\Core\Database\Connection::query()
54
 * @see \Drupal\Core\Database\Connection::defaultOptions()
55 56 57 58 59 60 61 62 63 64 65 66
 */
function db_query($query, array $args = array(), array $options = array()) {
  if (empty($options['target'])) {
    $options['target'] = 'default';
  }

  return Database::getConnection($options['target'])->query($query, $args, $options);
}

/**
 * Executes a query against the active database, restricted to a range.
 *
67
 * @param string $query
68 69 70 71 72 73 74
 *   The prepared statement query to run. Although it will accept both named and
 *   unnamed placeholders, named placeholders are strongly preferred as they are
 *   more self-documenting.
 * @param $from
 *   The first record from the result set to return.
 * @param $count
 *   The number of records to return from the result set.
75
 * @param array $args
76 77 78 79
 *   An array of values to substitute into the query. If the query uses named
 *   placeholders, this is an associative array in any order. If the query uses
 *   unnamed placeholders (?), this is an indexed array and the order must match
 *   the order of placeholders in the query string.
80
 * @param array $options
81 82
 *   An array of options to control how the query operates.
 *
83
 * @return \Drupal\Core\Database\StatementInterface
84 85
 *   A prepared statement object, already executed.
 *
86 87 88 89 90 91
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call queryRange() on it. E.g.
 *   $injected_database->queryRange($query, $from, $count, $args, $options);
 *
 * @see \Drupal\Core\Database\Connection::queryRange()
92
 * @see \Drupal\Core\Database\Connection::defaultOptions()
93 94 95 96 97 98 99 100 101 102
 */
function db_query_range($query, $from, $count, array $args = array(), array $options = array()) {
  if (empty($options['target'])) {
    $options['target'] = 'default';
  }

  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.
104 105 106
 *
 * The execution of the query string happens against the active database.
 *
107
 * @param string $query
108 109 110
 *   The prepared SELECT statement query to run. Although it will accept both
 *   named and unnamed placeholders, named placeholders are strongly preferred
 *   as they are more self-documenting.
111
 * @param array $args
112 113 114 115
 *   An array of values to substitute into the query. If the query uses named
 *   placeholders, this is an associative array in any order. If the query uses
 *   unnamed placeholders (?), this is an indexed array and the order must match
 *   the order of placeholders in the query string.
116
 * @param array $options
117 118 119 120 121
 *   An array of options to control how the query operates.
 *
 * @return
 *   The name of the temporary table.
 *
122 123 124 125 126 127
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call queryTemporary() on it. E.g.
 *   $injected_database->queryTemporary($query, $args, $options);
 *
 * @see \Drupal\Core\Database\Connection::queryTemporary()
128
 * @see \Drupal\Core\Database\Connection::defaultOptions()
129 130 131 132 133 134 135 136 137 138 139 140
 */
function db_query_temporary($query, array $args = array(), array $options = array()) {
  if (empty($options['target'])) {
    $options['target'] = 'default';
  }

  return Database::getConnection($options['target'])->queryTemporary($query, $args, $options);
}

/**
 * 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 145
 *   An array of options to control how the query operates.
 *
146 147
 * @return \Drupal\Core\Database\Query\Insert
 *   A new Insert object for this connection.
148 149 150 151 152 153 154
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call insert() on it. E.g. $injected_database->insert($table, $options);
 *
 * @see \Drupal\Core\Database\Connection::insert()
 * @see \Drupal\Core\Database\Connection::defaultOptions()
155 156
 */
function db_insert($table, array $options = array()) {
157
  if (empty($options['target']) || $options['target'] == 'replica') {
158 159 160 161 162 163 164 165
    $options['target'] = 'default';
  }
  return Database::getConnection($options['target'])->insert($table, $options);
}

/**
 * Returns a new MergeQuery object for the active database.
 *
166 167 168
 * @param string $table
 *   Name of the table to associate with this query.
 * @param array $options
169 170
 *   An array of options to control how the query operates.
 *
171 172
 * @return \Drupal\Core\Database\Query\Merge
 *   A new Merge object for this connection.
173 174 175 176 177 178 179
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call merge() on it. E.g. $injected_database->merge($table, $options);
 *
 * @see \Drupal\Core\Database\Connection::merge()
 * @see \Drupal\Core\Database\Connection::defaultOptions()
180 181
 */
function db_merge($table, array $options = array()) {
182
  if (empty($options['target']) || $options['target'] == 'replica') {
183 184 185 186 187 188 189 190
    $options['target'] = 'default';
  }
  return Database::getConnection($options['target'])->merge($table, $options);
}

/**
 * Returns a new UpdateQuery object for the active database.
 *
191
 * @param string $table
192
 *   The table to update.
193
 * @param array $options
194 195
 *   An array of options to control how the query operates.
 *
196 197
 * @return \Drupal\Core\Database\Query\Update
 *   A new Update object for this connection.
198 199 200 201 202 203 204
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call update() on it. E.g. $injected_database->update($table, $options);
 *
 * @see \Drupal\Core\Database\Connection::update()
 * @see \Drupal\Core\Database\Connection::defaultOptions()
205 206
 */
function db_update($table, array $options = array()) {
207
  if (empty($options['target']) || $options['target'] == 'replica') {
208 209 210 211 212 213 214 215
    $options['target'] = 'default';
  }
  return Database::getConnection($options['target'])->update($table, $options);
}

/**
 * Returns a new DeleteQuery object for the active database.
 *
216
 * @param string $table
217
 *   The table from which to delete.
218
 * @param array $options
219 220
 *   An array of options to control how the query operates.
 *
221 222
 * @return \Drupal\Core\Database\Query\Delete
 *   A new Delete object for this connection.
223 224 225 226 227 228 229
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call delete() on it. E.g. $injected_database->delete($table, $options);
 *
 * @see \Drupal\Core\Database\Connection::delete()
 * @see \Drupal\Core\Database\Connection::defaultOptions()
230 231
 */
function db_delete($table, array $options = array()) {
232
  if (empty($options['target']) || $options['target'] == 'replica') {
233 234 235 236 237 238 239 240
    $options['target'] = 'default';
  }
  return Database::getConnection($options['target'])->delete($table, $options);
}

/**
 * Returns a new TruncateQuery object for the active database.
 *
241 242 243
 * @param string $table
 *   The table from which to truncate.
 * @param array $options
244 245
 *   An array of options to control how the query operates.
 *
246 247
 * @return \Drupal\Core\Database\Query\Truncate
 *   A new Truncate object for this connection.
248 249 250 251 252 253 254
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call truncate() on it. E.g. $injected_database->truncate($table, $options);
 *
 * @see \Drupal\Core\Database\Connection::truncate()
 * @see \Drupal\Core\Database\Connection::defaultOptions()
255 256
 */
function db_truncate($table, array $options = array()) {
257
  if (empty($options['target']) || $options['target'] == 'replica') {
258 259 260 261 262 263 264 265
    $options['target'] = 'default';
  }
  return Database::getConnection($options['target'])->truncate($table, $options);
}

/**
 * Returns a new SelectQuery object for the active database.
 *
266 267 268 269 270
 * @param string|\Drupal\Core\Database\Query\SelectInterface $table
 *   The base table for this query. May be a string or another SelectInterface
 *   object. If a SelectInterface object is passed, it will be used as a
 *   subselect.
 * @param string $alias
271
 *   (optional) The alias for the base table of this query.
272
 * @param array $options
273
 *   (optional) An array of options to control how the query operates.
274
 *
275 276
 * @return \Drupal\Core\Database\Query\Select
 *   A new Select object for this connection.
277 278 279 280 281 282 283 284
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call select() on it. E.g.
 *   $injected_database->select($table, $alias, $options);
 *
 * @see \Drupal\Core\Database\Connection::select()
 * @see \Drupal\Core\Database\Connection::defaultOptions()
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
 */
function db_select($table, $alias = NULL, array $options = array()) {
  if (empty($options['target'])) {
    $options['target'] = 'default';
  }
  return Database::getConnection($options['target'])->select($table, $alias, $options);
}

/**
 * Returns a new transaction object for the active database.
 *
 * @param string $name
 *   Optional name of the transaction.
 * @param array $options
 *   An array of options to control how the transaction operates:
 *   - target: The database target name.
 *
302 303
 * @return \Drupal\Core\Database\Transaction
 *   A new Transaction object for this connection.
304 305 306 307 308 309 310 311
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call startTransaction() on it. E.g.
 *   $injected_database->startTransaction($name);
 *
 * @see \Drupal\Core\Database\Connection::startTransaction()
 * @see \Drupal\Core\Database\Connection::defaultOptions()
312 313 314 315 316 317 318 319 320 321 322 323 324 325
 */
function db_transaction($name = NULL, array $options = array()) {
  if (empty($options['target'])) {
    $options['target'] = 'default';
  }
  return Database::getConnection($options['target'])->startTransaction($name);
}

/**
 * Sets a new active database.
 *
 * @param $key
 *   The key in the $databases array to set as the default database.
 *
326
 * @return string|null
327
 *   The key of the formerly active database.
328 329 330
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Use
 * \Drupal\Core\Database\Database::setActiveConnection().
331 332 333 334 335 336 337 338 339 340 341 342 343
 */
function db_set_active($key = 'default') {
  return Database::setActiveConnection($key);
}

/**
 * Restricts a dynamic table name to safe characters.
 *
 * Only keeps alphanumeric and underscores.
 *
 * @param $table
 *   The table name to escape.
 *
344
 * @return string
345
 *   The escaped table name as a string.
346 347 348 349 350 351
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call escapeTable() on it. E.g. $injected_database->escapeTable($table);
 *
 * @see \Drupal\Core\Database\Connection::escapeTable()
352 353 354 355 356 357 358 359 360 361
 */
function db_escape_table($table) {
  return Database::getConnection()->escapeTable($table);
}

/**
 * Restricts a dynamic column or constraint name to safe characters.
 *
 * Only keeps alphanumeric and underscores.
 *
362
 * @param string $field
363 364
 *   The field name to escape.
 *
365
 * @return string
366
 *   The escaped field name as a string.
367 368 369 370 371 372
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call escapeTable() on it. E.g. $injected_database->escapeTable($table);
 *
 * @see \Drupal\Core\Database\Connection::escapeField()
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
 */
function db_escape_field($field) {
  return Database::getConnection()->escapeField($field);
}

/**
 * Escapes characters that work as wildcard characters in a LIKE pattern.
 *
 * The wildcard characters "%" and "_" as well as backslash are prefixed with
 * a backslash. Use this to do a search for a verbatim string without any
 * wildcard behavior.
 *
 * You must use a query builder like db_select() in order to use db_like() on
 * all supported database systems. Using db_like() with db_query() or
 * db_query_range() is not supported.
 *
 * For example, the following does a case-insensitive query for all rows whose
 * name starts with $prefix:
 * @code
 * $result = db_select('person', 'p')
 *   ->fields('p')
 *   ->condition('name', db_like($prefix) . '%', 'LIKE')
 *   ->execute()
 *   ->fetchAll();
 * @endcode
 *
 * Backslash is defined as escape character for LIKE patterns in
 * DatabaseCondition::mapConditionOperator().
 *
402
 * @param string $string
403 404
 *   The string to escape.
 *
405
 * @return string
406
 *   The escaped string.
407 408 409 410 411 412
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call escapeLike() on it. E.g. $injected_database->escapeLike($string);
 *
 * @see \Drupal\Core\Database\Connection::escapeLike()
413 414 415 416 417 418 419 420
 */
function db_like($string) {
  return Database::getConnection()->escapeLike($string);
}

/**
 * Retrieves the name of the currently active database driver.
 *
421
 * @return string
422
 *   The name of the currently active database driver.
423 424 425 426 427 428
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call driver() on it. E.g. $injected_database->driver($string);
 *
 * @see \Drupal\Core\Database\Connection::driver()
429 430 431 432 433 434 435 436
 */
function db_driver() {
  return Database::getConnection()->driver();
}

/**
 * Closes the active database connection.
 *
437
 * @param array $options
438 439
 *   An array of options to control which connection is closed. Only the target
 *   key has any meaning in this case.
440 441 442 443 444
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Use
 *   \Drupal\Core\Database\Database::closeConnection($target).
 *
 * @see \Drupal\Core\Database\Database::closeConnection()
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
 */
function db_close(array $options = array()) {
  if (empty($options['target'])) {
    $options['target'] = NULL;
  }
  Database::closeConnection($options['target']);
}

/**
 * Retrieves a unique id.
 *
 * Use this function if for some reason you can't use a serial field. Using a
 * serial field is preferred, and InsertQuery::execute() returns the value of
 * the last ID inserted.
 *
460
 * @param int $existing_id
461 462 463 464
 *   After a database import, it might be that the sequences table is behind, so
 *   by passing in a minimum ID, it can be assured that we never issue the same
 *   ID.
 *
465
 * @return int
466
 *   An integer number larger than any number returned before for this sequence.
467 468 469 470 471 472
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container and
 *   call nextId() on it. E.g. $injected_database->nextId($existing_id);
 *
 * @see \Drupal\Core\Database\Connection::nextId()
473 474 475 476 477 478 479 480
 */
function db_next_id($existing_id = 0) {
  return Database::getConnection()->nextId($existing_id);
}

/**
 * Returns a new DatabaseCondition, set to "OR" all conditions together.
 *
481 482
 * @return \Drupal\Core\Database\Query\Condition
 *   A new Condition object, set to "OR" all conditions together.
483 484 485 486 487 488
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
 *   a \Drupal\Core\Database\Query\Condition object, specifying an OR
 *   conjunction: new Condition('OR');
 *
 * @see \Drupal\Core\Database\Query\Condition
489 490 491 492 493 494 495 496
 */
function db_or() {
  return new Condition('OR');
}

/**
 * Returns a new DatabaseCondition, set to "AND" all conditions together.
 *
497 498
 * @return \Drupal\Core\Database\Query\Condition
 *   A new Condition object, set to "AND" all conditions together.
499 500 501 502 503 504
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
 *   a \Drupal\Core\Database\Query\Condition object, specifying an AND
 *   conjunction: new Condition('AND');
 *
 * @see \Drupal\Core\Database\Query\Condition
505 506 507 508 509 510 511 512
 */
function db_and() {
  return new Condition('AND');
}

/**
 * Returns a new DatabaseCondition, set to "XOR" all conditions together.
 *
513 514
 * @return \Drupal\Core\Database\Query\Condition
 *   A new Condition object, set to "XOR" all conditions together.
515 516 517 518 519 520
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
 *   a \Drupal\Core\Database\Query\Condition object, specifying an XOR
 *   conjunction: new Condition('XOR');
 *
 * @see \Drupal\Core\Database\Query\Condition
521 522 523 524 525 526 527 528 529 530 531
 */
function db_xor() {
  return new Condition('XOR');
}

/**
 * Returns a new DatabaseCondition, set to the specified conjunction.
 *
 * Internal API function call.  The db_and(), db_or(), and db_xor()
 * functions are preferred.
 *
532
 * @param string $conjunction
533
 *   The conjunction to use for query conditions (AND, OR or XOR).
534 535 536
 *
 * @return \Drupal\Core\Database\Query\Condition
 *   A new Condition object, set to the specified conjunction.
537 538 539 540 541 542
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
 *   a \Drupal\Core\Database\Query\Condition object, specifying the desired
 *   conjunction: new Condition($conjunctin);
 *
 * @see \Drupal\Core\Database\Query\Condition
543 544 545 546 547 548
 */
function db_condition($conjunction) {
  return new Condition($conjunction);
}

/**
549
 * @} End of "addtogroup database".
550 551 552 553
 */


/**
554
 * @addtogroup schemaapi
555 556 557 558 559 560
 * @{
 */

/**
 * Creates a new table from a Drupal table definition.
 *
561
 * @param string $name
562
 *   The name of the table to create.
563
 * @param array $table
564
 *   A Schema API table definition array.
565 566 567 568 569 570 571
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call createTable() on it. E.g.
 *   $injected_database->schema()->createTable($name, $table);
 *
 * @see \Drupal\Core\Database\Schema::createTable()
572 573 574 575 576 577 578 579 580 581 582
 */
function db_create_table($name, $table) {
  return Database::getConnection()->schema()->createTable($name, $table);
}

/**
 * Returns an array of field names from an array of key/index column specifiers.
 *
 * This is usually an identity function but if a key/index uses a column prefix
 * specification, this function extracts just the name.
 *
583
 * @param array $fields
584 585
 *   An array of key/index column specifiers.
 *
586
 * @return array
587
 *   An array of field names.
588 589 590 591 592 593 594
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call fieldNames() on it. E.g.
 *   $injected_database->schema()->fieldNames($fields);
 *
 * @see \Drupal\Core\Database\Schema::fieldNames()
595 596 597 598 599 600 601 602
 */
function db_field_names($fields) {
  return Database::getConnection()->schema()->fieldNames($fields);
}

/**
 * Checks if an index exists in the given table.
 *
603
 * @param string $table
604
 *   The name of the table in drupal (no prefixing).
605
 * @param string $name
606 607
 *   The name of the index in drupal (no prefixing).
 *
608
 * @return bool
609
 *   TRUE if the given index exists, otherwise FALSE.
610 611 612 613 614 615 616
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call indexExists() on it. E.g.
 *   $injected_database->schema()->indexExists($table, $name);
 *
 * @see \Drupal\Core\Database\Schema::indexExists()
617 618 619 620 621 622 623 624
 */
function db_index_exists($table, $name) {
  return Database::getConnection()->schema()->indexExists($table, $name);
}

/**
 * Checks if a table exists.
 *
625
 * @param string $table
626 627
 *   The name of the table in drupal (no prefixing).
 *
628
 * @return bool
629
 *   TRUE if the given table exists, otherwise FALSE.
630 631 632 633 634 635 636
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call tableExists() on it. E.g.
 *   $injected_database->schema()->tableExists($table);
 *
 * @see \Drupal\Core\Database\Schema::tableExists()
637 638 639 640 641 642 643 644 645 646 647 648 649
 */
function db_table_exists($table) {
  return Database::getConnection()->schema()->tableExists($table);
}

/**
 * Checks if a column exists in the given table.
 *
 * @param $table
 *   The name of the table in drupal (no prefixing).
 * @param $field
 *   The name of the field.
 *
650
 * @return bool
651
 *   TRUE if the given column exists, otherwise FALSE.
652 653 654 655 656 657 658
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call fieldExists() on it. E.g.
 *   $injected_database->schema()->fieldExists($table, $field);
 *
 * @see \Drupal\Core\Database\Schema::fieldExists()
659 660 661 662 663 664 665 666
 */
function db_field_exists($table, $field) {
  return Database::getConnection()->schema()->fieldExists($table, $field);
}

/**
 * Finds all tables that are like the specified base table name.
 *
667
 * @param string $table_expression
668 669
 *   An SQL expression, for example "simpletest%" (without the quotes).
 *
670
 * @return array
671
 *   Array, both the keys and the values are the matching tables.
672 673 674 675 676 677 678
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call findTables() on it. E.g.
 *   $injected_database->schema()->findTables($table_expression);
 *
 * @see \Drupal\Core\Database\Schema::findTables()
679 680 681 682 683 684 685 686 687
 */
function db_find_tables($table_expression) {
  return Database::getConnection()->schema()->findTables($table_expression);
}

/**
 * Renames a table.
 *
 * @param $table
688
 *   The current name of the table to be renamed.
689 690
 * @param $new_name
 *   The new name for the table.
691 692 693 694 695 696 697
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call renameTable() on it. E.g.
 *   $injected_database->schema()->renameTable($table, $new_name);
 *
 * @see \Drupal\Core\Database\Schema::renameTable()
698 699 700 701 702 703 704 705 706 707
 */
function db_rename_table($table, $new_name) {
  return Database::getConnection()->schema()->renameTable($table, $new_name);
}

/**
 * Drops a table.
 *
 * @param $table
 *   The table to be dropped.
708 709 710 711 712 713 714
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call dropTable() on it. E.g.
 *   $injected_database->schema()->dropTable($table);
 *
 * @see \Drupal\Core\Database\Schema::dropTable()
715 716 717 718 719 720 721 722 723 724 725 726
 */
function db_drop_table($table) {
  return Database::getConnection()->schema()->dropTable($table);
}

/**
 * Adds a new field to a table.
 *
 * @param $table
 *   Name of the table to be altered.
 * @param $field
 *   Name of the field to be added.
727
 * @param array $spec
728 729 730 731
 *   The field specification array, as taken from a schema definition. The
 *   specification may also contain the key 'initial'; the newly-created field
 *   will be set to the value of the key in all rows. This is most useful for
 *   creating NOT NULL columns with no default value in existing tables.
732
 * @param array $keys_new
733
 *   (optional) Keys and indexes specification to be created on the table along
734 735 736 737 738
 *   with adding the field. The format is the same as a table specification, but
 *   without the 'fields' element. If you are adding a type 'serial' field, you
 *   MUST specify at least one key or index including it in this array. See
 *   db_change_field() for more explanation why.
 *
739 740 741 742 743 744
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call addField() on it. E.g.
 *   $injected_database->schema()->addField($table, $field, $spec, $keys_new);
 *
 * @see \Drupal\Core\Database\Schema::addField()
745 746 747 748 749 750 751 752 753 754 755 756 757
 * @see db_change_field()
 */
function db_add_field($table, $field, $spec, $keys_new = array()) {
  return Database::getConnection()->schema()->addField($table, $field, $spec, $keys_new);
}

/**
 * Drops a field.
 *
 * @param $table
 *   The table to be altered.
 * @param $field
 *   The field to be dropped.
758 759 760 761 762 763 764 765 766 767 768
 *
 * @return bool
 *   TRUE if the field was successfully dropped, FALSE if there was no field by
 *   that name to begin with.
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call dropField() on it. E.g.
 *   $injected_database->schema()->dropField($table, $field);
 *
 * @see \Drupal\Core\Database\Schema::dropField()
769 770 771 772 773 774 775 776 777 778 779 780 781 782
 */
function db_drop_field($table, $field) {
  return Database::getConnection()->schema()->dropField($table, $field);
}

/**
 * Sets the default value for a field.
 *
 * @param $table
 *   The table to be altered.
 * @param $field
 *   The field to be altered.
 * @param $default
 *   Default value to be set. NULL for 'default NULL'.
783 784 785 786 787 788 789
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call fieldSetDefault() on it. E.g.
 *   $injected_database->schema()->fieldSetDefault($table, $field, $default);
 *
 * @see \Drupal\Core\Database\Schema::fieldSetDefault()
790 791 792 793 794 795 796 797 798 799 800 801
 */
function db_field_set_default($table, $field, $default) {
  return Database::getConnection()->schema()->fieldSetDefault($table, $field, $default);
}

/**
 * Sets a field to have no default value.
 *
 * @param $table
 *   The table to be altered.
 * @param $field
 *   The field to be altered.
802 803 804 805 806 807 808
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call fieldSetNoDefault() on it. E.g.
 *   $injected_database->schema()->fieldSetNoDefault($table, $field);
 *
 * @see \Drupal\Core\Database\Schema::fieldSetNoDefault()
809 810 811 812 813 814 815 816 817 818 819 820
 */
function db_field_set_no_default($table, $field) {
  return Database::getConnection()->schema()->fieldSetNoDefault($table, $field);
}

/**
 * Adds a primary key to a database table.
 *
 * @param $table
 *   Name of the table to be altered.
 * @param $fields
 *   Array of fields for the primary key.
821 822 823 824 825 826 827
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call addPrimaryKey() on it. E.g.
 *   $injected_database->schema()->addPrimaryKey($table, $fields);
 *
 * @see \Drupal\Core\Database\Schema::addPrimaryKey()
828 829 830 831 832 833 834 835 836 837
 */
function db_add_primary_key($table, $fields) {
  return Database::getConnection()->schema()->addPrimaryKey($table, $fields);
}

/**
 * Drops the primary key of a database table.
 *
 * @param $table
 *   Name of the table to be altered.
838 839 840 841 842 843 844 845 846 847 848
 *
 * @return bool
 *   TRUE if the primary key was successfully dropped, FALSE if there was no
 *   primary key on this table to begin with.
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call dropPrimaryKey() on it. E.g.
 *   $injected_database->schema()->dropPrimaryKey($table);
 *
 * @see \Drupal\Core\Database\Schema::dropPrimaryKey()
849 850 851 852 853 854 855 856 857 858 859 860
 */
function db_drop_primary_key($table) {
  return Database::getConnection()->schema()->dropPrimaryKey($table);
}

/**
 * Adds a unique key.
 *
 * @param $table
 *   The table to be altered.
 * @param $name
 *   The name of the key.
861
 * @param array $fields
862
 *   An array of field names.
863 864 865 866 867 868 869
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call addUniqueKey() on it. E.g.
 *   $injected_database->schema()->addUniqueKey($table, $name, $fields);
 *
 * @see \Drupal\Core\Database\Schema::addUniqueKey()
870 871 872 873 874 875 876 877 878 879 880 881
 */
function db_add_unique_key($table, $name, $fields) {
  return Database::getConnection()->schema()->addUniqueKey($table, $name, $fields);
}

/**
 * Drops a unique key.
 *
 * @param $table
 *   The table to be altered.
 * @param $name
 *   The name of the key.
882 883 884 885 886 887 888 889 890 891 892
 *
 * @return bool
 *   TRUE if the key was successfully dropped, FALSE if there was no key by
 *   that name to begin with.
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call dropUniqueKey() on it. E.g.
 *   $injected_database->schema()->dropUniqueKey($table, $name);
 *
 * @see \Drupal\Core\Database\Schema::dropUniqueKey()
893 894 895 896 897 898 899 900 901 902 903 904
 */
function db_drop_unique_key($table, $name) {
  return Database::getConnection()->schema()->dropUniqueKey($table, $name);
}

/**
 * Adds an index.
 *
 * @param $table
 *   The table to be altered.
 * @param $name
 *   The name of the index.
905
 * @param array $fields
906
 *   An array of field names.
907 908 909 910
 * @param array $spec
 *   The table specification of the table to be altered, as taken from a schema
 *   definition. See \Drupal\Core\Database\Schema::addIndex() for how to obtain
 *   this specification.
911 912 913 914
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call addIndex() on it. E.g.
915
 *   $injected_database->schema()->addIndex($table, $name, $fields, $spec);
916
 *
917 918
 * @see hook_schema()
 * @see schemaapi
919
 * @see \Drupal\Core\Database\Schema::addIndex()
920
 */
921 922
function db_add_index($table, $name, $fields, array $spec) {
  return Database::getConnection()->schema()->addIndex($table, $name, $fields, $spec);
923 924 925 926 927 928 929 930 931
}

/**
 * Drops an index.
 *
 * @param $table
 *   The table to be altered.
 * @param $name
 *   The name of the index.
932 933 934 935 936 937 938 939 940 941 942
 *
 * @return bool
 *   TRUE if the index was successfully dropped, FALSE if there was no index
 *   by that name to begin with.
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call dropIndex() on it. E.g.
 *   $injected_database->schema()->dropIndex($table, $name);
 *
 * @see \Drupal\Core\Database\Schema::dropIndex()
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
 */
function db_drop_index($table, $name) {
  return Database::getConnection()->schema()->dropIndex($table, $name);
}

/**
 * Changes a field definition.
 *
 * IMPORTANT NOTE: To maintain database portability, you have to explicitly
 * recreate all indices and primary keys that are using the changed field.
 *
 * That means that you have to drop all affected keys and indexes with
 * db_drop_{primary_key,unique_key,index}() before calling db_change_field().
 * To recreate the keys and indices, pass the key definitions as the optional
 * $keys_new argument directly to db_change_field().
 *
 * For example, suppose you have:
 * @code
 * $schema['foo'] = array(
 *   'fields' => array(
 *     'bar' => array('type' => 'int', 'not null' => TRUE)
 *   ),
 *   'primary key' => array('bar')
 * );
 * @endcode
 * and you want to change foo.bar to be type serial, leaving it as the primary
 * key. The correct sequence is:
 * @code
 * db_drop_primary_key('foo');
 * db_change_field('foo', 'bar', 'bar',
 *   array('type' => 'serial', 'not null' => TRUE),
 *   array('primary key' => array('bar')));
 * @endcode
 *
 * The reasons for this are due to the different database engines:
 *
 * On PostgreSQL, changing a field definition involves adding a new field and
 * dropping an old one which causes any indices, primary keys and sequences
 * (from serial-type fields) that use the changed field to be dropped.
 *
 * On MySQL, all type 'serial' fields must be part of at least one key or index
 * as soon as they are created. You cannot use
 * db_add_{primary_key,unique_key,index}() for this purpose because the ALTER
 * TABLE command will fail to add the column without a key or index
 * specification. The solution is to use the optional $keys_new argument to
 * create the key or index at the same time as field.
 *
 * You could use db_add_{primary_key,unique_key,index}() in all cases unless you
 * are converting a field to be type serial. You can use the $keys_new argument
 * in all cases.
 *
 * @param $table
 *   Name of the table.
 * @param $field
 *   Name of the field to change.
 * @param $field_new
 *   New name for the field (set to the same as $field if you don't want to
 *   change the name).
 * @param $spec
 *   The field specification for the new field.
1003
 * @param array $keys_new
1004
 *   (optional) Keys and indexes specification to be created on the table along
1005 1006
 *   with changing the field. The format is the same as a table specification
 *   but without the 'fields' element.
1007 1008 1009 1010 1011 1012 1013
 *
 * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
 *   a database connection injected into your service from the container, get
 *   its schema driver, and call changeField() on it. E.g.
 *   $injected_database->schema()->changeField($table, $field, $field_new, $spec, $keys_new);
 *
 * @see \Drupal\Core\Database\Schema::changeField()
1014 1015 1016 1017 1018 1019
 */
function db_change_field($table, $field, $field_new, $spec, $keys_new = array()) {
  return Database::getConnection()->schema()->changeField($table, $field, $field_new, $spec, $keys_new);
}

/**
1020
 * @} End of "addtogroup schemaapi".
1021 1022 1023
 */

/**
1024 1025 1026
 * Sets a session variable specifying the lag time for ignoring a replica
 * server (A replica server is traditionally referred to as
 * a "slave" in database server documentation).
1027
 * @see https://www.drupal.org/node/2275877
1028
 */
1029
function db_ignore_replica() {
1030
  $connection_info = Database::getConnectionInfo();
1031 1032
  // Only set ignore_replica_server if there are replica servers being used,
  // which is assumed if there are more than one.
1033
  if (count($connection_info) > 1) {
1034
    // Five minutes is long enough to allow the replica to break and resume
1035 1036
    // interrupted replication without causing problems on the Drupal site from
    // the old data.
1037
    $duration = Settings::get('maximum_replication_lag', 300);
1038 1039
    // Set session variable with amount of time to delay before using replica.
    $_SESSION['ignore_replica_server'] = REQUEST_TIME + $duration;
1040 1041
  }
}