PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/source/class/db/db_driver_mysql_slave.php

https://github.com/jinbo51/DiscuzX
PHP | 93 lines | 70 code | 17 blank | 6 comment | 14 complexity | 90d9143f6dd7c692b61163df2eaa3680 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: db_driver_mysql_slave.php 31879 2012-10-18 09:27:24Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class db_driver_mysql_slave extends db_driver_mysql
  12. {
  13. public $slaveid = null;
  14. public $slavequery = 0;
  15. public $slaveexcept = false;
  16. public $excepttables = array();
  17. public $tablename = '';
  18. protected $_weighttable = array();
  19. public $serverid = null;
  20. function set_config($config) {
  21. parent::set_config($config);
  22. if($this->config['common']['slave_except_table']) {
  23. $this->excepttables = explode(',', str_replace(' ', '', $this->config['common']['slave_except_table']));
  24. }
  25. }
  26. public function table_name($tablename) {
  27. $this->tablename = $tablename;
  28. if(!$this->slaveexcept && $this->excepttables) {
  29. $this->slaveexcept = in_array($tablename, $this->excepttables, true);
  30. }
  31. $this->serverid = isset($this->map[$this->tablename]) ? $this->map[$this->tablename] : 1;
  32. return $this->tablepre.$tablename;
  33. }
  34. protected function _slave_connect() {
  35. if(!empty($this->config[$this->serverid]['slave'])) {
  36. $this->_choose_slave();
  37. if($this->slaveid) {
  38. if(!isset($this->link[$this->slaveid])) {
  39. $this->connect($this->slaveid);
  40. }
  41. $this->slavequery ++;
  42. $this->curlink = $this->link[$this->slaveid];
  43. }
  44. return true;
  45. } else {
  46. return false;
  47. }
  48. }
  49. protected function _choose_slave(){
  50. if(!isset($this->_weighttable[$this->serverid])) {
  51. foreach ($this->config[$this->serverid]['slave'] as $key => $value) {
  52. $this->_weighttable[$this->serverid] .= str_repeat($key, 1 + intval($value['weight']));
  53. }
  54. }
  55. $sid = $this->_weighttable[$this->serverid][mt_rand(0, strlen($this->_weighttable[$this->serverid]) -1)];
  56. $this->slaveid = $this->serverid.'_'.$sid;
  57. if(!isset($this->config[$this->slaveid])) {
  58. $this->config[$this->slaveid] = $this->config[$this->serverid]['slave'][$sid];
  59. }
  60. }
  61. protected function _master_connect() {
  62. if(!$this->link[$this->serverid]) {
  63. $this->connect($this->serverid);
  64. }
  65. $this->curlink = $this->link[$this->serverid];
  66. }
  67. public function query($sql, $silent = false, $unbuffered = false) {
  68. if(!(!$this->slaveexcept && strtoupper(substr($sql, 0 , 6)) === 'SELECT' && $this->_slave_connect())) {
  69. $this->_master_connect();
  70. }
  71. $this->tablename = '';
  72. $this->slaveexcept = false;
  73. return parent::query($sql, $silent, $unbuffered);
  74. }
  75. }
  76. ?>