DbException::setParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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