Total Complexity | 100 |
Total Lines | 554 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like db often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use db, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class db |
||
19 | { |
||
20 | private static $configs = array(); |
||
21 | private static $rsid; |
||
22 | private static $links = array(); |
||
23 | private static $link_name = 'default'; |
||
24 | private static $autocommiting = false; |
||
25 | |||
26 | public static function _init() |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * 重新设置连接 |
||
73 | * 传空的话就等于关闭数据库再连接 |
||
74 | * 在多进程环境下如果主进程已经调用过了,子进程一定要调用一次 clear_link,否则会报错: |
||
75 | * Error while reading greeting packet. PID=19615,这是两个进程互抢一个连接句柄引起的 |
||
76 | * |
||
77 | * @param array $config |
||
78 | * @return void |
||
79 | * @author seatle <[email protected]> |
||
80 | * @created time :2016-03-29 00:51 |
||
81 | */ |
||
82 | public static function clear_link() |
||
83 | { |
||
84 | if(self::$links) |
||
85 | { |
||
86 | foreach(self::$links as $k=>$v) |
||
87 | { |
||
88 | @mysqli_close($v['conn']); |
||
89 | unset(self::$links[$k]); |
||
90 | } |
||
91 | } |
||
92 | // 注意,只会连接最后一个,不过貌似也够用了啊 |
||
93 | self::_init(); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * 改变链接为指定配置的链接(如果不同时使用多个数据库,不会涉及这个操作) |
||
98 | * @parem $link_name 链接标识名 |
||
99 | * @parem $config 多次使用时, 这个数组只需传递一次 |
||
100 | * config 格式与 $GLOBALS['config']['db'] 一致 |
||
101 | * @return void |
||
102 | */ |
||
103 | public static function set_connect($link_name, $config = array()) |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | |||
120 | /** |
||
121 | * 还原为默认连接(如果不同时使用多个数据库,不会涉及这个操作) |
||
122 | * @parem $config 指定配置(默认使用inc_config.php的配置) |
||
123 | * @return void |
||
124 | */ |
||
125 | public static function set_connect_default() |
||
126 | { |
||
127 | $config = self::_get_default_config(); |
||
128 | self::set_connect('default', $config); |
||
129 | } |
||
130 | |||
131 | |||
132 | /** |
||
133 | * 获取默认配置 |
||
134 | */ |
||
135 | protected static function _get_default_config() |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * 返回查询游标 |
||
150 | * @return rsid |
||
151 | */ |
||
152 | protected static function _get_rsid($rsid = '') |
||
153 | { |
||
154 | return $rsid == '' ? self::$rsid : $rsid; |
||
155 | } |
||
156 | |||
157 | public static function autocommit($mode = false) |
||
158 | { |
||
159 | if ( self::$autocommiting ) |
||
160 | { |
||
161 | return true; |
||
162 | } |
||
163 | |||
164 | self::$autocommiting = true; |
||
165 | |||
166 | self::_init(); |
||
167 | return mysqli_autocommit(self::$links[self::$link_name]['conn'], $mode); |
||
168 | } |
||
169 | |||
170 | public static function begin_tran() |
||
173 | } |
||
174 | |||
175 | public static function commit() |
||
176 | { |
||
177 | mysqli_commit(self::$links[self::$link_name]['conn']); |
||
178 | self::autocommit(true); |
||
179 | return true; |
||
180 | } |
||
181 | |||
182 | |||
183 | public static function rollback() |
||
184 | { |
||
185 | mysqli_rollback(self::$links[self::$link_name]['conn']); |
||
186 | self::autocommit(true); |
||
187 | return true; |
||
188 | } |
||
189 | |||
190 | public static function query($sql) |
||
242 | } |
||
243 | } |
||
244 | |||
245 | public static function fetch($rsid = '') |
||
246 | { |
||
247 | $rsid = self::_get_rsid($rsid); |
||
248 | $row = mysqli_fetch_array($rsid, MYSQLI_ASSOC); |
||
249 | return $row; |
||
250 | } |
||
251 | |||
252 | public static function get_one($sql) |
||
253 | { |
||
254 | if (!preg_match("/limit/i", $sql)) |
||
255 | { |
||
256 | $sql = preg_replace("/[,;]$/i", '', trim($sql)) . " limit 1 "; |
||
257 | } |
||
258 | $rsid = self::query($sql); |
||
259 | if ($rsid === false) |
||
260 | { |
||
261 | return array(); |
||
262 | } |
||
263 | $row = self::fetch($rsid); |
||
264 | self::free($rsid); |
||
265 | return $row; |
||
266 | } |
||
267 | |||
268 | public static function get_all($sql) |
||
269 | { |
||
270 | $rsid = self::query($sql); |
||
271 | if ($rsid === false) |
||
272 | { |
||
273 | return array(); |
||
274 | } |
||
275 | while ( $row = self::fetch($rsid) ) |
||
276 | { |
||
277 | $rows[] = $row; |
||
278 | } |
||
279 | self::free($rsid); |
||
280 | return empty($rows) ? false : $rows; |
||
281 | } |
||
282 | |||
283 | public static function free($rsid) |
||
284 | { |
||
285 | return mysqli_free_result($rsid); |
||
286 | } |
||
287 | |||
288 | public static function insert_id() |
||
291 | } |
||
292 | |||
293 | public static function affected_rows() |
||
296 | } |
||
297 | |||
298 | public static function insert($table = '', $data = null, $return_sql = false) |
||
299 | { |
||
300 | $items_sql = $values_sql = ""; |
||
301 | foreach ($data as $k => $v) |
||
302 | { |
||
303 | $v = stripslashes($v); |
||
304 | $v = addslashes($v); |
||
305 | $items_sql .= "`$k`,"; |
||
306 | $values_sql .= "\"$v\","; |
||
307 | } |
||
308 | $sql = "Insert Ignore Into `{$table}` (" . substr($items_sql, 0, -1) . ") Values (" . substr($values_sql, 0, -1) . ")"; |
||
309 | if ($return_sql) |
||
310 | { |
||
311 | return $sql; |
||
312 | } |
||
313 | else |
||
314 | { |
||
315 | if (self::query($sql)) |
||
316 | { |
||
317 | return mysqli_insert_id(self::$links[self::$link_name]['conn']); |
||
318 | } |
||
319 | else |
||
320 | { |
||
321 | return false; |
||
322 | } |
||
323 | } |
||
324 | } |
||
325 | |||
326 | public static function insert_batch($table = '', $set = NULL, $return_sql = FALSE) |
||
365 | } |
||
366 | |||
367 | public static function update_batch($table = '', $set = NULL, $index = NULL, $where = NULL, $return_sql = FALSE) |
||
368 | { |
||
369 | if (empty($table) || is_null($set) || is_null($index)) |
||
370 | { |
||
371 | // 不要用exit,会中断程序 |
||
372 | return false; |
||
373 | } |
||
374 | $set = self::strsafe($set); |
||
375 | $fields = self::get_fields($table); |
||
376 | |||
377 | $ids = array(); |
||
378 | foreach ($set as $val) |
||
379 | { |
||
380 | ksort($val); |
||
381 | // 去重,其实不去也可以,因为相同的when只会执行第一个,后面的就直接跳过不执行了 |
||
382 | $key = md5($val[$index]); |
||
383 | $ids[$key] = $val[$index]; |
||
384 | |||
385 | foreach (array_keys($val) as $field) |
||
386 | { |
||
387 | if ($field != $index) |
||
388 | { |
||
389 | $final[$field][$key] = 'When `'.$index.'` = "'.$val[$index].'" Then "'.$val[$field].'"'; |
||
390 | } |
||
391 | } |
||
392 | } |
||
393 | //$ids = array_values($ids); |
||
394 | |||
395 | // 如果不是数组而且不为空,就转数组 |
||
396 | if (!is_array($where) && !empty($where)) |
||
397 | { |
||
398 | $where = array($where); |
||
399 | } |
||
400 | $where[] = $index.' In ("'.implode('","', $ids).'")'; |
||
401 | $where = empty($where) ? "" : " Where ".implode(" And ", $where); |
||
402 | |||
403 | $sql = "Update `".$table."` Set "; |
||
404 | $cases = ''; |
||
405 | |||
406 | foreach ($final as $k => $v) |
||
407 | { |
||
408 | // 过滤掉数据库没有的字段 |
||
409 | if (!in_array($k, $fields)) |
||
410 | { |
||
411 | continue; |
||
412 | } |
||
413 | $cases .= '`'.$k.'` = Case '."\n"; |
||
414 | foreach ($v as $row) |
||
415 | { |
||
416 | $cases .= $row."\n"; |
||
417 | } |
||
418 | |||
419 | $cases .= 'Else `'.$k.'` End, '; |
||
420 | } |
||
421 | |||
422 | $sql .= substr($cases, 0, -2); |
||
423 | |||
424 | // 其实不带 Where In ($index) 的条件也可以的 |
||
425 | $sql .= $where; |
||
426 | |||
427 | if ($return_sql) return $sql; |
||
428 | |||
429 | $rt = self::query($sql); |
||
430 | $insert_id = self::affected_rows(); |
||
431 | $return = empty($affected_rows) ? $rt : $affected_rows; |
||
432 | return $return; |
||
433 | } |
||
434 | |||
435 | public static function update($table = '', $data = array(), $where = null, $return_sql = false) |
||
436 | { |
||
437 | $sql = "UPDATE `{$table}` SET "; |
||
438 | foreach ($data as $k => $v) |
||
439 | { |
||
440 | $v = stripslashes($v); |
||
441 | $v = addslashes($v); |
||
442 | $sql .= "`{$k}` = \"{$v}\","; |
||
443 | } |
||
444 | if (!is_array($where)) |
||
445 | { |
||
446 | $where = array($where); |
||
447 | } |
||
448 | // 删除空字段,不然array("")会成为WHERE |
||
449 | foreach ($where as $k => $v) |
||
450 | { |
||
451 | if (empty($v)) |
||
452 | { |
||
453 | unset($where[$k]); |
||
454 | } |
||
455 | } |
||
456 | $where = empty($where) ? "" : " Where " . implode(" And ", $where); |
||
457 | $sql = substr($sql, 0, -1) . $where; |
||
458 | if ($return_sql) |
||
459 | { |
||
460 | return $sql; |
||
461 | } |
||
462 | else |
||
463 | { |
||
464 | if (self::query($sql)) |
||
465 | { |
||
466 | return mysqli_affected_rows(self::$links[self::$link_name]['conn']); |
||
467 | } |
||
468 | else |
||
469 | { |
||
470 | return false; |
||
471 | } |
||
472 | } |
||
473 | } |
||
474 | |||
475 | public static function delete($table = '', $where = null, $return_sql = false) |
||
476 | { |
||
477 | // 小心全部被删除了 |
||
478 | if (empty($where)) |
||
479 | { |
||
480 | return false; |
||
481 | } |
||
482 | $where = 'Where ' . (!is_array($where) ? $where : implode(' And ', $where)); |
||
483 | $sql = "Delete From `{$table}` {$where}"; |
||
484 | if ($return_sql) |
||
485 | { |
||
486 | return $sql; |
||
487 | } |
||
488 | else |
||
489 | { |
||
490 | if (self::query($sql)) |
||
491 | { |
||
492 | return mysqli_affected_rows(self::$links[self::$link_name]['conn']); |
||
493 | } |
||
494 | else |
||
495 | { |
||
496 | return false; |
||
497 | } |
||
498 | } |
||
499 | } |
||
500 | |||
501 | public static function ping() |
||
502 | { |
||
503 | if (!mysqli_ping(self::$links[self::$link_name]['conn'])) |
||
504 | { |
||
505 | @mysqli_close(self::$links[self::$link_name]['conn']); |
||
506 | self::$links[self::$link_name]['conn'] = null; |
||
507 | self::_init(); |
||
508 | } |
||
509 | } |
||
510 | |||
511 | public static function strsafe($array) |
||
541 | } |
||
542 | } |
||
543 | |||
544 | // 这个是给insert、update、insert_batch、update_batch用的 |
||
545 | public static function get_fields($table) |
||
546 | { |
||
547 | // $sql = "SHOW COLUMNS FROM $table"; //和下面的语句效果一样 |
||
548 | $rows = self::get_all("Desc `{$table}`"); |
||
549 | $fields = array(); |
||
550 | foreach ($rows as $k => $v) |
||
551 | { |
||
552 | // 过滤自增主键 |
||
553 | // if ($v['Key'] != 'PRI') |
||
554 | if ($v['Extra'] != 'auto_increment') |
||
555 | { |
||
556 | $fields[] = $v['Field']; |
||
557 | } |
||
558 | } |
||
559 | return $fields; |
||
560 | } |
||
561 | |||
562 | public static function table_exists($table_name) |
||
572 | } |
||
573 | } |
||
574 | |||
575 | |||
576 | |||
577 | |||
578 | |||
579 | |||
580 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.