| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- namespace WY\app\libs;
- use WY\app\Config;
- use WY\app\libs\Log;
- use PDO;
- use PDOException;
- if (!defined('WY_ROOT')) {
- exit;
- }
- class Db
- {
- private $db = null;
- static $instance = null;
- function __construct()
- {
- }
- function getConfig()
- {
- return Config::db();
- }
- static function getInstance()
- {
- if (self::$instance == null) {
- self::$instance = new Db();
- }
- return self::$instance;
- }
- function connect()
- {
- try {
- $this->db = new PDO('mysql:host=' . $this->getConfig()['server'] . ';port=' . $this->getConfig()['port'] . ';dbname=' . $this->getConfig()['name'] . ';charset=utf8', $this->getConfig()['user'], $this->getConfig()['pass']);
- $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $e) {
- if ($this->getConfig()['debug']) {
- Log::$type = 'mysql';
- Log::write($e->getMessage());
- }
- echo 'database connect error.';
- exit;
- }
- return $this->db;
- }
- function isConnected()
- {
- if ($this->db == null) {
- return false;
- }
- return true;
- }
- function prepare($sql)
- {
- $stm = $this->db->prepare($sql);
- return $stm;
- }
- function bindValue($stm, $params)
- {
- if (!$params) {
- return false;
- }
- foreach ($params as $key => $val) {
- $key++;
- $stm->bindValue($key, $val);
- }
- }
- function bindParam($stm, $params)
- {
- if (!$params) {
- return false;
- }
- foreach ($params as $key => $val) {
- $key++;
- $stm->bindParam($key, $val);
- }
- }
- function execute($stm)
- {
- try {
- $stm->execute();
- } catch (PDOException $e) {
- $trace = $e->getTrace();
- $str = '';
- foreach ($trace[2] as $key => $val) {
- if ($key == 'file' || $key == 'line' || $key == 'class') {
- $str .= $str ? "\n" : '';
- $str .= '[' . $key . ']' . $val;
- }
- }
- Log::write('error in ' . $e->getFile() . ' ' . $e->getLine() . "\n" . $e->getMessage() . "\n" . $stm->queryString . "\n" . $str);
- }
- }
- function query($sql)
- {
- try {
- return $this->db->query($sql);
- } catch (PDOException $e) {
- Log::write('error in ' . $e->getFile() . ' ' . $e->getLine() . "\n" . $e->getMessage() . "\n" . $stm->queryString);
- }
- }
- function exec($sql)
- {
- try {
- return $this->db->exec($sql);
- } catch (PDOException $e) {
- Log::write('error in ' . $e->getFile() . ' ' . $e->getLine() . "\n" . $e->getMessage() . "\n" . $stm->queryString);
- }
- }
- function fetchAll($stm)
- {
- return $stm->fetchAll(PDO::FETCH_ASSOC);
- }
- function fetchRow($stm)
- {
- return $stm->fetch(PDO::FETCH_ASSOC);
- }
- function insert($table, $data)
- {
- if (!$data) {
- return false;
- }
- $result = $this->parseQues($data);
- $sql = "INSERT INTO " . $table . " (" . implode(',', $result['fields']) . ") VALUES(" . implode(',', $result['ques']) . ")";
- $stm = $this->prepare($sql);
- $this->bindValue($stm, $result['values']);
- $this->execute($stm);
- return $this->db->lastInsertId();
- }
- function getTable($table)
- {
- $config = $this->getConfig();
- return $config['prefix'] . $table;
- }
- function delete($table, $where = [])
- {
- $result = $this->parseWhere($where);
- $where = $result && $result['where'] ? $result['where'] : '';
- $sql = "DELETE FROM " . $table . " " . $where;
- $stm = $this->prepare($sql);
- if ($result && $result['values']) {
- $this->bindValue($stm, $result['values']);
- }
- $this->execute($stm);
- return $stm->rowCount();
- }
- function deleteIn($table, $where = [])
- {
- $cons = '';
- if ($where) {
- foreach ($where as $key => $val) {
- $cons = 'WHERE ' . $key . ' IN (' . implode(',', $val) . ')';
- }
- }
- return $this->exec("DELETE FROM " . $table . " " . $cons);
- }
- function update($table, $set, $where = [])
- {
- $result = $this->parseWhere($where);
- $where = $result && $result['where'] ? $result['where'] : '';
- $data = $this->parseQues($set);
- //print_r( $data );
- $fields = '';
- foreach ($data['fields'] as $field) {
- $fields .= $fields ? ',' : '';
- $fields .= $field . '=?';
- }
- $sql = "UPDATE " . $table . " SET " . $fields . " " . $where;
- $stm = $this->prepare($sql);
- if ($result && $result['values']) {
- $arr = array_merge($data['values'], $result['values']);
- } else {
- $arr = $data['values'];
- }
- $this->bindValue($stm, $arr);
- $this->execute($stm);
- return $stm->rowCount();
- }
- function count($table, $where = array())
- {
- $result = $this->parseWhere($where);
- $where = $result && $result['where'] ? $result['where'] : '';
- $sql = "SELECT COUNT(*) AS num FROM " . $table . " " . $where;
- $stm = $this->prepare($sql);
- if ($result && $result['values']) {
- $this->bindValue($stm, $result['values']);
- }
- $this->execute($stm);
- $result = $this->fetchRow($stm);
- return $result['num'];
- }
- function sum($table, $fields = array(), $where = array())
- {
- $result = $this->parseWhere($where);
- $where = $result && $result['where'] ? $result['where'] : '';
- if (!$fields || !is_array($fields)) {
- return 0;
- }
- $sum = '';
- foreach ($fields as $key => $val) {
- $sum .= $sum ? ',' : '';
- $sum .= 'sum(' . $val . ') as ' . $key;
- }
- $sql = "SELECT " . $sum . " FROM " . $table . " " . $where;
- $stm = $this->prepare($sql);
- if ($result && $result['values']) {
- $this->bindValue($stm, $result['values']);
- }
- $this->execute($stm);
- $result = $this->fetchRow($stm);
- foreach ($result as $key => $val) {
- $result[$key] = $val == null ? 0 : number_format($val, 2, '.', '');
- }
- return $result;
- }
- function parseQues($data)
- {
- $result = [];
- foreach ($data as $key => $val) {
- $result['fields'][] = $key;
- $result['ques'][] = '?';
- $result['values'][] = $val;
- }
- return $result;
- }
- function parseWhere($where)
- {
- if (!$where) {
- return false;
- }
- if (!$where['fields']) {
- return false;
- }
- return array('where' => 'where ' . $where['fields'], 'values' => $where['values']);
- }
- function hasOper($key, $val)
- {
- if (strpos($val, '>') !== false || strpos($val, '>=') !== false || strpos($val, '<') !== false || strpos($val, '<=') !== false || strpos($val, '<>') !== false) {
- preg_match('/<>|>=|<=|>|<|/', $val, $match);
- return $key . $match[0] . '?';
- } else {
- return "{$key}=?";
- }
- }
- }
|