1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
6
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
7
|
|
|
use Doctrine\DBAL\Types\Type; |
8
|
|
|
use IteratorAggregate; |
9
|
|
|
use PDO; |
10
|
|
|
use Throwable; |
11
|
|
|
use function is_array; |
12
|
|
|
use function is_string; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support |
16
|
|
|
* for logging, DBAL mapping types, etc. |
17
|
|
|
*/ |
18
|
|
|
class Statement implements IteratorAggregate, DriverStatement |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The SQL statement. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $sql; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The bound parameters. |
29
|
|
|
* |
30
|
|
|
* @var mixed[] |
31
|
|
|
*/ |
32
|
|
|
protected $params = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The parameter types. |
36
|
|
|
* |
37
|
|
|
* @var int[]|string[] |
38
|
|
|
*/ |
39
|
|
|
protected $types = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The underlying driver statement. |
43
|
|
|
* |
44
|
|
|
* @var \Doctrine\DBAL\Driver\Statement |
45
|
|
|
*/ |
46
|
|
|
protected $stmt; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The underlying database platform. |
50
|
|
|
* |
51
|
|
|
* @var AbstractPlatform |
52
|
|
|
*/ |
53
|
|
|
protected $platform; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The connection this statement is bound to and executed on. |
57
|
|
|
* |
58
|
|
|
* @var Connection |
59
|
|
|
*/ |
60
|
|
|
protected $conn; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>. |
64
|
|
|
* |
65
|
|
|
* @param string $sql The SQL of the statement. |
66
|
|
|
* @param Connection $conn The connection on which the statement should be executed. |
67
|
|
|
*/ |
68
|
1124 |
|
public function __construct($sql, Connection $conn) |
69
|
|
|
{ |
70
|
1124 |
|
$this->sql = $sql; |
71
|
1124 |
|
$this->stmt = $conn->getWrappedConnection()->prepare($sql); |
72
|
1123 |
|
$this->conn = $conn; |
73
|
1123 |
|
$this->platform = $conn->getDatabasePlatform(); |
74
|
1123 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Binds a parameter value to the statement. |
78
|
|
|
* |
79
|
|
|
* The value can optionally be bound with a PDO binding type or a DBAL mapping type. |
80
|
|
|
* If bound with a DBAL mapping type, the binding type is derived from the mapping |
81
|
|
|
* type and the value undergoes the conversion routines of the mapping type before |
82
|
|
|
* being bound. |
83
|
|
|
* |
84
|
|
|
* @param string|int $name The name or position of the parameter. |
85
|
|
|
* @param mixed $value The value of the parameter. |
86
|
|
|
* @param mixed $type Either a PDO binding type or a DBAL mapping type name or instance. |
87
|
|
|
* |
88
|
|
|
* @return bool TRUE on success, FALSE on failure. |
89
|
|
|
*/ |
90
|
987 |
|
public function bindValue($name, $value, $type = ParameterType::STRING) |
91
|
|
|
{ |
92
|
987 |
|
$this->params[$name] = $value; |
93
|
987 |
|
$this->types[$name] = $type; |
94
|
987 |
|
if ($type !== null) { |
95
|
987 |
|
if (is_string($type)) { |
96
|
813 |
|
$type = Type::getType($type); |
97
|
|
|
} |
98
|
|
|
|
99
|
987 |
|
if ($type instanceof Type) { |
100
|
814 |
|
$value = $type->convertToDatabaseValue($value, $this->platform); |
101
|
814 |
|
$bindingType = $type->getBindingType(); |
102
|
|
|
} else { |
103
|
984 |
|
$bindingType = $type; |
104
|
|
|
} |
105
|
|
|
|
106
|
987 |
|
return $this->stmt->bindValue($name, $value, $bindingType); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->stmt->bindValue($name, $value); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Binds a parameter to a value by reference. |
114
|
|
|
* |
115
|
|
|
* Binding a parameter by reference does not support DBAL mapping types. |
116
|
|
|
* |
117
|
|
|
* @param string|int $name The name or position of the parameter. |
118
|
|
|
* @param mixed $var The reference to the variable to bind. |
119
|
|
|
* @param int $type The PDO binding type. |
120
|
|
|
* @param int|null $length Must be specified when using an OUT bind |
121
|
|
|
* so that PHP allocates enough memory to hold the returned value. |
122
|
|
|
* |
123
|
|
|
* @return bool TRUE on success, FALSE on failure. |
124
|
|
|
*/ |
125
|
1010 |
|
public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null) |
126
|
|
|
{ |
127
|
1010 |
|
$this->params[$name] = $var; |
128
|
1010 |
|
$this->types[$name] = $type; |
129
|
|
|
|
130
|
1010 |
|
return $this->stmt->bindParam($name, $var, $type, $length); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Executes the statement with the currently bound parameters. |
135
|
|
|
* |
136
|
|
|
* @param mixed[]|null $params |
137
|
|
|
* |
138
|
|
|
* @return bool TRUE on success, FALSE on failure. |
139
|
|
|
* |
140
|
|
|
* @throws DBALException |
141
|
|
|
*/ |
142
|
1113 |
|
public function execute($params = null) |
143
|
|
|
{ |
144
|
1113 |
|
if (is_array($params)) { |
145
|
845 |
|
$this->params = $params; |
146
|
|
|
} |
147
|
|
|
|
148
|
1113 |
|
$logger = $this->conn->getConfiguration()->getSQLLogger(); |
149
|
1113 |
|
if ($logger) { |
150
|
1113 |
|
$logger->startQuery($this->sql, $this->params, $this->types); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
try { |
154
|
1113 |
|
$stmt = $this->stmt->execute($params); |
155
|
47 |
|
} catch (Throwable $ex) { |
156
|
47 |
|
if ($logger) { |
157
|
47 |
|
$logger->stopQuery(); |
158
|
|
|
} |
159
|
|
|
|
160
|
47 |
|
throw DBALException::driverExceptionDuringQuery( |
161
|
47 |
|
$this->conn->getDriver(), |
162
|
|
|
$ex, |
163
|
47 |
|
$this->sql, |
164
|
47 |
|
$this->conn->resolveParams($this->params, $this->types) |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
1112 |
|
if ($logger) { |
169
|
1112 |
|
$logger->stopQuery(); |
170
|
|
|
} |
171
|
|
|
|
172
|
1112 |
|
$this->params = []; |
173
|
1112 |
|
$this->types = []; |
174
|
|
|
|
175
|
1112 |
|
return $stmt; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Closes the cursor, freeing the database resources used by this statement. |
180
|
|
|
* |
181
|
|
|
* @return bool TRUE on success, FALSE on failure. |
182
|
|
|
*/ |
183
|
688 |
|
public function closeCursor() |
184
|
|
|
{ |
185
|
688 |
|
return $this->stmt->closeCursor(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Returns the number of columns in the result set. |
190
|
|
|
* |
191
|
|
|
* @return int |
192
|
|
|
*/ |
193
|
|
|
public function columnCount() |
194
|
|
|
{ |
195
|
|
|
return $this->stmt->columnCount(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Fetches the SQLSTATE associated with the last operation on the statement. |
200
|
|
|
* |
201
|
|
|
* @return string|int|bool |
202
|
|
|
*/ |
203
|
|
|
public function errorCode() |
204
|
|
|
{ |
205
|
|
|
return $this->stmt->errorCode(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritDoc} |
210
|
|
|
*/ |
211
|
|
|
public function errorInfo() |
212
|
|
|
{ |
213
|
|
|
return $this->stmt->errorInfo(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* {@inheritdoc} |
218
|
|
|
*/ |
219
|
1118 |
|
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
220
|
|
|
{ |
221
|
1118 |
|
if ($arg2 === null) { |
222
|
1118 |
|
return $this->stmt->setFetchMode($fetchMode); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
if ($arg3 === null) { |
226
|
|
|
return $this->stmt->setFetchMode($fetchMode, $arg2); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Required by interface IteratorAggregate. |
234
|
|
|
* |
235
|
|
|
* {@inheritdoc} |
236
|
|
|
*/ |
237
|
881 |
|
public function getIterator() |
238
|
|
|
{ |
239
|
881 |
|
return $this->stmt; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* {@inheritdoc} |
244
|
|
|
*/ |
245
|
996 |
|
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
246
|
|
|
{ |
247
|
996 |
|
return $this->stmt->fetch($fetchMode); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@inheritdoc} |
252
|
|
|
*/ |
253
|
941 |
|
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
254
|
|
|
{ |
255
|
941 |
|
return $this->stmt->fetchAll($fetchMode, $fetchArgument, $ctorArgs); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* {@inheritDoc} |
260
|
|
|
*/ |
261
|
916 |
|
public function fetchColumn($columnIndex = 0) |
262
|
|
|
{ |
263
|
916 |
|
return $this->stmt->fetchColumn($columnIndex); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Returns the number of rows affected by the last execution of this statement. |
268
|
|
|
* |
269
|
|
|
* @return int The number of affected rows. |
270
|
|
|
*/ |
271
|
211 |
|
public function rowCount() |
272
|
|
|
{ |
273
|
211 |
|
return $this->stmt->rowCount(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Gets the wrapped driver statement. |
278
|
|
|
* |
279
|
|
|
* @return \Doctrine\DBAL\Driver\Statement |
280
|
|
|
*/ |
281
|
32 |
|
public function getWrappedStatement() |
282
|
|
|
{ |
283
|
32 |
|
return $this->stmt; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|