1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Driver; |
21
|
|
|
|
22
|
|
|
use IteratorAggregate; |
23
|
|
|
use PDO; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The PDO implementation of the Statement interface. |
27
|
|
|
* Used by all PDO-based drivers. |
28
|
|
|
* |
29
|
|
|
* @since 2.0 |
30
|
|
|
*/ |
31
|
|
|
class PDOStatement implements IteratorAggregate, Statement |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var array<int,int> |
35
|
|
|
*/ |
36
|
|
|
private static $paramTypeMap = [ |
37
|
|
|
self::PARAM_NULL => PDO::PARAM_NULL, |
38
|
|
|
self::PARAM_INT => PDO::PARAM_INT, |
39
|
|
|
self::PARAM_STR => PDO::PARAM_STR, |
40
|
|
|
self::PARAM_LOB => PDO::PARAM_LOB, |
41
|
|
|
self::PARAM_BOOL => PDO::PARAM_BOOL, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array<int,int> |
46
|
|
|
*/ |
47
|
|
|
private static $fetchModeMap = [ |
48
|
|
|
self::FETCH_ASSOC => PDO::FETCH_ASSOC, |
49
|
|
|
self::FETCH_NUM => PDO::FETCH_NUM, |
50
|
|
|
self::FETCH_BOTH => PDO::FETCH_BOTH, |
51
|
|
|
self::FETCH_OBJ => PDO::FETCH_OBJ, |
52
|
|
|
self::FETCH_COLUMN => PDO::FETCH_COLUMN, |
53
|
|
|
self::FETCH_CLASS => PDO::FETCH_CLASS, |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var \PDOStatement |
58
|
|
|
*/ |
59
|
|
|
private $stmt; |
60
|
|
|
|
61
|
234 |
|
public function __construct(\PDOStatement $stmt) |
62
|
|
|
{ |
63
|
234 |
|
$this->stmt = $stmt; |
64
|
234 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
222 |
|
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
70
|
|
|
{ |
71
|
222 |
|
$fetchMode = $this->convertFetchMode($fetchMode); |
72
|
|
|
|
73
|
|
|
// This thin wrapper is necessary to shield against the weird signature |
74
|
|
|
// of PDOStatement::setFetchMode(): even if the second and third |
75
|
|
|
// parameters are optional, PHP will not let us remove it from this |
76
|
|
|
// declaration. |
77
|
|
|
try { |
78
|
222 |
|
if ($arg2 === null && $arg3 === null) { |
79
|
222 |
|
return $this->stmt->setFetchMode($fetchMode); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
if ($arg3 === null) { |
83
|
2 |
|
return $this->stmt->setFetchMode($fetchMode, $arg2); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3); |
87
|
|
|
} catch (\PDOException $exception) { |
88
|
|
|
throw new PDOException($exception); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
33 |
|
public function bindValue($param, $value, $type = self::PARAM_STR) |
96
|
|
|
{ |
97
|
33 |
|
$type = $this->convertParamType($type); |
98
|
|
|
|
99
|
|
|
try { |
100
|
33 |
|
return $this->stmt->bindValue($param, $value, $type); |
101
|
|
|
} catch (\PDOException $exception) { |
102
|
|
|
throw new PDOException($exception); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
7 |
|
public function bindParam($column, &$variable, $type = self::PARAM_STR, $length = null, $driverOptions = null) |
110
|
|
|
{ |
111
|
7 |
|
$type = $this->convertParamType($type); |
112
|
|
|
|
113
|
|
|
try { |
114
|
7 |
|
return $this->stmt->bindParam($column, $variable, $type, $length, $driverOptions); |
115
|
|
|
} catch (\PDOException $exception) { |
116
|
|
|
throw new PDOException($exception); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
20 |
|
public function closeCursor() |
124
|
|
|
{ |
125
|
|
|
try { |
126
|
20 |
|
return $this->stmt->closeCursor(); |
127
|
|
|
} catch (\PDOException $exception) { |
128
|
|
|
// Exceptions not allowed by the interface. |
129
|
|
|
// In case driver implementations do not adhere to the interface, silence exceptions here. |
130
|
|
|
return true; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
4 |
|
public function columnCount() |
138
|
|
|
{ |
139
|
4 |
|
return $this->stmt->columnCount(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function errorCode() |
146
|
|
|
{ |
147
|
|
|
return $this->stmt->errorCode(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function errorInfo() |
154
|
|
|
{ |
155
|
|
|
return $this->stmt->errorInfo(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
120 |
|
public function execute($params = null) |
162
|
|
|
{ |
163
|
|
|
try { |
164
|
120 |
|
return $this->stmt->execute($params); |
165
|
7 |
|
} catch (\PDOException $exception) { |
166
|
3 |
|
throw new PDOException($exception); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
83 |
|
public function rowCount() |
174
|
|
|
{ |
175
|
83 |
|
return $this->stmt->rowCount(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
68 |
|
public function fetch($fetchMode = null) |
182
|
|
|
{ |
183
|
68 |
|
$fetchMode = $this->convertFetchMode($fetchMode); |
184
|
|
|
|
185
|
|
|
try { |
186
|
68 |
|
if ($fetchMode === null) { |
187
|
9 |
|
return $this->stmt->fetch(); |
188
|
|
|
} |
189
|
|
|
|
190
|
59 |
|
return $this->stmt->fetch($fetchMode); |
191
|
|
|
} catch (\PDOException $exception) { |
192
|
|
|
throw new PDOException($exception); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
103 |
|
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
200
|
|
|
{ |
201
|
103 |
|
$fetchMode = $this->convertFetchMode($fetchMode); |
202
|
|
|
|
203
|
|
|
try { |
204
|
103 |
|
if ($fetchMode === null && null === $fetchArgument && null === $ctorArgs) { |
205
|
90 |
|
return $this->stmt->fetchAll(); |
206
|
|
|
} |
207
|
|
|
|
208
|
55 |
|
if (null === $fetchArgument && null === $ctorArgs) { |
209
|
54 |
|
return $this->stmt->fetchAll($fetchMode); |
210
|
|
|
} |
211
|
|
|
|
212
|
1 |
|
if (null === $ctorArgs) { |
213
|
1 |
|
return $this->stmt->fetchAll($fetchMode, $fetchArgument); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this->stmt->fetchAll($fetchMode, $fetchArgument, $ctorArgs); |
217
|
|
|
} catch (\PDOException $exception) { |
218
|
|
|
throw new PDOException($exception); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritdoc} |
224
|
|
|
*/ |
225
|
42 |
|
public function fetchColumn($columnIndex = 0) |
226
|
|
|
{ |
227
|
|
|
try { |
228
|
42 |
|
return $this->stmt->fetchColumn($columnIndex); |
229
|
|
|
} catch (\PDOException $exception) { |
230
|
|
|
throw new PDOException($exception); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Converts DBAL parameter type to PDO parameter type |
236
|
|
|
* |
237
|
|
|
* @param int $type Parameter type |
238
|
|
|
* @return int |
239
|
|
|
*/ |
240
|
40 |
|
private function convertParamType(int $type) : int |
241
|
|
|
{ |
242
|
40 |
|
if ( ! isset(self::$paramTypeMap[$type])) { |
243
|
|
|
throw new \InvalidArgumentException('Invalid parameter type: ' . $type); |
244
|
|
|
} |
245
|
|
|
|
246
|
40 |
|
return self::$paramTypeMap[$type]; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Converts DBAL fetch mode to PDO fetch mode |
251
|
|
|
* |
252
|
|
|
* @param int|null $fetchMode Fetch mode |
253
|
|
|
* @return int|null |
254
|
|
|
*/ |
255
|
222 |
|
private function convertFetchMode(?int $fetchMode) : ?int |
256
|
|
|
{ |
257
|
222 |
|
if ($fetchMode === null) { |
258
|
99 |
|
return null; |
259
|
|
|
} |
260
|
|
|
|
261
|
222 |
|
if ( ! isset(self::$fetchModeMap[$fetchMode])) { |
262
|
|
|
throw new \InvalidArgumentException('Invalid fetch mode: ' . $fetchMode); |
263
|
|
|
} |
264
|
|
|
|
265
|
222 |
|
return self::$fetchModeMap[$fetchMode]; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* {@inheritdoc} |
270
|
|
|
*/ |
271
|
1 |
|
public function getIterator() |
272
|
|
|
{ |
273
|
1 |
|
yield from $this->stmt; |
274
|
1 |
|
} |
275
|
|
|
} |
276
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more 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.