annotate core/includes/database.inc @ 19:fa3358dc1485 tip

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