作者 邓超

添加异常

<?php
namespace Lib;
use Throwable;
/**
* 数据库 sql语句错误时的异常
* @author:dc
* @time 2024/7/24 9:10
* Class DbException
* @package Lib
*/
class DbException extends \Exception {
protected $sql = '';
protected $bindData = [];
public function __construct($message = "",$sql='',$bindData=[],$previous = null)
{
parent::__construct($message, 0,$previous);
}
public function getSql(){
return $this->sql;
}
public function getBindData(){
return $this->bindData;
}
}
... ...
... ... @@ -16,12 +16,28 @@ trait DbQuery {
*/
protected $client;
/**
* 是否抛出异常
* @var bool
*/
protected $isThrow = false;
public function getClient()
{
return $this->client;
}
/**
* @return $this
* @author:dc
* @time 2024/7/24 9:09
*/
public function throw(){
$this->isThrow = true;
return $this;
}
/**
* 查询
... ... @@ -46,6 +62,9 @@ trait DbQuery {
$query = $this->getClient()->prepare($sql);
$ret = $query->execute($params);
}catch (\Throwable $e){
if($this->isThrow){
throw new DbException($e->getMessage(),$sql,$params,$e);
}
logs([
$sql,$params,
$e->getMessage(),
... ...