-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathMysqlidb.php
More file actions
executable file
·464 lines (417 loc) · 12.4 KB
/
Copy pathMysqlidb.php
File metadata and controls
executable file
·464 lines (417 loc) · 12.4 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?php
// +----------------------------------------------------------------------
// | Buddy Framework
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://buddy.woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <xinqiyang@gmail.com>
// +----------------------------------------------------------------------
/**
* Mysqli简单高性能高稳定性的数据访问类
* 开发中和线上必须配置mysqli的扩展
* 获取大量数据的测试代码:
*
* $sql = 'select * from sz_account limit 1';
* $sqllarge = 'select * from sz_stream';
* $db = Base::instance('Mysqlidb');
* $result = $db->fetchRow($sql);
* //查询超大对象,获取连接对象,进行查询
* $db->connect()->real_query($sqllarge);
* //获取查询结果
* $result2 = $db->connect()->use_result();
* //循环输出结果
* while($row = $result2->fetch_row()){
* var_dump($row);
* }
* echo "-------<br />";
* var_dump($result);
*
* 查询小量但是不想从db返回后在做二次循环时候用这个
* if ($db->query($sqlproduct)) {
* while ($arrRow = mysqli_fetch_assoc($db->getQuery())) {
* var_dump($arrRow);
* echo "--------------<br />";
* }
* }
*/
class Mysqlidb {
/**
* DB配置节点
* @var array
*/
protected $_arrConfig = null;
/**
* 是否自动释放
* @var bool
*/
protected $_bolAutoFree = false;
/**
* 是否使用持续链接
* @var bool
*/
protected $_bolPconnect = false;
/**
* 事务次数
* @var integer
*/
protected $_intTransTimes = 0;
/**
* 查询语句
* @var string
*/
protected $_strQueryStr = '';
/**
* 最后自增ID
* @var integer
*/
protected $_intLastInsID = null;
/**
* 影响行数
* @var integer
*/
protected $_intNumRows = 0;
/**
* 当前的链接对象
* @var mysqli
*/
protected $_objLinkID = null;
/**
* 当前查询的对象
* @var object
*/
protected $_objQueryID = null;
/**
* 是否使用主库
* @var bool
*/
protected $_bolIsMaster = false;
/**
* 当前是否需要连接到主库
* @var bool
*/
protected $_bolMaster = false;
/**
* 标记是否成功
* @var bool
*/
protected $_bolConnected = false;
/**
* 构造函数
* 获取配置信息传入,构造DB对象
*/
public function __construct($dbNode='mysqli') {
if (!extension_loaded('mysqli')) {
throw new Exception('MYSQLI EXTENSTION NOT LOADED!');
}
//加载配置
$arrConfig = C('mysqli.'.$dbNode);
if (!empty($arrConfig)) {
$this->_arrConfig = $arrConfig;
} else {
throw new Exception('DB CONFIG ITEMS IS EMPTY!');
}
}
/**
* 获取配置
* @param bool $bolMaster 是否使用主库
* @param int $intRetry 重试的配置获取
* @return array
*/
private function _get_db_config($bolMaster, $intRetry = 0) {
if ($bolMaster) {
return $this->_arrConfig['master'];
} else {
//如果不是连接在主库上的话,就通过从库来读
switch ($intRetry) {
case 0:
return $this->_arrConfig['near'][array_rand($this->_arrConfig['near'])];
case 1:
return $this->_arrConfig['backup'][array_rand($this->_arrConfig['backup'])];
case 2:
return $this->_arrConfig['master'];
default:
return $this->_arrConfig['master'];
}
}
}
/**
* 连接数据库获取实例
* 根据传入的linkNum来实现db的切换
* 连接到DB SERVER的配置
*/
public function connect() {
if ($this->_objLinkID && $this->_bolIsMaster == false && $this->_bolMaster == true) {
//已连接到slave但需要连接到master,则断开已有连接
$this->close();
}
if (!isset($this->_objLinkID)) {
//创建连接
$connect_success = false;
for ($i = 0; $i < 3; $i++) {
$arrConfig = $this->_get_db_config($this->_bolMaster, $i);
$this->_objLinkID = new mysqli($arrConfig['hostname'], $arrConfig['username'],
$arrConfig['password'], $arrConfig['database'], $arrConfig['hostport']);
if (mysqli_connect_errno()) {
continue;
} else {
$connect_success = true;
break;
}
}
if (!$connect_success) {
throw new Exception(mysqli_connect_error());
}
if (!$this->_objLinkID->set_charset('utf8')) {
throw new Exception("Failed to set character set utf-8!");
}
$this->_bolConnected = true;
$this->_bolIsMaster = $this->_bolMaster;
}
return $this->_objLinkID;
}
/**
* 查询SQL语句返回结果
* @param string $str sql语句
* @return array 查询结果
*/
public function query($strSql) {
//连接数据库
$this->connect();
$this->_strQueryStr = $strSql;
//释放上次的查询
$this->free();
$this->_objQueryID = mysqli_query($this->_objLinkID, $this->_strQueryStr);
if (!$this->_objQueryID) {
//查询失败则抛出异常
throw new Exception(
"QUERY ERROR:" . $this->_objLinkID->error . " SQL:" . $strSql);
return false;
} else {
$this->_intNumRows = mysqli_num_rows($this->_objQueryID);
return true;
}
}
/**
* 获取query后的查询结果
*
*/
public function fetch() {
return mysqli_fetch_assoc($this->_objQueryID);
}
/**
* 查询返回单条记录
*
* $db->fetchRow($strSql);
* @param string $strSql SQL语句
* @return array/empty array 返回当条数据或者是空数组
*/
public function fetchRow($strSql) {
if ($this->query($strSql)) {
return $this->fetch();
}
return array();
}
/**
* 查询返回一列的全部记录,例如SELECT field FROM table WHERE id = 3;
*
* $db->fetchColumn($strSql);
* @param string $strSql SQL语句
* @return array/empty array 返回当条数据或者是空数组
*/
public function fetchColumn($strSql) {
$arrColumn = array();
if ($this->query($strSql)) {
while ($arrRow = $this->fetch()) {
$arrRow = array_values($arrRow);
$arrColumn[] = $arrRow[0];
}
}
return $arrColumn;
}
/**
*
* 查询返回第一个记录的第一个字段的值,例如SELECT field FROM table WHERE id = 12;
*
* $db->fetchOne($strSql);
* @param string $strSql
*/
public function fetchOne($strSql) {
$arrRow = $this->fetchRow($strSql);
if (!is_array($arrRow)) {
return false;
}
$arrRow = array_values($arrRow);
return $arrRow[0];
}
/**
*
* 查询全部行,例如SELECT * FROM table WHERE id > 3;
*
* $db->fetchAll($strSql);
* @param string $strSql
*/
public function fetchAll($strSql) {
$arrAll = array();
if ($this->query($strSql)) {
return mysqli_fetch_all($this->_objQueryID, MYSQLI_ASSOC);
}
return $arrAll;
}
/**
* 执行SQL语句
* @param string $str sql语句
* @return int 返回execute影响的行数
*/
public function execute($strSql) {
//连接到主库
$this->_bolMaster = true;
$this->connect();
$this->_strQueryStr = $strSql;
$this->free();
//$startInterval = microtime(true);
$result = mysqli_query($this->_objLinkID, $this->_strQueryStr);
if (false === $result) {
throw new Topaz_Exception("QUERY ERROR:" . $this->_objLinkID->error . " SQL:" . $strSql);
return false;
} else {
$this->_intNumRows = mysqli_affected_rows($this->_objLinkID);
$this->_intLastInsID = mysqli_insert_id($this->_objLinkID);
//$endInterval = microtime(true);
//error_log(($endInterval - $startInterval) . " " . $strSql . "\n", 3 ,"/tmp/mccsql.log");
return $this->_intNumRows;
}
}
/**
* 插入数据库
* @TODO: 需要处理下table的前缀问题
* @param type $strTable
* @param type $arrBind
* @return type
*/
public function insert($strTable, $arrBind) {
$fields = "(";
$values = "(";
foreach ($arrBind as $key => $value) {
$fields .= "`" . $key . "`";
$fields .= ",";
$values .= ("'" . $this->escape($value) . "'");
$values .= ",";
}
$fields = rtrim($fields, ",");
$values = rtrim($values, ",");
$fields .= ")";
$values .= ")";
$sql = "INSERT INTO {$strTable} {$fields} VALUES {$values}";
return $this->execute($sql);
}
/**
* 更新操作
* @param type $strTable 表名
* @param type $arrBind 待绑定的数组
* @param type $strWhere 查询条件
* @return type
*/
public function update($strTable, $arrBind, $strWhere) {
$sql = "UPDATE {$strTable} SET ";
$setString = "";
foreach ($arrBind as $key => $value) {
$strSeg = "`" . $key . "`";
$valSeg = ("'" . $this->escape($value) . "'");
$strSeg .= (" = " . $valSeg);
$strSeg .= ",";
$setString .= $strSeg;
}
$setString = rtrim($setString, ',');
$sql .= $setString;
$sql .= (" WHERE " . $strWhere);
return $this->execute($sql);
}
/**
* 开始事务
* @return null
*/
public function beginTransaction() {
$this->_bolMaster = true;
$this->_objLinkID = $this->connect();
if (!$this->_objLinkID) {
return false;
}
$result = mysqli_query($this->_objLinkID, 'START TRANSACTION');
return $result;
}
/**
* 提交
* @return bool true/false
*/
public function commit() {
$result = mysqli_query($this->_objLinkID, 'COMMIT');
return $result;
}
/**
* 回滚
* @return bool 结果
*/
public function rollBack() {
$result = mysqli_query($this->_objLinkID, 'ROLLBACK');
return $result;
}
/**
* 释放最近一次查询结果
*/
public function free() {
if (!empty($this->_objQueryID)) {
mysqli_free_result($this->_objQueryID);
$this->_objQueryID = null;
}
}
/**
* 回收资源
*/
public function close() {
if (!empty($this->_objQueryID)) {
mysqli_free_result($this->_objQueryID);
}
if (!empty($this->_objLinkID)) {
mysqli_close($this->_objLinkID);
}
unset($this->_objLinkID);
unset($this->_objQueryID);
$this->_bolConnected = false;
}
/**
* destruct
*/
public function __destruct() {
$this->close();
}
/**
* 获取最后一条查询的SQL
* @return string
*/
public function getLastSql() {
return $this->_strQueryStr;
}
public function getLastInsertId() {
return $this->_intLastInsID;
}
public function escape($string) {
$this->connect();
return $this->_objLinkID->real_escape_string($string);
}
public function getNumRows() {
return $this->_intNumRows;
}
public function getConnection($bolMaster) {
if (!isset($bolMaster) || $bolMaster === null) {
$bolMaster = $this->_bolMaster;
}
$this->_bolMaster = $bolMaster;
$this->connect();
return $this->_objLinkID;
}
public function getQuery() {
return $this->_objQueryID;
}
}