1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\EntityManager\RetryConnection; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
6
|
|
|
use Doctrine\DBAL\ParameterType; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
10
|
|
|
*/ |
11
|
|
|
class Statement implements \IteratorAggregate, DriverStatement |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var RetryConnection |
15
|
|
|
*/ |
16
|
|
|
private $connection; |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $params = []; |
21
|
|
|
/** |
22
|
|
|
* @var ShouldConnectionByRetried |
23
|
|
|
*/ |
24
|
|
|
private $shouldConnectionByRetried; |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $sql; |
29
|
|
|
/**y |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $values = []; |
34
|
|
|
/** |
35
|
|
|
* @TODO Required setting as mixed due to stan reasons, pending a better solution |
36
|
|
|
* @var mixed |
37
|
|
|
*/ |
38
|
|
|
private $wrappedStatement; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $sql |
42
|
|
|
* @param RetryConnection $conn |
43
|
|
|
* @param ShouldConnectionByRetried $shouldConnectionByRetried |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
string $sql, |
47
|
|
|
RetryConnection $conn, |
48
|
|
|
ShouldConnectionByRetried $shouldConnectionByRetried |
49
|
|
|
) { |
50
|
|
|
$this->sql = $sql; |
51
|
|
|
$this->connection = $conn; |
52
|
|
|
$this->shouldConnectionByRetried = $shouldConnectionByRetried; |
53
|
|
|
$this->createStatement(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create Statement. |
58
|
|
|
*/ |
59
|
|
|
private function createStatement() |
60
|
|
|
{ |
61
|
|
|
$this->wrappedStatement = $this->connection->prepareUnwrapped($this->sql); |
62
|
|
|
foreach ($this->params as $params) { |
63
|
|
|
$this->bindParam(...$params); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
$this->params = []; |
66
|
|
|
foreach ($this->values as $values) { |
67
|
|
|
$this->bindValue(...$values); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
$this->values = []; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
|
|
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) |
76
|
|
|
{ |
77
|
|
|
$this->params[] = [$column, $variable, $type, $length]; |
78
|
|
|
|
79
|
|
|
return $this->wrappedStatement->bindParam($column, $variable, $type, $length); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
|
|
public function bindValue($param, $value, $type = ParameterType::STRING) |
86
|
|
|
{ |
87
|
|
|
$this->values[] = [$param, $value, $type]; |
88
|
|
|
|
89
|
|
|
return $this->wrappedStatement->bindValue($param, $value, $type); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritdoc |
94
|
|
|
*/ |
95
|
|
|
public function closeCursor() |
96
|
|
|
{ |
97
|
|
|
return $this->wrappedStatement->closeCursor(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritdoc |
102
|
|
|
*/ |
103
|
|
|
public function columnCount() |
104
|
|
|
{ |
105
|
|
|
return $this->wrappedStatement->columnCount(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritdoc |
110
|
|
|
*/ |
111
|
|
|
public function errorCode() |
112
|
|
|
{ |
113
|
|
|
return $this->wrappedStatement->errorCode(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritdoc |
118
|
|
|
*/ |
119
|
|
|
public function errorInfo() |
120
|
|
|
{ |
121
|
|
|
return $this->wrappedStatement->errorInfo(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @inheritdoc |
126
|
|
|
*/ |
127
|
|
|
public function execute($params = null) |
128
|
|
|
{ |
129
|
|
|
$stmt = null; |
130
|
|
|
$attempt = 0; |
131
|
|
|
$retry = true; |
132
|
|
|
while ($retry) { |
133
|
|
|
$retry = false; |
134
|
|
|
try { |
135
|
|
|
$stmt = $this->wrappedStatement->execute($params); |
136
|
|
|
} catch (\Exception $e) { |
137
|
|
|
$nesting = $this->connection->getTransactionNestingLevel(); |
138
|
|
|
$retry = $this->shouldConnectionByRetried->checkAndSleep($e, $nesting, $attempt, false); |
139
|
|
|
if ($retry === false) { |
140
|
|
|
throw $e; |
141
|
|
|
} |
142
|
|
|
$this->connection->close(); |
143
|
|
|
$this->createStatement(); |
144
|
|
|
$attempt++; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $stmt; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritdoc |
153
|
|
|
*/ |
154
|
|
|
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
155
|
|
|
{ |
156
|
|
|
return $this->wrappedStatement->fetch($fetchMode, $cursorOrientation, $cursorOffset); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @inheritdoc |
161
|
|
|
*/ |
162
|
|
|
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
163
|
|
|
{ |
164
|
|
|
return $this->wrappedStatement->fetchAll($fetchMode, $fetchArgument, $ctorArgs); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @inheritdoc |
169
|
|
|
*/ |
170
|
|
|
public function fetchColumn($columnIndex = 0) |
171
|
|
|
{ |
172
|
|
|
return $this->wrappedStatement->fetchColumn($columnIndex); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @inheritdoc |
177
|
|
|
*/ |
178
|
|
|
public function getIterator() |
179
|
|
|
{ |
180
|
|
|
return $this->wrappedStatement->getIterator(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @inheritdoc |
185
|
|
|
*/ |
186
|
|
|
public function rowCount() |
187
|
|
|
{ |
188
|
|
|
return $this->wrappedStatement->rowCount(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @inheritdoc |
193
|
|
|
*/ |
194
|
|
|
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
195
|
|
|
{ |
196
|
|
|
return $this->wrappedStatement->setFetchMode($fetchMode, $arg2, $arg3); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.