diff options
-rw-r--r-- | CHANGELOG.txt | 6 | ||||
-rw-r--r-- | includes/conf.php | 5 | ||||
-rw-r--r-- | includes/database.inc | 51 | ||||
-rw-r--r-- | includes/database.mysql.inc | 7 | ||||
-rw-r--r-- | includes/database.pear.inc | 26 |
5 files changed, 72 insertions, 23 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt index bdff215b8..fc7f42e6e 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -7,7 +7,11 @@ Drupal x.x.x, xxxx-xx-xx - menu module: * made it possible to customize menus. - 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: * slightly reorganized navigation menus. diff --git a/includes/conf.php b/includes/conf.php index 00009a6ec..cf72381f8 100644 --- a/includes/conf.php +++ b/includes/conf.php @@ -11,6 +11,11 @@ # That is, the use of ':', '/', '@', '?', '=' and '#', ''', '"', # and so on is likely to confuse the parser; use alpha-numerical # 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 = "pgsql://user:password@hostname/database"; 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(); ?> diff --git a/includes/database.mysql.inc b/includes/database.mysql.inc index 73e331615..aeb31828a 100644 --- a/includes/database.mysql.inc +++ b/includes/database.mysql.inc @@ -9,9 +9,11 @@ function db_connect($url) { $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"); + return $connection; + /* ** Note that you can change the 'mysql_connect' statement to 'mysql_pconnect' ** if you want to use persistent connections. This is not recommended on @@ -70,6 +72,7 @@ function db_queryd($query) { // private function _db_query($query, $debug = 0) { + global $active_db; global $queries; if (variable_get("dev_query", 0)) { @@ -77,7 +80,7 @@ function _db_query($query, $debug = 0) { $timer = (float)$usec + (float)$sec; } - $result = mysql_query($query); + $result = mysql_query($query, $active_db); if (variable_get("dev_query", 0)) { list($usec, $sec) = explode(" ", microtime()); diff --git a/includes/database.pear.inc b/includes/database.pear.inc index c335f6566..8ab8b4072 100644 --- a/includes/database.pear.inc +++ b/includes/database.pear.inc @@ -4,8 +4,6 @@ require_once 'DB.php'; function db_connect($url) { - global $db_handle; - $db_handle = DB::connect($url); if (DB::isError($db_handle)) { @@ -13,6 +11,8 @@ function db_connect($url) { } $db_handle->setFetchMode(DB_FETCHMODE_ASSOC); + + return $db_handle; } /** @@ -65,14 +65,14 @@ function db_queryd($query) { // private function _db_query($query, $debug = 0) { - global $db_handle, $queries; + global $active_db, $queries; if (variable_get("dev_query", 0)) { list($usec, $sec) = explode(" ", microtime()); $timer = (float)$usec + (float)$sec; } - $result = $db_handle->query($query); + $result = $active_db->query($query); if (variable_get("dev_query", 0)) { list($usec, $sec) = explode(" ", microtime()); @@ -119,16 +119,16 @@ function db_result($result, $row = 0) { } function db_error() { - global $db_handle; + global $active_db; - return DB::isError($db_handle); + return DB::isError($active_db); } function db_next_id($name) { - global $db_handle; + global $active_db; $name = db_prefix_tables($name); - $result = $db_handle->nextID($name); + $result = $active_db->nextID($name); if (DB::isError($result)) { watchdog("error", "database: ". $result->getMessage() ."\nsequence table: $name"); } @@ -138,9 +138,9 @@ function db_next_id($name) { } 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() { * @return a DB_Result object or a DB_Error */ function db_query_range($query) { - global $db_handle, $queries; + global $active_db, $queries; if (variable_get("dev_query", 0)) { list($usec, $sec) = explode(" ", microtime()); @@ -167,12 +167,12 @@ function db_query_range($query) { $args = array_map("check_query", $args); $query = db_prefix_tables($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 { $query = func_get_arg(0); $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)) { |