Commit e4d45aae authored by Dries's avatar Dries

- Patch by Adrian: added support for multiple database connections.
parent dfd66f99
...@@ -7,7 +7,11 @@ Drupal x.x.x, xxxx-xx-xx ...@@ -7,7 +7,11 @@ Drupal x.x.x, xxxx-xx-xx
- menu module: - menu module:
* made it possible to customize menus. * made it possible to customize menus.
- refactored 403 (forbidden) handling and added support for custom 403 pages. - refactored 403 (forbidden) handling and added support for custom 403 pages.
- added support for RSS ping-notifications of http://technorati.com/. - syndication:
* added support for RSS ping-notifications of http://technorati.com/.
- database backend:
* added support for mutiple database connections.
* refactored the categorization of news items.
- usability: - usability:
* slightly reorganized navigation menus. * slightly reorganized navigation menus.
......
...@@ -11,6 +11,11 @@ ...@@ -11,6 +11,11 @@
# That is, the use of ':', '/', '@', '?', '=' and '#', ''', '"', # That is, the use of ':', '/', '@', '?', '=' and '#', ''', '"',
# and so on is likely to confuse the parser; use alpha-numerical # and so on is likely to confuse the parser; use alpha-numerical
# characters instead. # characters instead.
#
# To specify multiple connections to be used in your site (i.e. for
# complex custom modules) you can also specify an associative array
# of $db_url variables with the 'default' element used until otherwise
# requested.
# $db_url = "mysql://user:password@hostname/database"; # $db_url = "mysql://user:password@hostname/database";
# $db_url = "pgsql://user:password@hostname/database"; # $db_url = "pgsql://user:password@hostname/database";
......
...@@ -18,16 +18,53 @@ function db_prefix_tables($sql) { ...@@ -18,16 +18,53 @@ function db_prefix_tables($sql) {
return strtr($sql, array("{" => $prefix, "}" => "")); return strtr($sql, array("{" => $prefix, "}" => ""));
} }
$db_type = substr($db_url, 0, strpos($db_url, "://"));
if ($db_type == "mysql") { /**
include_once "includes/database.mysql.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.
else { * Be very careful to switch the connection back to the default connection, so as to avoid errors. As the $name
include_once "includes/database.pear.inc"; * 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();
?> ?>
...@@ -9,9 +9,11 @@ function db_connect($url) { ...@@ -9,9 +9,11 @@ function db_connect($url) {
$url["host"] = $url["host"] . ":" . $url["port"]; $url["host"] = $url["host"] . ":" . $url["port"];
} }
mysql_connect($url["host"], $url["user"], $url["pass"]) or die(mysql_error()); $connection = mysql_connect($url["host"], $url["user"], $url["pass"]) or die(mysql_error());
mysql_select_db(substr($url["path"], 1)) or die("unable to select database"); mysql_select_db(substr($url["path"], 1)) or die("unable to select database");
return $connection;
/* /*
** Note that you can change the 'mysql_connect' statement to 'mysql_pconnect' ** Note that you can change the 'mysql_connect' statement to 'mysql_pconnect'
** if you want to use persistent connections. This is not recommended on ** if you want to use persistent connections. This is not recommended on
...@@ -70,6 +72,7 @@ function db_queryd($query) { ...@@ -70,6 +72,7 @@ function db_queryd($query) {
// private // private
function _db_query($query, $debug = 0) { function _db_query($query, $debug = 0) {
global $active_db;
global $queries; global $queries;
if (variable_get("dev_query", 0)) { if (variable_get("dev_query", 0)) {
...@@ -77,7 +80,7 @@ function _db_query($query, $debug = 0) { ...@@ -77,7 +80,7 @@ function _db_query($query, $debug = 0) {
$timer = (float)$usec + (float)$sec; $timer = (float)$usec + (float)$sec;
} }
$result = mysql_query($query); $result = mysql_query($query, $active_db);
if (variable_get("dev_query", 0)) { if (variable_get("dev_query", 0)) {
list($usec, $sec) = explode(" ", microtime()); list($usec, $sec) = explode(" ", microtime());
......
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
require_once 'DB.php'; require_once 'DB.php';
function db_connect($url) { function db_connect($url) {
global $db_handle;
$db_handle = DB::connect($url); $db_handle = DB::connect($url);
if (DB::isError($db_handle)) { if (DB::isError($db_handle)) {
...@@ -13,6 +11,8 @@ function db_connect($url) { ...@@ -13,6 +11,8 @@ function db_connect($url) {
} }
$db_handle->setFetchMode(DB_FETCHMODE_ASSOC); $db_handle->setFetchMode(DB_FETCHMODE_ASSOC);
return $db_handle;
} }
/** /**
...@@ -65,14 +65,14 @@ function db_queryd($query) { ...@@ -65,14 +65,14 @@ function db_queryd($query) {
// private // private
function _db_query($query, $debug = 0) { function _db_query($query, $debug = 0) {
global $db_handle, $queries; global $active_db, $queries;
if (variable_get("dev_query", 0)) { if (variable_get("dev_query", 0)) {
list($usec, $sec) = explode(" ", microtime()); list($usec, $sec) = explode(" ", microtime());
$timer = (float)$usec + (float)$sec; $timer = (float)$usec + (float)$sec;
} }
$result = $db_handle->query($query); $result = $active_db->query($query);
if (variable_get("dev_query", 0)) { if (variable_get("dev_query", 0)) {
list($usec, $sec) = explode(" ", microtime()); list($usec, $sec) = explode(" ", microtime());
...@@ -119,16 +119,16 @@ function db_result($result, $row = 0) { ...@@ -119,16 +119,16 @@ function db_result($result, $row = 0) {
} }
function db_error() { function db_error() {
global $db_handle; global $active_db;
return DB::isError($db_handle); return DB::isError($active_db);
} }
function db_next_id($name) { function db_next_id($name) {
global $db_handle; global $active_db;
$name = db_prefix_tables($name); $name = db_prefix_tables($name);
$result = $db_handle->nextID($name); $result = $active_db->nextID($name);
if (DB::isError($result)) { if (DB::isError($result)) {
watchdog("error", "database: ". $result->getMessage() ."\nsequence table: $name"); watchdog("error", "database: ". $result->getMessage() ."\nsequence table: $name");
} }
...@@ -138,9 +138,9 @@ function db_next_id($name) { ...@@ -138,9 +138,9 @@ function db_next_id($name) {
} }
function db_affected_rows() { function db_affected_rows() {
global $db_handle; global $active_db;
return $db_handle->affectedRows(); return $active_db->affectedRows();
} }
/** /**
...@@ -153,7 +153,7 @@ function db_affected_rows() { ...@@ -153,7 +153,7 @@ function db_affected_rows() {
* @return a DB_Result object or a DB_Error * @return a DB_Result object or a DB_Error
*/ */
function db_query_range($query) { function db_query_range($query) {
global $db_handle, $queries; global $active_db, $queries;
if (variable_get("dev_query", 0)) { if (variable_get("dev_query", 0)) {
list($usec, $sec) = explode(" ", microtime()); list($usec, $sec) = explode(" ", microtime());
...@@ -167,12 +167,12 @@ function db_query_range($query) { ...@@ -167,12 +167,12 @@ function db_query_range($query) {
$args = array_map("check_query", $args); $args = array_map("check_query", $args);
$query = db_prefix_tables($query); $query = db_prefix_tables($query);
$args[0] = $query; $args[0] = $query;
$result = $db_handle->limitQuery(call_user_func_array("sprintf", $args), $from, $count); $result = $active_db->limitQuery(call_user_func_array("sprintf", $args), $from, $count);
} }
else { else {
$query = func_get_arg(0); $query = func_get_arg(0);
$query = db_prefix_tables($query); $query = db_prefix_tables($query);
$result = $db_handle->limitQuery( $query, $from, $count); $result = $active_db->limitQuery( $query, $from, $count);
} }
if (variable_get("dev_query", 0)) { if (variable_get("dev_query", 0)) {
......
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