1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Driver\DriverException; |
8
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
9
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
10
|
|
|
use Doctrine\DBAL\Types\Type; |
11
|
|
|
use IteratorAggregate; |
12
|
|
|
use Throwable; |
13
|
|
|
use function is_array; |
14
|
|
|
use function is_string; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support |
18
|
|
|
* for logging, DBAL mapping types, etc. |
19
|
|
|
*/ |
20
|
|
|
class Statement implements IteratorAggregate, DriverStatement |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The SQL statement. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $sql; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The bound parameters. |
31
|
|
|
* |
32
|
|
|
* @var mixed[] |
33
|
|
|
*/ |
34
|
|
|
protected $params = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The parameter types. |
38
|
|
|
* |
39
|
|
|
* @var int[]|string[] |
40
|
|
|
*/ |
41
|
|
|
protected $types = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The underlying driver statement. |
45
|
|
|
* |
46
|
|
|
* @var \Doctrine\DBAL\Driver\Statement |
47
|
|
|
*/ |
48
|
|
|
protected $stmt; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The underlying database platform. |
52
|
|
|
* |
53
|
|
|
* @var AbstractPlatform |
54
|
|
|
*/ |
55
|
|
|
protected $platform; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The connection this statement is bound to and executed on. |
59
|
|
|
* |
60
|
|
|
* @var Connection |
61
|
|
|
*/ |
62
|
|
|
protected $conn; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>. |
66
|
|
|
* |
67
|
|
|
* @param string $sql The SQL of the statement. |
68
|
|
|
* @param Connection $conn The connection on which the statement should be executed. |
69
|
|
|
*/ |
70
|
1823 |
|
public function __construct($sql, Connection $conn) |
71
|
|
|
{ |
72
|
1823 |
|
$this->sql = $sql; |
73
|
1823 |
|
$this->stmt = $conn->getWrappedConnection()->prepare($sql); |
74
|
1799 |
|
$this->conn = $conn; |
75
|
1799 |
|
$this->platform = $conn->getDatabasePlatform(); |
76
|
1799 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Binds a parameter value to the statement. |
80
|
|
|
* |
81
|
|
|
* The value can optionally be bound with a PDO binding type or a DBAL mapping type. |
82
|
|
|
* If bound with a DBAL mapping type, the binding type is derived from the mapping |
83
|
|
|
* type and the value undergoes the conversion routines of the mapping type before |
84
|
|
|
* being bound. |
85
|
|
|
* |
86
|
|
|
* @param string|int $name The name or position of the parameter. |
87
|
|
|
* @param mixed $value The value of the parameter. |
88
|
|
|
* @param mixed $type Either a PDO binding type or a DBAL mapping type name or instance. |
89
|
|
|
* |
90
|
|
|
* @throws DBALException |
91
|
|
|
* @throws DriverException |
92
|
|
|
*/ |
93
|
1751 |
|
public function bindValue($name, $value, $type = ParameterType::STRING) : void |
94
|
|
|
{ |
95
|
1751 |
|
$this->params[$name] = $value; |
96
|
1751 |
|
$this->types[$name] = $type; |
97
|
|
|
|
98
|
1751 |
|
if (is_string($type)) { |
99
|
1575 |
|
$type = Type::getType($type); |
100
|
|
|
} |
101
|
|
|
|
102
|
1751 |
|
if ($type instanceof Type) { |
103
|
1575 |
|
$value = $type->convertToDatabaseValue($value, $this->platform); |
104
|
1575 |
|
$bindingType = $type->getBindingType(); |
105
|
|
|
} else { |
106
|
1751 |
|
$bindingType = $type; |
107
|
|
|
} |
108
|
|
|
|
109
|
1751 |
|
$this->stmt->bindValue($name, $value, $bindingType); |
|
|
|
|
110
|
1751 |
|
} |
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
|
|
|
* @throws DriverException |
124
|
|
|
*/ |
125
|
1775 |
|
public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null) : void |
126
|
|
|
{ |
127
|
1775 |
|
$this->params[$name] = $var; |
128
|
1775 |
|
$this->types[$name] = $type; |
129
|
|
|
|
130
|
1775 |
|
$this->stmt->bindParam($name, $var, $type, $length); |
131
|
1775 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritDoc} |
135
|
|
|
* |
136
|
|
|
* @throws DBALException |
137
|
|
|
*/ |
138
|
1775 |
|
public function execute($params = null) : void |
139
|
|
|
{ |
140
|
1775 |
|
if (is_array($params)) { |
141
|
1599 |
|
$this->params = $params; |
142
|
|
|
} |
143
|
|
|
|
144
|
1775 |
|
$logger = $this->conn->getConfiguration()->getSQLLogger(); |
145
|
1775 |
|
$logger->startQuery($this->sql, $this->params, $this->types); |
146
|
|
|
|
147
|
|
|
try { |
148
|
1775 |
|
$this->stmt->execute($params); |
149
|
166 |
|
} catch (Throwable $ex) { |
150
|
166 |
|
throw DBALException::driverExceptionDuringQuery( |
151
|
166 |
|
$this->conn->getDriver(), |
152
|
166 |
|
$ex, |
153
|
166 |
|
$this->sql, |
154
|
166 |
|
$this->conn->resolveParams($this->params, $this->types) |
155
|
|
|
); |
156
|
1775 |
|
} finally { |
157
|
1775 |
|
$logger->stopQuery(); |
158
|
|
|
} |
159
|
|
|
|
160
|
1775 |
|
$this->params = []; |
161
|
1775 |
|
$this->types = []; |
162
|
1775 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritDoc} |
166
|
|
|
*/ |
167
|
644 |
|
public function closeCursor() : void |
168
|
|
|
{ |
169
|
644 |
|
$this->stmt->closeCursor(); |
170
|
644 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Returns the number of columns in the result set. |
174
|
|
|
* |
175
|
|
|
* @return int |
176
|
|
|
*/ |
177
|
|
|
public function columnCount() |
178
|
|
|
{ |
179
|
|
|
return $this->stmt->columnCount(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
1799 |
|
public function setFetchMode($fetchMode, ...$args) : void |
186
|
|
|
{ |
187
|
1799 |
|
$this->stmt->setFetchMode($fetchMode, ...$args); |
188
|
1799 |
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Required by interface IteratorAggregate. |
192
|
|
|
* |
193
|
|
|
* {@inheritdoc} |
194
|
|
|
*/ |
195
|
1647 |
|
public function getIterator() |
196
|
|
|
{ |
197
|
1647 |
|
return $this->stmt; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
1751 |
|
public function fetch($fetchMode = null, ...$args) |
204
|
|
|
{ |
205
|
1751 |
|
return $this->stmt->fetch($fetchMode, ...$args); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
1703 |
|
public function fetchAll($fetchMode = null, ...$args) |
212
|
|
|
{ |
213
|
1703 |
|
return $this->stmt->fetchAll($fetchMode, ...$args); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* {@inheritDoc} |
218
|
|
|
*/ |
219
|
1671 |
|
public function fetchColumn($columnIndex = 0) |
220
|
|
|
{ |
221
|
1671 |
|
return $this->stmt->fetchColumn($columnIndex); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Returns the number of rows affected by the last execution of this statement. |
226
|
|
|
* |
227
|
|
|
* @return int The number of affected rows. |
228
|
|
|
*/ |
229
|
192 |
|
public function rowCount() : int |
230
|
|
|
{ |
231
|
192 |
|
return $this->stmt->rowCount(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Gets the wrapped driver statement. |
236
|
|
|
* |
237
|
|
|
* @return \Doctrine\DBAL\Driver\Statement |
238
|
|
|
*/ |
239
|
30 |
|
public function getWrappedStatement() |
240
|
|
|
{ |
241
|
30 |
|
return $this->stmt; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|