初始提交
This commit is contained in:
39
fastphp/db/Db.php
Normal file
39
fastphp/db/Db.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace fastphp\db;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
/**
|
||||
* 数据库操作类
|
||||
* 其属性$pdo为静态属性,所以在页面执行周期内
|
||||
* 只要一次赋值,以后获取的还是首次赋值的内容
|
||||
* 这里就是pdo对象,这样可以保证运行期间只有
|
||||
* 一个数据库连接对象,简单的单例模式
|
||||
* Class Db
|
||||
* @package fastphp\db
|
||||
*/
|
||||
class Db
|
||||
{
|
||||
private static $pdo = null;
|
||||
|
||||
public static function pdo(){
|
||||
if(self::$pdo !== null){
|
||||
return self::$pdo;
|
||||
}
|
||||
|
||||
try{
|
||||
$dns = sprintf('mysql:host=%s;post=%s;dbname=%s;chartset=utf8',DB_HOST,DB_PORT,DB_NAME);
|
||||
$option = array(PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC);
|
||||
self::$pdo = new PDO($dns,DB_USER,DB_PASS,$option);
|
||||
self::$pdo->query("set names utf8");
|
||||
|
||||
return self::$pdo;
|
||||
|
||||
} catch (PDOException $e){
|
||||
exit($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
247
fastphp/db/Sql.php
Normal file
247
fastphp/db/Sql.php
Normal file
@@ -0,0 +1,247 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace fastphp\db;
|
||||
|
||||
use \PDOStatement;
|
||||
|
||||
class Sql
|
||||
{
|
||||
//数据库表名
|
||||
protected $table;
|
||||
|
||||
//where 和 order拼装后的条件
|
||||
private $filter = '';
|
||||
|
||||
//pdo 绑定的参数集合
|
||||
private $param = array();
|
||||
|
||||
private $field = '*';
|
||||
|
||||
/**
|
||||
* 查询条件拼接
|
||||
*
|
||||
* @param $where 条件
|
||||
* @return $this
|
||||
* ['id'=>1] 或者 '`id`=1'
|
||||
*/
|
||||
public function where($where){
|
||||
$this->param = [];
|
||||
$this->filter = '';
|
||||
if($where){
|
||||
$this->filter .= 'WHERE ';
|
||||
if(is_string($where)){
|
||||
$this->filter .= $where;
|
||||
} else {
|
||||
$flag = true;
|
||||
foreach ($where as $key => $value){
|
||||
if($flag){
|
||||
$flag = false;
|
||||
if(is_array($value)){
|
||||
$this->filter .= '`'.$key.'` '.$value[0].' :'.$key;
|
||||
$this->param[$key] = $value[1];
|
||||
} else {
|
||||
$this->filter .= '`'.$key.'` = :'.$key;
|
||||
$this->param[$key] = $value;
|
||||
}
|
||||
} else {
|
||||
if(is_array($value)){
|
||||
$this->filter .= ' AND `'.$key.'` '.$value[0].' :'.$key;
|
||||
$this->param[$key] = $value[1];
|
||||
} else {
|
||||
$this->filter .= ' AND `'.$key.'` = :'.$key;
|
||||
$this->param[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼装排序条件
|
||||
* @param array $order 排序条件
|
||||
* @return $this
|
||||
*/
|
||||
public function order($order = []){
|
||||
if($order){
|
||||
$this->filter .= 'ORDER BY ';
|
||||
$this->filter .= implode(' ,',$order);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* group by
|
||||
* @param array $order
|
||||
* @return $this
|
||||
*/
|
||||
public function group($order = []){
|
||||
if($order){
|
||||
$this->filter .= 'GROUP BY ';
|
||||
$this->filter .= implode(' ,',$order);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询字段
|
||||
* @param string $field
|
||||
* @return $this
|
||||
*/
|
||||
public function field($field = '*'){
|
||||
$this->field = $field;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetchAll(){
|
||||
$sql = sprintf('SELECT %s FROM `%s` %s',$this->field,$this->table,$this->filter);
|
||||
$this->filter = '';
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth = $this->formatParam($sth,$this->param);
|
||||
$sth->execute();
|
||||
|
||||
return $sth->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询一条
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetch(){
|
||||
$sql = sprintf('SELECT %s FROM `%s` %s',$this->field,$this->table,$this->filter);
|
||||
$this->filter = '';
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth = $this->formatParam($sth,$this->param);
|
||||
$sth->execute();
|
||||
|
||||
return $sth->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据条件(id)删除
|
||||
* @param $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete(){
|
||||
$sql = sprintf('DELETE FROM `%s` %s',$this->table,$this->filter);
|
||||
$this->filter = '';
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth = $this->formatParam($sth,$this->param);
|
||||
$sth->execute();
|
||||
|
||||
return $sth->rowCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增数据
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function add($data){
|
||||
$sql = sprintf('INSERT INTO `%s` %s',$this->table,$this->formatInsert($data));
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth = $this->formatParam($sth,$data);
|
||||
$sth->execute();
|
||||
|
||||
return $sth->rowCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增多条数据
|
||||
* @param $data
|
||||
* @return int
|
||||
*/
|
||||
public function addAll($data){
|
||||
$sql = sprintf('INSERT INTO `%s` %s',$this->table,$this->formatInsertAll($data));
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth->execute();
|
||||
|
||||
return $sth->rowCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改数据
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function update($data){
|
||||
$sql = sprintf('UPDATE `%s` SET %s %s',$this->table,$this->formatUpdate($data),$this->filter);
|
||||
$this->filter = '';
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth = $this->formatParam($sth,$data);
|
||||
$sth = $this->formatParam($sth,$this->param);
|
||||
$sth->execute();
|
||||
|
||||
return $sth->rowCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定具体变量值
|
||||
* @param PDOStatement $sth
|
||||
* @param array $params
|
||||
* @return PDOStatement
|
||||
*/
|
||||
public function formatParam(PDOStatement $sth,$params = []){
|
||||
foreach ($params as $param=>&$value){
|
||||
$param = is_int($param)?$param+1:':'.ltrim($param,':');
|
||||
$sth->bindParam($param,$value);
|
||||
}
|
||||
return $sth;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数组转化为插入格式的sql语句
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
private function formatInsert($data){
|
||||
$fields = [];
|
||||
$names = [];
|
||||
foreach ($data as $key=>$value) {
|
||||
$fields[] = sprintf('`%s`',$key);
|
||||
$names[] = sprintf(':%s',$key);
|
||||
}
|
||||
$field = implode(',',$fields);
|
||||
$name = implode(',',$names);
|
||||
return sprintf('(%s) VALUES(%s)',$field,$name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化插入数据
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
private function formatInsertAll($data){
|
||||
$fields = [];
|
||||
$values ='';
|
||||
foreach ($data[0] as $key=>$value) {
|
||||
$fields[] = sprintf('`%s`',$key);
|
||||
}
|
||||
foreach ($data as $k=>$v){
|
||||
$values .= '(\''.implode('\',\'',$v).'\'),';
|
||||
}
|
||||
$field = implode(',',$fields);
|
||||
$values = rtrim($values,',');
|
||||
return sprintf('(%s) VALUES %s',$field,$values);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新数据的格式转换
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
private function formatUpdate($data){
|
||||
$fields = [];
|
||||
foreach ($data as $key=>$value){
|
||||
$fields[] = sprintf('`%s`=:%s',$key,$key);
|
||||
$this->param[$key] = $value;
|
||||
}
|
||||
return implode(',',$fields);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user