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