DbBase.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * @author: 郭永磊
  4. * @description: 数据库接口基类
  5. * @create date: 2015-8-13
  6. */
  7. class DbBase{
  8. public $server;
  9. public $db;
  10. public $usedPrefix = false;
  11. private $_defaultServer;
  12. private $_defaultDb;
  13. private $_conn;
  14. private $_useDefaultServer = false;
  15. private $_queryString;
  16. public $dbTable = array();
  17. public $mappingName = "default"; //默认映射
  18. function __construct(){
  19. $this->_defaultServer = Yii::app()->params["default_server"];
  20. $this->_defaultDb = Yii::app()->params["default_db"];
  21. $this->_resetServer();
  22. $this->dbTable["default"] = " school_id = ".$this->_getSchoolId();
  23. $params = func_get_args();
  24. if($params){
  25. if(!empty($params[0]) && isset($params[1])){
  26. $this->db["name"] = $params[0];
  27. $this->_useDefaultServer = true;
  28. if(!empty($params[1]))
  29. $this->db["t_pre"] = $params[1];
  30. }
  31. else if(!empty($params[0]))
  32. $this->mappingName = $params[0];
  33. }
  34. else if($this->_getSchoolId() === false){
  35. $this->_useDefaultServer = true;
  36. }
  37. $this->_condition = $this->dbTable[$this->mappingName];
  38. }
  39. private function _getSchoolId(){
  40. return !empty(Yii::app()->session["school_id"]) ? Yii::app()->session["school_id"] : false;
  41. }
  42. private function _resetServer(){
  43. $this->server = $this->_defaultServer;
  44. $this->db = $this->_defaultDb;
  45. }
  46. private function _connect(){
  47. $dsn = "mysql:host={$this->server["addr"]};dbname={$this->db["name"]}";
  48. $this->_conn = new PDO($dsn, $this->server["username"], $this->server["password"]);
  49. }
  50. private function _connTrigger(){
  51. if(!is_object($this->_conn))
  52. $this->connect();
  53. }
  54. private function _connectTarget(){
  55. $this->_resetServer();
  56. $this->_connect();
  57. $sql = "select * from `{$this->_defaultDb["t_pre"]}{$this->_defaultDb["t_servers"]}`
  58. where{$this->_condition}";
  59. $rs = $this->getRow($sql);
  60. if($rs){
  61. $this->server = array(
  62. "addr" => $rs["database_host"],
  63. "username" => $rs["database_user"],
  64. "password" => $rs["database_password"],
  65. );
  66. $this->db = array(
  67. "name" => $rs["database_name"],
  68. "t_pre" => isset($rs["t_pre"]) ? $rs["t_pre"] : "",
  69. );
  70. $this->_connect();
  71. }
  72. }
  73. public function connect(){
  74. if($this->_useDefaultServer)
  75. $this->_connect();
  76. else
  77. $this->_connectTarget();
  78. }
  79. private function _getSqlType($sql){
  80. $arr = explode(" ", trim(str_replace(" ", " ", $sql)), 2);
  81. return strtolower($arr[0]);
  82. }
  83. private function _limit($offset, $limit){
  84. return $limit > 0 ? " limit {$offset}, {$limit}" : "";
  85. }
  86. private function _query($sql, $fetch = null, $offset = 0, $limit = -1){
  87. $this->_connTrigger();
  88. if(is_object($this->_conn) && $fetch){
  89. $sqlType = $this->_getSqlType($sql);
  90. if($this->usedPrefix)
  91. $sql = preg_replace("/{{(.*?)}}/", "{$this->db["t_pre"]}$1", $sql);
  92. if($sqlType === "select" || $sqlType === "show"){
  93. if($fetch === "ALL" && $limit > 0){
  94. $sql = preg_replace("/\s*limit[^\)]*$/i", "", $sql);
  95. $sql .= $this->_limit($offset, $limit);
  96. }
  97. $handle = $this->_conn->prepare($sql);
  98. $handle->execute();
  99. switch($fetch){
  100. case "ALL":
  101. $rs = $handle->fetchAll(PDO::FETCH_ASSOC);
  102. break;
  103. case "ROW":
  104. $rs = $handle->fetch(PDO::FETCH_ASSOC);
  105. break;
  106. case "COUNT":
  107. $rs = $handle->rowCount();
  108. break;
  109. case "HANDLE":
  110. $rs = $handle;
  111. break;
  112. default:
  113. $rs = false;
  114. break;
  115. }
  116. }
  117. else
  118. $rs = $this->_conn->exec($sql);
  119. return $rs;
  120. }
  121. return false;
  122. }
  123. public function getConn(){
  124. $this->_connTrigger();
  125. return $this->_conn;
  126. }
  127. public function exec($sql){
  128. $this->_connTrigger();
  129. return $this->_conn->exec($sql);
  130. }
  131. public function getHandle($sql){
  132. return $this->_query($sql, "HANDLE");
  133. }
  134. public function getRs($sql, $offset = 0, $limit = -1){
  135. return $this->_query($sql, "ALL", $offset, $limit);
  136. }
  137. public function getRow($sql){
  138. return $this->_query($sql, "ROW");
  139. }
  140. public function countRs($sql){
  141. return $this->_query($sql, "COUNT");
  142. }
  143. public function insert($tableName, $data){
  144. if($tableName && $data){
  145. $dimension = Arr::countDimension($data);
  146. if($dimension === 1){
  147. $fields = "";
  148. $values = "";
  149. foreach($data as $field => $val){
  150. $fields .= ",`{$field}`";
  151. $values .= ",'{$val}'";
  152. }
  153. $fields = substr($fields, 1);
  154. $values = substr($values, 1);
  155. $sql = "insert into `{$tableName}`({$fields}) values({$values})";
  156. }
  157. else if($dimension === 2){
  158. }
  159. return $this->exec($sql);
  160. }
  161. return false;
  162. }
  163. }