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