1 Star 4 Fork 0

logic/CTG_DBLib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
CTG_DBLib.min.php 6.14 KB
一键复制 编辑 原始数据 按行查看 历史
logic 提交于 2014-12-04 17:19 . update
<?php
class CTG_DBLib { protected $pdo; protected $statement; protected $sql; protected $whereStr = ''; protected $valuesArr = array (); protected $table; protected $selectStr = 'SELECT * FROM '; protected $limit = ''; protected $order = ''; protected $config = array ( 'HOST' => 'localhost', 'PORT' => 3306, 'CHARSET' => 'utf8', 'DB_NAME' => '', 'DB_USER' => 'root', 'DB_PWD' => '', 'TABLE' => '' ); public function __construct($config = array()) { if (! class_exists ( 'PDO' )) { throw new Exception ( '没有找到PDO这个类,请配置相关项!' ); } $this->config = array_merge ( $this->config, $config ); $this->table = $this->config ['TABLE']; try { $this->pdo = new PDO ( 'mysql:host=' . $this->config ['HOST'] . ';dbname=' . $this->config ['DB_NAME'] . ';port=' . $this->config ['PORT'], $this->config ['DB_USER'], $this->config ['DB_PWD'], array ( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => 'set names ' . $this->config ['CHARSET'] ) ); $this->pdo->setAttribute ( PDO::ATTR_EMULATE_PREPARES, false ); } catch ( PDOException $p ) { throw new Exception ( $p->getMessage () ); } } public function table($table) { $this->table = $table; return $this; } public function count($where = array()) { if (! empty ( $where )) { $this->where ( $where ); } $this->selectStr = 'SELECT COUNT(*) AS _count FROM '; $this->limit = ' LIMIT 1 '; $re = $this->find (); return $re [0] ['_count']; } public function has($where = array()) { if (! empty ( $where )) $this->where ( $where ); elseif (empty ( $this->whereStr )) return false; $this->sql = $this->selectStr . $this->table . ' WHERE ' . trim ( $this->whereStr, ' AND ' ) . ' LIMIT 1'; $row = $this->prExRo (); return $row ? true : false; } public function limit($start, $end = '') { $this->limit = ' LIMIT ' . (( int ) $start) . (empty ( $end ) ? '' : ',' . ( int ) $end); return $this; } public function order($order) { $this->order = ' ORDER BY ' . $order . ' '; return $this; } public function update($data) { $keys = array_keys ( $data ); $this->valuesArr = array_merge ( array_values ( $data ), $this->valuesArr ); $kstr = implode ( ',', array_map ( function ($v) { return '`' . $v . '` = ? '; }, $keys ) ); $this->sql = 'UPDATE ' . $this->table . ' SET ' . $kstr . ' WHERE ' . trim ( $this->whereStr, ' AND ' ); return $this->prExRo (); } public function delete($where = array()) { if (! empty ( $where )) { $this->where ( $where ); } if (empty ( $this->valuesArr )) return 0; $this->sql = 'DELETE FROM ' . $this->table . ' WHERE ' . $this->whereStr; return $this->prExRo (); } public function insert($data) { $key = array_keys ( $data ); $this->valuesArr = array_values ( $data ); $kstr = implode ( ',', array_map ( function ($v) { return '`' . $v . '`'; }, $key ) ); $vstr = implode ( ',', array_fill ( 0, count ( $key ), '?' ) ); $vstr = ' VALUES ( ' . $vstr . ' ) '; $kstr = ' ( ' . $kstr . ' )'; $this->sql = 'INSERT INTO ' . $this->table . $kstr . $vstr; $this->prExRo (); return $this->pdo->lastInsertId (); } private function arrToStr($arr, $ks, &$arrV) { $kstr = ''; foreach ( $arr as $k => $v ) { $k = strtoupper ( $k ); $flip = array_flip ( $v ); if (is_int ( end ( $flip ) )) continue; $kstr .= '(' . implode ( ' ' . $k . ' ', array_map ( function ($c) { return (strrpos ( $c, '.' ) || strrpos ( $c, '(' )) ? ' ' . $c . '= ? ' : ' `' . $c . '` = ? '; }, $flip ) ) . ') ' . $ks . ' '; foreach ( $v as $vv ) { $arrV [] = $vv; } } return trim ( $kstr, ' ' . $ks . ' ' ) . ' AND '; } private function arrayW($arr) { $c = 0; if (is_array ( $arr )) { $c ++; foreach ( $arr as $v ) { $c += self::arrayW ( $v ); break; } } return $c; } public function where($where) { $this->whereStr = ''; $kstrt = ''; $arrV = array (); if (self::arrayW ( $where ) == 1) { foreach ( $where as $k => $v ) { $kstrt = (strrpos ( $k, '.' ) || strrpos ( $k, '(' )) ? ' ' . $k . '= ? ' : '( `' . $k . '` = ? )'; $arrV [] = $v; } } else { foreach ( $where as $ks => $vs ) { if (strtoupper ( $ks ) == 'LIKE') { foreach ( $vs as $lk => $lv ) { $kstrt .= '(' . $lk . ' LIKE ?' . ') AND '; $arrV [] = $lv; } continue; } elseif ($ks == '&') { $kstrt .= '(' . implode ( '=', $vs ) . ') AND '; continue; } $kstrt .= self::arrToStr ( self::arrayW ( $where ) == 2 ? $where : $vs, strtoupper ( $ks ), $arrV ); } } $this->whereStr .= $kstrt; $this->valuesArr = $arrV; return $this; } public function field($fields = array()) { $fieldArr = array (); foreach ( $fields as $k => $v ) { $fieldArr [] = (is_int ( $k ) ? '' : ((strrpos ( $k, '.' ) || strrpos ( $k, '(' )) ? $k . ' AS ' : '`' . $k . '` AS ')) . ((strrpos ( $v, '.' ) || strrpos ( $v, '(' )) ? $v : '`' . $v . '`'); } $fieldStr = implode ( ',', $fieldArr ); $this->selectStr = 'SELECT ' . (empty ( $fieldStr ) ? '*' : $fieldStr) . ' FROM '; return $this; } public function find() { $this->sql = $this->selectStr . $this->table . (empty ( $this->whereStr ) ? '' : (' WHERE ' . trim ( $this->whereStr, ' AND ' ))) . $this->order . $this->limit; $this->prExRo (); $this->statement->setFetchMode ( PDO::FETCH_ASSOC ); $resultArr = $this->statement->fetchAll (); $this->selectStr = 'SELECT * FROM '; return $resultArr; } protected function prExRo() { $this->statement = $this->pdo->prepare ( $this->sql ); $this->statement->execute ( $this->valuesArr ); return $this->statement->rowCount (); } public function rawQuery($sql, $values = array()) { if (empty ( $values )) { return self::query ( $sql ); } $this->statement = $this->pdo->prepare ( $sql ); $this->statement->execute ( $values ); $this->statement->setFetchMode ( PDO::FETCH_ASSOC ); $resultArr = $this->statement->fetchAll (); return empty ( $resultArr ) ? $this->statement->rowCount () : $resultArr; } public function query($sql) { $this->sql = $sql; $this->statement = $this->pdo->query ( $sql ); $this->statement->setFetchMode ( PDO::FETCH_ASSOC ); $row = $this->statement->rowCount (); $arr = $this->statement->fetchAll (); return empty ( $arr ) ? $row : $arr; } public function quote($string, $type = PDO::PARAM_STR) { return $this->pdo->quote ( $string, $type ); } public function querySql() { $vas = array (); foreach ( $this->valuesArr as $v ) { $vas [] = $this->pdo->quote ( $v ); } return vsprintf ( str_replace ( '?', '%s', $this->sql ), $vas ); } public function preSqlStr() { return $this->sql; } }
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/cntaige/CTG_DBLib.git
git@gitee.com:cntaige/CTG_DBLib.git
cntaige
CTG_DBLib
CTG_DBLib
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385