From 90c586422da74ef4e56bc93c2b16aeab22bc613f Mon Sep 17 00:00:00 2001 From: donncha Date: Wed, 27 Jul 2005 16:15:29 +0000 Subject: Use multiple databases. git-svn-id: http://svn.automattic.com/wordpress-mu/trunk@80 7be80a69-a1ef-0310-a953-fb0f7c49ff36 --- wp-inst/wp-includes/wp-db.php | 51 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'wp-inst/wp-includes/wp-db.php') diff --git a/wp-inst/wp-includes/wp-db.php b/wp-inst/wp-includes/wp-db.php index 73d4be7..765b0c0 100644 --- a/wp-inst/wp-includes/wp-db.php +++ b/wp-inst/wp-includes/wp-db.php @@ -53,6 +53,10 @@ class wpdb {

If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.

"); } + $this->db_host = $dbhost; + $this->db_user = $dbuser; + $this->db_password = $dbpass; + $this->db_name = $dbname; $this->select($dbname); } @@ -124,6 +128,48 @@ class wpdb { $this->last_query = null; } + function db_connect( $query ) { + global $db_list; + if( is_array( $db_list ) == false ) + return true; + + if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) { + $details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ]; + } else { + $details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ]; + } + + if( $this->db_host != $details[ 'db_host' ] || $this->db_user != $details[ 'db_user' ] || $this->db_password != $details[ 'db_password' ] ) { + if ($this->dbh) { + @mysql_close( $this->dbh ); + } + $this->db_host = $details[ 'db_host' ]; + $this->db_user = $details[ 'db_user' ]; + $this->db_password = $details[ 'db_password' ]; + + $this->dbh = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); + if (!$this->dbh) { + $this->bail(" +

Error establishing a database connection

+

This either means that the username and password information in your wp-config.php file is incorrect or we can't contact the database server at $dbhost. This could mean your host's database server is down.

+ +

If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.

+"); + } + $this->db_name = $details[ 'db_name' ]; + $this->select( $details[ 'db_name' ] ); + } + + if( $this->db_name != $details[ 'db_name' ] ) { + $this->db_name = $details[ 'db_name' ]; + $this->select( $details[ 'db_name' ] ); + } + } + // ================================================================== // Basic Query - see docs for more detail @@ -142,6 +188,9 @@ class wpdb { if (SAVEQUERIES) $this->timer_start(); + if( defined( "WP_USE_MULTIPLE_DB" ) && CONSTANT( "WP_USE_MULTIPLE_DB" ) == true ) + $this->db_connect( $query ); + $this->result = @mysql_query($query, $this->dbh); ++$this->num_queries; @@ -359,4 +408,4 @@ HEAD; } $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); -?> \ No newline at end of file +?> -- cgit