1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Driver; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\FetchMode; |
6
|
|
|
use Doctrine\DBAL\ParameterType; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use IteratorAggregate; |
9
|
|
|
use PDO; |
10
|
|
|
use function array_slice; |
11
|
|
|
use function assert; |
12
|
|
|
use function func_get_args; |
13
|
|
|
use function is_array; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The PDO implementation of the Statement interface. |
17
|
|
|
* Used by all PDO-based drivers. |
18
|
|
|
*/ |
19
|
|
|
class PDOStatement implements IteratorAggregate, Statement |
20
|
|
|
{ |
21
|
|
|
private const PARAM_TYPE_MAP = [ |
22
|
|
|
ParameterType::NULL => PDO::PARAM_NULL, |
23
|
|
|
ParameterType::INTEGER => PDO::PARAM_INT, |
24
|
|
|
ParameterType::STRING => PDO::PARAM_STR, |
25
|
|
|
ParameterType::BINARY => PDO::PARAM_LOB, |
26
|
|
|
ParameterType::LARGE_OBJECT => PDO::PARAM_LOB, |
27
|
|
|
ParameterType::BOOLEAN => PDO::PARAM_BOOL, |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
private const FETCH_MODE_MAP = [ |
31
|
|
|
FetchMode::ASSOCIATIVE => PDO::FETCH_ASSOC, |
32
|
|
|
FetchMode::NUMERIC => PDO::FETCH_NUM, |
33
|
|
|
FetchMode::MIXED => PDO::FETCH_BOTH, |
34
|
|
|
FetchMode::COLUMN => PDO::FETCH_COLUMN, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** @var \PDOStatement */ |
38
|
|
|
private $stmt; |
39
|
|
|
|
40
|
4124 |
|
public function __construct(\PDOStatement $stmt) |
41
|
|
|
{ |
42
|
4124 |
|
$this->stmt = $stmt; |
43
|
4124 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
3881 |
|
public function setFetchMode($fetchMode) |
49
|
|
|
{ |
50
|
3881 |
|
$fetchMode = $this->convertFetchMode($fetchMode); |
51
|
|
|
|
52
|
|
|
try { |
53
|
3881 |
|
return $this->stmt->setFetchMode($fetchMode); |
54
|
|
|
} catch (\PDOException $exception) { |
55
|
|
|
throw new PDOException($exception); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
1782 |
|
public function bindValue($param, $value, $type = ParameterType::STRING) |
63
|
|
|
{ |
64
|
1782 |
|
$type = $this->convertParamType($type); |
65
|
|
|
|
66
|
|
|
try { |
67
|
1782 |
|
return $this->stmt->bindValue($param, $value, $type); |
68
|
52 |
|
} catch (\PDOException $exception) { |
69
|
|
|
throw new PDOException($exception); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param mixed $column |
75
|
|
|
* @param mixed $variable |
76
|
|
|
* @param int $type |
77
|
|
|
* @param int|null $length |
78
|
|
|
* @param mixed $driverOptions |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
104 |
|
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null) |
83
|
|
|
{ |
84
|
104 |
|
$type = $this->convertParamType($type); |
85
|
|
|
|
86
|
|
|
try { |
87
|
104 |
|
return $this->stmt->bindParam($column, $variable, $type, ...array_slice(func_get_args(), 3)); |
|
|
|
|
88
|
|
|
} catch (\PDOException $exception) { |
89
|
|
|
throw new PDOException($exception); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
281 |
|
public function closeCursor() |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
281 |
|
return $this->stmt->closeCursor(); |
100
|
|
|
} catch (\PDOException $exception) { |
101
|
|
|
// Exceptions not allowed by the interface. |
102
|
|
|
// In case driver implementations do not adhere to the interface, silence exceptions here. |
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
52 |
|
public function columnCount() |
111
|
|
|
{ |
112
|
52 |
|
return $this->stmt->columnCount(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function errorCode() |
119
|
|
|
{ |
120
|
|
|
return $this->stmt->errorCode(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function errorInfo() |
127
|
|
|
{ |
128
|
|
|
return $this->stmt->errorInfo(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
1879 |
|
public function execute($params = null) |
135
|
|
|
{ |
136
|
|
|
try { |
137
|
1879 |
|
return $this->stmt->execute($params); |
138
|
89 |
|
} catch (\PDOException $exception) { |
139
|
89 |
|
throw new PDOException($exception); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
1219 |
|
public function rowCount() : int |
144
|
|
|
{ |
145
|
1219 |
|
return $this->stmt->rowCount(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
1648 |
|
public function fetch($fetchMode = null) |
152
|
|
|
{ |
153
|
|
|
try { |
154
|
1648 |
|
if ($fetchMode === null) { |
155
|
104 |
|
return $this->stmt->fetch(); |
156
|
|
|
} |
157
|
|
|
|
158
|
1544 |
|
return $this->stmt->fetch( |
159
|
1544 |
|
$this->convertFetchMode($fetchMode) |
160
|
|
|
); |
161
|
|
|
} catch (\PDOException $exception) { |
162
|
|
|
throw new PDOException($exception); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
1950 |
|
public function fetchAll($fetchMode = null) |
170
|
|
|
{ |
171
|
|
|
try { |
172
|
1950 |
|
if ($fetchMode === null) { |
173
|
1729 |
|
$data = $this->stmt->fetchAll(); |
174
|
|
|
} else { |
175
|
492 |
|
$data = $this->stmt->fetchAll( |
176
|
1950 |
|
$this->convertFetchMode($fetchMode) |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
} catch (\PDOException $exception) { |
180
|
|
|
throw new PDOException($exception); |
181
|
|
|
} |
182
|
|
|
|
183
|
1950 |
|
assert(is_array($data)); |
184
|
|
|
|
185
|
1950 |
|
return $data; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
1132 |
|
public function fetchColumn() |
192
|
|
|
{ |
193
|
|
|
try { |
194
|
1132 |
|
return $this->stmt->fetchColumn(); |
195
|
|
|
} catch (\PDOException $exception) { |
196
|
|
|
throw new PDOException($exception); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Converts DBAL parameter type to PDO parameter type |
202
|
|
|
* |
203
|
|
|
* @param int $type Parameter type |
204
|
|
|
*/ |
205
|
1860 |
|
private function convertParamType(int $type) : int |
206
|
|
|
{ |
207
|
1860 |
|
if (! isset(self::PARAM_TYPE_MAP[$type])) { |
208
|
|
|
throw new InvalidArgumentException('Invalid parameter type: ' . $type); |
209
|
|
|
} |
210
|
|
|
|
211
|
1860 |
|
return self::PARAM_TYPE_MAP[$type]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Converts DBAL fetch mode to PDO fetch mode |
216
|
|
|
* |
217
|
|
|
* @param int $fetchMode Fetch mode |
218
|
|
|
*/ |
219
|
3881 |
|
private function convertFetchMode(int $fetchMode) : int |
220
|
|
|
{ |
221
|
3881 |
|
if (! isset(self::FETCH_MODE_MAP[$fetchMode])) { |
222
|
|
|
throw new InvalidArgumentException('Invalid fetch mode: ' . $fetchMode); |
223
|
|
|
} |
224
|
|
|
|
225
|
3881 |
|
return self::FETCH_MODE_MAP[$fetchMode]; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* {@inheritdoc} |
230
|
|
|
*/ |
231
|
13 |
|
public function getIterator() |
232
|
|
|
{ |
233
|
13 |
|
yield from $this->stmt; |
234
|
13 |
|
} |
235
|
|
|
} |
236
|
|
|
|