1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL; |
21
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Logging\SQLLogger2; |
23
|
|
|
use Doctrine\DBAL\Types\Type; |
24
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
25
|
|
|
use function is_array; |
26
|
|
|
use function is_string; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support |
30
|
|
|
* for logging, DBAL mapping types, etc. |
31
|
|
|
* |
32
|
|
|
* @author Roman Borschel <[email protected]> |
33
|
|
|
* @since 2.0 |
34
|
|
|
*/ |
35
|
|
|
class Statement implements \IteratorAggregate, DriverStatement |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* The SQL statement. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $sql; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The bound parameters. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $params = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The parameter types. |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $types = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The underlying driver statement. |
60
|
|
|
* |
61
|
|
|
* @var \Doctrine\DBAL\Driver\Statement |
62
|
|
|
*/ |
63
|
|
|
protected $stmt; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The underlying database platform. |
67
|
|
|
* |
68
|
|
|
* @var \Doctrine\DBAL\Platforms\AbstractPlatform |
69
|
|
|
*/ |
70
|
|
|
protected $platform; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The connection this statement is bound to and executed on. |
74
|
|
|
* |
75
|
|
|
* @var \Doctrine\DBAL\Connection |
76
|
|
|
*/ |
77
|
|
|
protected $conn; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>. |
81
|
|
|
* |
82
|
|
|
* @param string $sql The SQL of the statement. |
83
|
|
|
* @param \Doctrine\DBAL\Connection $conn The connection on which the statement should be executed. |
84
|
|
|
*/ |
85
|
886 |
|
public function __construct($sql, Connection $conn) |
86
|
|
|
{ |
87
|
886 |
|
$this->sql = $sql; |
88
|
886 |
|
$this->stmt = $conn->getWrappedConnection()->prepare($sql); |
89
|
867 |
|
$this->conn = $conn; |
90
|
867 |
|
$this->platform = $conn->getDatabasePlatform(); |
91
|
867 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Binds a parameter value to the statement. |
95
|
|
|
* |
96
|
|
|
* The value can optionally be bound with a PDO binding type or a DBAL mapping type. |
97
|
|
|
* If bound with a DBAL mapping type, the binding type is derived from the mapping |
98
|
|
|
* type and the value undergoes the conversion routines of the mapping type before |
99
|
|
|
* being bound. |
100
|
|
|
* |
101
|
|
|
* @param string $name The name or position of the parameter. |
102
|
|
|
* @param mixed $value The value of the parameter. |
103
|
|
|
* @param mixed $type Either a PDO binding type or a DBAL mapping type name or instance. |
104
|
|
|
* |
105
|
|
|
* @return bool TRUE on success, FALSE on failure. |
106
|
|
|
*/ |
107
|
194 |
|
public function bindValue($name, $value, $type = ParameterType::STRING) |
108
|
|
|
{ |
109
|
194 |
|
$this->params[$name] = $value; |
110
|
194 |
|
$this->types[$name] = $type; |
111
|
194 |
|
if ($type !== null) { |
112
|
194 |
|
if (is_string($type)) { |
113
|
38 |
|
$type = Type::getType($type); |
114
|
|
|
} |
115
|
194 |
|
if ($type instanceof Type) { |
116
|
57 |
|
$value = $type->convertToDatabaseValue($value, $this->platform); |
117
|
57 |
|
$bindingType = $type->getBindingType(); |
118
|
|
|
} else { |
119
|
137 |
|
$bindingType = $type; |
120
|
|
|
} |
121
|
|
|
|
122
|
194 |
|
return $this->stmt->bindValue($name, $value, $bindingType); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->stmt->bindValue($name, $value); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Binds a parameter to a value by reference. |
130
|
|
|
* |
131
|
|
|
* Binding a parameter by reference does not support DBAL mapping types. |
132
|
|
|
* |
133
|
|
|
* @param string $name The name or position of the parameter. |
134
|
|
|
* @param mixed $var The reference to the variable to bind. |
135
|
|
|
* @param int $type The PDO binding type. |
136
|
|
|
* @param int|null $length Must be specified when using an OUT bind |
137
|
|
|
* so that PHP allocates enough memory to hold the returned value. |
138
|
|
|
* |
139
|
|
|
* @return bool TRUE on success, FALSE on failure. |
140
|
|
|
*/ |
141
|
152 |
|
public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null) |
142
|
|
|
{ |
143
|
152 |
|
$this->params[$name] = $var; |
144
|
152 |
|
$this->types[$name] = $type; |
145
|
|
|
|
146
|
152 |
|
return $this->stmt->bindParam($name, $var, $type, $length); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Executes the statement with the currently bound parameters. |
151
|
|
|
* |
152
|
|
|
* @param array|null $params |
153
|
|
|
* |
154
|
|
|
* @return bool TRUE on success, FALSE on failure. |
155
|
|
|
* |
156
|
|
|
* @throws \Doctrine\DBAL\DBALException |
157
|
|
|
*/ |
158
|
695 |
|
public function execute($params = null) |
159
|
|
|
{ |
160
|
695 |
|
if (is_array($params)) { |
161
|
76 |
|
$this->params = $params; |
162
|
|
|
} |
163
|
|
|
|
164
|
695 |
|
$logger = $this->conn->getConfiguration()->getSQLLogger(); |
165
|
695 |
|
if ($logger) { |
166
|
695 |
|
$logger->startQuery($this->sql, $this->params, $this->types); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
try { |
170
|
695 |
|
$stmt = $this->stmt->execute($params); |
171
|
19 |
|
} catch (\Exception $ex) { |
172
|
19 |
|
if ($logger && ($logger instanceof SQLLogger2)) { |
173
|
|
|
$logger->fail($ex); |
174
|
|
|
} |
175
|
19 |
|
throw DBALException::driverExceptionDuringQuery( |
176
|
19 |
|
$this->conn->getDriver(), |
177
|
19 |
|
$ex, |
178
|
19 |
|
$this->sql, |
179
|
19 |
|
$this->conn->resolveParams($this->params, $this->types) |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
676 |
|
if ($logger) { |
184
|
676 |
|
$logger->stopQuery(); |
185
|
|
|
} |
186
|
676 |
|
$this->params = []; |
187
|
676 |
|
$this->types = []; |
188
|
|
|
|
189
|
676 |
|
return $stmt; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Closes the cursor, freeing the database resources used by this statement. |
194
|
|
|
* |
195
|
|
|
* @return bool TRUE on success, FALSE on failure. |
196
|
|
|
*/ |
197
|
199 |
|
public function closeCursor() |
198
|
|
|
{ |
199
|
199 |
|
return $this->stmt->closeCursor(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Returns the number of columns in the result set. |
204
|
|
|
* |
205
|
|
|
* @return int |
206
|
|
|
*/ |
207
|
|
|
public function columnCount() |
208
|
|
|
{ |
209
|
|
|
return $this->stmt->columnCount(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Fetches the SQLSTATE associated with the last operation on the statement. |
214
|
|
|
* |
215
|
|
|
* @return string|int|bool |
216
|
|
|
*/ |
217
|
|
|
public function errorCode() |
218
|
|
|
{ |
219
|
|
|
return $this->stmt->errorCode(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Fetches extended error information associated with the last operation on the statement. |
224
|
|
|
* |
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
|
|
public function errorInfo() |
228
|
|
|
{ |
229
|
|
|
return $this->stmt->errorInfo(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritdoc} |
234
|
|
|
*/ |
235
|
791 |
|
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
236
|
|
|
{ |
237
|
791 |
|
if ($arg2 === null) { |
238
|
791 |
|
return $this->stmt->setFetchMode($fetchMode); |
239
|
|
|
} elseif ($arg3 === null) { |
240
|
|
|
return $this->stmt->setFetchMode($fetchMode, $arg2); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Required by interface IteratorAggregate. |
248
|
|
|
* |
249
|
|
|
* {@inheritdoc} |
250
|
|
|
*/ |
251
|
19 |
|
public function getIterator() |
252
|
|
|
{ |
253
|
19 |
|
return $this->stmt; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
190 |
|
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
260
|
|
|
{ |
261
|
190 |
|
return $this->stmt->fetch($fetchMode); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* {@inheritdoc} |
266
|
|
|
*/ |
267
|
148 |
|
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
268
|
|
|
{ |
269
|
148 |
|
if ($fetchArgument) { |
270
|
15 |
|
return $this->stmt->fetchAll($fetchMode, $fetchArgument); |
271
|
|
|
} |
272
|
|
|
|
273
|
133 |
|
return $this->stmt->fetchAll($fetchMode); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Returns a single column from the next row of a result set. |
278
|
|
|
* |
279
|
|
|
* @param int $columnIndex |
280
|
|
|
* |
281
|
|
|
* @return mixed A single column from the next row of a result set or FALSE if there are no more rows. |
282
|
|
|
*/ |
283
|
256 |
|
public function fetchColumn($columnIndex = 0) |
284
|
|
|
{ |
285
|
256 |
|
return $this->stmt->fetchColumn($columnIndex); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Returns the number of rows affected by the last execution of this statement. |
290
|
|
|
* |
291
|
|
|
* @return int The number of affected rows. |
292
|
|
|
*/ |
293
|
76 |
|
public function rowCount() |
294
|
|
|
{ |
295
|
76 |
|
return $this->stmt->rowCount(); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Gets the wrapped driver statement. |
300
|
|
|
* |
301
|
|
|
* @return \Doctrine\DBAL\Driver\Statement |
302
|
|
|
*/ |
303
|
1 |
|
public function getWrappedStatement() |
304
|
|
|
{ |
305
|
1 |
|
return $this->stmt; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|