...
|
...
|
@@ -31,18 +31,6 @@ class DbPool { |
|
|
return $this->client;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 表
|
|
|
* @var string
|
|
|
*/
|
|
|
private string $table;
|
|
|
|
|
|
/**
|
|
|
* 查询条件
|
|
|
* @var string|array
|
|
|
*/
|
|
|
private string | array $where;
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
{
|
...
|
...
|
@@ -65,46 +53,83 @@ class DbPool { |
|
|
|
|
|
|
|
|
/**
|
|
|
* 表
|
|
|
* @param $table
|
|
|
* @return $this
|
|
|
* 查询
|
|
|
* @param string|array $sql
|
|
|
* @return false|\PDOStatement
|
|
|
* @author:dc
|
|
|
* @time 2023/2/13 14:36
|
|
|
* @time 2023/2/17 10:01
|
|
|
*/
|
|
|
public function table($table){
|
|
|
$this->table = $table;
|
|
|
return $this;
|
|
|
private function query(string|array $sql){
|
|
|
if(is_array($sql)){
|
|
|
list($sql,$params) = $sql;
|
|
|
}else{
|
|
|
$params = null;
|
|
|
}
|
|
|
$query = $this->client->prepare($sql);
|
|
|
|
|
|
// todo:: 记录日志,生产请注释
|
|
|
logs([$sql,$params]);
|
|
|
|
|
|
if($query->execute($params)){
|
|
|
return $query;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 条件
|
|
|
* @param string|array $where
|
|
|
* @return $this
|
|
|
* 更新数据
|
|
|
* @param string $table
|
|
|
* @param array $data
|
|
|
* @param string $where
|
|
|
* @param bool $timeauto
|
|
|
* @return int
|
|
|
* @author:dc
|
|
|
* @time 2023/2/13 14:38
|
|
|
* @time 2023/2/17 14:03
|
|
|
*/
|
|
|
public function where(string|array $where){
|
|
|
$this->where = $where;
|
|
|
return $this;
|
|
|
public function update(string $table, array $data, string $where, $timeauto = true):int {
|
|
|
|
|
|
if($timeauto){
|
|
|
$data['updated_at'] = empty($data['updated_at']) ? date('Y-m-d H:i:s') : $data['updated_at'];
|
|
|
}
|
|
|
|
|
|
$sql = "update `{$table}` set ".dbUpdate($data). " where ".$where;
|
|
|
$query = $this->query([$sql,$data]);
|
|
|
if($query){
|
|
|
return $query->rowCount();
|
|
|
}
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* @param $sql
|
|
|
* @param null $params
|
|
|
* @return false|\PDOStatement
|
|
|
* 插入数据
|
|
|
* @param string $table
|
|
|
* @param array $data
|
|
|
* @param bool $timeauto
|
|
|
* @return int
|
|
|
* @author:dc
|
|
|
* @time 2023/2/13 14:41
|
|
|
* @time 2023/2/17 14:04
|
|
|
*/
|
|
|
private function query($sql,$params=null){
|
|
|
public function insert(string $table, array $data, $timeauto = true):int {
|
|
|
|
|
|
if($timeauto){
|
|
|
$data['created_at'] = empty($data['created_at']) ? date('Y-m-d H:i:s') : $data['created_at'];
|
|
|
}
|
|
|
|
|
|
$sql = "insert into `{$table}` set ".dbUpdate($data);
|
|
|
|
|
|
public function get($sql){
|
|
|
$query = $this->query([$sql,$data]);
|
|
|
|
|
|
if($query){
|
|
|
return $this->client->lastInsertId();
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 统计数量
|
|
|
* @param string $sql
|
...
|
...
|
@@ -112,16 +137,29 @@ class DbPool { |
|
|
* @author:dc
|
|
|
* @time 2023/2/14 16:19
|
|
|
*/
|
|
|
public function count(string $sql):int{
|
|
|
$query = $this->client->prepare($sql);
|
|
|
|
|
|
if($query->execute()){
|
|
|
public function count(string|array $sql):int{
|
|
|
$query = $this->query($sql);
|
|
|
if($query){
|
|
|
return $query->fetch(\PDO::FETCH_COLUMN);
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 某个值
|
|
|
* @param string|array $sql
|
|
|
* @return mixed|null
|
|
|
* @author:dc
|
|
|
* @time 2023/2/17 11:03
|
|
|
*/
|
|
|
public function value(string|array $sql){
|
|
|
$query = $this->query($sql);
|
|
|
if($query){
|
|
|
return $query->fetch(\PDO::FETCH_COLUMN);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 查询一条数据
|
...
|
...
|
@@ -132,20 +170,62 @@ class DbPool { |
|
|
*/
|
|
|
public function first(string|array $sql){
|
|
|
|
|
|
$query = $this->client->prepare(is_array($sql) ? $sql[0] : $sql);
|
|
|
$query = $this->query($sql);
|
|
|
|
|
|
if($query->execute(is_array($sql) ? $sql[1] : null)){
|
|
|
if($query){
|
|
|
return $query->fetch();
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询列表
|
|
|
* @param string|array $sql
|
|
|
* @return mixed|null
|
|
|
* @author:dc
|
|
|
* @time 2023/2/13 14:54
|
|
|
*/
|
|
|
public function all(string|array $sql){
|
|
|
|
|
|
public function delete(){
|
|
|
$query = $this->query($sql);
|
|
|
|
|
|
if($query){
|
|
|
return $query->fetchAll();
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 事务开启
|
|
|
* @author:dc
|
|
|
* @time 2023/2/17 11:35
|
|
|
*/
|
|
|
public function transaction(){
|
|
|
$this->client->beginTransaction();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 事务回滚
|
|
|
* @author:dc
|
|
|
* @time 2023/2/17 11:35
|
|
|
*/
|
|
|
public function rollBack(){
|
|
|
$this->client->rollBack();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 事务提交
|
|
|
* @author:dc
|
|
|
* @time 2023/2/17 11:35
|
|
|
*/
|
|
|
public function commit(){
|
|
|
$this->client->commit();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* @param $cid
|
...
|
...
|
|