1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Driver\DriverException; |
6
|
|
|
use Doctrine\DBAL\Types\Type; |
7
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
8
|
|
|
use function is_array; |
9
|
|
|
use function is_string; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support |
13
|
|
|
* for logging, DBAL mapping types, etc. |
14
|
|
|
* |
15
|
|
|
* @author Roman Borschel <[email protected]> |
16
|
|
|
* @since 2.0 |
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 array |
31
|
|
|
*/ |
32
|
|
|
protected $params = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The parameter types. |
36
|
|
|
* |
37
|
|
|
* @var array |
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 \Doctrine\DBAL\Platforms\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
|
|
|
* @throws DBALException |
69
|
|
|
*/ |
70
|
|
|
public function __construct(string $sql, Connection $conn) |
71
|
|
|
{ |
72
|
|
|
$this->sql = $sql; |
73
|
|
|
$this->stmt = $conn->getWrappedConnection()->prepare($sql); |
74
|
|
|
$this->conn = $conn; |
75
|
|
|
$this->platform = $conn->getDatabasePlatform(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
* |
81
|
|
|
* @throws DBALException |
82
|
|
|
*/ |
83
|
|
|
public function bindValue($name, $value, $type = ParameterType::STRING) : void |
84
|
|
|
{ |
85
|
|
|
$this->params[$name] = $value; |
86
|
|
|
$this->types[$name] = $type; |
87
|
|
|
|
88
|
|
|
if ($type === null) { |
89
|
|
|
$this->stmt->bindValue($name, $value); |
90
|
|
|
|
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (is_string($type)) { |
95
|
|
|
$type = Type::getType($type); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($type instanceof Type) { |
99
|
|
|
$value = $type->convertToDatabaseValue($value, $this->platform); |
100
|
|
|
$bindingType = $type->getBindingType(); |
101
|
|
|
} else { |
102
|
|
|
$bindingType = $type; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->stmt->bindValue($name, $value, $bindingType); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
*/ |
111
|
|
|
public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null) : void |
112
|
|
|
{ |
113
|
|
|
$this->params[$name] = $var; |
114
|
|
|
$this->types[$name] = $type; |
115
|
|
|
|
116
|
|
|
$this->stmt->bindParam($name, $var, $type, $length); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Executes the statement with the currently bound parameters. |
121
|
|
|
* |
122
|
|
|
* @param array|null $params |
123
|
|
|
* |
124
|
|
|
* @throws DBALException |
125
|
|
|
* @throws DriverException |
126
|
|
|
*/ |
127
|
|
|
public function execute($params = null) : void |
128
|
|
|
{ |
129
|
|
|
if (is_array($params)) { |
130
|
|
|
$this->params = $params; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$logger = $this->conn->getConfiguration()->getSQLLogger(); |
134
|
|
|
$logger->startQuery($this->sql, $this->params, $this->types); |
135
|
|
|
|
136
|
|
|
try { |
137
|
|
|
$this->stmt->execute($params); |
138
|
|
|
} catch (\Exception $ex) { |
139
|
|
|
$logger->stopQuery(); |
140
|
|
|
throw DBALException::driverExceptionDuringQuery( |
141
|
|
|
$this->conn->getDriver(), |
142
|
|
|
$ex, |
143
|
|
|
$this->sql, |
144
|
|
|
$this->conn->resolveParams($this->params, $this->types) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$logger->stopQuery(); |
149
|
|
|
$this->params = []; |
150
|
|
|
$this->types = []; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritDoc} |
155
|
|
|
*/ |
156
|
|
|
public function closeCursor() : void |
157
|
|
|
{ |
158
|
|
|
$this->stmt->closeCursor(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritDoc} |
163
|
|
|
*/ |
164
|
|
|
public function columnCount() : int |
165
|
|
|
{ |
166
|
|
|
return $this->stmt->columnCount(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Fetches the SQLSTATE associated with the last operation on the statement. |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function errorCode() |
175
|
|
|
{ |
176
|
|
|
return $this->stmt->errorCode(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Fetches extended error information associated with the last operation on the statement. |
181
|
|
|
* |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
|
|
public function errorInfo() |
185
|
|
|
{ |
186
|
|
|
return $this->stmt->errorInfo(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
|
|
public function setFetchMode($fetchMode, ...$args) : void |
193
|
|
|
{ |
194
|
|
|
$this->stmt->setFetchMode($fetchMode, ...$args); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Required by interface IteratorAggregate. |
199
|
|
|
* |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
|
|
public function getIterator() |
203
|
|
|
{ |
204
|
|
|
return $this->stmt; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritdoc} |
209
|
|
|
*/ |
210
|
|
|
public function fetch($fetchMode = null, ...$args) |
211
|
|
|
{ |
212
|
|
|
return $this->stmt->fetch($fetchMode, ...$args); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
|
|
public function fetchAll($fetchMode = null, ...$args) : array |
219
|
|
|
{ |
220
|
|
|
return $this->stmt->fetchAll($fetchMode, ...$args); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* {@inheritDoc} |
225
|
|
|
*/ |
226
|
|
|
public function fetchColumn($columnIndex = 0) |
227
|
|
|
{ |
228
|
|
|
return $this->stmt->fetchColumn($columnIndex); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Returns the number of rows affected by the last execution of this statement. |
233
|
|
|
* |
234
|
|
|
* @return int The number of affected rows. |
235
|
|
|
*/ |
236
|
|
|
public function rowCount() : int |
237
|
|
|
{ |
238
|
|
|
return $this->stmt->rowCount(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Gets the wrapped driver statement. |
243
|
|
|
* |
244
|
|
|
* @return \Doctrine\DBAL\Driver\Statement |
245
|
|
|
*/ |
246
|
|
|
public function getWrappedStatement() |
247
|
|
|
{ |
248
|
|
|
return $this->stmt; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|