|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Driver; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\DBALException; |
|
8
|
|
|
use Doctrine\DBAL\FetchMode; |
|
9
|
|
|
use Doctrine\DBAL\ParameterType; |
|
10
|
|
|
use IteratorAggregate; |
|
11
|
|
|
use PDO; |
|
12
|
|
|
use const E_USER_DEPRECATED; |
|
13
|
|
|
use function array_slice; |
|
14
|
|
|
use function assert; |
|
15
|
|
|
use function count; |
|
16
|
|
|
use function func_get_args; |
|
17
|
|
|
use function is_array; |
|
18
|
|
|
use function sprintf; |
|
19
|
|
|
use function trigger_error; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The PDO implementation of the Statement interface. |
|
23
|
|
|
* Used by all PDO-based drivers. |
|
24
|
|
|
*/ |
|
25
|
|
|
class PDOStatement implements IteratorAggregate, Statement |
|
26
|
|
|
{ |
|
27
|
|
|
private const PARAM_TYPE_MAP = [ |
|
28
|
|
|
ParameterType::NULL => PDO::PARAM_NULL, |
|
29
|
|
|
ParameterType::INTEGER => PDO::PARAM_INT, |
|
30
|
|
|
ParameterType::STRING => PDO::PARAM_STR, |
|
31
|
|
|
ParameterType::BINARY => PDO::PARAM_LOB, |
|
32
|
|
|
ParameterType::LARGE_OBJECT => PDO::PARAM_LOB, |
|
33
|
|
|
ParameterType::BOOLEAN => PDO::PARAM_BOOL, |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
private const FETCH_MODE_MAP = [ |
|
37
|
|
|
FetchMode::ASSOCIATIVE => PDO::FETCH_ASSOC, |
|
38
|
|
|
FetchMode::NUMERIC => PDO::FETCH_NUM, |
|
39
|
|
|
FetchMode::MIXED => PDO::FETCH_BOTH, |
|
40
|
|
|
FetchMode::STANDARD_OBJECT => PDO::FETCH_OBJ, |
|
41
|
|
|
FetchMode::COLUMN => PDO::FETCH_COLUMN, |
|
42
|
|
|
FetchMode::CUSTOM_OBJECT => PDO::FETCH_CLASS, |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
3967 |
|
/** @var \PDOStatement */ |
|
46
|
|
|
private $stmt; |
|
47
|
3967 |
|
|
|
48
|
3967 |
|
public function __construct(\PDOStatement $stmt) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->stmt = $stmt; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3952 |
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
3952 |
|
*/ |
|
56
|
|
|
public function setFetchMode($fetchMode, ...$args) : void |
|
57
|
|
|
{ |
|
58
|
3952 |
|
$fetchMode = $this->convertFetchMode($fetchMode); |
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
|
|
$this->stmt->setFetchMode($fetchMode, ...$args); |
|
62
|
3952 |
|
} catch (\PDOException $exception) { |
|
63
|
|
|
throw new PDOException($exception); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3695 |
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
3695 |
|
*/ |
|
70
|
|
|
public function bindValue($param, $value, $type = ParameterType::STRING) : void |
|
71
|
|
|
{ |
|
72
|
3695 |
|
$type = $this->convertParamType($type); |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
|
|
$this->stmt->bindValue($param, $value, $type); |
|
76
|
3695 |
|
} catch (\PDOException $exception) { |
|
77
|
|
|
throw new PDOException($exception); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
3857 |
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
3857 |
|
*/ |
|
84
|
|
|
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null) : void |
|
85
|
|
|
{ |
|
86
|
3857 |
|
$type = $this->convertParamType($type); |
|
87
|
|
|
$extraParameters = array_slice(func_get_args(), 3); |
|
88
|
|
|
|
|
89
|
|
|
if (count($extraParameters) !== 0) { |
|
90
|
3857 |
|
$extraParameters[0] = $extraParameters[0] ?? 0; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
try { |
|
94
|
|
|
$this->stmt->bindParam($column, $variable, $type, ...$extraParameters); |
|
|
|
|
|
|
95
|
2167 |
|
} catch (\PDOException $exception) { |
|
96
|
|
|
throw new PDOException($exception); |
|
97
|
2167 |
|
} |
|
98
|
2167 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
2804 |
|
public function closeCursor() : void |
|
104
|
|
|
{ |
|
105
|
2804 |
|
$this->stmt->closeCursor(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function columnCount() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->stmt->columnCount(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
|
|
public function execute($params = null) : void |
|
120
|
|
|
{ |
|
121
|
|
|
try { |
|
122
|
|
|
$this->stmt->execute($params); |
|
123
|
|
|
} catch (\PDOException $exception) { |
|
124
|
|
|
throw new PDOException($exception); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
3956 |
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritdoc} |
|
130
|
3956 |
|
*/ |
|
131
|
3657 |
|
public function rowCount() : int |
|
132
|
2790 |
|
{ |
|
133
|
|
|
return $this->stmt->rowCount(); |
|
134
|
3956 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* {@inheritdoc} |
|
138
|
|
|
*/ |
|
139
|
3927 |
|
public function fetch($fetchMode = null, ...$args) |
|
140
|
|
|
{ |
|
141
|
3927 |
|
try { |
|
142
|
|
|
if ($fetchMode === null) { |
|
143
|
|
|
return $this->stmt->fetch(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$fetchMode = $this->convertFetchMode($fetchMode); |
|
147
|
3792 |
|
|
|
148
|
|
|
return $this->stmt->fetch($fetchMode, ...$args); |
|
|
|
|
|
|
149
|
|
|
} catch (\PDOException $exception) { |
|
150
|
3792 |
|
throw new PDOException($exception); |
|
151
|
3627 |
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
3792 |
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
3792 |
|
*/ |
|
157
|
|
|
public function fetchAll($fetchMode = null, ...$args) |
|
158
|
|
|
{ |
|
159
|
|
|
try { |
|
160
|
|
|
if ($fetchMode === null) { |
|
161
|
|
|
$data = $this->stmt->fetchAll(); |
|
162
|
|
|
} else { |
|
163
|
|
|
$data = $this->stmt->fetchAll( |
|
164
|
|
|
$this->convertFetchMode($fetchMode), |
|
165
|
3951 |
|
...$args |
|
|
|
|
|
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
3951 |
|
} catch (\PDOException $exception) { |
|
169
|
3726 |
|
throw new PDOException($exception); |
|
170
|
|
|
} |
|
171
|
3941 |
|
|
|
172
|
3941 |
|
assert(is_array($data)); |
|
173
|
3951 |
|
|
|
174
|
|
|
return $data; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* {@inheritdoc} |
|
179
|
|
|
*/ |
|
180
|
3951 |
|
public function fetchColumn($columnIndex = 0) |
|
181
|
|
|
{ |
|
182
|
3951 |
|
try { |
|
183
|
|
|
$value = $this->stmt->fetchColumn($columnIndex); |
|
184
|
|
|
|
|
185
|
|
|
if ($value === null) { |
|
186
|
|
|
$columnCount = $this->columnCount(); |
|
187
|
|
|
|
|
188
|
3777 |
|
if ($columnIndex < 0 || $columnIndex >= $columnCount) { |
|
189
|
|
|
throw DBALException::invalidColumnIndex($columnIndex, $columnCount); |
|
190
|
|
|
} |
|
191
|
3777 |
|
} |
|
192
|
|
|
|
|
193
|
3777 |
|
return $value; |
|
194
|
2804 |
|
} catch (\PDOException $exception) { |
|
195
|
|
|
throw new PDOException($exception); |
|
196
|
2804 |
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Converts DBAL parameter type to PDO parameter type |
|
201
|
3777 |
|
* |
|
202
|
|
|
* @param int $type Parameter type |
|
203
|
|
|
*/ |
|
204
|
|
|
private function convertParamType(int $type) : int |
|
205
|
|
|
{ |
|
206
|
|
|
if (! isset(self::PARAM_TYPE_MAP[$type])) { |
|
207
|
|
|
// TODO: next major: throw an exception |
|
208
|
|
|
@trigger_error(sprintf( |
|
209
|
|
|
'Using a PDO parameter type (%d given) is deprecated and will cause an error in Doctrine 3.0', |
|
210
|
|
|
$type |
|
211
|
|
|
), E_USER_DEPRECATED); |
|
212
|
3927 |
|
|
|
213
|
|
|
return $type; |
|
214
|
3927 |
|
} |
|
215
|
|
|
|
|
216
|
|
|
return self::PARAM_TYPE_MAP[$type]; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Converts DBAL fetch mode to PDO fetch mode |
|
221
|
|
|
* |
|
222
|
|
|
* @param int $fetchMode Fetch mode |
|
223
|
|
|
*/ |
|
224
|
3927 |
|
private function convertFetchMode(int $fetchMode) : int |
|
225
|
|
|
{ |
|
226
|
|
|
if (! isset(self::FETCH_MODE_MAP[$fetchMode])) { |
|
227
|
|
|
// TODO: next major: throw an exception |
|
228
|
|
|
@trigger_error(sprintf( |
|
229
|
|
|
'Using a PDO fetch mode or their combination (%d given)' . |
|
230
|
|
|
' is deprecated and will cause an error in Doctrine 3.0', |
|
231
|
|
|
$fetchMode |
|
232
|
3952 |
|
), E_USER_DEPRECATED); |
|
233
|
|
|
|
|
234
|
3952 |
|
return $fetchMode; |
|
235
|
|
|
} |
|
236
|
2304 |
|
|
|
237
|
|
|
return self::FETCH_MODE_MAP[$fetchMode]; |
|
238
|
|
|
} |
|
239
|
2304 |
|
|
|
240
|
2304 |
|
/** |
|
241
|
|
|
* {@inheritdoc} |
|
242
|
2304 |
|
*/ |
|
243
|
|
|
public function getIterator() |
|
244
|
|
|
{ |
|
245
|
3952 |
|
yield from $this->stmt; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|