db_driver_mysql.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_mysql.php 36278 2016-12-09 07:52:35Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class db_driver_mysql
  12. {
  13. var $tablepre;
  14. var $version = '';
  15. var $drivertype = 'mysql';
  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. if($pconnect) {
  72. $link = @mysql_pconnect($dbhost, $dbuser, $dbpw, MYSQL_CLIENT_COMPRESS);
  73. } else {
  74. $link = @mysql_connect($dbhost, $dbuser, $dbpw, 1, MYSQL_CLIENT_COMPRESS);
  75. }
  76. if(!$link) {
  77. $halt && $this->halt('notconnect', $this->errno());
  78. } else {
  79. $this->curlink = $link;
  80. if($this->version() > '4.1') {
  81. $dbcharset = $dbcharset ? $dbcharset : $this->config[1]['dbcharset'];
  82. $serverset = $dbcharset ? 'character_set_connection='.$dbcharset.', character_set_results='.$dbcharset.', character_set_client=binary' : '';
  83. $serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',').'sql_mode=\'\'') : '';
  84. $serverset && mysql_query("SET $serverset", $link);
  85. }
  86. $dbname && @mysql_select_db($dbname, $link);
  87. }
  88. return $link;
  89. }
  90. function table_name($tablename) {
  91. if(!empty($this->map) && !empty($this->map[$tablename])) {
  92. $id = $this->map[$tablename];
  93. if(!$this->link[$id]) {
  94. $this->connect($id);
  95. }
  96. $this->curlink = $this->link[$id];
  97. } else {
  98. $this->curlink = $this->link[1];
  99. }
  100. return $this->tablepre.$tablename;
  101. }
  102. function select_db($dbname) {
  103. return mysql_select_db($dbname, $this->curlink);
  104. }
  105. function fetch_array($query, $result_type = MYSQL_ASSOC) {
  106. return mysql_fetch_array($query, $result_type);
  107. }
  108. function fetch_first($sql) {
  109. return $this->fetch_array($this->query($sql));
  110. }
  111. function result_first($sql) {
  112. return $this->result($this->query($sql), 0);
  113. }
  114. public function query($sql, $silent = false, $unbuffered = false) {
  115. if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
  116. $starttime = microtime(true);
  117. }
  118. if('UNBUFFERED' === $silent) {
  119. $silent = false;
  120. $unbuffered = true;
  121. } elseif('SILENT' === $silent) {
  122. $silent = true;
  123. $unbuffered = false;
  124. }
  125. $func = $unbuffered ? 'mysql_unbuffered_query' : 'mysql_query';
  126. if(!($query = $func($sql, $this->curlink))) {
  127. if(in_array($this->errno(), array(2006, 2013)) && substr($silent, 0, 5) != 'RETRY') {
  128. $this->connect();
  129. return $this->query($sql, 'RETRY'.$silent);
  130. }
  131. if(!$silent) {
  132. $this->halt($this->error(), $this->errno(), $sql);
  133. }
  134. }
  135. if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
  136. $this->sqldebug[] = array($sql, number_format((microtime(true) - $starttime), 6), debug_backtrace(), $this->curlink);
  137. }
  138. $this->querynum++;
  139. return $query;
  140. }
  141. function affected_rows() {
  142. return mysql_affected_rows($this->curlink);
  143. }
  144. function error() {
  145. return (($this->curlink) ? mysql_error($this->curlink) : mysql_error());
  146. }
  147. function errno() {
  148. return intval(($this->curlink) ? mysql_errno($this->curlink) : mysql_errno());
  149. }
  150. function result($query, $row = 0) {
  151. $query = @mysql_result($query, $row);
  152. return $query;
  153. }
  154. function num_rows($query) {
  155. $query = mysql_num_rows($query);
  156. return $query;
  157. }
  158. function num_fields($query) {
  159. return mysql_num_fields($query);
  160. }
  161. function free_result($query) {
  162. return mysql_free_result($query);
  163. }
  164. function insert_id() {
  165. return ($id = mysql_insert_id($this->curlink)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  166. }
  167. function fetch_row($query) {
  168. $query = mysql_fetch_row($query);
  169. return $query;
  170. }
  171. function fetch_fields($query) {
  172. return mysql_fetch_field($query);
  173. }
  174. function version() {
  175. if(empty($this->version)) {
  176. $this->version = mysql_get_server_info($this->curlink);
  177. }
  178. return $this->version;
  179. }
  180. function escape_string($str) {
  181. return mysql_escape_string($str);
  182. }
  183. function close() {
  184. return mysql_close($this->curlink);
  185. }
  186. function halt($message = '', $code = 0, $sql = '') {
  187. throw new DbException($message, $code, $sql);
  188. }
  189. }
  190. ?>