1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BsbDoctrineReconnect\DBAL; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\DBALException; |
6
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
7
|
|
|
use IteratorAggregate; |
8
|
|
|
use PDO; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Statement |
12
|
|
|
* |
13
|
|
|
* @package BsbDoctrineReconnect\DBAL |
14
|
|
|
*/ |
15
|
|
|
class Statement implements IteratorAggregate, DriverStatement |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $sql; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Doctrine\DBAL\Statement |
24
|
|
|
*/ |
25
|
|
|
private $stmt; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Connection |
29
|
|
|
*/ |
30
|
|
|
private $conn; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array binding values |
34
|
|
|
*/ |
35
|
|
|
private $values = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array binding parameters |
39
|
|
|
*/ |
40
|
|
|
private $params = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $sql |
44
|
|
|
* @param Connection $conn |
45
|
|
|
*/ |
46
|
|
|
public function __construct($sql, Connection $conn) |
47
|
|
|
{ |
48
|
|
|
$this->sql = $sql; |
49
|
|
|
$this->conn = $conn; |
50
|
|
|
|
51
|
|
|
$this->createStatement(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
private function createStatement() |
58
|
|
|
{ |
59
|
|
|
$this->stmt = $this->conn->prepareUnwrapped($this->sql); |
|
|
|
|
60
|
|
|
|
61
|
|
|
foreach ($this->values as $args) { |
62
|
|
|
$this->bindValue($args[0], $args[1], $args[2]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
foreach ($this->params as $args) { |
66
|
|
|
$this->bindParam($args[0], $args[1], $args[2]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param null $params |
72
|
|
|
* @return bool|null |
73
|
|
|
* @throws DBALException |
74
|
|
|
*/ |
75
|
|
|
public function execute($params = null) |
76
|
|
|
{ |
77
|
|
|
$stmt = null; |
78
|
|
|
$attempt = 0; |
79
|
|
|
$retry = true; |
80
|
|
|
|
81
|
|
|
while ($retry) { |
82
|
|
|
$retry = false; |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$stmt = $this->stmt->execute($params); |
86
|
|
|
} catch (DBALException $e) { |
87
|
|
View Code Duplication |
if ($this->conn->validateReconnectAttempt($e, $attempt)) { |
|
|
|
|
88
|
|
|
$this->conn->close(); |
89
|
|
|
$this->createStatement(); |
90
|
|
|
$attempt++; |
91
|
|
|
$retry = true; |
92
|
|
|
} else { |
93
|
|
|
throw $e; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $stmt; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param mixed $param |
103
|
|
|
* @param mixed $value |
104
|
|
|
* @param null $type |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function bindValue($param, $value, $type = null) |
108
|
|
|
{ |
109
|
|
|
$this->values[$param] = [$param, $value, $type]; |
110
|
|
|
|
111
|
|
|
return $this->stmt->bindValue($param, $value, $type); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param mixed $column |
116
|
|
|
* @param mixed $variable |
117
|
|
|
* @param int|null $type |
118
|
|
|
* @param null $length |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function bindParam($column, &$variable, $type = PDO::PARAM_STR, $length = null) |
122
|
|
|
{ |
123
|
|
|
$this->values[$column] = [$column, &$variable, $type]; |
124
|
|
|
|
125
|
|
|
return $this->stmt->bindParam($column, $variable, $type); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function closeCursor() |
132
|
|
|
{ |
133
|
|
|
return $this->stmt->closeCursor(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return int |
138
|
|
|
*/ |
139
|
|
|
public function columnCount() |
140
|
|
|
{ |
141
|
|
|
return $this->stmt->columnCount(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function errorCode() |
148
|
|
|
{ |
149
|
|
|
return $this->stmt->errorCode(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
public function errorInfo() |
156
|
|
|
{ |
157
|
|
|
return $this->stmt->errorInfo(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param int|null $fetchStyle |
162
|
|
|
* @return mixed |
163
|
|
|
*/ |
164
|
|
|
public function fetch($fetchStyle = PDO::FETCH_BOTH) |
165
|
|
|
{ |
166
|
|
|
return $this->stmt->fetch($fetchStyle); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param int|null $fetchStyle |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
|
|
public function fetchAll($fetchStyle = PDO::FETCH_BOTH) |
174
|
|
|
{ |
175
|
|
|
return $this->stmt->fetchAll($fetchStyle); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param int $columnIndex |
180
|
|
|
* @return mixed |
181
|
|
|
*/ |
182
|
|
|
public function fetchColumn($columnIndex = 0) |
183
|
|
|
{ |
184
|
|
|
return $this->stmt->fetchColumn($columnIndex); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return int |
189
|
|
|
*/ |
190
|
|
|
public function rowCount() |
191
|
|
|
{ |
192
|
|
|
return $this->stmt->rowCount(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
|
|
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
199
|
|
|
{ |
200
|
|
|
// This thin wrapper is necessary to shield against the weird signature |
201
|
|
|
// of PDOStatement::setFetchMode(): even if the second and third |
202
|
|
|
// parameters are optional, PHP will not let us remove it from this |
203
|
|
|
// declaration. |
204
|
|
|
if ($arg2 === null && $arg3 === null) { |
205
|
|
|
return parent::setFetchMode($fetchMode); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if ($arg3 === null) { |
209
|
|
|
return parent::setFetchMode($fetchMode, $arg2); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return parent::setFetchMode($fetchMode, $arg2, $arg3); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Required by interface IteratorAggregate. |
217
|
|
|
* |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public function getIterator() |
221
|
|
|
{ |
222
|
|
|
return $this->stmt; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
This method has been deprecated.