|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Adgoal\DBALFaultTolerance; |
|
6
|
|
|
|
|
7
|
|
|
use Adgoal\DBALFaultTolerance\Events\Args\ReconnectEventArgs; |
|
8
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
|
9
|
|
|
use IteratorAggregate; |
|
10
|
|
|
use PDO; |
|
11
|
|
|
use Throwable; |
|
12
|
|
|
use Traversable; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Statement. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Statement implements IteratorAggregate, DriverStatement |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $sql; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \Doctrine\DBAL\Statement |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $stmt; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ConnectionInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $conn; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $boundValues = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private $boundParams = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array|null |
|
46
|
|
|
*/ |
|
47
|
|
|
private $fetchMode; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Statement constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $sql |
|
53
|
|
|
* @param ConnectionInterface $conn |
|
54
|
|
|
*/ |
|
55
|
8 |
|
public function __construct(string $sql, ConnectionInterface $conn) |
|
56
|
|
|
{ |
|
57
|
8 |
|
$this->sql = $sql; |
|
58
|
8 |
|
$this->conn = $conn; |
|
59
|
8 |
|
$this->createStatement(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param array|null $params |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
* |
|
67
|
|
|
* @throws Throwable |
|
68
|
|
|
*/ |
|
69
|
6 |
|
public function execute($params = null) |
|
70
|
|
|
{ |
|
71
|
6 |
|
$stmt = false; |
|
72
|
6 |
|
$attempt = 0; |
|
73
|
6 |
|
$retry = true; |
|
74
|
6 |
|
while ($retry) { |
|
75
|
6 |
|
$retry = false; |
|
76
|
|
|
|
|
77
|
|
|
try { |
|
78
|
6 |
|
$stmt = $this->stmt->execute($params); |
|
79
|
5 |
|
} catch (Throwable $e) { |
|
80
|
|
|
if ( |
|
81
|
5 |
|
$this->conn->canTryAgain($attempt) |
|
82
|
|
|
&& |
|
83
|
5 |
|
$this->conn->isRetryableException($e, $this->sql) |
|
84
|
|
|
) { |
|
85
|
4 |
|
$this->conn->close(); |
|
86
|
4 |
|
$this->recreateStatement(); |
|
87
|
4 |
|
++$attempt; |
|
88
|
4 |
|
$retry = true; |
|
89
|
|
|
|
|
90
|
4 |
|
$this->conn->getEventManager()->dispatchEvent( |
|
91
|
4 |
|
Events\Events::RECONNECT_TO_DATABASE, |
|
92
|
4 |
|
new ReconnectEventArgs(__FUNCTION__, $attempt, $this->sql) |
|
93
|
|
|
); |
|
94
|
|
|
} else { |
|
95
|
2 |
|
throw $e; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
4 |
|
return $stmt; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $name |
|
105
|
|
|
* @param mixed $value |
|
106
|
|
|
* @param mixed $type |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
3 |
|
public function bindValue($name, $value, $type = PDO::PARAM_STR) |
|
111
|
|
|
{ |
|
112
|
3 |
|
if ($this->stmt->bindValue($name, $value, $type)) { |
|
113
|
2 |
|
$this->boundValues[$name] = [$name, $value, $type]; |
|
114
|
|
|
|
|
115
|
2 |
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $name |
|
123
|
|
|
* @param mixed $var |
|
124
|
|
|
* @param int $type |
|
125
|
|
|
* @param int|null $length |
|
126
|
|
|
* |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
3 |
|
public function bindParam($name, &$var, $type = PDO::PARAM_STR, $length = null) |
|
130
|
|
|
{ |
|
131
|
3 |
|
if ($this->stmt->bindParam($name, $var, $type, $length)) { |
|
132
|
2 |
|
$this->boundParams[$name] = [$name, &$var, $type, $length]; |
|
133
|
|
|
|
|
134
|
2 |
|
return true; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
return false; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
public function closeCursor() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->stmt->closeCursor(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return int |
|
150
|
|
|
*/ |
|
151
|
|
|
public function columnCount() |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->stmt->columnCount(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return string|int|bool |
|
158
|
|
|
*/ |
|
159
|
|
|
public function errorCode() |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->stmt->errorCode(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return array |
|
166
|
|
|
*/ |
|
167
|
|
|
public function errorInfo() |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->stmt->errorInfo(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param int $fetchMode |
|
174
|
|
|
* @param mixed $arg2 |
|
175
|
|
|
* @param mixed $arg3 |
|
176
|
|
|
* |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
179
|
3 |
|
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
|
180
|
|
|
{ |
|
181
|
3 |
|
if ($this->stmt->setFetchMode($fetchMode, $arg2, $arg3)) { |
|
182
|
2 |
|
$this->fetchMode = [$fetchMode, $arg2, $arg3]; |
|
183
|
|
|
|
|
184
|
2 |
|
return true; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
1 |
|
return false; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return Traversable |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getIterator() |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->stmt; |
|
|
|
|
|
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param int|null $fetchMode |
|
200
|
|
|
* @param int $cursorOrientation Only for doctrine/DBAL >= 2.6 |
|
201
|
|
|
* @param int $cursorOffset Only for doctrine/DBAL >= 2.6 |
|
202
|
|
|
* |
|
203
|
|
|
* @return mixed |
|
204
|
|
|
*/ |
|
205
|
|
|
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->stmt->fetch($fetchMode, $cursorOrientation, $cursorOffset); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param int|null $fetchMode |
|
212
|
|
|
* @param int $fetchArgument Only for doctrine/DBAL >= 2.6 |
|
213
|
|
|
* @param null $ctorArgs Only for doctrine/DBAL >= 2.6 |
|
214
|
|
|
* |
|
215
|
|
|
* @return mixed |
|
216
|
|
|
*/ |
|
217
|
|
|
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
|
218
|
|
|
{ |
|
219
|
|
|
return $this->stmt->fetchAll($fetchMode, $fetchArgument, $ctorArgs); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @param int $columnIndex |
|
224
|
|
|
* |
|
225
|
|
|
* @return mixed |
|
226
|
|
|
*/ |
|
227
|
|
|
public function fetchColumn($columnIndex = 0) |
|
228
|
|
|
{ |
|
229
|
|
|
return $this->stmt->fetchColumn($columnIndex); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @return int |
|
234
|
|
|
*/ |
|
235
|
|
|
public function rowCount() |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->stmt->rowCount(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Create Statement. |
|
242
|
|
|
*/ |
|
243
|
8 |
|
private function createStatement() |
|
244
|
|
|
{ |
|
245
|
8 |
|
$this->stmt = $this->conn->prepareUnwrapped($this->sql); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Recreate statement for retry. |
|
250
|
|
|
*/ |
|
251
|
4 |
|
private function recreateStatement() |
|
252
|
|
|
{ |
|
253
|
4 |
|
$this->createStatement(); |
|
254
|
4 |
|
if ($this->fetchMode !== null) { |
|
255
|
1 |
|
call_user_func_array([$this->stmt, 'setFetchMode'], $this->fetchMode); |
|
256
|
|
|
} |
|
257
|
4 |
|
foreach ($this->boundValues as $boundValue) { |
|
258
|
1 |
|
call_user_func_array([$this->stmt, 'bindValue'], $boundValue); |
|
259
|
|
|
} |
|
260
|
4 |
|
foreach ($this->boundParams as $boundParam) { |
|
261
|
1 |
|
call_user_func_array([$this->stmt, 'bindParam'], $boundParam); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.