BPDB_Multi

The BuddyPress BPDB Multi class.

Defined (1)

The class is defined in the following location(s).

/bp-forums/bbpress/bb-includes/backpress/class.bpdb-multi.php  
  1. class BPDB_Multi extends BPDB { 
  2. /** 
  3. * Associative array (dbhname => dbh) for established mysql connections 
  4. * @var array 
  5. */ 
  6. var $dbhs = array(); 
  7.  
  8. var $_force_dbhname = false; 
  9. var $last_table = ''; 
  10. var $db_tables = array(); 
  11. var $db_servers = array(); 
  12.  
  13. // function BPDB_Multi() {} // Not used - rely on PHP4 constructor from BPDB to call BPDB_Multi::__construct 
  14.  
  15. function __construct() { 
  16. $args = func_get_args(); 
  17. $args = call_user_func_array( array(&$this, '_init'), $args ); 
  18.  
  19. if ( $args['host'] ) { 
  20. $this->db_servers['dbh_global'] = $args; 
  21. $this->db_connect( '/* */' ); 
  22.  
  23. /** 
  24. * Figure out which database server should handle the query, and connect to it. 
  25. * @param string query 
  26. * @return resource mysql database connection 
  27. */ 
  28. function &db_connect( $query = '' ) { 
  29. $false = false; 
  30. if ( empty( $query ) ) 
  31. return $false; 
  32.  
  33. $this->last_table = $table = $this->get_table_from_query( $query ); 
  34.  
  35. // We can attempt to force the connection identifier in use 
  36. if ( $this->_force_dbhname && isset($this->db_servers[$this->_force_dbhname]) ) 
  37. $dbhname = $this->_force_dbhname; 
  38.  
  39. if ( !isset($dbhname) ) { 
  40. if ( isset( $this->db_tables[$table] ) ) 
  41. $dbhname = $this->db_tables[$table]; 
  42. else 
  43. $dbhname = 'dbh_global'; 
  44.  
  45. if ( !isset($this->db_servers[$dbhname]) ) 
  46. return $false; 
  47.  
  48. if ( isset($this->dbhs[$dbhname]) && is_resource($this->dbhs[$dbhname]) ) // We're already connected! 
  49. return $this->dbhs[$dbhname]; 
  50.  
  51. $success = $this->db_connect_host( $this->db_servers[$dbhname] ); 
  52.  
  53. if ( $success && is_resource($this->dbh) ) { 
  54. $this->dbhs[$dbhname] =& $this->dbh; 
  55. } else { 
  56. unset($this->dbhs[$dbhname]); 
  57. unset($this->dbh); 
  58. return $false; 
  59.  
  60. return $this->dbhs[$dbhname]; 
  61.  
  62. /** 
  63. * Sets the prefix of the database tables 
  64. * @param string prefix 
  65. * @param false|array tables (optional: false) 
  66. * table identifiers are array keys 
  67. * array values 
  68. * empty: set prefix: array( 'posts' => false, 'users' => false, ... ) 
  69. * string: set to that array value: array( 'posts' => 'my_posts', 'users' => 'my_users' ) 
  70. * array: array[0] is DB identifier, array[1] is table name: array( 'posts' => array( 'global', 'my_posts' ), 'users' => array( 'users', 'my_users' ) ) 
  71. * OR array values (with numeric keys): array( 'posts', 'users', ... ) 
  72. * @return string the previous prefix (mostly only meaningful if all $table parameter was false) 
  73. */ 
  74. function set_prefix( $prefix, $tables = false ) { 
  75. $old_prefix = parent::set_prefix( $prefix, $tables ); 
  76. if ( !$old_prefix || is_wp_error($old_prefix) ) { 
  77. return $old_prefix; 
  78.  
  79. if ( $tables && is_array($tables) ) { 
  80. $_tables = $tables; 
  81. } else { 
  82. $_tables = $this->tables; 
  83.  
  84. foreach ( $_tables as $key => $value ) { 
  85. // array( 'posts' => array( 'global', 'my_posts' ), 'users' => array( 'users', 'my_users' ) ) 
  86. if ( is_array($value) && isset($this->db_servers['dbh_' . $value[0]]) ) { 
  87. $this->add_db_table( $value[0], $value[1] ); 
  88. $this->$key = $value[1]; 
  89.  
  90. return $old_prefix; 
  91.  
  92. /** 
  93. * Find the first table name referenced in a query 
  94. * @param string query 
  95. * @return string table 
  96. */ 
  97. function get_table_from_query ( $q ) { 
  98. // Remove characters that can legally trail the table name 
  99. rtrim($q, ';/-#'); 
  100.  
  101. // Quickly match most common queries 
  102. if ( preg_match('/^\s*(?:' 
  103. . 'SELECT.*?\s+FROM' 
  104. . '|INSERT(?:\s+IGNORE)?(?:\s+INTO)?' 
  105. . '|REPLACE(?:\s+INTO)?' 
  106. . '|UPDATE(?:\s+IGNORE)?' 
  107. . '|DELETE(?:\s+IGNORE)?(?:\s+FROM)?' 
  108. . ')\s+`?(\w+)`?/is', $q, $maybe) ) 
  109. return $maybe[1]; 
  110.  
  111. // Refer to the previous query 
  112. if ( preg_match('/^\s*SELECT.*?\s+FOUND_ROWS\(\)/is', $q) ) 
  113. return $this->last_table; 
  114.  
  115. // Big pattern for the rest of the table-related queries in MySQL 5.0 
  116. if ( preg_match('/^\s*(?:' 
  117. . '(?:EXPLAIN\s+(?:EXTENDED\s+)?)?SELECT.*?\s+FROM' 
  118. . '|INSERT(?:\s+LOW_PRIORITY|\s+DELAYED|\s+HIGH_PRIORITY)?(?:\s+IGNORE)?(?:\s+INTO)?' 
  119. . '|REPLACE(?:\s+LOW_PRIORITY|\s+DELAYED)?(?:\s+INTO)?' 
  120. . '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?' 
  121. . '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:\s+FROM)?' 
  122. . '|DESCRIBE|DESC|EXPLAIN|HANDLER' 
  123. . '|(?:LOCK|UNLOCK)\s+TABLE(?:S)?' 
  124. . '|(?:RENAME|OPTIMIZE|BACKUP|RESTORE|CHECK|CHECKSUM|ANALYZE|OPTIMIZE|REPAIR).*\s+TABLE' 
  125. . '|TRUNCATE(?:\s+TABLE)?' 
  126. . '|CREATE(?:\s+TEMPORARY)?\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?' 
  127. . '|ALTER(?:\s+IGNORE)?' 
  128. . '|DROP\s+TABLE(?:\s+IF\s+EXISTS)?' 
  129. . '|CREATE(?:\s+\w+)?\s+INDEX.*\s+ON' 
  130. . '|DROP\s+INDEX.*\s+ON' 
  131. . '|LOAD\s+DATA.*INFILE.*INTO\s+TABLE' 
  132. . '|(?:GRANT|REVOKE).*ON\s+TABLE' 
  133. . '|SHOW\s+(?:.*FROM|.*TABLE)' 
  134. . ')\s+`?(\w+)`?/is', $q, $maybe) ) 
  135. return $maybe[1]; 
  136.  
  137. // All unmatched queries automatically fall to the global master 
  138. return ''; 
  139.  
  140. /** 
  141. * Add a database server's information. Does not automatically connect. 
  142. * @param string $ds Dataset: the name of the dataset. 
  143. * @param array $args 
  144. * name => string DB name (required) 
  145. * user => string DB user (optional: false) 
  146. * password => string DB user password (optional: false) 
  147. * host => string DB hostname (optional: 'localhost') 
  148. * charset => string DB default charset. Used in a SET NAMES query. (optional) 
  149. * collate => string DB default collation. If charset supplied, optionally added to the SET NAMES query (optional) 
  150. */ 
  151. function add_db_server( $ds, $args = null ) { 
  152. $defaults = array( 
  153. 'user' => false,  
  154. 'password' => false,  
  155. 'name' => false,  
  156. 'host' => 'localhost',  
  157. 'charset' => false,  
  158. 'collate' => false 
  159. ); 
  160.  
  161. $args = wp_parse_args( $args, $defaults ); 
  162. $args['ds'] = 'dbh_' . $ds; 
  163.  
  164. $this->db_servers['dbh_' . $ds] = $args; 
  165.  
  166. /** 
  167. * Maps a table to a dataset. 
  168. * @param string $ds Dataset: the name of the dataset. 
  169. * @param string $table 
  170. */ 
  171. function add_db_table( $ds, $table ) { 
  172. $this->db_tables[$table] = 'dbh_' . $ds;