diff -u b/core/includes/database.inc b/core/includes/database.inc --- b/core/includes/database.inc +++ b/core/includes/database.inc @@ -53,9 +53,7 @@ * @see \Drupal\Core\Database\Connection::defaultOptions() */ function db_query($query, array $args = [], array $options = []) { - $target = empty($options['target']) ? 'default' : $options['target']; - unset($options['target']); - return Database::getConnection($target)->query($query, $args, $options); + return Database::getConnection(Database::getTarget($options))->query($query, $args, $options); } /** @@ -90,9 +88,7 @@ */ function db_query_range($query, $from, $count, array $args = [], array $options = []) { @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); - $target = empty($options['target']) ? 'default' : $options['target']; - unset($options['target']); - return Database::getConnection($target)->queryRange($query, $from, $count, $args, $options); + return Database::getConnection(Database::getTarget($options))->queryRange($query, $from, $count, $args, $options); } /** @@ -125,9 +121,7 @@ */ function db_query_temporary($query, array $args = [], array $options = []) { @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); - $target = empty($options['target']) ? 'default' : $options['target']; - unset($options['target']); - return Database::getConnection($target)->queryTemporary($query, $args, $options); + return Database::getConnection(Database::getTarget($options))->queryTemporary($query, $args, $options); } /** @@ -151,9 +145,7 @@ */ function db_insert($table, array $options = []) { @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); - $target = empty($options['target']) || $options['target'] == 'replica' ? 'default' : $options['target']; - unset($options['target']); - return Database::getConnection($target)->insert($table, $options); + return Database::getConnection(Database::getTarget($options, 'default', ['replica']))->insert($table, $options); } /** @@ -177,9 +169,7 @@ */ function db_merge($table, array $options = []) { @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); - $target = empty($options['target']) || $options['target'] == 'replica' ? 'default' : $options['target']; - unset($options['target']); - return Database::getConnection($target)->merge($table, $options); + return Database::getConnection(Database::getTarget($options, 'default', ['replica']))->merge($table, $options); } /** @@ -203,9 +193,7 @@ */ function db_update($table, array $options = []) { @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); - $target = empty($options['target']) || $options['target'] == 'replica' ? 'default' : $options['target']; - unset($options['target']); - return Database::getConnection($target)->update($table, $options); + return Database::getConnection(Database::getTarget($options, 'default', ['replica']))->update($table, $options); } /** @@ -229,9 +217,7 @@ */ function db_delete($table, array $options = []) { @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); - $target = empty($options['target']) || $options['target'] == 'replica' ? 'default' : $options['target']; - unset($options['target']); - return Database::getConnection($target)->delete($table, $options); + return Database::getConnection(Database::getTarget($options, 'default', ['replica']))->delete($table, $options); } /** @@ -255,9 +241,7 @@ */ function db_truncate($table, array $options = []) { @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); - $target = empty($options['target']) || $options['target'] == 'replica' ? 'default' : $options['target']; - unset($options['target']); - return Database::getConnection($target)->truncate($table, $options); + return Database::getConnection(Database::getTarget($options, 'default', ['replica']))->truncate($table, $options); } /** @@ -285,9 +269,7 @@ */ function db_select($table, $alias = NULL, array $options = []) { @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); - $target = empty($options['target']) ? 'default' : $options['target']; - unset($options['target']); - return Database::getConnection($target)->select($table, $alias, $options); + return Database::getConnection(Database::getTarget($options))->select($table, $alias, $options); } /** @@ -312,8 +294,7 @@ */ function db_transaction($name = NULL, array $options = []) { @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); - $target = empty($options['target']) ? 'default' : $options['target']; - return Database::getConnection($target)->startTransaction($name); + return Database::getConnection(Database::getTarget($options))->startTransaction($name); } /** @@ -452,8 +433,7 @@ */ function db_close(array $options = []) { @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); - $target = empty($options['target']) ? NULL : $options['target']; - Database::closeConnection($target); + Database::closeConnection(Database::getTarget($options, NULL)); } /** only in patch2: unchanged: --- a/core/lib/Drupal/Core/Database/Database.php +++ b/core/lib/Drupal/Core/Database/Database.php @@ -511,4 +511,39 @@ protected static function getDatabaseDriverNamespace(array $connection_info) { return 'Drupal\\Core\\Database\\Driver\\' . $connection_info['driver']; } + /** + * Get target helper. + * + * Helps get "target" database from the query options. + * + * @param array $options + * An array of options to control how the query operates. + * @param string|null $default + * The default database target name. + * @param string[] $default_aliases + * Aliases for the target that should be recognized as a default target. + * For cases like target "replica" that should be changed to the "default". + * + * @return mixed|string + * The target database for the database connection. + */ + public static function getTarget(array &$options, $default = 'default', array $default_aliases = []) { + if (empty($options['target'])) { + $target = $default; + } + else { + $target = $options['target']; + if (!empty($default_aliases)) { + foreach ($default_aliases as $alias) { + if ($target === $alias) { + $target = $default; + break; + } + } + } + } + unset($options['target']); + return $target; + } + }