|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* General db exception class |
|
4
|
|
|
* |
|
5
|
|
|
* @file DbException.php |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 8.0+ |
|
8
|
|
|
* |
|
9
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
10
|
|
|
* @copyright © 2012-2021 Alexander Yancharuk |
|
11
|
|
|
* @date Птн Мар 09 01:40:46 2012 |
|
12
|
|
|
* @license The BSD 3-Clause License |
|
13
|
|
|
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Veles\DataBase\Exceptions; |
|
17
|
|
|
|
|
18
|
|
|
use Exception; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* General db exception class |
|
22
|
|
|
* |
|
23
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
24
|
|
|
*/ |
|
25
|
|
|
class DbException extends Exception |
|
26
|
|
|
{ |
|
27
|
|
|
protected $ansi_code; |
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
protected $sql = ''; |
|
30
|
|
|
/** @var array */ |
|
31
|
|
|
protected $params = []; |
|
32
|
|
|
|
|
33
|
14 |
|
public function __construct($msg, $code, $exception) |
|
34
|
|
|
{ |
|
35
|
14 |
|
parent::__construct($msg, $code, $exception); |
|
36
|
|
|
|
|
37
|
14 |
|
if ($exception instanceof \PDOException) { |
|
38
|
14 |
|
$pattern1 = '/SQLSTATE\[(.+)\]:[\s\w]+: ([\w\d]+) ([\s\S]+)$/'; |
|
39
|
14 |
|
$pattern2 = '/SQLSTATE\[(.+)\] \[([\w\d]+)\] ([\s\S]+)/'; |
|
40
|
|
|
|
|
41
|
14 |
|
preg_match($pattern1, $exception->getMessage(), $match) |
|
42
|
14 |
|
|| preg_match($pattern2, $exception->getMessage(), $match); |
|
43
|
|
|
|
|
44
|
14 |
|
$this->setAnsiCode($match[1]); |
|
45
|
14 |
|
$this->code = (int) $match[2]; |
|
46
|
14 |
|
$this->message = $match[3]; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return mixed |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function getAnsiCode() |
|
54
|
|
|
{ |
|
55
|
1 |
|
return $this->ansi_code; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param mixed $ansi_code |
|
60
|
|
|
*/ |
|
61
|
11 |
|
public function setAnsiCode($ansi_code) |
|
62
|
|
|
{ |
|
63
|
11 |
|
$this->ansi_code = $ansi_code; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function getSql() |
|
70
|
|
|
{ |
|
71
|
1 |
|
return $this->sql; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $sql |
|
76
|
|
|
*/ |
|
77
|
6 |
|
public function setSql($sql) |
|
78
|
|
|
{ |
|
79
|
6 |
|
$this->sql = $sql; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function getParams() |
|
86
|
|
|
{ |
|
87
|
1 |
|
return $this->params; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param array $params |
|
92
|
|
|
*/ |
|
93
|
6 |
|
public function setParams(array $params) |
|
94
|
|
|
{ |
|
95
|
6 |
|
$this->params = $params; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|