Db.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * @author: 郭永磊
  4. * @function description: 数据库接口
  5. * @create date: 2015-8-13
  6. */
  7. class Db{
  8. private $_conn;
  9. private $_defaultServer;
  10. private $_defaultDb;
  11. private $_useDefaultServer = false;
  12. private $_condition;
  13. public $server;
  14. public $db;
  15. public $dbTable = array();
  16. public $mappingName = "default"; //默认映射
  17. function __construct(){
  18. $this->_defaultServer = Yii::app()->params["default_server"];
  19. $this->_defaultDb = Yii::app()->params["default_db"];
  20. $this->_resetServer();
  21. $this->dbTable["default"] = " school_id = ".$this->_getSchoolId();
  22. $params = func_get_args();
  23. if($params){
  24. if(!empty($params[0]) && isset($params[1])){
  25. $this->db["name"] = $params[0];
  26. $this->_useDefaultServer = true;
  27. if(!empty($params[1]))
  28. $this->db["t_pre"] = $params[1];
  29. }
  30. else if(!empty($params[0]))
  31. $this->mappingName = $params[0];
  32. }
  33. else if($this->_getSchoolId() === false){
  34. $this->_useDefaultServer = true;
  35. }
  36. $this->_condition = $this->dbTable[$this->mappingName];
  37. }
  38. private function _getSchoolId(){
  39. return !empty(Yii::app()->session["school_id"]) ? Yii::app()->session["school_id"] : false;
  40. }
  41. //重置数据库服务器信息
  42. private function _resetServer(){
  43. $this->server = $this->_defaultServer;
  44. $this->db = $this->_defaultDb;
  45. }
  46. //连接任意服务器
  47. private function _connect(){
  48. $dsn = "mysql:host={$this->server["addr"]};dbname={$this->db["name"]}";
  49. $this->_conn = new CDbConnection($dsn, $this->server["username"], $this->server["password"]);
  50. $this->_conn->tablePrefix = $this->db["t_pre"];
  51. $this->_conn->active = true;
  52. }
  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 = current($this->_query($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. else
  73. $this->_conn->active = false;
  74. }
  75. //无差别数据库语句执行器
  76. private function _query($sql){
  77. if(is_object($this->_conn)){
  78. $sql_type = $this->getSqlType($sql);
  79. $commander = $this->_conn->createCommand($sql);
  80. return ($sql_type === "select" || $sql_type === "show") ? $commander->queryAll() : $commander->execute();
  81. }
  82. return false;
  83. }
  84. private function _connTrigger(){
  85. if(!is_object($this->_conn))
  86. $this->connect();
  87. }
  88. public function connect(){
  89. if($this->_useDefaultServer){
  90. $this->_connect();
  91. }
  92. else
  93. $this->_connectTarget();
  94. return $this->_conn;
  95. }
  96. //判断SQL语句类型
  97. public function getSqlType($sql){
  98. $arr = explode(" ", trim(str_replace(" ", " ", $sql)), 2);
  99. return strtolower($arr[0]);
  100. }
  101. public function doQuery($sql){
  102. $this->_connTrigger();
  103. return $this->_query($sql);
  104. }
  105. public function escape($value)
  106. {
  107. return str_replace(array("\\", "\0", "\n", "\r", "\x1a", "'", '"'), array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"'), $value);
  108. }
  109. public function createQuery($query = null){
  110. $this->_connTrigger();
  111. return $this->_conn->createCommand($query);
  112. }
  113. public function UUID($replace = FALSE)
  114. {
  115. $this->_connTrigger();
  116. $query = $this->_conn->createCommand("SELECT UUID() AS UUID")->query()->read();
  117. $result = $query['UUID'];
  118. if ($replace)
  119. {
  120. $result = str_replace('-', '', $result);
  121. }
  122. return $result;
  123. }
  124. public function UUID_SHORT($length = 20)
  125. {
  126. $this->_connTrigger();
  127. $query = $this->createQuery("SELECT UUID_SHORT() AS UUID_SHORT")->query()->read();
  128. $result = $query['UUID_SHORT'];
  129. if ($length AND is_int($length))
  130. {
  131. $result = substr($result, ($length * -1));
  132. }
  133. return $result;
  134. }
  135. public function beginTransaction()
  136. {
  137. $this->_connTrigger();
  138. return $this->_conn->beginTransaction();
  139. }
  140. public function insert($table, $data)
  141. {
  142. if ( ! empty($table) AND ! empty($data) AND is_array($data))
  143. {
  144. $sql = "INSERT INTO ". $table ." SET ";
  145. foreach ($data AS $field => $value)
  146. {
  147. $sql .= "`". $field ."` = '". $value ."', ";
  148. }
  149. $sql = substr($sql, 0, -2);
  150. $this->createQuery($sql)->query();
  151. }
  152. }
  153. public function update($table, $data, $condition = array())
  154. {
  155. if ( ! empty($table) AND ! empty($data) AND is_array($data))
  156. {
  157. $sql = "UPDATE ". $table ." SET ";
  158. foreach ($data AS $field => $value)
  159. {
  160. $sql .= "`". $field ."` = '". $value ."', ";
  161. }
  162. $sql = substr($sql, 0, -2);
  163. if ( ! empty($condition))
  164. {
  165. if (is_array($condition))
  166. {
  167. $sql .= " WHERE ";
  168. foreach ($condition AS $field => $value)
  169. {
  170. $sql .= "`". $field ."` = '". $value ."' AND ";
  171. }
  172. $sql = substr($sql, 0, -5);
  173. }
  174. else
  175. {
  176. $sql .= " WHERE ". $condition;
  177. }
  178. }
  179. $this->createQuery($sql)->query();
  180. }
  181. }
  182. public function getTotals($table, $condition)
  183. {
  184. if ( ! empty($table))
  185. {
  186. $sql = "SELECT COUNT(0) AS COUNT FROM ". $table ." ";
  187. if ( ! empty($condition))
  188. {
  189. if (is_array($condition))
  190. {
  191. $sql .= " WHERE ";
  192. foreach ($condition AS $field => $value)
  193. {
  194. $sql .= "`". $field ."` = '". $value ."' AND ";
  195. }
  196. $sql = substr($sql, 0, -5);
  197. }
  198. else
  199. {
  200. $sql .= " WHERE ". $condition;
  201. }
  202. }
  203. $result = $this->createQuery($sql)->query()->read();
  204. return (int)$result['COUNT'];
  205. }
  206. else
  207. {
  208. return FALSE;
  209. }
  210. }
  211. }