ConnBase.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Qii\Driver;
  3. /**
  4. * 数据库连接基类
  5. *
  6. * @author Jinhui Zhu<jinhui.zhu@live.cn> 2015-12-10 14:34
  7. */
  8. class ConnBase
  9. {
  10. const VERSION = '1.2';
  11. /**
  12. * @var array $allowExec 读写对应的数据库配置文件
  13. */
  14. public $allowExec = array("WRITE" => 'master', 'READ' => 'slave');
  15. /**
  16. * @var array $_dbInfo 数据库配置
  17. */
  18. protected $_dbInfo;
  19. /**
  20. * @var array $_connections 数据库链接
  21. */
  22. protected $_connections;
  23. /**
  24. * @var resource $_writeConnection 获取写的链接
  25. */
  26. protected static $_writeConnection = null;
  27. /**
  28. * @var resource$_readConnection 获取读的链接
  29. */
  30. protected static $_readConnection = null;
  31. /**
  32. * 获取数据库配置中指定的key的值,不指定则获取全部
  33. * @param string key 数据库配置中指定的key
  34. */
  35. public function getDBInfo($key = null)
  36. {
  37. if ($key != null) return isset($this->_dbInfo[$key]) ? $this->_dbInfo[$key] : false;
  38. return $this->_dbInfo;
  39. }
  40. /**
  41. * 通过sql语句判断是读还是写操作
  42. * @param string $sql 读/写的sql语句
  43. */
  44. protected function prepare($sql)
  45. {
  46. $default = "READ";
  47. //$readMode = "/^SELECT\s/u";
  48. $writeMode = "/^(UPDATE)|(REPLACE)|(DELETE)|(INSERT)\s/u";
  49. //$isRead = preg_match($readMode, $sql);
  50. $isWrite = preg_match($writeMode, $sql);
  51. if ($isWrite) $default = "WRITE";
  52. if (!isset($this->allowExec[$default])) $default = 'WRITE';
  53. return $default;
  54. }
  55. /**
  56. * 通过sql获取连接资源
  57. *
  58. * @param String $sql 通过sql语句获取读/写操作对应的res
  59. * @return res
  60. */
  61. public function getConnectionBySQL($sql)
  62. {
  63. $default = $this->prepare($sql);
  64. if (isset($this->_connections[$default])) return $this->_connections[$default];
  65. switch ($default) {
  66. case 'READ':
  67. $this->_connections['READ'] = $connection = $this->getReadConnection();
  68. break;
  69. default:
  70. $this->_connections['WRITE'] = $connection = $this->getWriteConnection();
  71. break;
  72. }
  73. if(get_called_class() == 'Qii\Driver\Pdo\Connection'){
  74. $connection->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
  75. $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  76. $connection->query('set names utf8mb4');
  77. }
  78. return $connection;
  79. }
  80. /**
  81. * 获取DBInfo 和 Salve
  82. *
  83. * @return array
  84. */
  85. protected function getDBInfoAndSalve() {
  86. $dbInfo = $this->_dbInfo['master'];
  87. $useSlave = false;
  88. if ($this->_dbInfo['readOrWriteSeparation'] && $this->_dbInfo['slave']) {
  89. $i = rand(0, count($this->_dbInfo['slave']) - 1);
  90. $dbInfo = $this->_dbInfo['slave'][$i];
  91. $useSlave = true;
  92. }
  93. return array($dbInfo, $useSlave);
  94. }
  95. }