|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SetBased\Stratum\MySql\Exception; |
|
5
|
|
|
|
|
6
|
|
|
use SetBased\Stratum\Middle\Exception\DataLayerException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Exception for situations where the execution of s SQL query has failed. |
|
10
|
|
|
*/ |
|
11
|
|
|
class MySqlDataLayerException extends \RuntimeException implements DataLayerException |
|
12
|
|
|
{ |
|
13
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
14
|
|
|
/** |
|
15
|
|
|
* The error code value of the error ($mysqli->errno). |
|
16
|
|
|
* |
|
17
|
|
|
* @var int |
|
18
|
|
|
*/ |
|
19
|
|
|
protected int $errno; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Description of the last error ($mysqli->error). |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected string $error; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The method. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected string $method; |
|
34
|
|
|
|
|
35
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
36
|
|
|
/** |
|
37
|
|
|
* Object constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param int $errno The error code value of the error ($mysqli->errno). |
|
40
|
|
|
* @param string $error Description of the last error ($mysqli->error). |
|
41
|
|
|
* @param string $method The name of the executed method. |
|
42
|
|
|
*/ |
|
43
|
8 |
|
public function __construct(int $errno, string $error, string $method) |
|
44
|
|
|
{ |
|
45
|
8 |
|
$this->errno = $errno; |
|
46
|
8 |
|
$this->error = $error; |
|
47
|
8 |
|
$this->method = $method; |
|
48
|
|
|
|
|
49
|
8 |
|
parent::__construct($this->implodeMessage($this->composerMessage())); |
|
50
|
8 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
53
|
|
|
/** |
|
54
|
|
|
* Returns the error code of the error |
|
55
|
|
|
* |
|
56
|
|
|
* @return int |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getErrno(): int |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->errno; |
|
61
|
|
|
} |
|
62
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
63
|
|
|
/** |
|
64
|
|
|
* Returns the description of the error. |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getError(): string |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->error; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
74
|
|
|
/** |
|
75
|
|
|
* @inheritdoc |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getName(): string |
|
78
|
|
|
{ |
|
79
|
|
|
return 'MySQL Error'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
83
|
|
|
/** |
|
84
|
|
|
* Composes the message of this exception as array of lines. |
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
4 |
|
protected function composerMessage(): array |
|
89
|
|
|
{ |
|
90
|
4 |
|
return array_merge($this->splitIntoTwoColumns('MySQL Errno', (string)$this->errno), |
|
91
|
4 |
|
$this->splitIntoTwoColumns('Error', $this->error), |
|
92
|
4 |
|
$this->splitIntoTwoColumns('Method', $this->method)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
96
|
|
|
/** |
|
97
|
|
|
* Splits a string (may contain multiple lines) into columns: The first columns will contain a header, the second |
|
98
|
|
|
* column the lines of the string. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $header The header. |
|
101
|
|
|
* @param string $string The string possible with multiple lines. |
|
102
|
|
|
* |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
8 |
|
protected function splitIntoTwoColumns(string $header, string $string): array |
|
106
|
|
|
{ |
|
107
|
8 |
|
$lines = []; |
|
108
|
|
|
|
|
109
|
8 |
|
$parts = explode(PHP_EOL, trim($string)); |
|
110
|
8 |
|
foreach ($parts as $i => $part) |
|
111
|
|
|
{ |
|
112
|
8 |
|
$lines[] = [($i===0 ? $header : ''), $part]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
8 |
|
return $lines; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
119
|
|
|
/** |
|
120
|
|
|
* Implodes an array with lines of an error message to a string. Each line of the each message consists out of two |
|
121
|
|
|
* columns. |
|
122
|
|
|
* |
|
123
|
|
|
* @param array $lines The lines of the error message. |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
8 |
|
private function implodeMessage(array $lines): string |
|
128
|
|
|
{ |
|
129
|
8 |
|
$max = 0; |
|
130
|
8 |
|
foreach ($lines as $line) |
|
131
|
|
|
{ |
|
132
|
8 |
|
$max = max($max, mb_strlen($line[0])); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
8 |
|
$format = sprintf('%%-%ds: %%s', $max); |
|
136
|
8 |
|
$tmp = ['']; |
|
137
|
8 |
|
foreach ($lines as $line) |
|
138
|
|
|
{ |
|
139
|
8 |
|
$tmp[] = sprintf($format, $line[0], $line[1]); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
8 |
|
return implode(PHP_EOL, $tmp); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
149
|
|
|
|