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::STANDARD_OBJECT => PDO::FETCH_OBJ, |
35
|
|
|
FetchMode::COLUMN => PDO::FETCH_COLUMN, |
36
|
|
|
FetchMode::CUSTOM_OBJECT => PDO::FETCH_CLASS, |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** @var \PDOStatement */ |
40
|
|
|
private $stmt; |
41
|
|
|
|
42
|
|
|
public function __construct(\PDOStatement $stmt) |
43
|
4526 |
|
{ |
44
|
|
|
$this->stmt = $stmt; |
45
|
4526 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
4495 |
|
public function setFetchMode($fetchMode, ...$args) |
51
|
|
|
{ |
52
|
4495 |
|
$fetchMode = $this->convertFetchMode($fetchMode); |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
return $this->stmt->setFetchMode($fetchMode, ...$args); |
56
|
|
|
} catch (\PDOException $exception) { |
57
|
|
|
throw new PDOException($exception); |
58
|
|
|
} |
59
|
4495 |
|
} |
60
|
4495 |
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
3271 |
|
*/ |
64
|
3271 |
|
public function bindValue($param, $value, $type = ParameterType::STRING) |
65
|
|
|
{ |
66
|
|
|
$type = $this->convertParamType($type); |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
return $this->stmt->bindValue($param, $value, $type); |
70
|
|
|
} catch (\PDOException $exception) { |
71
|
|
|
throw new PDOException($exception); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
4009 |
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
4009 |
|
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null) |
79
|
|
|
{ |
80
|
|
|
$type = $this->convertParamType($type); |
81
|
4009 |
|
|
82
|
|
|
try { |
83
|
|
|
return $this->stmt->bindParam($column, $variable, $type, ...array_slice(func_get_args(), 3)); |
|
|
|
|
84
|
|
|
} catch (\PDOException $exception) { |
85
|
|
|
throw new PDOException($exception); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
4142 |
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
4142 |
|
public function closeCursor() |
93
|
|
|
{ |
94
|
|
|
try { |
95
|
4142 |
|
return $this->stmt->closeCursor(); |
96
|
|
|
} catch (\PDOException $exception) { |
97
|
|
|
// Exceptions not allowed by the interface. |
98
|
|
|
// In case driver implementations do not adhere to the interface, silence exceptions here. |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function columnCount() |
104
|
2493 |
|
{ |
105
|
|
|
return $this->stmt->columnCount(); |
106
|
|
|
} |
107
|
2493 |
|
|
108
|
|
|
public function errorCode() |
109
|
|
|
{ |
110
|
|
|
return $this->stmt->errorCode(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function errorInfo() |
114
|
|
|
{ |
115
|
|
|
return $this->stmt->errorInfo(); |
116
|
|
|
} |
117
|
|
|
|
118
|
4363 |
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
4363 |
|
public function execute($params = null) |
122
|
3917 |
|
{ |
123
|
3162 |
|
try { |
124
|
|
|
return $this->stmt->execute($params); |
125
|
|
|
} catch (\PDOException $exception) { |
126
|
|
|
throw new PDOException($exception); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
4163 |
|
public function rowCount() : int |
131
|
|
|
{ |
132
|
4163 |
|
return $this->stmt->rowCount(); |
133
|
|
|
} |
134
|
4163 |
|
|
135
|
4154 |
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function fetch($fetchMode = null, ...$args) |
139
|
4163 |
|
{ |
140
|
|
|
try { |
141
|
|
|
if ($fetchMode === null) { |
142
|
|
|
return $this->stmt->fetch(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this->stmt->fetch( |
146
|
|
|
$this->convertFetchMode($fetchMode), |
147
|
|
|
...$args |
|
|
|
|
148
|
4353 |
|
); |
149
|
|
|
} catch (\PDOException $exception) { |
150
|
4353 |
|
throw new PDOException($exception); |
151
|
|
|
} |
152
|
4353 |
|
} |
153
|
4247 |
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
4353 |
|
*/ |
157
|
4083 |
|
public function fetchAll($fetchMode = null, ...$args) |
158
|
4247 |
|
{ |
159
|
4246 |
|
try { |
160
|
3302 |
|
if ($fetchMode === null) { |
161
|
3302 |
|
$data = $this->stmt->fetchAll(); |
162
|
|
|
} else { |
163
|
|
|
$data = $this->stmt->fetchAll( |
164
|
|
|
$this->convertFetchMode($fetchMode), |
165
|
|
|
...$args |
|
|
|
|
166
|
|
|
); |
167
|
4353 |
|
} |
168
|
4353 |
|
} catch (\PDOException $exception) { |
169
|
|
|
throw new PDOException($exception); |
170
|
4353 |
|
} |
171
|
|
|
|
172
|
|
|
assert(is_array($data)); |
173
|
|
|
|
174
|
|
|
return $data; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
4208 |
|
*/ |
180
|
|
|
public function fetchColumn($columnIndex = 0) |
181
|
|
|
{ |
182
|
4208 |
|
try { |
183
|
|
|
return $this->stmt->fetchColumn($columnIndex); |
184
|
|
|
} catch (\PDOException $exception) { |
185
|
|
|
throw new PDOException($exception); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Converts DBAL parameter type to PDO parameter type |
191
|
|
|
* |
192
|
|
|
* @param int $type Parameter type |
193
|
4246 |
|
*/ |
194
|
|
|
private function convertParamType(int $type) : int |
195
|
4246 |
|
{ |
196
|
|
|
if (! isset(self::PARAM_TYPE_MAP[$type])) { |
197
|
|
|
throw new InvalidArgumentException('Invalid parameter type: ' . $type); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return self::PARAM_TYPE_MAP[$type]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Converts DBAL fetch mode to PDO fetch mode |
205
|
4246 |
|
* |
206
|
|
|
* @param int $fetchMode Fetch mode |
207
|
|
|
*/ |
208
|
|
|
private function convertFetchMode(int $fetchMode) : int |
209
|
|
|
{ |
210
|
|
|
if (! isset(self::FETCH_MODE_MAP[$fetchMode])) { |
211
|
|
|
throw new InvalidArgumentException('Invalid fetch mode: ' . $fetchMode); |
212
|
|
|
} |
213
|
4495 |
|
|
214
|
|
|
return self::FETCH_MODE_MAP[$fetchMode]; |
215
|
4495 |
|
} |
216
|
|
|
|
217
|
2657 |
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
1 |
|
*/ |
220
|
2657 |
|
public function getIterator() |
221
|
2657 |
|
{ |
222
|
|
|
yield from $this->stmt; |
223
|
2657 |
|
} |
224
|
|
|
} |
225
|
|
|
|