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