正在显示
2 个修改的文件
包含
54 行增加
和
0 行删除
lib/DbException.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | +namespace Lib; | ||
4 | + | ||
5 | +use Throwable; | ||
6 | + | ||
7 | +/** | ||
8 | + * 数据库 sql语句错误时的异常 | ||
9 | + * @author:dc | ||
10 | + * @time 2024/7/24 9:10 | ||
11 | + * Class DbException | ||
12 | + * @package Lib | ||
13 | + */ | ||
14 | +class DbException extends \Exception { | ||
15 | + | ||
16 | + | ||
17 | + protected $sql = ''; | ||
18 | + | ||
19 | + protected $bindData = []; | ||
20 | + | ||
21 | + public function __construct($message = "",$sql='',$bindData=[],$previous = null) | ||
22 | + { | ||
23 | + parent::__construct($message, 0,$previous); | ||
24 | + } | ||
25 | + | ||
26 | + | ||
27 | + public function getSql(){ | ||
28 | + return $this->sql; | ||
29 | + } | ||
30 | + | ||
31 | + public function getBindData(){ | ||
32 | + return $this->bindData; | ||
33 | + } | ||
34 | + | ||
35 | +} |
@@ -16,12 +16,28 @@ trait DbQuery { | @@ -16,12 +16,28 @@ trait DbQuery { | ||
16 | */ | 16 | */ |
17 | protected $client; | 17 | protected $client; |
18 | 18 | ||
19 | + /** | ||
20 | + * 是否抛出异常 | ||
21 | + * @var bool | ||
22 | + */ | ||
23 | + protected $isThrow = false; | ||
24 | + | ||
19 | 25 | ||
20 | public function getClient() | 26 | public function getClient() |
21 | { | 27 | { |
22 | return $this->client; | 28 | return $this->client; |
23 | } | 29 | } |
24 | 30 | ||
31 | + /** | ||
32 | + * @return $this | ||
33 | + * @author:dc | ||
34 | + * @time 2024/7/24 9:09 | ||
35 | + */ | ||
36 | + public function throw(){ | ||
37 | + $this->isThrow = true; | ||
38 | + return $this; | ||
39 | + } | ||
40 | + | ||
25 | 41 | ||
26 | /** | 42 | /** |
27 | * 查询 | 43 | * 查询 |
@@ -46,6 +62,9 @@ trait DbQuery { | @@ -46,6 +62,9 @@ trait DbQuery { | ||
46 | $query = $this->getClient()->prepare($sql); | 62 | $query = $this->getClient()->prepare($sql); |
47 | $ret = $query->execute($params); | 63 | $ret = $query->execute($params); |
48 | }catch (\Throwable $e){ | 64 | }catch (\Throwable $e){ |
65 | + if($this->isThrow){ | ||
66 | + throw new DbException($e->getMessage(),$sql,$params,$e); | ||
67 | + } | ||
49 | logs([ | 68 | logs([ |
50 | $sql,$params, | 69 | $sql,$params, |
51 | $e->getMessage(), | 70 | $e->getMessage(), |
-
请 注册 或 登录 后发表评论