slave.db.class.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->classs('db');
  8. class SlaveDb extends DB {
  9. private $weight;
  10. private $slavequery = 0;
  11. private $slaveid = null;
  12. public function prepare($sql) {
  13. $this->init_connect($sql);
  14. return parent::prepare($sql);
  15. }
  16. public function query($sql, $params = array()) {
  17. $starttime = microtime();
  18. if (!empty($params)) {
  19. return parent::query($sql, $params);
  20. }
  21. $this->init_connect($sql);
  22. $result = $this->pdo->exec($sql);
  23. if(PDO_DEBUG) {
  24. $info = array();
  25. $info['sql'] = $sql;
  26. $info['error'] = $this->pdo->errorInfo();
  27. $this->debug(false, $info);
  28. }
  29. return $result;
  30. }
  31. public function slave_connect() {
  32. $this->slave_choose();
  33. if($this->slaveid) {
  34. if(!isset($this->link[$this->slaveid])) {
  35. $this->connect($this->slaveid);
  36. }
  37. $this->slavequery ++;
  38. $this->pdo = $this->link[$this->slaveid];
  39. }
  40. return true;
  41. }
  42. protected function slave_choose(){
  43. if(!isset($this->weight)) {
  44. foreach ($this->cfg['slave'] as $key => $value) {
  45. $this->weight .= str_repeat($key, 1 + intval($value['weight']));
  46. }
  47. }
  48. $sid = $this->weight[mt_rand(0, strlen($this->weight) -1)];
  49. $this->slaveid = 'slave_' . $sid;
  50. if(!isset($this->cfg[$this->slaveid])) {
  51. $this->cfg[$this->slaveid] = $this->cfg['slave'][$sid];
  52. }
  53. }
  54. public function init_connect($sql) {
  55. if(!($this->cfg['slave_status'] == true && !empty($this->cfg['slave']))) {
  56. $this->master_connect();
  57. } else {
  58. $sql = trim($sql);
  59. $sql_lower = strtolower($sql);
  60. $slave_except = false;
  61. if(!strexists($sql_lower, 'where ')) {
  62. $tablename = substr($sql_lower, strpos($sql_lower, 'from ') + 5);
  63. } else {
  64. $tablename = substr($sql_lower, strpos($sql_lower, 'from ') + 5, strpos($sql_lower, ' where') - strpos($sql_lower, 'from ') - 5);
  65. }
  66. $tablename = trim($tablename, '`');
  67. $tablename = str_replace($this->tablepre, '', $tablename);
  68. if(!empty($this->cfg['common']['slave_except_table']) && in_array(strtolower($tablename), $this->cfg['common']['slave_except_table'])) {
  69. $slave_except = true;
  70. }
  71. if(!(!$slave_except && strtoupper(substr($sql, 0 , 6)) === 'SELECT' && $this->slave_connect())) {
  72. $this->master_connect();
  73. }
  74. }
  75. return true;
  76. }
  77. public function master_connect() {
  78. if(!isset($this->link['master'])) {
  79. $this->connect('master');
  80. }
  81. $this->pdo = $this->link['master'];
  82. }
  83. public function insertid() {
  84. $this->master_connect();
  85. return parent::insertid();
  86. }
  87. public function begin() {
  88. $this->master_connect();
  89. return parent::begin();
  90. }
  91. public function commit() {
  92. $this->master_connect();
  93. return parent::commit();
  94. }
  95. public function rollback() {
  96. $this->master_connect();
  97. return parent::rollback();
  98. }
  99. }