summaryrefslogtreecommitdiff
path: root/includes/database/database.inc
diff options
context:
space:
mode:
Diffstat (limited to 'includes/database/database.inc')
-rw-r--r--includes/database/database.inc152
1 files changed, 113 insertions, 39 deletions
diff --git a/includes/database/database.inc b/includes/database/database.inc
index a4f00dc46..6b878d99f 100644
--- a/includes/database/database.inc
+++ b/includes/database/database.inc
@@ -259,7 +259,26 @@ abstract class DatabaseConnection extends PDO {
*/
protected $schema = NULL;
+ /**
+ * The default prefix used by this database connection.
+ *
+ * Separated from the other prefixes for performance reasons.
+ *
+ * @var string
+ */
+ protected $defaultPrefix = '';
+
+ /**
+ * The non-default prefixes used by this database connection.
+ *
+ * @var array
+ */
+ protected $prefixes = array();
+
function __construct($dsn, $username, $password, $driver_options = array()) {
+ // Initialize and prepare the connection prefix.
+ $this->setPrefix(isset($this->connectionOptions['prefix']) ? $this->connectionOptions['prefix'] : '');
+
// Because the other methods don't seem to work right.
$driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
@@ -343,6 +362,25 @@ abstract class DatabaseConnection extends PDO {
}
/**
+ * Preprocess the prefixes used by this database connection.
+ *
+ * @param $prefix
+ * The prefixes, in any of the multiple forms documented in
+ * default.settings.php.
+ */
+ protected function setPrefix($prefix) {
+ if (is_array($prefix)) {
+ $this->defaultPrefix = isset($prefix['default']) ? $prefix['default'] : '';
+ unset($prefix['default']);
+ $this->prefixes = $prefix;
+ }
+ else {
+ $this->defaultPrefix = $prefix;
+ $this->prefixes = array();
+ }
+ }
+
+ /**
* Appends a database prefix to all tables in a query.
*
* Queries sent to Drupal should wrap all table names in curly brackets. This
@@ -357,27 +395,12 @@ abstract class DatabaseConnection extends PDO {
* The properly-prefixed string.
*/
public function prefixTables($sql) {
- global $db_prefix;
-
- if (is_array($db_prefix)) {
- if (array_key_exists('default', $db_prefix)) {
- $tmp = $db_prefix;
- unset($tmp['default']);
- foreach ($tmp as $key => $val) {
- $sql = strtr($sql, array('{' . $key . '}' => $val . $key));
- }
- return strtr($sql, array('{' => $db_prefix['default'] , '}' => ''));
- }
- else {
- foreach ($db_prefix as $key => $val) {
- $sql = strtr($sql, array('{' . $key . '}' => $val . $key));
- }
- return strtr($sql, array('{' => '' , '}' => ''));
- }
- }
- else {
- return strtr($sql, array('{' => $db_prefix , '}' => ''));
+ // Replace specific table prefixes first.
+ foreach ($this->prefixes as $key => $val) {
+ $sql = strtr($sql, array('{' . $key . '}' => $val . $key));
}
+ // Then replace remaining tables with the default prefix.
+ return strtr($sql, array('{' => $this->defaultPrefix , '}' => ''));
}
/**
@@ -387,17 +410,12 @@ abstract class DatabaseConnection extends PDO {
* is not used in prefixTables due to performance reasons.
*/
public function tablePrefix($table = 'default') {
- global $db_prefix;
- if (is_array($db_prefix)) {
- if (isset($db_prefix[$table])) {
- return $db_prefix[$table];
- }
- elseif (isset($db_prefix['default'])) {
- return $db_prefix['default'];
- }
- return '';
+ if (isset($this->prefixes[$table])) {
+ return $this->prefixes[$table];
+ }
+ else {
+ return $this->defaultPrefix;
}
- return $db_prefix;
}
/**
@@ -1314,6 +1332,20 @@ abstract class Database {
if (empty($value['driver'])) {
$database_info[$index][$target] = $database_info[$index][$target][mt_rand(0, count($database_info[$index][$target]) - 1)];
}
+
+ // Parse the prefix information.
+ if (!isset($database_info[$index][$target]['prefix'])) {
+ // Default to an empty prefix.
+ $database_info[$index][$target]['prefix'] = array(
+ 'default' => '',
+ );
+ }
+ else if (!is_array($database_info[$index][$target]['prefix'])) {
+ // Transform the flat form into an array form.
+ $database_info[$index][$target]['prefix'] = array(
+ 'default' => $database_info[$index][$target]['prefix'],
+ );
+ }
}
}
@@ -1373,7 +1405,58 @@ abstract class Database {
if (!empty(self::$databaseInfo[$key])) {
return self::$databaseInfo[$key];
}
+ }
+
+ /**
+ * Rename a connection and its corresponding connection information.
+ *
+ * @param $old_key
+ * The old connection key.
+ * @param $new_key
+ * The new connection key.
+ * @return
+ * TRUE in case of success, FALSE otherwise.
+ */
+ final public static function renameConnection($old_key, $new_key) {
+ if (empty(self::$databaseInfo)) {
+ self::parseConnectionInfo();
+ }
+ if (!empty(self::$databaseInfo[$old_key]) && empty(self::$databaseInfo[$new_key])) {
+ // Migrate the database connection information.
+ self::$databaseInfo[$new_key] = self::$databaseInfo[$old_key];
+ unset(self::$databaseInfo[$old_key]);
+
+ // Migrate over the DatabaseConnection object if it exists.
+ if (isset(self::$connections[$old_key])) {
+ self::$connections[$new_key] = self::$connections[$old_key];
+ unset(self::$connections[$old_key]);
+ }
+
+ return TRUE;
+ }
+ else {
+ return FALSE;
+ }
+ }
+
+ /**
+ * Remove a connection and its corresponding connection information.
+ *
+ * @param $key
+ * The connection key.
+ * @return
+ * TRUE in case of success, FALSE otherwise.
+ */
+ final public static function removeConnection($key) {
+ if (isset(self::$databaseInfo[$key])) {
+ unset(self::$databaseInfo[$key]);
+ unset(self::$connections[$key]);
+ return TRUE;
+ }
+ else {
+ return FALSE;
+ }
}
/**
@@ -1386,8 +1469,6 @@ abstract class Database {
* The database target to open.
*/
final protected static function openConnection($key, $target) {
- global $db_prefix;
-
if (empty(self::$databaseInfo)) {
self::parseConnectionInfo();
}
@@ -1415,13 +1496,6 @@ abstract class Database {
$new_connection->setLogger(self::$logs[$key]);
}
- // We need to pass around the simpletest database prefix in the request
- // and we put that in the user_agent header. The header HMAC was already
- // validated in bootstrap.inc.
- if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/^(simpletest\d+);/", $_SERVER['HTTP_USER_AGENT'], $matches)) {
- $db_prefix_string = is_array($db_prefix) ? $db_prefix['default'] : $db_prefix;
- $db_prefix = $db_prefix_string . $matches[1];
- }
return $new_connection;
}