database.inc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. <?php
  2. /**
  3. * @file
  4. * Core systems for the database layer.
  5. *
  6. * Classes required for basic functioning of the database system should be
  7. * placed in this file. All utility functions should also be placed in this
  8. * file only, as they cannot auto-load the way classes can.
  9. */
  10. use Drupal\Core\Database\Database;
  11. use Drupal\Core\Database\Query\Condition;
  12. use Drupal\Core\Site\Settings;
  13. /**
  14. * @addtogroup database
  15. * @{
  16. */
  17. /**
  18. * Executes an arbitrary query string against the active database.
  19. *
  20. * Use this function for SELECT queries if it is just a simple query string.
  21. * If the caller or other modules need to change the query, use db_select()
  22. * instead.
  23. *
  24. * Do not use this function for INSERT, UPDATE, or DELETE queries. Those should
  25. * be handled via db_insert(), db_update() and db_delete() respectively.
  26. *
  27. * @param string|\Drupal\Core\Database\StatementInterface $query
  28. * The prepared statement query to run. Although it will accept both named and
  29. * unnamed placeholders, named placeholders are strongly preferred as they are
  30. * more self-documenting. If the argument corresponding to a placeholder is
  31. * an array of values to be expanded (for example, with an IN query), the
  32. * placeholder should be named with a trailing bracket like :example[].
  33. * @param array $args
  34. * An array of values to substitute into the query. If the query uses named
  35. * placeholders, this is an associative array in any order. If the query uses
  36. * unnamed placeholders (?), this is an indexed array and the order must match
  37. * the order of placeholders in the query string.
  38. * @param array $options
  39. * An array of options to control how the query operates.
  40. *
  41. * @return \Drupal\Core\Database\StatementInterface
  42. * A prepared statement object, already executed.
  43. *
  44. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  45. * a database connection injected into your service from the container and
  46. * call query() on it. For example,
  47. * $injected_database->query($query, $args, $options);
  48. *
  49. * @see \Drupal\Core\Database\Connection::query()
  50. * @see \Drupal\Core\Database\Connection::defaultOptions()
  51. */
  52. function db_query($query, array $args = [], array $options = []) {
  53. if (empty($options['target'])) {
  54. $options['target'] = 'default';
  55. }
  56. return Database::getConnection($options['target'])->query($query, $args, $options);
  57. }
  58. /**
  59. * Executes a query against the active database, restricted to a range.
  60. *
  61. * @param string $query
  62. * The prepared statement query to run. Although it will accept both named and
  63. * unnamed placeholders, named placeholders are strongly preferred as they are
  64. * more self-documenting.
  65. * @param $from
  66. * The first record from the result set to return.
  67. * @param $count
  68. * The number of records to return from the result set.
  69. * @param array $args
  70. * An array of values to substitute into the query. If the query uses named
  71. * placeholders, this is an associative array in any order. If the query uses
  72. * unnamed placeholders (?), this is an indexed array and the order must match
  73. * the order of placeholders in the query string.
  74. * @param array $options
  75. * An array of options to control how the query operates.
  76. *
  77. * @return \Drupal\Core\Database\StatementInterface
  78. * A prepared statement object, already executed.
  79. *
  80. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  81. * a database connection injected into your service from the container and
  82. * call queryRange() on it. For example,
  83. * $injected_database->queryRange($query, $from, $count, $args, $options);
  84. *
  85. * @see \Drupal\Core\Database\Connection::queryRange()
  86. * @see \Drupal\Core\Database\Connection::defaultOptions()
  87. */
  88. function db_query_range($query, $from, $count, array $args = [], array $options = []) {
  89. if (empty($options['target'])) {
  90. $options['target'] = 'default';
  91. }
  92. return Database::getConnection($options['target'])->queryRange($query, $from, $count, $args, $options);
  93. }
  94. /**
  95. * Executes a SELECT query string and saves the result set to a temporary table.
  96. *
  97. * The execution of the query string happens against the active database.
  98. *
  99. * @param string $query
  100. * The prepared SELECT statement query to run. Although it will accept both
  101. * named and unnamed placeholders, named placeholders are strongly preferred
  102. * as they are more self-documenting.
  103. * @param array $args
  104. * An array of values to substitute into the query. If the query uses named
  105. * placeholders, this is an associative array in any order. If the query uses
  106. * unnamed placeholders (?), this is an indexed array and the order must match
  107. * the order of placeholders in the query string.
  108. * @param array $options
  109. * An array of options to control how the query operates.
  110. *
  111. * @return
  112. * The name of the temporary table.
  113. *
  114. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  115. * a database connection injected into your service from the container and
  116. * call queryTemporary() on it. For example,
  117. * $injected_database->queryTemporary($query, $args, $options);
  118. *
  119. * @see \Drupal\Core\Database\Connection::queryTemporary()
  120. * @see \Drupal\Core\Database\Connection::defaultOptions()
  121. */
  122. function db_query_temporary($query, array $args = [], array $options = []) {
  123. if (empty($options['target'])) {
  124. $options['target'] = 'default';
  125. }
  126. return Database::getConnection($options['target'])->queryTemporary($query, $args, $options);
  127. }
  128. /**
  129. * Returns a new InsertQuery object for the active database.
  130. *
  131. * @param string $table
  132. * The table into which to insert.
  133. * @param array $options
  134. * An array of options to control how the query operates.
  135. *
  136. * @return \Drupal\Core\Database\Query\Insert
  137. * A new Insert object for this connection.
  138. *
  139. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  140. * a database connection injected into your service from the container and
  141. * call insert() on it. For example,
  142. * $injected_database->insert($table, $options);
  143. *
  144. * @see \Drupal\Core\Database\Connection::insert()
  145. * @see \Drupal\Core\Database\Connection::defaultOptions()
  146. */
  147. function db_insert($table, array $options = []) {
  148. if (empty($options['target']) || $options['target'] == 'replica') {
  149. $options['target'] = 'default';
  150. }
  151. return Database::getConnection($options['target'])->insert($table, $options);
  152. }
  153. /**
  154. * Returns a new MergeQuery object for the active database.
  155. *
  156. * @param string $table
  157. * Name of the table to associate with this query.
  158. * @param array $options
  159. * An array of options to control how the query operates.
  160. *
  161. * @return \Drupal\Core\Database\Query\Merge
  162. * A new Merge object for this connection.
  163. *
  164. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  165. * a database connection injected into your service from the container and
  166. * call merge() on it. For example,
  167. * $injected_database->merge($table, $options);
  168. *
  169. * @see \Drupal\Core\Database\Connection::merge()
  170. * @see \Drupal\Core\Database\Connection::defaultOptions()
  171. */
  172. function db_merge($table, array $options = []) {
  173. if (empty($options['target']) || $options['target'] == 'replica') {
  174. $options['target'] = 'default';
  175. }
  176. return Database::getConnection($options['target'])->merge($table, $options);
  177. }
  178. /**
  179. * Returns a new UpdateQuery object for the active database.
  180. *
  181. * @param string $table
  182. * The table to update.
  183. * @param array $options
  184. * An array of options to control how the query operates.
  185. *
  186. * @return \Drupal\Core\Database\Query\Update
  187. * A new Update object for this connection.
  188. *
  189. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  190. * a database connection injected into your service from the container and
  191. * call update() on it. For example,
  192. * $injected_database->update($table, $options);
  193. *
  194. * @see \Drupal\Core\Database\Connection::update()
  195. * @see \Drupal\Core\Database\Connection::defaultOptions()
  196. */
  197. function db_update($table, array $options = []) {
  198. if (empty($options['target']) || $options['target'] == 'replica') {
  199. $options['target'] = 'default';
  200. }
  201. return Database::getConnection($options['target'])->update($table, $options);
  202. }
  203. /**
  204. * Returns a new DeleteQuery object for the active database.
  205. *
  206. * @param string $table
  207. * The table from which to delete.
  208. * @param array $options
  209. * An array of options to control how the query operates.
  210. *
  211. * @return \Drupal\Core\Database\Query\Delete
  212. * A new Delete object for this connection.
  213. *
  214. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  215. * a database connection injected into your service from the container and
  216. * call delete() on it. For example,
  217. * $injected_database->delete($table, $options);
  218. *
  219. * @see \Drupal\Core\Database\Connection::delete()
  220. * @see \Drupal\Core\Database\Connection::defaultOptions()
  221. */
  222. function db_delete($table, array $options = []) {
  223. if (empty($options['target']) || $options['target'] == 'replica') {
  224. $options['target'] = 'default';
  225. }
  226. return Database::getConnection($options['target'])->delete($table, $options);
  227. }
  228. /**
  229. * Returns a new TruncateQuery object for the active database.
  230. *
  231. * @param string $table
  232. * The table from which to truncate.
  233. * @param array $options
  234. * An array of options to control how the query operates.
  235. *
  236. * @return \Drupal\Core\Database\Query\Truncate
  237. * A new Truncate object for this connection.
  238. *
  239. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  240. * a database connection injected into your service from the container and
  241. * call truncate() on it. For example,
  242. * $injected_database->truncate($table, $options);
  243. *
  244. * @see \Drupal\Core\Database\Connection::truncate()
  245. * @see \Drupal\Core\Database\Connection::defaultOptions()
  246. */
  247. function db_truncate($table, array $options = []) {
  248. if (empty($options['target']) || $options['target'] == 'replica') {
  249. $options['target'] = 'default';
  250. }
  251. return Database::getConnection($options['target'])->truncate($table, $options);
  252. }
  253. /**
  254. * Returns a new SelectQuery object for the active database.
  255. *
  256. * @param string|\Drupal\Core\Database\Query\SelectInterface $table
  257. * The base table for this query. May be a string or another SelectInterface
  258. * object. If a SelectInterface object is passed, it will be used as a
  259. * subselect.
  260. * @param string $alias
  261. * (optional) The alias for the base table of this query.
  262. * @param array $options
  263. * (optional) An array of options to control how the query operates.
  264. *
  265. * @return \Drupal\Core\Database\Query\Select
  266. * A new Select object for this connection.
  267. *
  268. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  269. * a database connection injected into your service from the container and
  270. * call select() on it. For example,
  271. * $injected_database->select($table, $alias, $options);
  272. *
  273. * @see \Drupal\Core\Database\Connection::select()
  274. * @see \Drupal\Core\Database\Connection::defaultOptions()
  275. */
  276. function db_select($table, $alias = NULL, array $options = []) {
  277. if (empty($options['target'])) {
  278. $options['target'] = 'default';
  279. }
  280. return Database::getConnection($options['target'])->select($table, $alias, $options);
  281. }
  282. /**
  283. * Returns a new transaction object for the active database.
  284. *
  285. * @param string $name
  286. * Optional name of the transaction.
  287. * @param array $options
  288. * An array of options to control how the transaction operates:
  289. * - target: The database target name.
  290. *
  291. * @return \Drupal\Core\Database\Transaction
  292. * A new Transaction object for this connection.
  293. *
  294. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  295. * a database connection injected into your service from the container and
  296. * call startTransaction() on it. For example,
  297. * $injected_database->startTransaction($name);
  298. *
  299. * @see \Drupal\Core\Database\Connection::startTransaction()
  300. * @see \Drupal\Core\Database\Connection::defaultOptions()
  301. */
  302. function db_transaction($name = NULL, array $options = []) {
  303. if (empty($options['target'])) {
  304. $options['target'] = 'default';
  305. }
  306. return Database::getConnection($options['target'])->startTransaction($name);
  307. }
  308. /**
  309. * Sets a new active database.
  310. *
  311. * @param $key
  312. * The key in the $databases array to set as the default database.
  313. *
  314. * @return string|null
  315. * The key of the formerly active database.
  316. *
  317. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Use
  318. * \Drupal\Core\Database\Database::setActiveConnection().
  319. */
  320. function db_set_active($key = 'default') {
  321. @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);
  322. return Database::setActiveConnection($key);
  323. }
  324. /**
  325. * Restricts a dynamic table name to safe characters.
  326. *
  327. * Only keeps alphanumeric and underscores.
  328. *
  329. * @param $table
  330. * The table name to escape.
  331. *
  332. * @return string
  333. * The escaped table name as a string.
  334. *
  335. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  336. * a database connection injected into your service from the container and
  337. * call escapeTable() on it. For example,
  338. * $injected_database->escapeTable($table);
  339. *
  340. * @see \Drupal\Core\Database\Connection::escapeTable()
  341. */
  342. function db_escape_table($table) {
  343. return Database::getConnection()->escapeTable($table);
  344. }
  345. /**
  346. * Restricts a dynamic column or constraint name to safe characters.
  347. *
  348. * Only keeps alphanumeric and underscores.
  349. *
  350. * @param string $field
  351. * The field name to escape.
  352. *
  353. * @return string
  354. * The escaped field name as a string.
  355. *
  356. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  357. * a database connection injected into your service from the container and
  358. * call escapeTable() on it. For example,
  359. * $injected_database->escapeTable($table);
  360. *
  361. * @see \Drupal\Core\Database\Connection::escapeField()
  362. */
  363. function db_escape_field($field) {
  364. return Database::getConnection()->escapeField($field);
  365. }
  366. /**
  367. * Escapes characters that work as wildcard characters in a LIKE pattern.
  368. *
  369. * The wildcard characters "%" and "_" as well as backslash are prefixed with
  370. * a backslash. Use this to do a search for a verbatim string without any
  371. * wildcard behavior.
  372. *
  373. * You must use a query builder like db_select() in order to use db_like() on
  374. * all supported database systems. Using db_like() with db_query() or
  375. * db_query_range() is not supported.
  376. *
  377. * For example, the following does a case-insensitive query for all rows whose
  378. * name starts with $prefix:
  379. * @code
  380. * $result = db_select('person', 'p')
  381. * ->fields('p')
  382. * ->condition('name', db_like($prefix) . '%', 'LIKE')
  383. * ->execute()
  384. * ->fetchAll();
  385. * @endcode
  386. *
  387. * Backslash is defined as escape character for LIKE patterns in
  388. * DatabaseCondition::mapConditionOperator().
  389. *
  390. * @param string $string
  391. * The string to escape.
  392. *
  393. * @return string
  394. * The escaped string.
  395. *
  396. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  397. * a database connection injected into your service from the container and
  398. * call escapeLike() on it. For example,
  399. * $injected_database->escapeLike($string);
  400. *
  401. * @see \Drupal\Core\Database\Connection::escapeLike()
  402. */
  403. function db_like($string) {
  404. return Database::getConnection()->escapeLike($string);
  405. }
  406. /**
  407. * Retrieves the name of the currently active database driver.
  408. *
  409. * @return string
  410. * The name of the currently active database driver.
  411. *
  412. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  413. * a database connection injected into your service from the container and
  414. * call driver() on it. For example, $injected_database->driver($string);
  415. *
  416. * @see \Drupal\Core\Database\Connection::driver()
  417. */
  418. function db_driver() {
  419. return Database::getConnection()->driver();
  420. }
  421. /**
  422. * Closes the active database connection.
  423. *
  424. * @param array $options
  425. * An array of options to control which connection is closed. Only the target
  426. * key has any meaning in this case.
  427. *
  428. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Use
  429. * \Drupal\Core\Database\Database::closeConnection($target).
  430. *
  431. * @see \Drupal\Core\Database\Database::closeConnection()
  432. */
  433. function db_close(array $options = []) {
  434. if (empty($options['target'])) {
  435. $options['target'] = NULL;
  436. }
  437. Database::closeConnection($options['target']);
  438. }
  439. /**
  440. * Retrieves a unique id.
  441. *
  442. * Use this function if for some reason you can't use a serial field. Using a
  443. * serial field is preferred, and InsertQuery::execute() returns the value of
  444. * the last ID inserted.
  445. *
  446. * @param int $existing_id
  447. * After a database import, it might be that the sequences table is behind, so
  448. * by passing in a minimum ID, it can be assured that we never issue the same
  449. * ID.
  450. *
  451. * @return int
  452. * An integer number larger than any number returned before for this sequence.
  453. *
  454. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  455. * a database connection injected into your service from the container and
  456. * call nextId() on it. For example, $injected_database->nextId($existing_id);
  457. *
  458. * @see \Drupal\Core\Database\Connection::nextId()
  459. */
  460. function db_next_id($existing_id = 0) {
  461. return Database::getConnection()->nextId($existing_id);
  462. }
  463. /**
  464. * Returns a new DatabaseCondition, set to "OR" all conditions together.
  465. *
  466. * @return \Drupal\Core\Database\Query\Condition
  467. * A new Condition object, set to "OR" all conditions together.
  468. *
  469. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
  470. * a \Drupal\Core\Database\Query\Condition object, specifying an OR
  471. * conjunction: new Condition('OR');
  472. *
  473. * @see \Drupal\Core\Database\Query\Condition
  474. */
  475. function db_or() {
  476. return new Condition('OR');
  477. }
  478. /**
  479. * Returns a new DatabaseCondition, set to "AND" all conditions together.
  480. *
  481. * @return \Drupal\Core\Database\Query\Condition
  482. * A new Condition object, set to "AND" all conditions together.
  483. *
  484. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
  485. * a \Drupal\Core\Database\Query\Condition object, specifying an AND
  486. * conjunction: new Condition('AND');
  487. *
  488. * @see \Drupal\Core\Database\Query\Condition
  489. */
  490. function db_and() {
  491. return new Condition('AND');
  492. }
  493. /**
  494. * Returns a new DatabaseCondition, set to "XOR" all conditions together.
  495. *
  496. * @return \Drupal\Core\Database\Query\Condition
  497. * A new Condition object, set to "XOR" all conditions together.
  498. *
  499. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
  500. * a \Drupal\Core\Database\Query\Condition object, specifying an XOR
  501. * conjunction: new Condition('XOR');
  502. *
  503. * @see \Drupal\Core\Database\Query\Condition
  504. */
  505. function db_xor() {
  506. return new Condition('XOR');
  507. }
  508. /**
  509. * Returns a new DatabaseCondition, set to the specified conjunction.
  510. *
  511. * Internal API function call. The db_and(), db_or(), and db_xor()
  512. * functions are preferred.
  513. *
  514. * @param string $conjunction
  515. * The conjunction to use for query conditions (AND, OR or XOR).
  516. *
  517. * @return \Drupal\Core\Database\Query\Condition
  518. * A new Condition object, set to the specified conjunction.
  519. *
  520. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Create
  521. * a \Drupal\Core\Database\Query\Condition object, specifying the desired
  522. * conjunction: new Condition($conjunctin);
  523. *
  524. * @see \Drupal\Core\Database\Query\Condition
  525. */
  526. function db_condition($conjunction) {
  527. return new Condition($conjunction);
  528. }
  529. /**
  530. * @} End of "addtogroup database".
  531. */
  532. /**
  533. * @addtogroup schemaapi
  534. * @{
  535. */
  536. /**
  537. * Creates a new table from a Drupal table definition.
  538. *
  539. * @param string $name
  540. * The name of the table to create.
  541. * @param array $table
  542. * A Schema API table definition array.
  543. *
  544. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  545. * a database connection injected into your service from the container, get
  546. * its schema driver, and call createTable() on it. For example,
  547. * $injected_database->schema()->createTable($name, $table);
  548. *
  549. * @see \Drupal\Core\Database\Schema::createTable()
  550. */
  551. function db_create_table($name, $table) {
  552. return Database::getConnection()->schema()->createTable($name, $table);
  553. }
  554. /**
  555. * Returns an array of field names from an array of key/index column specifiers.
  556. *
  557. * This is usually an identity function but if a key/index uses a column prefix
  558. * specification, this function extracts just the name.
  559. *
  560. * @param array $fields
  561. * An array of key/index column specifiers.
  562. *
  563. * @return array
  564. * An array of field names.
  565. *
  566. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  567. * a database connection injected into your service from the container, get
  568. * its schema driver, and call fieldNames() on it. For example,
  569. * $injected_database->schema()->fieldNames($fields);
  570. *
  571. * @see \Drupal\Core\Database\Schema::fieldNames()
  572. */
  573. function db_field_names($fields) {
  574. return Database::getConnection()->schema()->fieldNames($fields);
  575. }
  576. /**
  577. * Checks if an index exists in the given table.
  578. *
  579. * @param string $table
  580. * The name of the table in drupal (no prefixing).
  581. * @param string $name
  582. * The name of the index in drupal (no prefixing).
  583. *
  584. * @return bool
  585. * TRUE if the given index exists, otherwise FALSE.
  586. *
  587. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  588. * a database connection injected into your service from the container, get
  589. * its schema driver, and call indexExists() on it. For example,
  590. * $injected_database->schema()->indexExists($table, $name);
  591. *
  592. * @see \Drupal\Core\Database\Schema::indexExists()
  593. */
  594. function db_index_exists($table, $name) {
  595. return Database::getConnection()->schema()->indexExists($table, $name);
  596. }
  597. /**
  598. * Checks if a table exists.
  599. *
  600. * @param string $table
  601. * The name of the table in drupal (no prefixing).
  602. *
  603. * @return bool
  604. * TRUE if the given table exists, otherwise FALSE.
  605. *
  606. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  607. * a database connection injected into your service from the container, get
  608. * its schema driver, and call tableExists() on it. For example,
  609. * $injected_database->schema()->tableExists($table);
  610. *
  611. * @see \Drupal\Core\Database\Schema::tableExists()
  612. */
  613. function db_table_exists($table) {
  614. @trigger_error(
  615. '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.',
  616. E_USER_DEPRECATED
  617. );
  618. return Database::getConnection()->schema()->tableExists($table);
  619. }
  620. /**
  621. * Checks if a column exists in the given table.
  622. *
  623. * @param $table
  624. * The name of the table in drupal (no prefixing).
  625. * @param $field
  626. * The name of the field.
  627. *
  628. * @return bool
  629. * TRUE if the given column exists, otherwise FALSE.
  630. *
  631. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  632. * a database connection injected into your service from the container, get
  633. * its schema driver, and call fieldExists() on it. For example,
  634. * $injected_database->schema()->fieldExists($table, $field);
  635. *
  636. * @see \Drupal\Core\Database\Schema::fieldExists()
  637. */
  638. function db_field_exists($table, $field) {
  639. return Database::getConnection()->schema()->fieldExists($table, $field);
  640. }
  641. /**
  642. * Finds all tables that are like the specified base table name.
  643. *
  644. * @param string $table_expression
  645. * An SQL expression, for example "simpletest%" (without the quotes).
  646. *
  647. * @return array
  648. * Array, both the keys and the values are the matching tables.
  649. *
  650. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  651. * a database connection injected into your service from the container, get
  652. * its schema driver, and call findTables() on it. For example,
  653. * $injected_database->schema()->findTables($table_expression);
  654. *
  655. * @see \Drupal\Core\Database\Schema::findTables()
  656. */
  657. function db_find_tables($table_expression) {
  658. return Database::getConnection()->schema()->findTables($table_expression);
  659. }
  660. /**
  661. * Renames a table.
  662. *
  663. * @param $table
  664. * The current name of the table to be renamed.
  665. * @param $new_name
  666. * The new name for the table.
  667. *
  668. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  669. * a database connection injected into your service from the container, get
  670. * its schema driver, and call renameTable() on it. For example,
  671. * $injected_database->schema()->renameTable($table, $new_name);
  672. *
  673. * @see \Drupal\Core\Database\Schema::renameTable()
  674. */
  675. function db_rename_table($table, $new_name) {
  676. return Database::getConnection()->schema()->renameTable($table, $new_name);
  677. }
  678. /**
  679. * Drops a table.
  680. *
  681. * @param $table
  682. * The table to be dropped.
  683. *
  684. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  685. * a database connection injected into your service from the container, get
  686. * its schema driver, and call dropTable() on it. For example,
  687. * $injected_database->schema()->dropTable($table);
  688. *
  689. * @see \Drupal\Core\Database\Schema::dropTable()
  690. */
  691. function db_drop_table($table) {
  692. @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);
  693. return Database::getConnection()->schema()->dropTable($table);
  694. }
  695. /**
  696. * Adds a new field to a table.
  697. *
  698. * @param $table
  699. * Name of the table to be altered.
  700. * @param $field
  701. * Name of the field to be added.
  702. * @param array $spec
  703. * The field specification array, as taken from a schema definition. The
  704. * specification may also contain the key 'initial'; the newly-created field
  705. * will be set to the value of the key in all rows. This is most useful for
  706. * creating NOT NULL columns with no default value in existing tables.
  707. * @param array $keys_new
  708. * (optional) Keys and indexes specification to be created on the table along
  709. * with adding the field. The format is the same as a table specification, but
  710. * without the 'fields' element. If you are adding a type 'serial' field, you
  711. * MUST specify at least one key or index including it in this array. See
  712. * db_change_field() for more explanation why.
  713. *
  714. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  715. * a database connection injected into your service from the container, get
  716. * its schema driver, and call addField() on it. For example,
  717. * $injected_database->schema()->addField($table, $field, $spec, $keys_new);
  718. *
  719. * @see \Drupal\Core\Database\Schema::addField()
  720. * @see db_change_field()
  721. */
  722. function db_add_field($table, $field, $spec, $keys_new = []) {
  723. return Database::getConnection()->schema()->addField($table, $field, $spec, $keys_new);
  724. }
  725. /**
  726. * Drops a field.
  727. *
  728. * @param $table
  729. * The table to be altered.
  730. * @param $field
  731. * The field to be dropped.
  732. *
  733. * @return bool
  734. * TRUE if the field was successfully dropped, FALSE if there was no field by
  735. * that name to begin with.
  736. *
  737. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  738. * a database connection injected into your service from the container, get
  739. * its schema driver, and call dropField() on it. For example,
  740. * $injected_database->schema()->dropField($table, $field);
  741. *
  742. * @see \Drupal\Core\Database\Schema::dropField()
  743. */
  744. function db_drop_field($table, $field) {
  745. return Database::getConnection()->schema()->dropField($table, $field);
  746. }
  747. /**
  748. * Sets the default value for a field.
  749. *
  750. * @param $table
  751. * The table to be altered.
  752. * @param $field
  753. * The field to be altered.
  754. * @param $default
  755. * Default value to be set. NULL for 'default NULL'.
  756. *
  757. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  758. * a database connection injected into your service from the container, get
  759. * its schema driver, and call fieldSetDefault() on it. For example,
  760. * $injected_database->schema()->fieldSetDefault($table, $field, $default);
  761. *
  762. * @see \Drupal\Core\Database\Schema::fieldSetDefault()
  763. */
  764. function db_field_set_default($table, $field, $default) {
  765. return Database::getConnection()->schema()->fieldSetDefault($table, $field, $default);
  766. }
  767. /**
  768. * Sets a field to have no default value.
  769. *
  770. * @param $table
  771. * The table to be altered.
  772. * @param $field
  773. * The field to be altered.
  774. *
  775. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  776. * a database connection injected into your service from the container, get
  777. * its schema driver, and call fieldSetNoDefault() on it. For example,
  778. * $injected_database->schema()->fieldSetNoDefault($table, $field);
  779. *
  780. * @see \Drupal\Core\Database\Schema::fieldSetNoDefault()
  781. */
  782. function db_field_set_no_default($table, $field) {
  783. return Database::getConnection()->schema()->fieldSetNoDefault($table, $field);
  784. }
  785. /**
  786. * Adds a primary key to a database table.
  787. *
  788. * @param $table
  789. * Name of the table to be altered.
  790. * @param $fields
  791. * Array of fields for the primary key.
  792. *
  793. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  794. * a database connection injected into your service from the container, get
  795. * its schema driver, and call addPrimaryKey() on it. For example,
  796. * $injected_database->schema()->addPrimaryKey($table, $fields);
  797. *
  798. * @see \Drupal\Core\Database\Schema::addPrimaryKey()
  799. */
  800. function db_add_primary_key($table, $fields) {
  801. return Database::getConnection()->schema()->addPrimaryKey($table, $fields);
  802. }
  803. /**
  804. * Drops the primary key of a database table.
  805. *
  806. * @param $table
  807. * Name of the table to be altered.
  808. *
  809. * @return bool
  810. * TRUE if the primary key was successfully dropped, FALSE if there was no
  811. * primary key on this table to begin with.
  812. *
  813. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  814. * a database connection injected into your service from the container, get
  815. * its schema driver, and call dropPrimaryKey() on it. For example,
  816. * $injected_database->schema()->dropPrimaryKey($table);
  817. *
  818. * @see \Drupal\Core\Database\Schema::dropPrimaryKey()
  819. */
  820. function db_drop_primary_key($table) {
  821. return Database::getConnection()->schema()->dropPrimaryKey($table);
  822. }
  823. /**
  824. * Adds a unique key.
  825. *
  826. * @param $table
  827. * The table to be altered.
  828. * @param $name
  829. * The name of the key.
  830. * @param array $fields
  831. * An array of field names.
  832. *
  833. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  834. * a database connection injected into your service from the container, get
  835. * its schema driver, and call addUniqueKey() on it. For example,
  836. * $injected_database->schema()->addUniqueKey($table, $name, $fields);
  837. *
  838. * @see \Drupal\Core\Database\Schema::addUniqueKey()
  839. */
  840. function db_add_unique_key($table, $name, $fields) {
  841. return Database::getConnection()->schema()->addUniqueKey($table, $name, $fields);
  842. }
  843. /**
  844. * Drops a unique key.
  845. *
  846. * @param $table
  847. * The table to be altered.
  848. * @param $name
  849. * The name of the key.
  850. *
  851. * @return bool
  852. * TRUE if the key was successfully dropped, FALSE if there was no key by
  853. * that name to begin with.
  854. *
  855. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  856. * a database connection injected into your service from the container, get
  857. * its schema driver, and call dropUniqueKey() on it. For example,
  858. * $injected_database->schema()->dropUniqueKey($table, $name);
  859. *
  860. * @see \Drupal\Core\Database\Schema::dropUniqueKey()
  861. */
  862. function db_drop_unique_key($table, $name) {
  863. return Database::getConnection()->schema()->dropUniqueKey($table, $name);
  864. }
  865. /**
  866. * Adds an index.
  867. *
  868. * @param $table
  869. * The table to be altered.
  870. * @param $name
  871. * The name of the index.
  872. * @param array $fields
  873. * An array of field names.
  874. * @param array $spec
  875. * The table specification of the table to be altered, as taken from a schema
  876. * definition. See \Drupal\Core\Database\Schema::addIndex() for how to obtain
  877. * this specification.
  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 addIndex() on it. For example,
  882. * $injected_database->schema()->addIndex($table, $name, $fields, $spec);
  883. *
  884. * @see hook_schema()
  885. * @see schemaapi
  886. * @see \Drupal\Core\Database\Schema::addIndex()
  887. */
  888. function db_add_index($table, $name, $fields, array $spec) {
  889. return Database::getConnection()->schema()->addIndex($table, $name, $fields, $spec);
  890. }
  891. /**
  892. * Drops an index.
  893. *
  894. * @param $table
  895. * The table to be altered.
  896. * @param $name
  897. * The name of the index.
  898. *
  899. * @return bool
  900. * TRUE if the index was successfully dropped, FALSE if there was no index
  901. * by that name to begin with.
  902. *
  903. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  904. * a database connection injected into your service from the container, get
  905. * its schema driver, and call dropIndex() on it. For example,
  906. * $injected_database->schema()->dropIndex($table, $name);
  907. *
  908. * @see \Drupal\Core\Database\Schema::dropIndex()
  909. */
  910. function db_drop_index($table, $name) {
  911. return Database::getConnection()->schema()->dropIndex($table, $name);
  912. }
  913. /**
  914. * Changes a field definition.
  915. *
  916. * IMPORTANT NOTE: To maintain database portability, you have to explicitly
  917. * recreate all indices and primary keys that are using the changed field.
  918. *
  919. * That means that you have to drop all affected keys and indexes with
  920. * db_drop_{primary_key,unique_key,index}() before calling db_change_field().
  921. * To recreate the keys and indices, pass the key definitions as the optional
  922. * $keys_new argument directly to db_change_field().
  923. *
  924. * For example, suppose you have:
  925. * @code
  926. * $schema['foo'] = array(
  927. * 'fields' => array(
  928. * 'bar' => array('type' => 'int', 'not null' => TRUE)
  929. * ),
  930. * 'primary key' => array('bar')
  931. * );
  932. * @endcode
  933. * and you want to change foo.bar to be type serial, leaving it as the primary
  934. * key. The correct sequence is:
  935. * @code
  936. * db_drop_primary_key('foo');
  937. * db_change_field('foo', 'bar', 'bar',
  938. * array('type' => 'serial', 'not null' => TRUE),
  939. * array('primary key' => array('bar')));
  940. * @endcode
  941. *
  942. * The reasons for this are due to the different database engines:
  943. *
  944. * On PostgreSQL, changing a field definition involves adding a new field and
  945. * dropping an old one which causes any indices, primary keys and sequences
  946. * (from serial-type fields) that use the changed field to be dropped.
  947. *
  948. * On MySQL, all type 'serial' fields must be part of at least one key or index
  949. * as soon as they are created. You cannot use
  950. * db_add_{primary_key,unique_key,index}() for this purpose because the ALTER
  951. * TABLE command will fail to add the column without a key or index
  952. * specification. The solution is to use the optional $keys_new argument to
  953. * create the key or index at the same time as field.
  954. *
  955. * You could use db_add_{primary_key,unique_key,index}() in all cases unless you
  956. * are converting a field to be type serial. You can use the $keys_new argument
  957. * in all cases.
  958. *
  959. * @param $table
  960. * Name of the table.
  961. * @param $field
  962. * Name of the field to change.
  963. * @param $field_new
  964. * New name for the field (set to the same as $field if you don't want to
  965. * change the name).
  966. * @param $spec
  967. * The field specification for the new field.
  968. * @param array $keys_new
  969. * (optional) Keys and indexes specification to be created on the table along
  970. * with changing the field. The format is the same as a table specification
  971. * but without the 'fields' element.
  972. *
  973. * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
  974. * a database connection injected into your service from the container, get
  975. * its schema driver, and call changeField() on it. For example,
  976. * $injected_database->schema()->changeField($table, $field, $field_new, $spec, $keys_new);
  977. *
  978. * @see \Drupal\Core\Database\Schema::changeField()
  979. */
  980. function db_change_field($table, $field, $field_new, $spec, $keys_new = []) {
  981. return Database::getConnection()->schema()->changeField($table, $field, $field_new, $spec, $keys_new);
  982. }
  983. /**
  984. * @} End of "addtogroup schemaapi".
  985. */
  986. /**
  987. * Sets a session variable specifying the lag time for ignoring a replica
  988. * server (A replica server is traditionally referred to as
  989. * a "slave" in database server documentation).
  990. * @see https://www.drupal.org/node/2275877
  991. */
  992. function db_ignore_replica() {
  993. $connection_info = Database::getConnectionInfo();
  994. // Only set ignore_replica_server if there are replica servers being used,
  995. // which is assumed if there are more than one.
  996. if (count($connection_info) > 1) {
  997. // Five minutes is long enough to allow the replica to break and resume
  998. // interrupted replication without causing problems on the Drupal site from
  999. // the old data.
  1000. $duration = Settings::get('maximum_replication_lag', 300);
  1001. // Set session variable with amount of time to delay before using replica.
  1002. $_SESSION['ignore_replica_server'] = REQUEST_TIME + $duration;
  1003. }
  1004. }