db_driver_mysqli.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_mysqli.php 36278 2016-12-09 07:52:35Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class db_driver_mysqli
  12. {
  13. var $tablepre;
  14. var $version = '';
  15. var $drivertype = 'mysqli';
  16. var $querynum = 0;
  17. var $slaveid = 0;
  18. var $curlink;
  19. var $link = array();
  20. var $config = array();
  21. var $sqldebug = array();
  22. var $map = array();
  23. function db_mysql($config = array()) {
  24. if(!empty($config)) {
  25. $this->set_config($config);
  26. }
  27. }
  28. function set_config($config) {
  29. $this->config = &$config;
  30. $this->tablepre = $config['1']['tablepre'];
  31. if(!empty($this->config['map'])) {
  32. $this->map = $this->config['map'];
  33. for($i = 1; $i <= 100; $i++) {
  34. if(isset($this->map['forum_thread'])) {
  35. $this->map['forum_thread_'.$i] = $this->map['forum_thread'];
  36. }
  37. if(isset($this->map['forum_post'])) {
  38. $this->map['forum_post_'.$i] = $this->map['forum_post'];
  39. }
  40. if(isset($this->map['forum_attachment']) && $i <= 10) {
  41. $this->map['forum_attachment_'.($i-1)] = $this->map['forum_attachment'];
  42. }
  43. }
  44. if(isset($this->map['common_member'])) {
  45. $this->map['common_member_archive'] =
  46. $this->map['common_member_count'] = $this->map['common_member_count_archive'] =
  47. $this->map['common_member_status'] = $this->map['common_member_status_archive'] =
  48. $this->map['common_member_profile'] = $this->map['common_member_profile_archive'] =
  49. $this->map['common_member_field_forum'] = $this->map['common_member_field_forum_archive'] =
  50. $this->map['common_member_field_home'] = $this->map['common_member_field_home_archive'] =
  51. $this->map['common_member_validate'] = $this->map['common_member_verify'] =
  52. $this->map['common_member_verify_info'] = $this->map['common_member'];
  53. }
  54. }
  55. }
  56. function connect($serverid = 1) {
  57. if(empty($this->config) || empty($this->config[$serverid])) {
  58. $this->halt('config_db_not_found');
  59. }
  60. $this->link[$serverid] = $this->_dbconnect(
  61. $this->config[$serverid]['dbhost'],
  62. $this->config[$serverid]['dbuser'],
  63. $this->config[$serverid]['dbpw'],
  64. $this->config[$serverid]['dbcharset'],
  65. $this->config[$serverid]['dbname'],
  66. $this->config[$serverid]['pconnect']
  67. );
  68. $this->curlink = $this->link[$serverid];
  69. }
  70. function _dbconnect($dbhost, $dbuser, $dbpw, $dbcharset, $dbname, $pconnect, $halt = true) {
  71. $link = new mysqli();
  72. if(!$link->real_connect($dbhost, $dbuser, $dbpw, $dbname, null, null, MYSQLI_CLIENT_COMPRESS)) {
  73. $halt && $this->halt('notconnect', $this->errno());
  74. } else {
  75. $this->curlink = $link;
  76. if($this->version() > '4.1') {
  77. $link->set_charset($dbcharset ? $dbcharset : $this->config[1]['dbcharset']);
  78. $serverset = $this->version() > '5.0.1' ? 'sql_mode=\'\',' : '';
  79. $serverset .= 'character_set_client=binary';
  80. $serverset && $link->query("SET $serverset");
  81. }
  82. }
  83. return $link;
  84. }
  85. function table_name($tablename) {
  86. if(!empty($this->map) && !empty($this->map[$tablename])) {
  87. $id = $this->map[$tablename];
  88. if(!$this->link[$id]) {
  89. $this->connect($id);
  90. }
  91. $this->curlink = $this->link[$id];
  92. } else {
  93. $this->curlink = $this->link[1];
  94. }
  95. return $this->tablepre.$tablename;
  96. }
  97. function select_db($dbname) {
  98. return $this->curlink->select_db($dbname);
  99. }
  100. function fetch_array($query, $result_type = MYSQLI_ASSOC) {
  101. return $query ? $query->fetch_array($result_type) : null;
  102. }
  103. function fetch_first($sql) {
  104. return $this->fetch_array($this->query($sql));
  105. }
  106. function result_first($sql) {
  107. return $this->result($this->query($sql), 0);
  108. }
  109. public function query($sql, $silent = false, $unbuffered = false) {
  110. if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
  111. $starttime = microtime(true);
  112. }
  113. if('UNBUFFERED' === $silent) {
  114. $silent = false;
  115. $unbuffered = true;
  116. } elseif('SILENT' === $silent) {
  117. $silent = true;
  118. $unbuffered = false;
  119. }
  120. $resultmode = $unbuffered ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT;
  121. if(!($query = $this->curlink->query($sql, $resultmode))) {
  122. if(in_array($this->errno(), array(2006, 2013)) && substr($silent, 0, 5) != 'RETRY') {
  123. $this->connect();
  124. return $this->curlink->query($sql, 'RETRY'.$silent);
  125. }
  126. if(!$silent) {
  127. $this->halt($this->error(), $this->errno(), $sql);
  128. }
  129. }
  130. if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
  131. $this->sqldebug[] = array($sql, number_format((microtime(true) - $starttime), 6), debug_backtrace(), $this->curlink);
  132. }
  133. $this->querynum++;
  134. return $query;
  135. }
  136. function affected_rows() {
  137. return $this->curlink->affected_rows;
  138. }
  139. function error() {
  140. return (($this->curlink) ? $this->curlink->error : mysqli_error());
  141. }
  142. function errno() {
  143. return intval(($this->curlink) ? $this->curlink->errno : mysqli_errno());
  144. }
  145. function result($query, $row = 0) {
  146. if(!$query || $query->num_rows == 0) {
  147. return null;
  148. }
  149. $query->data_seek($row);
  150. $assocs = $query->fetch_row();
  151. return $assocs[0];
  152. }
  153. function num_rows($query) {
  154. $query = $query ? $query->num_rows : 0;
  155. return $query;
  156. }
  157. function num_fields($query) {
  158. return $query ? $query->field_count : null;
  159. }
  160. function free_result($query) {
  161. return $query ? $query->free() : false;
  162. }
  163. function insert_id() {
  164. return ($id = $this->curlink->insert_id) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  165. }
  166. function fetch_row($query) {
  167. $query = $query ? $query->fetch_row() : null;
  168. return $query;
  169. }
  170. function fetch_fields($query) {
  171. return $query ? $query->fetch_field() : null;
  172. }
  173. function version() {
  174. if(empty($this->version)) {
  175. $this->version = $this->curlink->server_info;
  176. }
  177. return $this->version;
  178. }
  179. function escape_string($str) {
  180. return $this->curlink->escape_string($str);
  181. }
  182. function close() {
  183. return $this->curlink->close();
  184. }
  185. function halt($message = '', $code = 0, $sql = '') {
  186. throw new DbException($message, $code, $sql);
  187. }
  188. }
  189. ?>