summaryrefslogtreecommitdiff
path: root/includes/database.inc
diff options
context:
space:
mode:
Diffstat (limited to 'includes/database.inc')
-rw-r--r--includes/database.inc51
1 files changed, 44 insertions, 7 deletions
diff --git a/includes/database.inc b/includes/database.inc
index 52851a6b7..aec692e5e 100644
--- a/includes/database.inc
+++ b/includes/database.inc
@@ -18,16 +18,53 @@ function db_prefix_tables($sql) {
return strtr($sql, array("{" => $prefix, "}" => ""));
}
-$db_type = substr($db_url, 0, strpos($db_url, "://"));
-if ($db_type == "mysql") {
- include_once "includes/database.mysql.inc";
-}
-else {
- include_once "includes/database.pear.inc";
+/**
+* Use the specified database connection for queries. Initialize the connection if it does not already exist,
+* and if no such member exists, a duplicate of the default connection is made.
+* Be very careful to switch the connection back to the default connection, so as to avoid errors. As the $name
+* parameter defaults to 'default', you only need to run db_set_active() without any arguments to use
+* the default database
+*
+* @param $name The named connection specified in the $db_url variable.
+*/
+function db_set_active($name = 'default') {
+ global $db_url;
+ global $active_db;
+ static $db_conns;
+
+ if (!isset($db_conns[$name])) {
+ //Initiate a new connection, using the named db url specified
+ if (is_array($db_url)) {
+ $connect_url = ($db_url[$name]) ? $db_url[$name] : $db_url['default'];
+ }
+ else {
+ $connect_url = $db_url;
+ }
+
+
+ $db_type = substr($connect_url, 0, strpos($connect_url, "://"));
+
+ //TODO : Allow more than one database api to be present. ie: pgsl and mysql
+ if ($db_type == "mysql") {
+ include_once "includes/database.mysql.inc";
+ }
+ else {
+ include_once "includes/database.pear.inc";
+ }
+
+ $db_conns[$name] = db_connect($connect_url);
+
+ }
+ //set the active connection
+ $active_db = $db_conns[$name];
+
+
}
-db_connect($db_url);
+
+// initialize the default db_url
+db_set_active();
?>