db_driver_mysql.php
6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: db_driver_mysql.php 33349 2013-05-30 09:00:26Z kamichen $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class db_driver_mysql
{
var $tablepre;
var $version = '';
var $drivertype = 'mysql';
var $querynum = 0;
var $slaveid = 0;
var $curlink;
var $link = array();
var $config = array();
var $sqldebug = array();
var $map = array();
function db_mysql($config = array()) {
if(!empty($config)) {
$this->set_config($config);
}
}
function set_config($config) {
$this->config = &$config;
$this->tablepre = $config['1']['tablepre'];
if(!empty($this->config['map'])) {
$this->map = $this->config['map'];
for($i = 1; $i <= 100; $i++) {
if(isset($this->map['forum_thread'])) {
$this->map['forum_thread_'.$i] = $this->map['forum_thread'];
}
if(isset($this->map['forum_post'])) {
$this->map['forum_post_'.$i] = $this->map['forum_post'];
}
if(isset($this->map['forum_attachment']) && $i <= 10) {
$this->map['forum_attachment_'.($i-1)] = $this->map['forum_attachment'];
}
}
if(isset($this->map['common_member'])) {
$this->map['common_member_archive'] =
$this->map['common_member_count'] = $this->map['common_member_count_archive'] =
$this->map['common_member_status'] = $this->map['common_member_status_archive'] =
$this->map['common_member_profile'] = $this->map['common_member_profile_archive'] =
$this->map['common_member_field_forum'] = $this->map['common_member_field_forum_archive'] =
$this->map['common_member_field_home'] = $this->map['common_member_field_home_archive'] =
$this->map['common_member_validate'] = $this->map['common_member_verify'] =
$this->map['common_member_verify_info'] = $this->map['common_member'];
}
}
}
function connect($serverid = 1) {
if(empty($this->config) || empty($this->config[$serverid])) {
$this->halt('config_db_not_found');
}
$this->link[$serverid] = $this->_dbconnect(
$this->config[$serverid]['dbhost'],
$this->config[$serverid]['dbuser'],
$this->config[$serverid]['dbpw'],
$this->config[$serverid]['dbcharset'],
$this->config[$serverid]['dbname'],
$this->config[$serverid]['pconnect']
);
$this->curlink = $this->link[$serverid];
}
function _dbconnect($dbhost, $dbuser, $dbpw, $dbcharset, $dbname, $pconnect, $halt = true) {
if($pconnect) {
$link = @mysql_pconnect($dbhost, $dbuser, $dbpw, MYSQL_CLIENT_COMPRESS);
} else {
$link = @mysql_connect($dbhost, $dbuser, $dbpw, 1, MYSQL_CLIENT_COMPRESS);
}
if(!$link) {
$halt && $this->halt('notconnect', $this->errno());
} else {
$this->curlink = $link;
if($this->version() > '4.1') {
$dbcharset = $dbcharset ? $dbcharset : $this->config[1]['dbcharset'];
$serverset = $dbcharset ? 'character_set_connection='.$dbcharset.', character_set_results='.$dbcharset.', character_set_client=binary' : '';
$serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',').'sql_mode=\'\'') : '';
$serverset && mysql_query("SET $serverset", $link);
}
$dbname && @mysql_select_db($dbname, $link);
}
return $link;
}
function table_name($tablename) {
if(!empty($this->map) && !empty($this->map[$tablename])) {
$id = $this->map[$tablename];
if(!$this->link[$id]) {
$this->connect($id);
}
$this->curlink = $this->link[$id];
} else {
$this->curlink = $this->link[1];
}
return $this->tablepre.$tablename;
}
function select_db($dbname) {
return mysql_select_db($dbname, $this->curlink);
}
function fetch_array($query, $result_type = MYSQL_ASSOC) {
if($result_type == 'MYSQL_ASSOC') $result_type = MYSQL_ASSOC;
return mysql_fetch_array($query, $result_type);
}
function fetch_first($sql) {
return $this->fetch_array($this->query($sql));
}
function result_first($sql) {
return $this->result($this->query($sql), 0);
}
public function query($sql, $silent = false, $unbuffered = false) {
if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
$starttime = microtime(true);
}
if('UNBUFFERED' === $silent) {
$silent = false;
$unbuffered = true;
} elseif('SILENT' === $silent) {
$silent = true;
$unbuffered = false;
}
$func = $unbuffered ? 'mysql_unbuffered_query' : 'mysql_query';
if(!($query = $func($sql, $this->curlink))) {
if(in_array($this->errno(), array(2006, 2013)) && substr($silent, 0, 5) != 'RETRY') {
$this->connect();
return $this->query($sql, 'RETRY'.$silent);
}
if(!$silent) {
$this->halt($this->error(), $this->errno(), $sql);
}
}
if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
$this->sqldebug[] = array($sql, number_format((microtime(true) - $starttime), 6), debug_backtrace(), $this->curlink);
}
$this->querynum++;
return $query;
}
function affected_rows() {
return mysql_affected_rows($this->curlink);
}
function error() {
return (($this->curlink) ? mysql_error($this->curlink) : mysql_error());
}
function errno() {
return intval(($this->curlink) ? mysql_errno($this->curlink) : mysql_errno());
}
function result($query, $row = 0) {
$query = @mysql_result($query, $row);
return $query;
}
function num_rows($query) {
$query = mysql_num_rows($query);
return $query;
}
function num_fields($query) {
return mysql_num_fields($query);
}
function free_result($query) {
return mysql_free_result($query);
}
function insert_id() {
return ($id = mysql_insert_id($this->curlink)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
}
function fetch_row($query) {
$query = mysql_fetch_row($query);
return $query;
}
function fetch_fields($query) {
return mysql_fetch_field($query);
}
function version() {
if(empty($this->version)) {
$this->version = mysql_get_server_info($this->curlink);
}
return $this->version;
}
function escape_string($str) {
return mysql_escape_string($str);
}
function close() {
return mysql_close($this->curlink);
}
function halt($message = '', $code = 0, $sql = '') {
throw new DbException($message, $code, $sql);
}
}
?>