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