Index: wp-admin/includes/deprecated.php
===================================================================
--- wp-admin/includes/deprecated.php	(revision 17501)
+++ wp-admin/includes/deprecated.php	(working copy)
@@ -493,11 +493,12 @@
 		$this->query_from = " FROM $wpdb->users";
 		$this->query_where = " WHERE 1=1 $search_sql";
 
+		$prefix = $wpdb->get_blog_prefix();
 		if ( $this->role ) {
 			$this->query_from .= " INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id";
-			$this->query_where .= $wpdb->prepare(" AND $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' AND $wpdb->usermeta.meta_value LIKE %s", '%' . $this->role . '%');
+			$this->query_where .= $wpdb->prepare(" AND $wpdb->usermeta.meta_key = '{$prefix}capabilities' AND $wpdb->usermeta.meta_value LIKE %s", '%' . $this->role . '%');
 		} elseif ( is_multisite() ) {
-			$level_key = $wpdb->prefix . 'capabilities'; // wpmu site admins don't have user_levels
+			$level_key = $prefix . 'capabilities'; // wpmu site admins don't have user_levels
 			$this->query_from .= ", $wpdb->usermeta";
 			$this->query_where .= " AND $wpdb->users.ID = $wpdb->usermeta.user_id AND meta_key = '{$level_key}'";
 		}
Index: wp-admin/includes/upgrade.php
===================================================================
--- wp-admin/includes/upgrade.php	(revision 17501)
+++ wp-admin/includes/upgrade.php	(working copy)
@@ -645,10 +645,11 @@
 	}
 
 	// Obsolete tables
-	$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues');
-	$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes');
-	$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups');
-	$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options');
+	$prefix = $wpdb->get_blog_prefix();
+	$wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'optionvalues');
+	$wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'optiontypes');
+	$wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'optiongroups');
+	$wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'optiongroup_options');
 
 	// Update comments table to use comment_type
 	$wpdb->query("UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'");
@@ -689,7 +690,7 @@
 		if ( !empty( $user->user_nickname ) )
 			update_user_meta( $user->ID, 'nickname', $wpdb->escape($user->user_nickname) );
 		if ( !empty( $user->user_level ) )
-			update_user_meta( $user->ID, $wpdb->prefix . 'user_level', $user->user_level );
+			update_user_meta( $user->ID, $wpdb->get_blog_prefix() . 'user_level', $user->user_level );
 		if ( !empty( $user->user_icq ) )
 			update_user_meta( $user->ID, 'icq', $wpdb->escape($user->user_icq) );
 		if ( !empty( $user->user_aim ) )
@@ -714,11 +715,12 @@
 		endif;
 
 		// FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set.
-		$caps = get_user_meta( $user->ID, $wpdb->prefix . 'capabilities');
+		$prefix = $wpdb->get_blog_prefix();
+		$caps = get_user_meta( $user->ID, $prefix . 'capabilities');
 		if ( empty($caps) || defined('RESET_CAPS') ) {
-			$level = get_user_meta($user->ID, $wpdb->prefix . 'user_level', true);
+			$level = get_user_meta($user->ID, $prefix . 'user_level', true);
 			$role = translate_level_to_role($level);
-			update_user_meta( $user->ID, $wpdb->prefix . 'capabilities', array($role => true) );
+			update_user_meta( $user->ID, $prefix . 'capabilities', array($role => true) );
 		}
 
 	endforeach;
@@ -897,7 +899,7 @@
 		$link_cat_id_map = array();
 		$default_link_cat = 0;
 		$tt_ids = array();
-		$link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->prefix . 'linkcategories');
+		$link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->get_blog_prefix() . 'linkcategories');
 		foreach ( $link_cats as $category) {
 			$cat_id = (int) $category->cat_id;
 			$term_id = 0;
@@ -955,7 +957,7 @@
 
 	if ( $wp_current_db_version < 4772 ) {
 		// Obsolete linkcategories table
-		$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories');
+		$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->get_blog_prefix() . 'linkcategories');
 	}
 
 	// Recalculate all counts
@@ -990,9 +992,10 @@
  */
 function upgrade_230_old_tables() {
 	global $wpdb;
-	$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'categories');
-	$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'link2cat');
-	$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'post2cat');
+	$prefix = $wpdb->get_blog_prefix();
+	$wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'categories');
+	$wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'link2cat');
+	$wpdb->query('DROP TABLE IF EXISTS ' . $prefix . 'post2cat');
 }
 
 /**
Index: wp-includes/capabilities.php
===================================================================
--- wp-includes/capabilities.php	(revision 17501)
+++ wp-includes/capabilities.php	(working copy)
@@ -98,7 +98,7 @@
 	 */
 	function _init () {
 		global $wpdb, $wp_user_roles;
-		$this->role_key = $wpdb->prefix . 'user_roles';
+		$this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
 		if ( ! empty( $wp_user_roles ) ) {
 			$this->roles = $wp_user_roles;
 			$this->use_db = false;
@@ -516,7 +516,7 @@
 	function _init_caps( $cap_key = '' ) {
 		global $wpdb;
 		if ( empty($cap_key) )
-			$this->cap_key = $wpdb->prefix . 'capabilities';
+			$this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
 		else
 			$this->cap_key = $cap_key;
 		$this->caps = &$this->{$this->cap_key};
@@ -662,7 +662,7 @@
 	function update_user_level_from_caps() {
 		global $wpdb;
 		$this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
-		update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
+		update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
 	}
 
 	/**
@@ -704,7 +704,7 @@
 		global $wpdb;
 		$this->caps = array();
 		delete_user_meta( $this->ID, $this->cap_key );
-		delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
+		delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
 		$this->get_role_caps();
 	}
 
Index: wp-includes/ms-blogs.php
===================================================================
--- wp-includes/ms-blogs.php	(revision 17501)
+++ wp-includes/ms-blogs.php	(working copy)
@@ -475,7 +475,7 @@
 	}
 
 	$wpdb->set_blog_id($new_blog);
-	$table_prefix = $wpdb->prefix;
+	$table_prefix = $wpdb->get_blog_prefix();
 	$prev_blog_id = $blog_id;
 	$blog_id = $new_blog;
 
@@ -541,7 +541,7 @@
 	$wpdb->set_blog_id($blog);
 	$prev_blog_id = $blog_id;
 	$blog_id = $blog;
-	$table_prefix = $wpdb->prefix;
+	$table_prefix = $wpdb->get_blog_prefix();
 
 	if ( is_object( $wp_roles ) ) {
 		$wpdb->suppress_errors();
Index: wp-includes/user.php
===================================================================
--- wp-includes/user.php	(revision 17501)
+++ wp-includes/user.php	(working copy)
@@ -262,9 +262,9 @@
 
 	// Keys used as object vars cannot have dashes.
 	$key = str_replace('-', '', $option);
-
-	if ( isset( $user->{$wpdb->prefix . $key} ) ) // Blog specific
-		$result = $user->{$wpdb->prefix . $key};
+	$prefix = $wpdb->get_blog_prefix();
+	if ( isset( $user->{$prefix . $key} ) ) // Blog specific
+		$result = $user->{$prefix . $key};
 	elseif ( isset( $user->{$key} ) ) // User specific and cross-blog
 		$result = $user->{$key};
 	else
@@ -295,7 +295,7 @@
 	global $wpdb;
 
 	if ( !$global )
-		$option_name = $wpdb->prefix . $option_name;
+		$option_name = $wpdb->get_blog_prefix() . $option_name;
 
 	// For backward compatibility. See differences between update_user_meta() and deprecated update_usermeta().
 	// http://core.trac.wordpress.org/ticket/13088
@@ -324,7 +324,7 @@
 	global $wpdb;
 
 	if ( !$global )
-		$option_name = $wpdb->prefix . $option_name;
+		$option_name = $wpdb->get_blog_prefix() . $option_name;
 	return delete_user_meta( $user_id, $option_name );
 }
 
@@ -1101,7 +1101,7 @@
 		$user->{$key} = $value;
 	}
 
-	$level = $wpdb->prefix . 'user_level';
+	$level = $wpdb->get_blog_prefix() . 'user_level';
 	if ( isset( $user->{$level} ) )
 		$user->user_level = $user->{$level};
 
Index: wp-includes/wp-db.php
===================================================================
--- wp-includes/wp-db.php	(revision 17501)
+++ wp-includes/wp-db.php	(working copy)
@@ -35,152 +35,65 @@
 define( 'ARRAY_N', 'ARRAY_N' );
 
 /**
- * WordPress Database Access Abstraction Object
+ * WordPress Database Access Abstraction Object Methods
  *
- * It is possible to replace this class with your own
- * by setting the $wpdb global variable in wp-content/db.php
- * file with your class. You can name it wpdb also, since
- * this file will not be included, if the other file is
- * available.
+ * @package WordPress
+ * @subpackage Database
+ * @since 3.2
+ */
+Interface WP_Db_Methods {
+	public function set_prefix( $prefix, $set_table_names = true );
+	public function set_blog_id( $blog_id, $site_id = 0 );
+	public function get_blog_prefix( $blog_id = null );
+	public function tables( $scope = 'all', $prefix = true, $blog_id = 0 );
+	public function escape( $data );
+	public function escape_by_ref( &$string );
+	public function prepare( $query = null );
+	public function show_errors( $show = true );
+	public function hide_errors();
+	public function suppress_errors( $suppress = true );
+	public function query( $query );
+	public function insert( $table, $data, $format = null );
+	public function replace( $table, $data, $format = null );
+	public function update( $table, $data, $where, $format = null, $where_format = null );
+	public function get_var( $query = null, $column = 0, $row = 0 );
+	public function get_row( $query = null, $output = null, $row = 0 );
+	public function get_col( $query = null , $column = 0 );
+	public function get_results( $query = null, $output = null );
+	public function get_col_info( $info_type = 'name', $col_offset = null );
+	public function has_cap( $feature );
+	public function db_version();
+}
+
+/**
+ * WordPress Database Access Abstraction Data Object
  *
- * @link http://codex.wordpress.org/Function_Reference/wpdb_Class
+ * All public object variables of the WordPress Database 
+ * Acccess Abstraction Object.
  *
  * @package WordPress
  * @subpackage Database
- * @since 0.71
+ * @since 3.2
  */
-class wpdb {
+Class WP_Db_Variables {
 
 	/**
-	 * Whether to show SQL/DB errors
-	 *
-	 * @since 0.71
-	 * @access private
-	 * @var bool
-	 */
-	var $show_errors = false;
-
-	/**
-	 * Whether to suppress errors during the DB bootstrapping.
-	 *
-	 * @access private
-	 * @since 2.5.0
-	 * @var bool
-	 */
-	var $suppress_errors = false;
-
-	/**
-	 * The last error during query.
-	 *
-	 * @see get_last_error()
-	 * @since 2.5.0
-	 * @access private
-	 * @var string
-	 */
-	var $last_error = '';
-
-	/**
-	 * Amount of queries made
-	 *
-	 * @since 1.2.0
-	 * @access private
-	 * @var int
-	 */
-	var $num_queries = 0;
-
-	/**
-	 * Count of rows returned by previous query
-	 *
-	 * @since 1.2.0
-	 * @access private
-	 * @var int
-	 */
-	var $num_rows = 0;
-
-	/**
-	 * Count of affected rows by previous query
-	 *
-	 * @since 0.71
-	 * @access private
-	 * @var int
-	 */
-	var $rows_affected = 0;
-
-	/**
 	 * The ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
 	 *
 	 * @since 0.71
 	 * @access public
 	 * @var int
 	 */
-	var $insert_id = 0;
+	public $insert_id = 0;
 
 	/**
-	 * Saved result of the last query made
-	 *
-	 * @since 1.2.0
-	 * @access private
-	 * @var array
-	 */
-	var $last_query;
-
-	/**
-	 * Results of the last query made
-	 *
-	 * @since 1.0.0
-	 * @access private
-	 * @var array|null
-	 */
-	var $last_result;
-
-	/**
-	 * Saved info on the table column
-	 *
-	 * @since 1.2.0
-	 * @access private
-	 * @var array
-	 */
-	var $col_info;
-
-	/**
-	 * Saved queries that were executed
-	 *
-	 * @since 1.5.0
-	 * @access private
-	 * @var array
-	 */
-	var $queries;
-
-	/**
-	 * WordPress table prefix
-	 *
-	 * You can set this to have multiple WordPress installations
-	 * in a single database. The second reason is for possible
-	 * security precautions.
-	 *
-	 * @since 0.71
-	 * @access private
-	 * @var string
-	 */
-	var $prefix = '';
-
-	/**
-	 * Whether the database queries are ready to start executing.
-	 *
-	 * @since 2.5.0
-	 * @access private
-	 * @var bool
-	 */
-	var $ready = false;
-
-	/**
 	 * {@internal Missing Description}}
 	 *
 	 * @since 3.0.0
 	 * @access public
 	 * @var int
 	 */
-	var $blogid = 0;
+	public $blogid = 0;
 
 	/**
 	 * {@internal Missing Description}}
@@ -189,60 +102,16 @@
 	 * @access public
 	 * @var int
 	 */
-	var $siteid = 0;
+	public $siteid = 0;
 
 	/**
-	 * List of WordPress per-blog tables
-	 *
-	 * @since 2.5.0
-	 * @access private
-	 * @see wpdb::tables()
-	 * @var array
-	 */
-	var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta',
-		'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' );
-
-	/**
-	 * List of deprecated WordPress tables
-	 *
-	 * categories, post2cat, and link2cat were deprecated in 2.3.0, db version 5539
-	 *
-	 * @since 2.9.0
-	 * @access private
-	 * @see wpdb::tables()
-	 * @var array
-	 */
-	var $old_tables = array( 'categories', 'post2cat', 'link2cat' );
-
-	/**
-	 * List of WordPress global tables
-	 *
-	 * @since 3.0.0
-	 * @access private
-	 * @see wpdb::tables()
-	 * @var array
-	 */
-	var $global_tables = array( 'users', 'usermeta' );
-
-	/**
-	 * List of Multisite global tables
-	 *
-	 * @since 3.0.0
-	 * @access private
-	 * @see wpdb::tables()
-	 * @var array
-	 */
-	var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
-		'sitecategories', 'registration_log', 'blog_versions' );
-
-	/**
 	 * WordPress Comments table
 	 *
 	 * @since 1.5.0
 	 * @access public
 	 * @var string
 	 */
-	var $comments;
+	public $comments;
 
 	/**
 	 * WordPress Comment Metadata table
@@ -251,7 +120,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $commentmeta;
+	public $commentmeta;
 
 	/**
 	 * WordPress Links table
@@ -260,7 +129,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $links;
+	public $links;
 
 	/**
 	 * WordPress Options table
@@ -269,7 +138,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $options;
+	public $options;
 
 	/**
 	 * WordPress Post Metadata table
@@ -278,7 +147,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $postmeta;
+	public $postmeta;
 
 	/**
 	 * WordPress Posts table
@@ -287,7 +156,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $posts;
+	public $posts;
 
 	/**
 	 * WordPress Terms table
@@ -296,7 +165,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $terms;
+	public $terms;
 
 	/**
 	 * WordPress Term Relationships table
@@ -305,7 +174,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $term_relationships;
+	public $term_relationships;
 
 	/**
 	 * WordPress Term Taxonomy table
@@ -314,7 +183,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $term_taxonomy;
+	public $term_taxonomy;
 
 	/*
 	 * Global and Multisite tables
@@ -327,7 +196,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $usermeta;
+	public $usermeta;
 
 	/**
 	 * WordPress Users table
@@ -336,7 +205,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $users;
+	public $users;
 
 	/**
 	 * Multisite Blogs table
@@ -345,7 +214,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $blogs;
+	public $blogs;
 
 	/**
 	 * Multisite Blog Versions table
@@ -354,7 +223,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $blog_versions;
+	public $blog_versions;
 
 	/**
 	 * Multisite Registration Log table
@@ -363,7 +232,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $registration_log;
+	public $registration_log;
 
 	/**
 	 * Multisite Signups table
@@ -372,7 +241,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $signups;
+	public $signups;
 
 	/**
 	 * Multisite Sites table
@@ -381,7 +250,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $site;
+	public $site;
 
 	/**
 	 * Multisite Sitewide Terms table
@@ -390,7 +259,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $sitecategories;
+	public $sitecategories;
 
 	/**
 	 * Multisite Site Metadata table
@@ -399,7 +268,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $sitemeta;
+	public $sitemeta;
 
 	/**
 	 * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load.
@@ -414,7 +283,7 @@
 	 * @access public
 	 * @var array
 	 */
-	var $field_types = array();
+	public $field_types = array();
 
 	/**
 	 * Database table columns charset
@@ -423,7 +292,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $charset;
+	public $charset;
 
 	/**
 	 * Database table columns collate
@@ -432,7 +301,7 @@
 	 * @access public
 	 * @var string
 	 */
-	var $collate;
+	public $collate;
 
 	/**
 	 * Whether to use mysql_real_escape_string
@@ -441,51 +310,251 @@
 	 * @access public
 	 * @var bool
 	 */
-	var $real_escape = false;
+	public $real_escape = false;
 
 	/**
-	 * Database Username
+	 * A textual description of the last query/get_row/get_var call
 	 *
-	 * @since 2.9.0
-	 * @access private
+	 * @since 3.0.0
+	 * @access public
 	 * @var string
 	 */
-	var $dbuser;
+	public $func_call;
+}
 
+/**
+ * WordPress Database Access Abstraction Object Base Class
+ *
+ * It is possible to replace the database abstraction by 
+ * extending this abtract class.
+ *
+ * @link http://core.trac.wordpress.org/ticket/3354
+ *
+ * @package WordPress
+ * @subpackage Database
+ * @since 3.2
+ */
+abstract class WP_Db_Base extends WP_Db_Variables implements WP_Db_Methods {
+	public function __construct() {
+		register_shutdown_function( array( $this, '__destruct' ) );
+	}
+
 	/**
-	 * A textual description of the last query/get_row/get_var call
+	 * Destructor that will run when database object is destroyed.
 	 *
-	 * @since 3.0.0
-	 * @access public
+	 * Callback of register_shutdown_function
+	 *
+	 * @see __construct()
+	 * @since 2.0.8
+	 * @return bool true
+	 */
+	public function __destruct() {
+		return true;
+	}
+}
+
+
+/**
+ * WordPress Database Access Abstraction Object
+ *
+ * It is possible to replace this class with your own
+ * by setting the $wpdb global variable in wp-content/db.php
+ * file with your class.
+ * 
+ * You can not name it wpdb, because this file will be included.
+ *
+ * @link http://codex.wordpress.org/Function_Reference/wpdb_Class
+ *
+ * @package WordPress
+ * @subpackage Database
+ * @since 0.71
+ */
+class wpdb extends WP_Db_Base {
+
+	/**
+	 * Whether to show SQL/DB errors
+	 *
+	 * @since 0.71
+	 * @access private
+	 * @var bool
+	 */
+	private $show_errors = false;
+
+	/**
+	 * Whether to suppress errors during the DB bootstrapping.
+	 *
+	 * @access private
+	 * @since 2.5.0
+	 * @var bool
+	 */
+	private $suppress_errors = false;
+
+	/**
+	 * The last error during query.
+	 *
+	 * @see get_last_error()
+	 * @since 2.5.0
+	 * @access private
 	 * @var string
 	 */
-	var $func_call;
+	private $last_error = '';
 
 	/**
-	 * Connects to the database server and selects a database
+	 * Amount of queries made
 	 *
-	 * PHP4 compatibility layer for calling the PHP5 constructor.
+	 * @since 1.2.0
+	 * @access private
+	 * @var int
+	 */
+	private $num_queries = 0;
+
+	/**
+	 * Count of rows returned by previous query
 	 *
-	 * @uses wpdb::__construct() Passes parameters and returns result
+	 * @since 1.2.0
+	 * @access private
+	 * @var int
+	 */
+	private $num_rows = 0;
+
+	/**
+	 * Count of affected rows by previous query
+	 *
 	 * @since 0.71
+	 * @access private
+	 * @var int
+	 */
+	private $rows_affected = 0;
+
+	/**
+	 * Saved result of the last query made
 	 *
-	 * @param string $dbuser MySQL database user
-	 * @param string $dbpassword MySQL database password
-	 * @param string $dbname MySQL database name
-	 * @param string $dbhost MySQL database host
+	 * @since 1.2.0
+	 * @access private
+	 * @var array
 	 */
-	function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) {
-		return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost );
-	}
+	private $last_query;
 
 	/**
+	 * Results of the last query made
+	 *
+	 * @since 1.0.0
+	 * @access private
+	 * @var array|null
+	 */
+	private $last_result;
+
+	/**
+	 * Saved info on the table column
+	 *
+	 * @since 1.2.0
+	 * @access private
+	 * @var array
+	 */
+	private $col_info;
+
+	/**
+	 * Saved queries that were executed
+	 *
+	 * @since 1.5.0
+	 * @access private
+	 * @var array
+	 */
+	private $queries;
+
+	/**
+	 * Timer start value, for debugging purposes.
+	 *
+	 * @since 1.5.0
+	 * @access private
+	 * @var float
+	 * @see timer_stop()
+	 * @see timer_start()
+	 */
+	private $time_start;
+
+	/**
+	 * WordPress table prefix
+	 *
+	 * You can set this to have multiple WordPress installations
+	 * in a single database. The second reason is for possible
+	 * security precautions.
+	 *
+	 * @since 0.71
+	 * @access private
+	 * @var string
+	 */
+	private $prefix = '';
+
+	/**
+	 * Whether the database queries are ready to start executing.
+	 *
+	 * @since 2.5.0
+	 * @access private
+	 * @var bool
+	 */
+	private $ready = false;
+
+	/**
+	 * List of WordPress per-blog tables
+	 *
+	 * @since 2.5.0
+	 * @access private
+	 * @see wpdb::tables()
+	 * @var array
+	 */
+	private $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta',
+		'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' );
+
+	/**
+	 * List of deprecated WordPress tables
+	 *
+	 * categories, post2cat, and link2cat were deprecated in 2.3.0, db version 5539
+	 *
+	 * @since 2.9.0
+	 * @access private
+	 * @see wpdb::tables()
+	 * @var array
+	 */
+	private $old_tables = array( 'categories', 'post2cat', 'link2cat' );
+
+	/**
+	 * List of WordPress global tables
+	 *
+	 * @since 3.0.0
+	 * @access private
+	 * @see wpdb::tables()
+	 * @var array
+	 */
+	private $global_tables = array( 'users', 'usermeta' );
+
+	/**
+	 * List of Multisite global tables
+	 *
+	 * @since 3.0.0
+	 * @access private
+	 * @see wpdb::tables()
+	 * @var array
+	 */
+	private $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
+		'sitecategories', 'registration_log', 'blog_versions' );
+
+	/**
+	 * Database Username
+	 *
+	 * @since 2.9.0
+	 * @access private
+	 * @var string
+	 */
+	private $dbuser;
+
+	/**
 	 * Connects to the database server and selects a database
 	 *
-	 * PHP5 style constructor for compatibility with PHP5. Does
+	 * Does
 	 * the actual setting up of the class properties and connection
 	 * to the database.
 	 *
-	 * @link http://core.trac.wordpress.org/ticket/3354
 	 * @since 2.0.8
 	 *
 	 * @param string $dbuser MySQL database user
@@ -493,12 +562,9 @@
 	 * @param string $dbname MySQL database name
 	 * @param string $dbhost MySQL database host
 	 */
-	function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
-		register_shutdown_function( array( &$this, '__destruct' ) );
+	public function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
+		parent::__construct();
 
-		if ( WP_DEBUG )
-			$this->show_errors();
-
 		$this->init_charset();
 
 		$this->dbuser = $dbuser;
@@ -510,22 +576,11 @@
 	}
 
 	/**
-	 * PHP5 style destructor and will run when database object is destroyed.
-	 *
-	 * @see wpdb::__construct()
-	 * @since 2.0.8
-	 * @return bool true
-	 */
-	function __destruct() {
-		return true;
-	}
-
-	/**
 	 * Set $this->charset and $this->collate
 	 *
 	 * @since 3.1.0
 	 */
-	function init_charset() {
+	private function init_charset() {
 		if ( function_exists('is_multisite') && is_multisite() ) {
 			$this->charset = 'utf8';
 			if ( defined( 'DB_COLLATE' ) && DB_COLLATE )
@@ -549,7 +604,7 @@
 	 * @param string   $charset The character set (optional)
 	 * @param string   $collate The collation (optional)
 	 */
-	function set_charset($dbh, $charset = null, $collate = null) {
+	private function set_charset($dbh, $charset = null, $collate = null) {
 		if ( !isset($charset) )
 			$charset = $this->charset;
 		if ( !isset($collate) )
@@ -575,7 +630,7 @@
 	 * @param string $prefix Alphanumeric name for the new prefix.
 	 * @return string|WP_Error Old prefix or WP_Error on error
 	 */
-	function set_prefix( $prefix, $set_table_names = true ) {
+	public function set_prefix( $prefix, $set_table_names = true ) {
 
 		if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
 			return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
@@ -614,7 +669,7 @@
 	 * @param int $site_id Optional.
 	 * @return string previous blog id
 	 */
-	function set_blog_id( $blog_id, $site_id = 0 ) {
+	public function set_blog_id( $blog_id, $site_id = 0 ) {
 		if ( ! empty( $site_id ) )
 			$this->siteid = $site_id;
 
@@ -640,7 +695,7 @@
 	 * @param int $blog_id Optional.
 	 * @return string Blog prefix.
 	 */
-	function get_blog_prefix( $blog_id = null ) {
+	public function get_blog_prefix( $blog_id = null ) {
 		if ( is_multisite() ) {
 			if ( null === $blog_id )
 				$blog_id = $this->blogid;
@@ -681,7 +736,7 @@
 	 * @param int $blog_id Optional. The blog_id to prefix. Defaults to wpdb::$blogid. Used only when prefix is requested.
 	 * @return array Table names. When a prefix is requested, the key is the unprefixed table name.
 	 */
-	function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
+	public function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
 		switch ( $scope ) {
 			case 'all' :
 				$tables = array_merge( $this->global_tables, $this->tables );
@@ -743,7 +798,7 @@
 	 * @param resource $dbh Optional link identifier.
 	 * @return null Always null.
 	 */
-	function select( $db, $dbh = null) {
+	private function select( $db, $dbh = null) {
 		if ( is_null($dbh) )
 			$dbh = $this->dbh;
 
@@ -771,7 +826,7 @@
 	 * @param string $string
 	 * @return string
 	 */
-	function _weak_escape( $string ) {
+	private function _weak_escape( $string ) {
 		return addslashes( $string );
 	}
 
@@ -786,7 +841,7 @@
 	 * @param  string $string to escape
 	 * @return string escaped
 	 */
-	function _real_escape( $string ) {
+	private function _real_escape( $string ) {
 		if ( $this->dbh && $this->real_escape )
 			return mysql_real_escape_string( $string, $this->dbh );
 		else
@@ -804,7 +859,7 @@
 	 * @param  string|array $data
 	 * @return string|array escaped
 	 */
-	function _escape( $data ) {
+	private function _escape( $data ) {
 		if ( is_array( $data ) ) {
 			foreach ( (array) $data as $k => $v ) {
 				if ( is_array($v) )
@@ -828,7 +883,7 @@
 	 * @param string|array $data to escape
 	 * @return string|array escaped as query safe string
 	 */
-	function escape( $data ) {
+	public function escape( $data ) {
 		if ( is_array( $data ) ) {
 			foreach ( (array) $data as $k => $v ) {
 				if ( is_array( $v ) )
@@ -851,7 +906,7 @@
 	 * @param string $string to escape
 	 * @return void
 	 */
-	function escape_by_ref( &$string ) {
+	public function escape_by_ref( &$string ) {
 		$string = $this->_real_escape( $string );
 	}
 
@@ -891,7 +946,7 @@
 	 * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string
 	 * 	if there was something to prepare
 	 */
-	function prepare( $query = null ) { // ( $query, *$args )
+	public function prepare( $query = null ) { // ( $query, *$args )
 		if ( is_null( $query ) )
 			return;
 
@@ -916,7 +971,7 @@
 	 * @param string $str The error to display
 	 * @return bool False if the showing of errors is disabled.
 	 */
-	function print_error( $str = '' ) {
+	private function print_error( $str = '' ) {
 		global $EZSQL_ERROR;
 
 		if ( !$str )
@@ -973,7 +1028,7 @@
 	 * @param bool $show Whether to show or hide errors
 	 * @return bool Old value for showing errors.
 	 */
-	function show_errors( $show = true ) {
+	public function show_errors( $show = true ) {
 		$errors = $this->show_errors;
 		$this->show_errors = $show;
 		return $errors;
@@ -989,7 +1044,7 @@
 	 *
 	 * @return bool Whether showing of errors was active
 	 */
-	function hide_errors() {
+	public function hide_errors() {
 		$show = $this->show_errors;
 		$this->show_errors = false;
 		return $show;
@@ -1006,7 +1061,7 @@
 	 * @param bool $suppress Optional. New value. Defaults to true.
 	 * @return bool Old value
 	 */
-	function suppress_errors( $suppress = true ) {
+	public function suppress_errors( $suppress = true ) {
 		$errors = $this->suppress_errors;
 		$this->suppress_errors = (bool) $suppress;
 		return $errors;
@@ -1018,7 +1073,7 @@
 	 * @since 0.71
 	 * @return void
 	 */
-	function flush() {
+	private function flush() {
 		$this->last_result = array();
 		$this->col_info    = null;
 		$this->last_query  = null;
@@ -1029,9 +1084,7 @@
 	 *
 	 * @since 3.0.0
 	 */
-	function db_connect() {
-		global $db_list, $global_db_list;
-
+	private function db_connect() {
 		if ( WP_DEBUG ) {
 			$this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
 		} else {
@@ -1075,7 +1128,7 @@
 	 * @param string $query Database query
 	 * @return int|false Number of rows affected/selected or false on error
 	 */
-	function query( $query ) {
+	public function query( $query ) {
 		if ( ! $this->ready )
 			return false;
 
@@ -1157,7 +1210,7 @@
 	 * 	A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
 	 * @return int|false The number of rows inserted, or false on error.
 	 */
-	function insert( $table, $data, $format = null ) {
+	public function insert( $table, $data, $format = null ) {
 		return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
 	}
 
@@ -1180,7 +1233,7 @@
 	 * 	A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
 	 * @return int|false The number of rows affected, or false on error.
 	 */
-	function replace( $table, $data, $format = null ) {
+	public function replace( $table, $data, $format = null ) {
 		return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
 	}
 
@@ -1201,7 +1254,7 @@
 	 * 	A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
 	 * @return int|false The number of rows affected, or false on error.
 	 */
-	function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
+	private function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
 		if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
 			return false;
 		$formats = $format = (array) $format;
@@ -1241,7 +1294,7 @@
 	 * @param array|string $format_where Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where.  A format is one of '%d', '%s' (decimal number, string).  If omitted, all values in $where will be treated as strings.
 	 * @return int|false The number of rows updated, or false on error.
 	 */
-	function update( $table, $data, $where, $format = null, $where_format = null ) {
+	public function update( $table, $data, $where, $format = null, $where_format = null ) {
 		if ( ! is_array( $data ) || ! is_array( $where ) )
 			return false;
 
@@ -1282,22 +1335,22 @@
 	 * @since 0.71
 	 *
 	 * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query.
-	 * @param int $x Optional. Column of value to return.  Indexed from 0.
-	 * @param int $y Optional. Row of value to return.  Indexed from 0.
+	 * @param int $column Optional. Column of value to return.  Indexed from 0.
+	 * @param int $row Optional. Row of value to return.  Indexed from 0.
 	 * @return string|null Database query result (as string), or null on failure
 	 */
-	function get_var( $query = null, $x = 0, $y = 0 ) {
-		$this->func_call = "\$db->get_var(\"$query\", $x, $y)";
+	public function get_var( $query = null, $column = 0, $row = 0 ) {
+		$this->func_call = "\$db->get_var(\"$query\", $column, $row)";
 		if ( $query )
 			$this->query( $query );
 
 		// Extract var out of cached results based x,y vals
-		if ( !empty( $this->last_result[$y] ) ) {
-			$values = array_values( get_object_vars( $this->last_result[$y] ) );
+		if ( !empty( $this->last_result[$row] ) ) {
+			$values = array_values( get_object_vars( $this->last_result[$row] ) );
 		}
 
 		// If there is a value return it else return null
-		return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null;
+		return ( isset( $values[$column] ) && $values[$column] !== '' ) ? $values[$column] : null;
 	}
 
 	/**
@@ -1310,25 +1363,25 @@
 	 * @param string|null $query SQL query.
 	 * @param string $output Optional. one of ARRAY_A | ARRAY_N | OBJECT constants. Return an associative array (column => value, ...),
 	 * 	a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively.
-	 * @param int $y Optional. Row to return. Indexed from 0.
+	 * @param int $row Optional. Row to return. Indexed from 0.
 	 * @return mixed Database query result in format specifed by $output or null on failure
 	 */
-	function get_row( $query = null, $output = OBJECT, $y = 0 ) {
-		$this->func_call = "\$db->get_row(\"$query\",$output,$y)";
+	public function get_row( $query = null, $output = null, $row = 0 ) {
+		$this->func_call = "\$db->get_row(\"$query\",$output,$row)";
 		if ( $query )
 			$this->query( $query );
 		else
 			return null;
 
-		if ( !isset( $this->last_result[$y] ) )
+		if ( !isset( $this->last_result[$row] ) )
 			return null;
 
-		if ( $output == OBJECT ) {
-			return $this->last_result[$y] ? $this->last_result[$y] : null;
+		if ( null === $output || $output == OBJECT ) {
+			return $this->last_result[$row] ? $this->last_result[$row] : null;
 		} elseif ( $output == ARRAY_A ) {
-			return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null;
+			return $this->last_result[$row] ? get_object_vars( $this->last_result[$row] ) : null;
 		} elseif ( $output == ARRAY_N ) {
-			return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null;
+			return $this->last_result[$row] ? array_values( get_object_vars( $this->last_result[$row] ) ) : null;
 		} else {
 			$this->print_error(/*WP_I18N_DB_GETROW_ERROR*/" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"/*/WP_I18N_DB_GETROW_ERROR*/);
 		}
@@ -1344,17 +1397,17 @@
 	 * @since 0.71
 	 *
 	 * @param string|null $query Optional. SQL query. Defaults to previous query.
-	 * @param int $x Optional. Column to return. Indexed from 0.
+	 * @param int $column Optional. Column to return. Indexed from 0.
 	 * @return array Database query result. Array indexed from 0 by SQL result row number.
 	 */
-	function get_col( $query = null , $x = 0 ) {
+	public function get_col( $query = null , $column = 0 ) {
 		if ( $query )
 			$this->query( $query );
 
 		$new_array = array();
 		// Extract the column values
 		for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
-			$new_array[$i] = $this->get_var( null, $x, $i );
+			$new_array[$i] = $this->get_var( null, $column, $i );
 		}
 		return $new_array;
 	}
@@ -1372,7 +1425,7 @@
 	 * 	With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value.  Duplicate keys are discarded.
 	 * @return mixed Database query results
 	 */
-	function get_results( $query = null, $output = OBJECT ) {
+	public function get_results( $query = null, $output = null ) {
 		$this->func_call = "\$db->get_results(\"$query\", $output)";
 
 		if ( $query )
@@ -1381,7 +1434,7 @@
 			return null;
 
 		$new_array = array();
-		if ( $output == OBJECT ) {
+		if ( $output === null || $output == OBJECT ) {
 			// Return an integer-keyed array of row objects
 			return $this->last_result;
 		} elseif ( $output == OBJECT_K ) {
@@ -1420,9 +1473,9 @@
 	 * @param int $col_offset Optional. 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type
 	 * @return mixed Column Results
 	 */
-	function get_col_info( $info_type = 'name', $col_offset = -1 ) {
+	public function get_col_info( $info_type = 'name', $col_offset = null ) {
 		if ( $this->col_info ) {
-			if ( $col_offset == -1 ) {
+			if ( $col_offset === null ) {
 				$i = 0;
 				$new_array = array();
 				foreach( (array) $this->col_info as $col ) {
@@ -1443,9 +1496,8 @@
 	 *
 	 * @return true
 	 */
-	function timer_start() {
-		$mtime            = explode( ' ', microtime() );
-		$this->time_start = $mtime[1] + $mtime[0];
+	private function timer_start() {
+		$this->time_start = microtime( true );
 		return true;
 	}
 
@@ -1456,11 +1508,9 @@
 	 *
 	 * @return int Total time spent on the query, in milliseconds
 	 */
-	function timer_stop() {
-		$mtime      = explode( ' ', microtime() );
-		$time_end   = $mtime[1] + $mtime[0];
-		$time_total = $time_end - $this->time_start;
-		return $time_total;
+	private function timer_stop() {
+		$time_total = microtime( true ) - $this->time_start;
+		return (int) $time_total;
 	}
 
 	/**
@@ -1474,7 +1524,7 @@
 	 * @param string $error_code Optional. A Computer readable string to identify the error.
 	 * @return false|void
 	 */
-	function bail( $message, $error_code = '500' ) {
+	private function bail( $message, $error_code = '500' ) {
 		if ( !$this->show_errors ) {
 			if ( class_exists( 'WP_Error' ) )
 				$this->error = new WP_Error($error_code, $message);
@@ -1494,7 +1544,7 @@
 	 *
 	 * @return WP_Error
 	 */
-	function check_database_version() {
+	public function check_database_version() {
 		global $wp_version, $required_mysql_version;
 		// Make sure the server has the required MySQL version
 		if ( version_compare($this->db_version(), $required_mysql_version, '<') )
@@ -1502,31 +1552,18 @@
 	}
 
 	/**
-	 * Whether the database supports collation.
-	 *
-	 * Called when WordPress is generating the table scheme.
-	 *
-	 * @since 2.5.0
-	 *
-	 * @return bool True if collation is supported, false if version does not
-	 */
-	function supports_collation() {
-		return $this->has_cap( 'collation' );
-	}
-
-	/**
 	 * Determine if a database supports a particular feature
 	 *
 	 * @since 2.7.0
 	 * @see   wpdb::db_version()
 	 *
-	 * @param string $db_cap the feature
+	 * @param string $feature the database capabilitiy feature
 	 * @return bool
 	 */
-	function has_cap( $db_cap ) {
+	public function has_cap( $feature ) {
 		$version = $this->db_version();
 
-		switch ( strtolower( $db_cap ) ) {
+		switch ( strtolower( $feature ) ) {
 			case 'collation' :    // @since 2.5.0
 			case 'group_concat' : // @since 2.7
 			case 'subqueries' :   // @since 2.7
@@ -1548,7 +1585,7 @@
 	 *
 	 * @return string The name of the calling function
 	 */
-	function get_caller() {
+	private function get_caller() {
 		$trace  = array_reverse( debug_backtrace() );
 		$caller = array();
 
@@ -1568,7 +1605,7 @@
 	 *
 	 * @return false|string false on failure, version number on success
 	 */
-	function db_version() {
+	public function db_version() {
 		return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
 	}
 }
