Commit 250e486b authored by webchick's avatar webchick
Browse files

#363687 by chx: Simplify DBTNG codeflow.

parent cf43ce12
......@@ -1579,7 +1579,7 @@ function _registry_check_code($type, $name = NULL) {
// This function may get called when the default database is not active, but
// there is no reason we'd ever want to not use the default database for
// this query.
$file = Database::getConnection('default')->query("SELECT filename FROM {registry} WHERE name = :name AND type = :type", array(
$file = Database::getConnection('default', 'default')->query("SELECT filename FROM {registry} WHERE name = :name AND type = :type", array(
':name' => $name,
':type' => $type,
))
......
......@@ -1160,38 +1160,22 @@ final public static function getLog($logging_key, $key = 'default') {
return $queries;
}
/**
* Gets the active connection object for the specified target.
*
* @return
* The active connection object.
*/
final public static function getActiveConnection($target = 'default') {
// This could just be a call to getConnection(), but that's an extra
// method call for every single query.
// If the requested target does not exist, or if it is ignored, we fall back
// to the default target. The target is typically either "default" or "slave",
// indicating to use a slave SQL server if one is available. If it's not
// available, then the default/master server is the correct server to use.
if (!empty(self::$ignoreTargets[self::$activeKey][$target]) || !isset(self::$databaseInfo[self::$activeKey][$target])) {
$target = 'default';
}
if (!isset(self::$connections[self::$activeKey][$target])) {
self::openConnection(self::$activeKey, $target);
}
return isset(self::$connections[self::$activeKey][$target]) ? self::$connections[self::$activeKey][$target] : NULL;
}
/**
* Gets the connection object for the specified database key and target.
*
* @param $target
* The database target name.
* @param $key
* The database connection key. Defaults to NULL which means the active
* key.
* @return
* The corresponding connection object.
*/
final public static function getConnection($key = 'default', $target = 'default') {
final public static function getConnection($target = 'default', $key = NULL) {
if (!isset($key)) {
// By default, we want the active connection, set in setActiveConnection.
$key = self::$activeKey;
}
// If the requested target does not exist, or if it is ignored, we fall back
// to the default target. The target is typically either "default" or "slave",
// indicating to use a slave SQL server if one is available. If it's not
......@@ -1201,7 +1185,8 @@ final public static function getConnection($key = 'default', $target = 'default'
}
if (!isset(self::$connections[$key][$target])) {
self::openConnection($key, $target);
// If necessary, a new connection is opened.
self::$connections[$key][$target] = self::openConnection($key, $target);
}
return isset(self::$connections[$key][$target]) ? self::$connections[$key][$target] : NULL;
......@@ -1333,13 +1318,13 @@ final protected static function openConnection($key, $target) {
// an open database connection.
$driver_class = 'DatabaseConnection_' . $driver;
require_once DRUPAL_ROOT . '/includes/database/' . $driver . '/database.inc';
self::$connections[$key][$target] = new $driver_class(self::$databaseInfo[$key][$target]);
self::$connections[$key][$target]->setTarget($target);
$new_connection = new $driver_class(self::$databaseInfo[$key][$target]);
$new_connection->setTarget($target);
// If we have any active logging objects for this connection key, we need
// to associate them with the connection we just opened.
if (!empty(self::$logs[$key])) {
self::$connections[$key][$target]->setLogger(self::$logs[$key]);
$new_connection->setLogger(self::$logs[$key]);
}
// We need to pass around the simpletest database prefix in the request
......@@ -1347,6 +1332,7 @@ final protected static function openConnection($key, $target) {
if (preg_match("/^simpletest\d+$/", $_SERVER['HTTP_USER_AGENT'])) {
$db_prefix .= $_SERVER['HTTP_USER_AGENT'];
}
return $new_connection;
}
catch (Exception $e) {
// It is extremely rare that an exception will be generated here other
......@@ -1761,7 +1747,7 @@ function db_query($query, $args = array(), $options = array()) {
}
list($query, $args, $options) = _db_query_process_args($query, $args, $options);
return Database::getActiveConnection($options['target'])->query($query, $args, $options);
return Database::getConnection($options['target'])->query($query, $args, $options);
}
/**
......@@ -1795,7 +1781,7 @@ function db_query_range($query, $args, $from = 0, $count = 0, $options = array()
}
list($query, $args, $options) = _db_query_process_args($query, $args, $options);
return Database::getActiveConnection($options['target'])->queryRange($query, $args, $from, $count, $options);
return Database::getConnection($options['target'])->queryRange($query, $args, $from, $count, $options);
}
/**
......@@ -1823,7 +1809,7 @@ function db_query_temporary($query, $args, $options = array()) {
}
list($query, $args, $options) = _db_query_process_args($query, $args, $options);
return Database::getActiveConnection($options['target'])->queryTemporary($query, $args, $options);
return Database::getConnection($options['target'])->queryTemporary($query, $args, $options);
}
/**
......@@ -1840,7 +1826,7 @@ function db_insert($table, array $options = array()) {
if (empty($options['target']) || $options['target'] == 'slave') {
$options['target'] = 'default';
}
return Database::getActiveConnection($options['target'])->insert($table, $options);
return Database::getConnection($options['target'])->insert($table, $options);
}
/**
......@@ -1857,7 +1843,7 @@ function db_merge($table, array $options = array()) {
if (empty($options['target']) || $options['target'] == 'slave') {
$options['target'] = 'default';
}
return Database::getActiveConnection($options['target'])->merge($table, $options);
return Database::getConnection($options['target'])->merge($table, $options);
}
/**
......@@ -1874,7 +1860,7 @@ function db_update($table, array $options = array()) {
if (empty($options['target']) || $options['target'] == 'slave') {
$options['target'] = 'default';
}
return Database::getActiveConnection($options['target'])->update($table, $options);
return Database::getConnection($options['target'])->update($table, $options);
}
/**
......@@ -1891,7 +1877,7 @@ function db_delete($table, array $options = array()) {
if (empty($options['target']) || $options['target'] == 'slave') {
$options['target'] = 'default';
}
return Database::getActiveConnection($options['target'])->delete($table, $options);
return Database::getConnection($options['target'])->delete($table, $options);
}
/**
......@@ -1911,7 +1897,7 @@ function db_select($table, $alias = NULL, array $options = array()) {
if (empty($options['target'])) {
$options['target'] = 'default';
}
return Database::getActiveConnection($options['target'])->select($table, $alias, $options);
return Database::getConnection($options['target'])->select($table, $alias, $options);
}
/**
......@@ -1931,7 +1917,7 @@ function db_transaction($required = FALSE, Array $options = array()) {
if (empty($options['target'])) {
$options['target'] = 'default';
}
return Database::getActiveConnection($options['target'])->startTransaction($required);
return Database::getConnection($options['target'])->startTransaction($required);
}
/**
......@@ -1970,7 +1956,7 @@ function db_is_active() {
* The escaped table name as a string.
*/
function db_escape_table($table) {
return Database::getActiveConnection()->escapeTable($table);
return Database::getConnection()->escapeTable($table);
}
/**
......@@ -1985,7 +1971,7 @@ function db_escape_table($table) {
* query: the SQL query executed, passed through check_plain()
*/
function update_sql($sql) {
$result = Database::getActiveConnection()->query($sql);
$result = Database::getConnection()->query($sql);
return array('success' => $result !== FALSE, 'query' => check_plain($sql));
}
......@@ -2023,7 +2009,7 @@ function db_placeholders($arguments, $type = 'int') {
* SQL query with the DISTINCT wrapper surrounding the given table.field.
*/
function db_distinct_field($table, $field, $query) {
return Database::getActiveConnection()->distinctField($table, $field, $query);
return Database::getConnection()->distinctField($table, $field, $query);
}
/**
......@@ -2033,7 +2019,7 @@ function db_distinct_field($table, $field, $query) {
* @return The name of the currently active database driver.
*/
function db_driver() {
return Database::getActiveConnection()->driver();
return Database::getConnection()->driver();
}
/**
......@@ -2058,7 +2044,7 @@ function db_driver() {
* A Schema API table definition array.
*/
function db_create_table(&$ret, $name, $table) {
return Database::getActiveConnection()->schema()->createTable($ret, $name, $table);
return Database::getConnection()->schema()->createTable($ret, $name, $table);
}
/**
......@@ -2073,21 +2059,21 @@ function db_create_table(&$ret, $name, $table) {
* An array of field names.
*/
function db_field_names($fields) {
return Database::getActiveConnection()->schema()->fieldNames($fields);
return Database::getConnection()->schema()->fieldNames($fields);
}
/**
* Check if a table exists.
*/
function db_table_exists($table) {
return Database::getActiveConnection()->schema()->tableExists($table);
return Database::getConnection()->schema()->tableExists($table);
}
/**
* Check if a column exists in the given table.
*/
function db_column_exists($table, $column) {
return Database::getActiveConnection()->schema()->columnExists($table, $column);
return Database::getConnection()->schema()->columnExists($table, $column);
}
/**
......@@ -2100,7 +2086,7 @@ function db_column_exists($table, $column) {
* Array, both the keys and the values are the matching tables.
*/
function db_find_tables($table_expression) {
return Database::getActiveConnection()->schema()->findTables($table_expression);
return Database::getConnection()->schema()->findTables($table_expression);
}
/**
......@@ -2148,7 +2134,7 @@ function db_type_placeholder($type) {
function _db_create_keys_sql($spec) {
return Database::getActiveConnection()->schema()->createKeysSql($spec);
return Database::getConnection()->schema()->createKeysSql($spec);
}
/**
......@@ -2156,7 +2142,7 @@ function _db_create_keys_sql($spec) {
* to the engine-specific data type.
*/
function db_type_map() {
return Database::getActiveConnection()->schema()->getFieldTypeMap();
return Database::getConnection()->schema()->getFieldTypeMap();
}
/**
......@@ -2170,7 +2156,7 @@ function db_type_map() {
* The new name for the table.
*/
function db_rename_table(&$ret, $table, $new_name) {
return Database::getActiveConnection()->schema()->renameTable($ret, $table, $new_name);
return Database::getConnection()->schema()->renameTable($ret, $table, $new_name);
}
/**
......@@ -2182,7 +2168,7 @@ function db_rename_table(&$ret, $table, $new_name) {
* The table to be dropped.
*/
function db_drop_table(&$ret, $table) {
return Database::getActiveConnection()->schema()->dropTable($ret, $table);
return Database::getConnection()->schema()->dropTable($ret, $table);
}
/**
......@@ -2209,7 +2195,7 @@ function db_drop_table(&$ret, $table) {
* explanation why.
*/
function db_add_field(&$ret, $table, $field, $spec, $keys_new = array()) {
return Database::getActiveConnection()->schema()->addField($ret, $table, $field, $spec, $keys_new);
return Database::getConnection()->schema()->addField($ret, $table, $field, $spec, $keys_new);
}
/**
......@@ -2223,7 +2209,7 @@ function db_add_field(&$ret, $table, $field, $spec, $keys_new = array()) {
* The field to be dropped.
*/
function db_drop_field(&$ret, $table, $field) {
return Database::getActiveConnection()->schema()->dropField($ret, $table, $field);
return Database::getConnection()->schema()->dropField($ret, $table, $field);
}
/**
......@@ -2239,7 +2225,7 @@ function db_drop_field(&$ret, $table, $field) {
* Default value to be set. NULL for 'default NULL'.
*/
function db_field_set_default(&$ret, $table, $field, $default) {
return Database::getActiveConnection()->schema()->fieldSetDefault($ret, $table, $field, $default);
return Database::getConnection()->schema()->fieldSetDefault($ret, $table, $field, $default);
}
/**
......@@ -2253,7 +2239,7 @@ function db_field_set_default(&$ret, $table, $field, $default) {
* The field to be altered.
*/
function db_field_set_no_default(&$ret, $table, $field) {
return Database::getActiveConnection()->schema()->fieldSetNoDefault($ret, $table, $field);
return Database::getConnection()->schema()->fieldSetNoDefault($ret, $table, $field);
}
/**
......@@ -2267,7 +2253,7 @@ function db_field_set_no_default(&$ret, $table, $field) {
* Fields for the primary key.
*/
function db_add_primary_key(&$ret, $table, $fields) {
return Database::getActiveConnection()->schema()->addPrimaryKey($ret, $table, $fields);
return Database::getConnection()->schema()->addPrimaryKey($ret, $table, $fields);
}
/**
......@@ -2279,7 +2265,7 @@ function db_add_primary_key(&$ret, $table, $fields) {
* The table to be altered.
*/
function db_drop_primary_key(&$ret, $table) {
return Database::getActiveConnection()->schema()->dropPrimaryKey($ret, $table);
return Database::getConnection()->schema()->dropPrimaryKey($ret, $table);
}
/**
......@@ -2295,7 +2281,7 @@ function db_drop_primary_key(&$ret, $table) {
* An array of field names.
*/
function db_add_unique_key(&$ret, $table, $name, $fields) {
return Database::getActiveConnection()->schema()->addUniqueKey($ret, $table, $name, $fields);
return Database::getConnection()->schema()->addUniqueKey($ret, $table, $name, $fields);
}
/**
......@@ -2309,7 +2295,7 @@ function db_add_unique_key(&$ret, $table, $name, $fields) {
* The name of the key.
*/
function db_drop_unique_key(&$ret, $table, $name) {
return Database::getActiveConnection()->schema()->dropUniqueKey($ret, $table, $name);
return Database::getConnection()->schema()->dropUniqueKey($ret, $table, $name);
}
/**
......@@ -2325,7 +2311,7 @@ function db_drop_unique_key(&$ret, $table, $name) {
* An array of field names.
*/
function db_add_index(&$ret, $table, $name, $fields) {
return Database::getActiveConnection()->schema()->addIndex($ret, $table, $name, $fields);
return Database::getConnection()->schema()->addIndex($ret, $table, $name, $fields);
}
/**
......@@ -2339,7 +2325,7 @@ function db_add_index(&$ret, $table, $name, $fields) {
* The name of the index.
*/
function db_drop_index(&$ret, $table, $name) {
return Database::getActiveConnection()->schema()->dropIndex($ret, $table, $name);
return Database::getConnection()->schema()->dropIndex($ret, $table, $name);
}
/**
......@@ -2406,7 +2392,7 @@ function db_drop_index(&$ret, $table, $name) {
*/
function db_change_field(&$ret, $table, $field, $field_new, $spec, $keys_new = array()) {
return Database::getActiveConnection()->schema()->changeField($ret, $table, $field, $field_new, $spec, $keys_new);
return Database::getConnection()->schema()->changeField($ret, $table, $field, $field_new, $spec, $keys_new);
}
/**
......@@ -2518,8 +2504,8 @@ function _db_query_process_args($query, $args, $options) {
* The name of the autoincrement field.
*/
function db_last_insert_id($table, $field) {
$sequence_name = Database::getActiveConnection()->makeSequenceName($table, $field);
return Database::getActiveConnection()->lastInsertId($sequence_name);
$sequence_name = Database::getConnection()->makeSequenceName($table, $field);
return Database::getConnection()->lastInsertId($sequence_name);
}
/**
......@@ -2530,7 +2516,7 @@ function db_last_insert_id($table, $field) {
* @todo Remove this function when all queries have been ported to db_update().
*/
function db_affected_rows() {
$statement = Database::getActiveConnection()->lastStatement;
$statement = Database::getConnection()->lastStatement;
if (!$statement) {
return 0;
}
......
......@@ -133,7 +133,7 @@ function _dblog_get_message_types() {
* Note some values may be truncated for database column size restrictions.
*/
function dblog_watchdog(array $log_entry) {
Database::getConnection('default')->insert('watchdog')
Database::getConnection('default', 'default')->insert('watchdog')
->fields(array(
'uid' => $log_entry['user']->uid,
'type' => substr($log_entry['type'], 0, 64),
......
......@@ -819,7 +819,7 @@ protected function setUp() {
$clean_url_original = variable_get('clean_url', 0);
// Generate temporary prefixed database to ensure that tests have a clean starting point.
$db_prefix = Database::getActiveConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
$db_prefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
include_once DRUPAL_ROOT . '/includes/install.inc';
drupal_install_system();
......
......@@ -522,7 +522,7 @@ function simpletest_clean_environment() {
* Removed prefixed tables from the database that are left over from crashed tests.
*/
function simpletest_clean_database() {
$tables = db_find_tables(Database::getActiveConnection()->prefixTables('{simpletest}') . '%');
$tables = db_find_tables(Database::getConnection()->prefixTables('{simpletest}') . '%');
$schema = drupal_get_schema_unprocessed('simpletest');
$ret = array();
foreach (array_diff_key($tables, $schema) as $table) {
......
......@@ -156,7 +156,7 @@ class DatabaseConnectionTestCase extends DatabaseTestCase {
Database::addConnectionInfo('default', 'slave', $connection_info['default']);
$db1 = Database::getConnection('default', 'default');
$db2 = Database::getConnection('default', 'slave');
$db2 = Database::getConnection('slave', 'default');
$this->assertNotNull($db1, t('default connection is a real connection object.'));
$this->assertNotNull($db2, t('slave connection is a real connection object.'));
......@@ -164,18 +164,18 @@ class DatabaseConnectionTestCase extends DatabaseTestCase {
// Try to open those targets another time, that should return the same objects.
$db1b = Database::getConnection('default', 'default');
$db2b = Database::getConnection('default', 'slave');
$db2b = Database::getConnection('slave', 'default');
$this->assertIdentical($db1, $db1b, t('A second call to getConnection() returns the same object.'));
$this->assertIdentical($db2, $db2b, t('A second call to getConnection() returns the same object.'));
// Try to open an unknown target.
$unknown_target = $this->randomName();
$db3 = Database::getConnection('default', $unknown_target);
$db3 = Database::getConnection($unknown_target, 'default');
$this->assertNotNull($db3, t('Opening an unknown target returns a real connection object.'));
$this->assertIdentical($db1, $db3, t('An unknown target opens the default connection.'));
// Try to open that unknown target another time, that should return the same object.
$db3b = Database::getConnection('default', $unknown_target);
$db3b = Database::getConnection($unknown_target, 'default');
$this->assertIdentical($db3, $db3b, t('A second call to getConnection() returns the same object.'));
}
......@@ -192,7 +192,7 @@ class DatabaseConnectionTestCase extends DatabaseTestCase {
Database::ignoreTarget('default', 'slave');
$db1 = Database::getConnection('default', 'default');
$db2 = Database::getConnection('default', 'slave');
$db2 = Database::getConnection('slave', 'default');
$this->assertIdentical($db1, $db2, t('Both targets refer to the same connection.'));
}
......@@ -2054,7 +2054,7 @@ class DatabaseInvalidDataTestCase extends DatabaseTestCase {
$name = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 63))->fetchField();
if ($name == 'Elvis') {
if (!Database::getActiveConnection()->supportsTransactions()) {
if (!Database::getConnection()->supportsTransactions()) {
// This is an expected fail.
// Database engines that don't support transactions can leave partial
// inserts in place when an error occurs. This is the case for MySQL
......@@ -2200,7 +2200,7 @@ class DatabaseTransactionTestCase extends DatabaseTestCase {
* Whether or not to try rolling back the transaction when we're done.
*/
protected function transactionOuterLayer($suffix, $rollback = FALSE) {
$connection = Database::getActiveConnection();
$connection = Database::getConnection();
$txn = db_transaction();
// Insert a single row into the testing table.
......@@ -2230,7 +2230,7 @@ class DatabaseTransactionTestCase extends DatabaseTestCase {
* Whether or not to try rolling back the transaction when we're done.
*/
protected function transactionInnerLayer($suffix, $rollback = FALSE) {
$connection = Database::getActiveConnection();
$connection = Database::getConnection();
// Start a transaction. If we're being called from ->transactionOuterLayer,
// then we're already in a transaction. Normally, that would make starting
......@@ -2263,7 +2263,7 @@ class DatabaseTransactionTestCase extends DatabaseTestCase {
*/
function testTransactionsSupported() {
try {
$connection = Database::getActiveConnection();
$connection = Database::getConnection();
if ($connection->supportsTransactions()) {
// Start a "required" transaction. This should fail if we do
......@@ -2284,7 +2284,7 @@ class DatabaseTransactionTestCase extends DatabaseTestCase {
*/
function testTransactionsNotSupported() {
try {
$connection = Database::getActiveConnection();
$connection = Database::getConnection();
if (!$connection->supportsTransactions()) {
// Start a "required" transaction. This should fail if we do this
......@@ -2306,7 +2306,7 @@ class DatabaseTransactionTestCase extends DatabaseTestCase {
*/
function testTransactionRollBackSupported() {
// This test won't work right if transactions are not supported.
if (!Database::getActiveConnection()->supportsTransactions()) {
if (!Database::getConnection()->supportsTransactions()) {
return;
}
try {
......@@ -2332,7 +2332,7 @@ class DatabaseTransactionTestCase extends DatabaseTestCase {
*/
function testTransactionRollBackNotSupported() {
// This test won't work right if transactions are supported.
if (Database::getActiveConnection()->supportsTransactions()) {
if (Database::getConnection()->supportsTransactions()) {
return;
}
try {
......
......@@ -24,7 +24,7 @@ class ModuleTestCase extends DrupalWebTestCase {
* specified base table. Defaults to TRUE.
*/
function assertTableCount($base_table, $count = TRUE) {
$tables = db_find_tables(Database::getActiveConnection()->prefixTables('{' . $base_table . '}') . '%');
$tables = db_find_tables(Database::getConnection()->prefixTables('{' . $base_table . '}') . '%');
if ($count) {
return $this->assertTrue($tables, t('Tables matching "@base_table" found.', array('@base_table' => $base_table)));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment