database.inc 52 KB

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