[ Index ]

PHP Cross Reference of BackPress

title

Body

[close]

/includes/ -> class.bpdb-multi.php (source)

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


Generated: Sat Nov 23 01:00:54 2024 Cross-referenced by PHPXref 0.7.1