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\Driver\ServerInfoAwareConnection; |
23
|
|
|
use Doctrine\DBAL\Exception\InvalidArgumentException; |
24
|
|
|
use Closure; |
25
|
|
|
use Exception; |
26
|
|
|
use Doctrine\DBAL\Types\Type; |
27
|
|
|
use Doctrine\DBAL\Driver\Connection as DriverConnection; |
28
|
|
|
use Doctrine\Common\EventManager; |
29
|
|
|
use Doctrine\DBAL\Cache\ResultCacheStatement; |
30
|
|
|
use Doctrine\DBAL\Cache\QueryCacheProfile; |
31
|
|
|
use Doctrine\DBAL\Cache\ArrayStatement; |
32
|
|
|
use Doctrine\DBAL\Cache\CacheException; |
33
|
|
|
use Doctrine\DBAL\Driver\PingableConnection; |
34
|
|
|
use Throwable; |
35
|
|
|
use function array_key_exists; |
36
|
|
|
use function array_merge; |
37
|
|
|
use function func_get_args; |
38
|
|
|
use function implode; |
39
|
|
|
use function is_int; |
40
|
|
|
use function is_string; |
41
|
|
|
use function key; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like |
45
|
|
|
* events, transaction isolation levels, configuration, emulated transaction nesting, |
46
|
|
|
* lazy connecting and more. |
47
|
|
|
* |
48
|
|
|
* @link www.doctrine-project.org |
49
|
|
|
* @since 2.0 |
50
|
|
|
* @author Guilherme Blanco <[email protected]> |
51
|
|
|
* @author Jonathan Wage <[email protected]> |
52
|
|
|
* @author Roman Borschel <[email protected]> |
53
|
|
|
* @author Konsta Vesterinen <[email protected]> |
54
|
|
|
* @author Lukas Smith <[email protected]> (MDB2 library) |
55
|
|
|
* @author Benjamin Eberlei <[email protected]> |
56
|
|
|
*/ |
57
|
|
|
class Connection implements DriverConnection |
58
|
|
|
{ |
59
|
|
|
/** |
60
|
|
|
* Constant for transaction isolation level READ UNCOMMITTED. |
61
|
|
|
* |
62
|
|
|
* @deprecated Use TransactionIsolationLevel::READ_UNCOMMITTED. |
63
|
|
|
*/ |
64
|
|
|
public const TRANSACTION_READ_UNCOMMITTED = TransactionIsolationLevel::READ_UNCOMMITTED; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Constant for transaction isolation level READ COMMITTED. |
68
|
|
|
* |
69
|
|
|
* @deprecated Use TransactionIsolationLevel::READ_COMMITTED. |
70
|
|
|
*/ |
71
|
|
|
public const TRANSACTION_READ_COMMITTED = TransactionIsolationLevel::READ_COMMITTED; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Constant for transaction isolation level REPEATABLE READ. |
75
|
|
|
* |
76
|
|
|
* @deprecated Use TransactionIsolationLevel::REPEATABLE_READ. |
77
|
|
|
*/ |
78
|
|
|
public const TRANSACTION_REPEATABLE_READ = TransactionIsolationLevel::REPEATABLE_READ; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Constant for transaction isolation level SERIALIZABLE. |
82
|
|
|
* |
83
|
|
|
* @deprecated Use TransactionIsolationLevel::SERIALIZABLE. |
84
|
|
|
*/ |
85
|
|
|
public const TRANSACTION_SERIALIZABLE = TransactionIsolationLevel::SERIALIZABLE; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Represents an array of ints to be expanded by Doctrine SQL parsing. |
89
|
|
|
* |
90
|
|
|
* @var int |
91
|
|
|
*/ |
92
|
|
|
public const PARAM_INT_ARRAY = ParameterType::INTEGER + self::ARRAY_PARAM_OFFSET; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Represents an array of strings to be expanded by Doctrine SQL parsing. |
96
|
|
|
* |
97
|
|
|
* @var int |
98
|
|
|
*/ |
99
|
|
|
public const PARAM_STR_ARRAY = ParameterType::STRING + self::ARRAY_PARAM_OFFSET; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Offset by which PARAM_* constants are detected as arrays of the param type. |
103
|
|
|
* |
104
|
|
|
* @var int |
105
|
|
|
*/ |
106
|
|
|
const ARRAY_PARAM_OFFSET = 100; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* The wrapped driver connection. |
110
|
|
|
* |
111
|
|
|
* @var \Doctrine\DBAL\Driver\Connection |
112
|
|
|
*/ |
113
|
|
|
protected $_conn; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @var \Doctrine\DBAL\Configuration |
117
|
|
|
*/ |
118
|
|
|
protected $_config; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @var \Doctrine\Common\EventManager |
122
|
|
|
*/ |
123
|
|
|
protected $_eventManager; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @var \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
127
|
|
|
*/ |
128
|
|
|
protected $_expr; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Whether or not a connection has been established. |
132
|
|
|
* |
133
|
|
|
* @var bool |
134
|
|
|
*/ |
135
|
|
|
private $_isConnected = false; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* The current auto-commit mode of this connection. |
139
|
|
|
* |
140
|
|
|
* @var bool |
141
|
|
|
*/ |
142
|
|
|
private $autoCommit = true; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* The transaction nesting level. |
146
|
|
|
* |
147
|
|
|
* @var int |
148
|
|
|
*/ |
149
|
|
|
private $_transactionNestingLevel = 0; |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* The currently active transaction isolation level. |
153
|
|
|
* |
154
|
|
|
* @var int |
155
|
|
|
*/ |
156
|
|
|
private $_transactionIsolationLevel; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* If nested transactions should use savepoints. |
160
|
|
|
* |
161
|
|
|
* @var bool |
162
|
|
|
*/ |
163
|
|
|
private $_nestTransactionsWithSavepoints = false; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* The parameters used during creation of the Connection instance. |
167
|
|
|
* |
168
|
|
|
* @var array |
169
|
|
|
*/ |
170
|
|
|
private $_params = []; |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* The DatabasePlatform object that provides information about the |
174
|
|
|
* database platform used by the connection. |
175
|
|
|
* |
176
|
|
|
* @var \Doctrine\DBAL\Platforms\AbstractPlatform |
177
|
|
|
*/ |
178
|
|
|
private $platform; |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* The schema manager. |
182
|
|
|
* |
183
|
|
|
* @var \Doctrine\DBAL\Schema\AbstractSchemaManager |
184
|
|
|
*/ |
185
|
|
|
protected $_schemaManager; |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* The used DBAL driver. |
189
|
|
|
* |
190
|
|
|
* @var \Doctrine\DBAL\Driver |
191
|
|
|
*/ |
192
|
|
|
protected $_driver; |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Flag that indicates whether the current transaction is marked for rollback only. |
196
|
|
|
* |
197
|
|
|
* @var bool |
198
|
|
|
*/ |
199
|
|
|
private $_isRollbackOnly = false; |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @var int |
203
|
|
|
*/ |
204
|
|
|
protected $defaultFetchMode = FetchMode::ASSOCIATIVE; |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Initializes a new instance of the Connection class. |
208
|
|
|
* |
209
|
|
|
* @param array $params The connection parameters. |
210
|
|
|
* @param \Doctrine\DBAL\Driver $driver The driver to use. |
211
|
|
|
* @param \Doctrine\DBAL\Configuration|null $config The configuration, optional. |
212
|
|
|
* @param \Doctrine\Common\EventManager|null $eventManager The event manager, optional. |
213
|
|
|
* |
214
|
|
|
* @throws \Doctrine\DBAL\DBALException |
215
|
|
|
*/ |
216
|
2435 |
|
public function __construct(array $params, Driver $driver, Configuration $config = null, |
217
|
|
|
EventManager $eventManager = null) |
218
|
|
|
{ |
219
|
2435 |
|
$this->_driver = $driver; |
220
|
2435 |
|
$this->_params = $params; |
221
|
|
|
|
222
|
2435 |
|
if (isset($params['pdo'])) { |
223
|
216 |
|
$this->_conn = $params['pdo']; |
224
|
216 |
|
$this->_isConnected = true; |
225
|
216 |
|
unset($this->_params['pdo']); |
226
|
|
|
} |
227
|
|
|
|
228
|
2435 |
|
if (isset($params["platform"])) { |
229
|
504 |
|
if ( ! $params['platform'] instanceof Platforms\AbstractPlatform) { |
230
|
18 |
|
throw DBALException::invalidPlatformType($params['platform']); |
231
|
|
|
} |
232
|
|
|
|
233
|
486 |
|
$this->platform = $params["platform"]; |
234
|
486 |
|
unset($this->_params["platform"]); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
// Create default config and event manager if none given |
238
|
2435 |
|
if ( ! $config) { |
239
|
558 |
|
$config = new Configuration(); |
240
|
|
|
} |
241
|
|
|
|
242
|
2435 |
|
if ( ! $eventManager) { |
243
|
558 |
|
$eventManager = new EventManager(); |
244
|
|
|
} |
245
|
|
|
|
246
|
2435 |
|
$this->_config = $config; |
247
|
2435 |
|
$this->_eventManager = $eventManager; |
248
|
|
|
|
249
|
2435 |
|
$this->_expr = new Query\Expression\ExpressionBuilder($this); |
250
|
|
|
|
251
|
2435 |
|
$this->autoCommit = $config->getAutoCommit(); |
252
|
2435 |
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Gets the parameters used during instantiation. |
256
|
|
|
* |
257
|
|
|
* @return array |
258
|
|
|
*/ |
259
|
2189 |
|
public function getParams() |
260
|
|
|
{ |
261
|
2189 |
|
return $this->_params; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Gets the name of the database this Connection is connected to. |
266
|
|
|
* |
267
|
|
|
* @return string |
268
|
|
|
*/ |
269
|
1130 |
|
public function getDatabase() |
270
|
|
|
{ |
271
|
1130 |
|
return $this->_driver->getDatabase($this); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Gets the hostname of the currently connected database. |
276
|
|
|
* |
277
|
|
|
* @return string|null |
278
|
|
|
*/ |
279
|
18 |
|
public function getHost() |
280
|
|
|
{ |
281
|
18 |
|
return $this->_params['host'] ?? null; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Gets the port of the currently connected database. |
286
|
|
|
* |
287
|
|
|
* @return mixed |
288
|
|
|
*/ |
289
|
18 |
|
public function getPort() |
290
|
|
|
{ |
291
|
18 |
|
return $this->_params['port'] ?? null; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Gets the username used by this connection. |
296
|
|
|
* |
297
|
|
|
* @return string|null |
298
|
|
|
*/ |
299
|
18 |
|
public function getUsername() |
300
|
|
|
{ |
301
|
18 |
|
return $this->_params['user'] ?? null; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Gets the password used by this connection. |
306
|
|
|
* |
307
|
|
|
* @return string|null |
308
|
|
|
*/ |
309
|
18 |
|
public function getPassword() |
310
|
|
|
{ |
311
|
18 |
|
return $this->_params['password'] ?? null; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Gets the DBAL driver instance. |
316
|
|
|
* |
317
|
|
|
* @return \Doctrine\DBAL\Driver |
318
|
|
|
*/ |
319
|
985 |
|
public function getDriver() |
320
|
|
|
{ |
321
|
985 |
|
return $this->_driver; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Gets the Configuration used by the Connection. |
326
|
|
|
* |
327
|
|
|
* @return \Doctrine\DBAL\Configuration |
328
|
|
|
*/ |
329
|
5314 |
|
public function getConfiguration() |
330
|
|
|
{ |
331
|
5314 |
|
return $this->_config; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Gets the EventManager used by the Connection. |
336
|
|
|
* |
337
|
|
|
* @return \Doctrine\Common\EventManager |
338
|
|
|
*/ |
339
|
249 |
|
public function getEventManager() |
340
|
|
|
{ |
341
|
249 |
|
return $this->_eventManager; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Gets the DatabasePlatform for the connection. |
346
|
|
|
* |
347
|
|
|
* @return \Doctrine\DBAL\Platforms\AbstractPlatform |
348
|
|
|
* |
349
|
|
|
* @throws \Doctrine\DBAL\DBALException |
350
|
|
|
*/ |
351
|
4461 |
|
public function getDatabasePlatform() |
352
|
|
|
{ |
353
|
4461 |
|
if (null === $this->platform) { |
354
|
675 |
|
$this->detectDatabasePlatform(); |
355
|
|
|
} |
356
|
|
|
|
357
|
4443 |
|
return $this->platform; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Gets the ExpressionBuilder for the connection. |
362
|
|
|
* |
363
|
|
|
* @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
364
|
|
|
*/ |
365
|
|
|
public function getExpressionBuilder() |
366
|
|
|
{ |
367
|
|
|
return $this->_expr; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Establishes the connection with the database. |
372
|
|
|
* |
373
|
|
|
* @return bool TRUE if the connection was successfully established, FALSE if |
374
|
|
|
* the connection is already open. |
375
|
|
|
*/ |
376
|
5425 |
|
public function connect() |
377
|
|
|
{ |
378
|
5425 |
|
if ($this->_isConnected) { |
379
|
5062 |
|
return false; |
380
|
|
|
} |
381
|
|
|
|
382
|
848 |
|
$driverOptions = $this->_params['driverOptions'] ?? []; |
383
|
848 |
|
$user = $this->_params['user'] ?? null; |
384
|
848 |
|
$password = $this->_params['password'] ?? null; |
385
|
|
|
|
386
|
848 |
|
$this->_conn = $this->_driver->connect($this->_params, $user, $password, $driverOptions); |
387
|
790 |
|
$this->_isConnected = true; |
388
|
|
|
|
389
|
790 |
|
if (false === $this->autoCommit) { |
390
|
54 |
|
$this->beginTransaction(); |
391
|
|
|
} |
392
|
|
|
|
393
|
790 |
|
if ($this->_eventManager->hasListeners(Events::postConnect)) { |
394
|
18 |
|
$eventArgs = new Event\ConnectionEventArgs($this); |
395
|
18 |
|
$this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs); |
396
|
|
|
} |
397
|
|
|
|
398
|
790 |
|
return true; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Detects and sets the database platform. |
403
|
|
|
* |
404
|
|
|
* Evaluates custom platform class and version in order to set the correct platform. |
405
|
|
|
* |
406
|
|
|
* @throws DBALException if an invalid platform was specified for this connection. |
407
|
|
|
*/ |
408
|
675 |
|
private function detectDatabasePlatform() |
409
|
|
|
{ |
410
|
675 |
|
$version = $this->getDatabasePlatformVersion(); |
411
|
|
|
|
412
|
618 |
|
if (null !== $version) { |
413
|
475 |
|
$this->platform = $this->_driver->createDatabasePlatformForVersion($version); |
|
|
|
|
414
|
|
|
} else { |
415
|
143 |
|
$this->platform = $this->_driver->getDatabasePlatform(); |
416
|
|
|
} |
417
|
|
|
|
418
|
618 |
|
$this->platform->setEventManager($this->_eventManager); |
419
|
618 |
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Returns the version of the related platform if applicable. |
423
|
|
|
* |
424
|
|
|
* Returns null if either the driver is not capable to create version |
425
|
|
|
* specific platform instances, no explicit server version was specified |
426
|
|
|
* or the underlying driver connection cannot determine the platform |
427
|
|
|
* version without having to query it (performance reasons). |
428
|
|
|
* |
429
|
|
|
* @return string|null |
430
|
|
|
* |
431
|
|
|
* @throws Exception |
432
|
|
|
*/ |
433
|
675 |
|
private function getDatabasePlatformVersion() |
434
|
|
|
{ |
435
|
|
|
// Driver does not support version specific platforms. |
436
|
675 |
|
if ( ! $this->_driver instanceof VersionAwarePlatformDriver) { |
437
|
125 |
|
return null; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
// Explicit platform version requested (supersedes auto-detection). |
441
|
550 |
|
if (isset($this->_params['serverVersion'])) { |
442
|
|
|
return $this->_params['serverVersion']; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
// If not connected, we need to connect now to determine the platform version. |
446
|
550 |
|
if (null === $this->_conn) { |
447
|
|
|
try { |
448
|
516 |
|
$this->connect(); |
449
|
74 |
|
} catch (\Exception $originalException) { |
450
|
74 |
|
if (empty($this->_params['dbname'])) { |
451
|
|
|
throw $originalException; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
// The database to connect to might not yet exist. |
455
|
|
|
// Retry detection without database name connection parameter. |
456
|
74 |
|
$databaseName = $this->_params['dbname']; |
457
|
74 |
|
$this->_params['dbname'] = null; |
458
|
|
|
|
459
|
|
|
try { |
460
|
74 |
|
$this->connect(); |
461
|
57 |
|
} catch (\Exception $fallbackException) { |
462
|
|
|
// Either the platform does not support database-less connections |
463
|
|
|
// or something else went wrong. |
464
|
|
|
// Reset connection parameters and rethrow the original exception. |
465
|
57 |
|
$this->_params['dbname'] = $databaseName; |
466
|
|
|
|
467
|
57 |
|
throw $originalException; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
// Reset connection parameters. |
471
|
17 |
|
$this->_params['dbname'] = $databaseName; |
472
|
17 |
|
$serverVersion = $this->getServerVersion(); |
473
|
|
|
|
474
|
|
|
// Close "temporary" connection to allow connecting to the real database again. |
475
|
17 |
|
$this->close(); |
476
|
|
|
|
477
|
17 |
|
return $serverVersion; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
} |
481
|
|
|
|
482
|
493 |
|
return $this->getServerVersion(); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Returns the database server version if the underlying driver supports it. |
487
|
|
|
* |
488
|
|
|
* @return string|null |
489
|
|
|
*/ |
490
|
493 |
|
private function getServerVersion() |
491
|
|
|
{ |
492
|
|
|
// Automatic platform version detection. |
493
|
493 |
|
if ($this->_conn instanceof ServerInfoAwareConnection && |
494
|
493 |
|
! $this->_conn->requiresQueryForServerVersion() |
495
|
|
|
) { |
496
|
475 |
|
return $this->_conn->getServerVersion(); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
// Unable to detect platform version. |
500
|
18 |
|
return null; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Returns the current auto-commit mode for this connection. |
505
|
|
|
* |
506
|
|
|
* @return bool True if auto-commit mode is currently enabled for this connection, false otherwise. |
507
|
|
|
* |
508
|
|
|
* @see setAutoCommit |
509
|
|
|
*/ |
510
|
36 |
|
public function isAutoCommit() |
511
|
|
|
{ |
512
|
36 |
|
return true === $this->autoCommit; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Sets auto-commit mode for this connection. |
517
|
|
|
* |
518
|
|
|
* If a connection is in auto-commit mode, then all its SQL statements will be executed and committed as individual |
519
|
|
|
* transactions. Otherwise, its SQL statements are grouped into transactions that are terminated by a call to either |
520
|
|
|
* the method commit or the method rollback. By default, new connections are in auto-commit mode. |
521
|
|
|
* |
522
|
|
|
* NOTE: If this method is called during a transaction and the auto-commit mode is changed, the transaction is |
523
|
|
|
* committed. If this method is called and the auto-commit mode is not changed, the call is a no-op. |
524
|
|
|
* |
525
|
|
|
* @param bool $autoCommit True to enable auto-commit mode; false to disable it. |
526
|
|
|
* |
527
|
|
|
* @see isAutoCommit |
528
|
|
|
*/ |
529
|
90 |
|
public function setAutoCommit($autoCommit) |
530
|
|
|
{ |
531
|
90 |
|
$autoCommit = (boolean) $autoCommit; |
532
|
|
|
|
533
|
|
|
// Mode not changed, no-op. |
534
|
90 |
|
if ($autoCommit === $this->autoCommit) { |
535
|
18 |
|
return; |
536
|
|
|
} |
537
|
|
|
|
538
|
90 |
|
$this->autoCommit = $autoCommit; |
539
|
|
|
|
540
|
|
|
// Commit all currently active transactions if any when switching auto-commit mode. |
541
|
90 |
|
if (true === $this->_isConnected && 0 !== $this->_transactionNestingLevel) { |
542
|
18 |
|
$this->commitAll(); |
543
|
|
|
} |
544
|
90 |
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* Sets the fetch mode. |
548
|
|
|
* |
549
|
|
|
* @param int $fetchMode |
550
|
|
|
* |
551
|
|
|
* @return void |
552
|
|
|
*/ |
553
|
18 |
|
public function setFetchMode($fetchMode) |
554
|
|
|
{ |
555
|
18 |
|
$this->defaultFetchMode = $fetchMode; |
556
|
18 |
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* Prepares and executes an SQL query and returns the first row of the result |
560
|
|
|
* as an associative array. |
561
|
|
|
* |
562
|
|
|
* @param string $statement The SQL query. |
563
|
|
|
* @param array $params The query parameters. |
564
|
|
|
* @param array $types The query parameter types. |
565
|
|
|
* |
566
|
|
|
* @return array|bool False is returned if no rows are found. |
567
|
|
|
* |
568
|
|
|
* @throws \Doctrine\DBAL\DBALException |
569
|
|
|
*/ |
570
|
845 |
|
public function fetchAssoc($statement, array $params = [], array $types = []) |
571
|
|
|
{ |
572
|
845 |
|
return $this->executeQuery($statement, $params, $types)->fetch(FetchMode::ASSOCIATIVE); |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* Prepares and executes an SQL query and returns the first row of the result |
577
|
|
|
* as a numerically indexed array. |
578
|
|
|
* |
579
|
|
|
* @param string $statement The SQL query to be executed. |
580
|
|
|
* @param array $params The prepared statement params. |
581
|
|
|
* @param array $types The query parameter types. |
582
|
|
|
* |
583
|
|
|
* @return array|bool False is returned if no rows are found. |
584
|
|
|
*/ |
585
|
68 |
|
public function fetchArray($statement, array $params = [], array $types = []) |
586
|
|
|
{ |
587
|
68 |
|
return $this->executeQuery($statement, $params, $types)->fetch(FetchMode::NUMERIC); |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* Prepares and executes an SQL query and returns the value of a single column |
592
|
|
|
* of the first row of the result. |
593
|
|
|
* |
594
|
|
|
* @param string $statement The SQL query to be executed. |
595
|
|
|
* @param array $params The prepared statement params. |
596
|
|
|
* @param int $column The 0-indexed column number to retrieve. |
597
|
|
|
* @param array $types The query parameter types. |
598
|
|
|
* |
599
|
|
|
* @return mixed|bool False is returned if no rows are found. |
600
|
|
|
* |
601
|
|
|
* @throws \Doctrine\DBAL\DBALException |
602
|
|
|
*/ |
603
|
629 |
|
public function fetchColumn($statement, array $params = [], $column = 0, array $types = []) |
604
|
|
|
{ |
605
|
629 |
|
return $this->executeQuery($statement, $params, $types)->fetchColumn($column); |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* Whether an actual connection to the database is established. |
610
|
|
|
* |
611
|
|
|
* @return bool |
612
|
|
|
*/ |
613
|
72 |
|
public function isConnected() |
614
|
|
|
{ |
615
|
72 |
|
return $this->_isConnected; |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
/** |
619
|
|
|
* Checks whether a transaction is currently active. |
620
|
|
|
* |
621
|
|
|
* @return bool TRUE if a transaction is currently active, FALSE otherwise. |
622
|
|
|
*/ |
623
|
5296 |
|
public function isTransactionActive() |
624
|
|
|
{ |
625
|
5296 |
|
return $this->_transactionNestingLevel > 0; |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* Gathers conditions for an update or delete call. |
630
|
|
|
* |
631
|
|
|
* @param array $identifiers Input array of columns to values |
632
|
|
|
* |
633
|
|
|
* @return string[][] a triplet with: |
634
|
|
|
* - the first key being the columns |
635
|
|
|
* - the second key being the values |
636
|
|
|
* - the third key being the conditions |
637
|
|
|
*/ |
638
|
263 |
|
private function gatherConditions(array $identifiers) |
639
|
|
|
{ |
640
|
263 |
|
$columns = []; |
641
|
263 |
|
$values = []; |
642
|
263 |
|
$conditions = []; |
643
|
|
|
|
644
|
263 |
|
foreach ($identifiers as $columnName => $value) { |
645
|
263 |
|
if (null === $value) { |
646
|
72 |
|
$conditions[] = $this->getDatabasePlatform()->getIsNullExpression($columnName); |
647
|
72 |
|
continue; |
648
|
|
|
} |
649
|
|
|
|
650
|
227 |
|
$columns[] = $columnName; |
651
|
227 |
|
$values[] = $value; |
652
|
227 |
|
$conditions[] = $columnName . ' = ?'; |
653
|
|
|
} |
654
|
|
|
|
655
|
263 |
|
return [$columns, $values, $conditions]; |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
/** |
659
|
|
|
* Executes an SQL DELETE statement on a table. |
660
|
|
|
* |
661
|
|
|
* Table expression and columns are not escaped and are not safe for user-input. |
662
|
|
|
* |
663
|
|
|
* @param string $tableExpression The expression of the table on which to delete. |
664
|
|
|
* @param array $identifier The deletion criteria. An associative array containing column-value pairs. |
665
|
|
|
* @param array $types The types of identifiers. |
666
|
|
|
* |
667
|
|
|
* @return int The number of affected rows. |
668
|
|
|
* |
669
|
|
|
* @throws \Doctrine\DBAL\DBALException |
670
|
|
|
* @throws InvalidArgumentException |
671
|
|
|
*/ |
672
|
123 |
|
public function delete($tableExpression, array $identifier, array $types = []) |
673
|
|
|
{ |
674
|
123 |
|
if (empty($identifier)) { |
675
|
18 |
|
throw InvalidArgumentException::fromEmptyCriteria(); |
676
|
|
|
} |
677
|
|
|
|
678
|
105 |
|
list($columns, $values, $conditions) = $this->gatherConditions($identifier); |
679
|
|
|
|
680
|
105 |
|
return $this->executeUpdate( |
681
|
105 |
|
'DELETE FROM ' . $tableExpression . ' WHERE ' . implode(' AND ', $conditions), |
682
|
105 |
|
$values, |
683
|
105 |
|
is_string(key($types)) ? $this->extractTypeValues($columns, $types) : $types |
684
|
|
|
); |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* Closes the connection. |
689
|
|
|
* |
690
|
|
|
* @return void |
691
|
|
|
*/ |
692
|
452 |
|
public function close() |
693
|
|
|
{ |
694
|
452 |
|
$this->_conn = null; |
695
|
|
|
|
696
|
452 |
|
$this->_isConnected = false; |
697
|
452 |
|
} |
698
|
|
|
|
699
|
|
|
/** |
700
|
|
|
* Sets the transaction isolation level. |
701
|
|
|
* |
702
|
|
|
* @param int $level The level to set. |
703
|
|
|
* |
704
|
|
|
* @return int |
705
|
|
|
*/ |
706
|
|
|
public function setTransactionIsolation($level) |
707
|
|
|
{ |
708
|
|
|
$this->_transactionIsolationLevel = $level; |
709
|
|
|
|
710
|
|
|
return $this->executeUpdate($this->getDatabasePlatform()->getSetTransactionIsolationSQL($level)); |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* Gets the currently active transaction isolation level. |
715
|
|
|
* |
716
|
|
|
* @return int The current transaction isolation level. |
717
|
|
|
*/ |
718
|
|
|
public function getTransactionIsolation() |
719
|
|
|
{ |
720
|
|
|
if (null === $this->_transactionIsolationLevel) { |
721
|
|
|
$this->_transactionIsolationLevel = $this->getDatabasePlatform()->getDefaultTransactionIsolationLevel(); |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
return $this->_transactionIsolationLevel; |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
/** |
728
|
|
|
* Executes an SQL UPDATE statement on a table. |
729
|
|
|
* |
730
|
|
|
* Table expression and columns are not escaped and are not safe for user-input. |
731
|
|
|
* |
732
|
|
|
* @param string $tableExpression The expression of the table to update quoted or unquoted. |
733
|
|
|
* @param array $data An associative array containing column-value pairs. |
734
|
|
|
* @param array $identifier The update criteria. An associative array containing column-value pairs. |
735
|
|
|
* @param array $types Types of the merged $data and $identifier arrays in that order. |
736
|
|
|
* |
737
|
|
|
* @return int The number of affected rows. |
738
|
|
|
* |
739
|
|
|
* @throws \Doctrine\DBAL\DBALException |
740
|
|
|
*/ |
741
|
158 |
|
public function update($tableExpression, array $data, array $identifier, array $types = []) |
742
|
|
|
{ |
743
|
158 |
|
$setColumns = []; |
744
|
158 |
|
$setValues = []; |
745
|
158 |
|
$set = []; |
746
|
|
|
|
747
|
158 |
|
foreach ($data as $columnName => $value) { |
748
|
158 |
|
$setColumns[] = $columnName; |
749
|
158 |
|
$setValues[] = $value; |
750
|
158 |
|
$set[] = $columnName . ' = ?'; |
751
|
|
|
} |
752
|
|
|
|
753
|
158 |
|
list($conditionColumns, $conditionValues, $conditions) = $this->gatherConditions($identifier); |
754
|
158 |
|
$columns = array_merge($setColumns, $conditionColumns); |
755
|
158 |
|
$values = array_merge($setValues, $conditionValues); |
756
|
|
|
|
757
|
158 |
|
if (is_string(key($types))) { |
758
|
90 |
|
$types = $this->extractTypeValues($columns, $types); |
759
|
|
|
} |
760
|
|
|
|
761
|
158 |
|
$sql = 'UPDATE ' . $tableExpression . ' SET ' . implode(', ', $set) |
762
|
158 |
|
. ' WHERE ' . implode(' AND ', $conditions); |
763
|
|
|
|
764
|
158 |
|
return $this->executeUpdate($sql, $values, $types); |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
/** |
768
|
|
|
* Inserts a table row with specified data. |
769
|
|
|
* |
770
|
|
|
* Table expression and columns are not escaped and are not safe for user-input. |
771
|
|
|
* |
772
|
|
|
* @param string $tableExpression The expression of the table to insert data into, quoted or unquoted. |
773
|
|
|
* @param array $data An associative array containing column-value pairs. |
774
|
|
|
* @param array $types Types of the inserted data. |
775
|
|
|
* |
776
|
|
|
* @return int The number of affected rows. |
777
|
|
|
* |
778
|
|
|
* @throws \Doctrine\DBAL\DBALException |
779
|
|
|
*/ |
780
|
1556 |
|
public function insert($tableExpression, array $data, array $types = []) |
781
|
|
|
{ |
782
|
1556 |
|
if (empty($data)) { |
783
|
18 |
|
return $this->executeUpdate('INSERT INTO ' . $tableExpression . ' ()' . ' VALUES ()'); |
784
|
|
|
} |
785
|
|
|
|
786
|
1538 |
|
$columns = []; |
787
|
1538 |
|
$values = []; |
788
|
1538 |
|
$set = []; |
789
|
|
|
|
790
|
1538 |
|
foreach ($data as $columnName => $value) { |
791
|
1538 |
|
$columns[] = $columnName; |
792
|
1538 |
|
$values[] = $value; |
793
|
1538 |
|
$set[] = '?'; |
794
|
|
|
} |
795
|
|
|
|
796
|
1538 |
|
return $this->executeUpdate( |
797
|
1538 |
|
'INSERT INTO ' . $tableExpression . ' (' . implode(', ', $columns) . ')' . |
798
|
1538 |
|
' VALUES (' . implode(', ', $set) . ')', |
799
|
1538 |
|
$values, |
800
|
1538 |
|
is_string(key($types)) ? $this->extractTypeValues($columns, $types) : $types |
801
|
|
|
); |
802
|
|
|
} |
803
|
|
|
|
804
|
|
|
/** |
805
|
|
|
* Extract ordered type list from an ordered column list and type map. |
806
|
|
|
* |
807
|
|
|
* @param array $columnList |
808
|
|
|
* @param array $types |
809
|
|
|
* |
810
|
|
|
* @return array |
811
|
|
|
*/ |
812
|
162 |
|
private function extractTypeValues(array $columnList, array $types) |
813
|
|
|
{ |
814
|
162 |
|
$typeValues = []; |
815
|
|
|
|
816
|
162 |
|
foreach ($columnList as $columnIndex => $columnName) { |
817
|
162 |
|
$typeValues[] = $types[$columnName] ?? ParameterType::STRING; |
818
|
|
|
} |
819
|
|
|
|
820
|
162 |
|
return $typeValues; |
821
|
|
|
} |
822
|
|
|
|
823
|
|
|
/** |
824
|
|
|
* Quotes a string so it can be safely used as a table or column name, even if |
825
|
|
|
* it is a reserved name. |
826
|
|
|
* |
827
|
|
|
* Delimiting style depends on the underlying database platform that is being used. |
828
|
|
|
* |
829
|
|
|
* NOTE: Just because you CAN use quoted identifiers does not mean |
830
|
|
|
* you SHOULD use them. In general, they end up causing way more |
831
|
|
|
* problems than they solve. |
832
|
|
|
* |
833
|
|
|
* @param string $str The name to be quoted. |
834
|
|
|
* |
835
|
|
|
* @return string The quoted name. |
836
|
|
|
*/ |
837
|
18 |
|
public function quoteIdentifier($str) |
838
|
|
|
{ |
839
|
18 |
|
return $this->getDatabasePlatform()->quoteIdentifier($str); |
840
|
|
|
} |
841
|
|
|
|
842
|
|
|
/** |
843
|
|
|
* Quotes a given input parameter. |
844
|
|
|
* |
845
|
|
|
* @param mixed $input The parameter to be quoted. |
846
|
|
|
* @param int|null $type The type of the parameter. |
847
|
|
|
* |
848
|
|
|
* @return string The quoted parameter. |
849
|
|
|
*/ |
850
|
80 |
|
public function quote($input, $type = null) |
851
|
|
|
{ |
852
|
80 |
|
$this->connect(); |
853
|
|
|
|
854
|
80 |
|
list($value, $bindingType) = $this->getBindingInfo($input, $type); |
855
|
|
|
|
856
|
80 |
|
return $this->_conn->quote($value, $bindingType); |
857
|
|
|
} |
858
|
|
|
|
859
|
|
|
/** |
860
|
|
|
* Prepares and executes an SQL query and returns the result as an associative array. |
861
|
|
|
* |
862
|
|
|
* @param string $sql The SQL query. |
863
|
|
|
* @param array $params The query parameters. |
864
|
|
|
* @param array $types The query parameter types. |
865
|
|
|
* |
866
|
|
|
* @return array |
867
|
|
|
*/ |
868
|
1861 |
|
public function fetchAll($sql, array $params = [], $types = []) |
869
|
|
|
{ |
870
|
1861 |
|
return $this->executeQuery($sql, $params, $types)->fetchAll(); |
871
|
|
|
} |
872
|
|
|
|
873
|
|
|
/** |
874
|
|
|
* Prepares an SQL statement. |
875
|
|
|
* |
876
|
|
|
* @param string $statement The SQL statement to prepare. |
877
|
|
|
* |
878
|
|
|
* @return \Doctrine\DBAL\Statement The prepared statement. |
879
|
|
|
* |
880
|
|
|
* @throws \Doctrine\DBAL\DBALException |
881
|
|
|
*/ |
882
|
769 |
|
public function prepare($statement) |
883
|
|
|
{ |
884
|
|
|
try { |
885
|
769 |
|
$stmt = new Statement($statement, $this); |
886
|
18 |
|
} catch (Exception $ex) { |
887
|
18 |
|
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement); |
888
|
|
|
} |
889
|
|
|
|
890
|
751 |
|
$stmt->setFetchMode($this->defaultFetchMode); |
891
|
|
|
|
892
|
751 |
|
return $stmt; |
893
|
|
|
} |
894
|
|
|
|
895
|
|
|
/** |
896
|
|
|
* Executes an, optionally parametrized, SQL query. |
897
|
|
|
* |
898
|
|
|
* If the query is parametrized, a prepared statement is used. |
899
|
|
|
* If an SQLLogger is configured, the execution is logged. |
900
|
|
|
* |
901
|
|
|
* @param string $query The SQL query to execute. |
902
|
|
|
* @param array $params The parameters to bind to the query, if any. |
903
|
|
|
* @param array $types The types the previous parameters are in. |
904
|
|
|
* @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
905
|
|
|
* |
906
|
|
|
* @return \Doctrine\DBAL\Driver\Statement The executed statement. |
907
|
|
|
* |
908
|
|
|
* @throws \Doctrine\DBAL\DBALException |
909
|
|
|
*/ |
910
|
3704 |
|
public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null) |
911
|
|
|
{ |
912
|
3704 |
|
if ($qcp !== null) { |
913
|
180 |
|
return $this->executeCacheQuery($query, $params, $types, $qcp); |
914
|
|
|
} |
915
|
|
|
|
916
|
3704 |
|
$this->connect(); |
917
|
|
|
|
918
|
3704 |
|
$logger = $this->_config->getSQLLogger(); |
919
|
3704 |
|
if ($logger) { |
920
|
3587 |
|
$logger->startQuery($query, $params, $types); |
921
|
|
|
} |
922
|
|
|
|
923
|
|
|
try { |
924
|
3704 |
|
if ($params) { |
|
|
|
|
925
|
647 |
|
list($query, $params, $types) = SQLParserUtils::expandListParameters($query, $params, $types); |
926
|
|
|
|
927
|
647 |
|
$stmt = $this->_conn->prepare($query); |
928
|
647 |
|
if ($types) { |
929
|
288 |
|
$this->_bindTypedValues($stmt, $params, $types); |
930
|
288 |
|
$stmt->execute(); |
931
|
|
|
} else { |
932
|
647 |
|
$stmt->execute($params); |
933
|
|
|
} |
934
|
|
|
} else { |
935
|
3648 |
|
$stmt = $this->_conn->query($query); |
|
|
|
|
936
|
|
|
} |
937
|
122 |
|
} catch (Exception $ex) { |
938
|
122 |
|
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types)); |
939
|
|
|
} |
940
|
|
|
|
941
|
3582 |
|
$stmt->setFetchMode($this->defaultFetchMode); |
942
|
|
|
|
943
|
3582 |
|
if ($logger) { |
944
|
3483 |
|
$logger->stopQuery(); |
945
|
|
|
} |
946
|
|
|
|
947
|
3582 |
|
return $stmt; |
948
|
|
|
} |
949
|
|
|
|
950
|
|
|
/** |
951
|
|
|
* Executes a caching query. |
952
|
|
|
* |
953
|
|
|
* @param string $query The SQL query to execute. |
954
|
|
|
* @param array $params The parameters to bind to the query, if any. |
955
|
|
|
* @param array $types The types the previous parameters are in. |
956
|
|
|
* @param \Doctrine\DBAL\Cache\QueryCacheProfile $qcp The query cache profile. |
957
|
|
|
* |
958
|
|
|
* @return \Doctrine\DBAL\Driver\ResultStatement |
959
|
|
|
* |
960
|
|
|
* @throws \Doctrine\DBAL\Cache\CacheException |
961
|
|
|
*/ |
962
|
216 |
|
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp) |
963
|
|
|
{ |
964
|
216 |
|
$resultCache = $qcp->getResultCacheDriver() ?: $this->_config->getResultCacheImpl(); |
965
|
216 |
|
if ( ! $resultCache) { |
966
|
|
|
throw CacheException::noResultDriverConfigured(); |
967
|
|
|
} |
968
|
|
|
|
969
|
216 |
|
list($cacheKey, $realKey) = $qcp->generateCacheKeys($query, $params, $types, $this->getParams()); |
970
|
|
|
|
971
|
|
|
// fetch the row pointers entry |
972
|
216 |
|
if ($data = $resultCache->fetch($cacheKey)) { |
973
|
|
|
// is the real key part of this row pointers map or is the cache only pointing to other cache keys? |
974
|
162 |
|
if (isset($data[$realKey])) { |
975
|
162 |
|
$stmt = new ArrayStatement($data[$realKey]); |
976
|
|
|
} elseif (array_key_exists($realKey, $data)) { |
977
|
|
|
$stmt = new ArrayStatement([]); |
978
|
|
|
} |
979
|
|
|
} |
980
|
|
|
|
981
|
216 |
|
if (!isset($stmt)) { |
982
|
180 |
|
$stmt = new ResultCacheStatement($this->executeQuery($query, $params, $types), $resultCache, $cacheKey, $realKey, $qcp->getLifetime()); |
983
|
|
|
} |
984
|
|
|
|
985
|
216 |
|
$stmt->setFetchMode($this->defaultFetchMode); |
986
|
|
|
|
987
|
216 |
|
return $stmt; |
988
|
|
|
} |
989
|
|
|
|
990
|
|
|
/** |
991
|
|
|
* Executes an, optionally parametrized, SQL query and returns the result, |
992
|
|
|
* applying a given projection/transformation function on each row of the result. |
993
|
|
|
* |
994
|
|
|
* @param string $query The SQL query to execute. |
995
|
|
|
* @param array $params The parameters, if any. |
996
|
|
|
* @param \Closure $function The transformation function that is applied on each row. |
997
|
|
|
* The function receives a single parameter, an array, that |
998
|
|
|
* represents a row of the result set. |
999
|
|
|
* |
1000
|
|
|
* @return array The projected result of the query. |
1001
|
|
|
*/ |
1002
|
|
|
public function project($query, array $params, Closure $function) |
1003
|
|
|
{ |
1004
|
|
|
$result = []; |
1005
|
|
|
$stmt = $this->executeQuery($query, $params); |
1006
|
|
|
|
1007
|
|
|
while ($row = $stmt->fetch()) { |
1008
|
|
|
$result[] = $function($row); |
1009
|
|
|
} |
1010
|
|
|
|
1011
|
|
|
$stmt->closeCursor(); |
1012
|
|
|
|
1013
|
|
|
return $result; |
1014
|
|
|
} |
1015
|
|
|
|
1016
|
|
|
/** |
1017
|
|
|
* Executes an SQL statement, returning a result set as a Statement object. |
1018
|
|
|
* |
1019
|
|
|
* @return \Doctrine\DBAL\Driver\Statement |
1020
|
|
|
* |
1021
|
|
|
* @throws \Doctrine\DBAL\DBALException |
1022
|
|
|
*/ |
1023
|
271 |
|
public function query() |
1024
|
|
|
{ |
1025
|
271 |
|
$this->connect(); |
1026
|
|
|
|
1027
|
271 |
|
$args = func_get_args(); |
1028
|
|
|
|
1029
|
271 |
|
$logger = $this->_config->getSQLLogger(); |
1030
|
271 |
|
if ($logger) { |
1031
|
253 |
|
$logger->startQuery($args[0]); |
1032
|
|
|
} |
1033
|
|
|
|
1034
|
|
|
try { |
1035
|
271 |
|
$statement = $this->_conn->query(...$args); |
|
|
|
|
1036
|
18 |
|
} catch (Exception $ex) { |
1037
|
18 |
|
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $args[0]); |
1038
|
|
|
} |
1039
|
|
|
|
1040
|
253 |
|
$statement->setFetchMode($this->defaultFetchMode); |
1041
|
|
|
|
1042
|
253 |
|
if ($logger) { |
1043
|
253 |
|
$logger->stopQuery(); |
1044
|
|
|
} |
1045
|
|
|
|
1046
|
253 |
|
return $statement; |
1047
|
|
|
} |
1048
|
|
|
|
1049
|
|
|
/** |
1050
|
|
|
* Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
1051
|
|
|
* and returns the number of affected rows. |
1052
|
|
|
* |
1053
|
|
|
* This method supports PDO binding types as well as DBAL mapping types. |
1054
|
|
|
* |
1055
|
|
|
* @param string $query The SQL query. |
1056
|
|
|
* @param array $params The query parameters. |
1057
|
|
|
* @param array $types The parameter types. |
1058
|
|
|
* |
1059
|
|
|
* @return int The number of affected rows. |
1060
|
|
|
* |
1061
|
|
|
* @throws \Doctrine\DBAL\DBALException |
1062
|
|
|
*/ |
1063
|
3316 |
|
public function executeUpdate($query, array $params = [], array $types = []) |
1064
|
|
|
{ |
1065
|
3316 |
|
$this->connect(); |
1066
|
|
|
|
1067
|
3316 |
|
$logger = $this->_config->getSQLLogger(); |
1068
|
3316 |
|
if ($logger) { |
1069
|
3226 |
|
$logger->startQuery($query, $params, $types); |
1070
|
|
|
} |
1071
|
|
|
|
1072
|
|
|
try { |
1073
|
3316 |
|
if ($params) { |
|
|
|
|
1074
|
1669 |
|
list($query, $params, $types) = SQLParserUtils::expandListParameters($query, $params, $types); |
1075
|
|
|
|
1076
|
1669 |
|
$stmt = $this->_conn->prepare($query); |
1077
|
1665 |
|
if ($types) { |
1078
|
237 |
|
$this->_bindTypedValues($stmt, $params, $types); |
1079
|
237 |
|
$stmt->execute(); |
1080
|
|
|
} else { |
1081
|
1428 |
|
$stmt->execute($params); |
1082
|
|
|
} |
1083
|
1637 |
|
$result = $stmt->rowCount(); |
1084
|
|
|
} else { |
1085
|
3284 |
|
$result = $this->_conn->exec($query); |
1086
|
|
|
} |
1087
|
2127 |
|
} catch (Exception $ex) { |
1088
|
2127 |
|
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types)); |
1089
|
|
|
} |
1090
|
|
|
|
1091
|
3032 |
|
if ($logger) { |
1092
|
2960 |
|
$logger->stopQuery(); |
1093
|
|
|
} |
1094
|
|
|
|
1095
|
3032 |
|
return $result; |
1096
|
|
|
} |
1097
|
|
|
|
1098
|
|
|
/** |
1099
|
|
|
* Executes an SQL statement and return the number of affected rows. |
1100
|
|
|
* |
1101
|
|
|
* @param string $statement |
1102
|
|
|
* |
1103
|
|
|
* @return int The number of affected rows. |
1104
|
|
|
* |
1105
|
|
|
* @throws \Doctrine\DBAL\DBALException |
1106
|
|
|
*/ |
1107
|
1740 |
|
public function exec($statement) |
1108
|
|
|
{ |
1109
|
1740 |
|
$this->connect(); |
1110
|
|
|
|
1111
|
1739 |
|
$logger = $this->_config->getSQLLogger(); |
1112
|
1739 |
|
if ($logger) { |
1113
|
1684 |
|
$logger->startQuery($statement); |
1114
|
|
|
} |
1115
|
|
|
|
1116
|
|
|
try { |
1117
|
1739 |
|
$result = $this->_conn->exec($statement); |
1118
|
1346 |
|
} catch (Exception $ex) { |
1119
|
1346 |
|
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement); |
1120
|
|
|
} |
1121
|
|
|
|
1122
|
454 |
|
if ($logger) { |
1123
|
418 |
|
$logger->stopQuery(); |
1124
|
|
|
} |
1125
|
|
|
|
1126
|
454 |
|
return $result; |
1127
|
|
|
} |
1128
|
|
|
|
1129
|
|
|
/** |
1130
|
|
|
* Returns the current transaction nesting level. |
1131
|
|
|
* |
1132
|
|
|
* @return int The nesting level. A value of 0 means there's no active transaction. |
1133
|
|
|
*/ |
1134
|
333 |
|
public function getTransactionNestingLevel() |
1135
|
|
|
{ |
1136
|
333 |
|
return $this->_transactionNestingLevel; |
1137
|
|
|
} |
1138
|
|
|
|
1139
|
|
|
/** |
1140
|
|
|
* Fetches the SQLSTATE associated with the last database operation. |
1141
|
|
|
* |
1142
|
|
|
* @return int The last error code. |
1143
|
|
|
*/ |
1144
|
|
|
public function errorCode() |
1145
|
|
|
{ |
1146
|
|
|
$this->connect(); |
1147
|
|
|
|
1148
|
|
|
return $this->_conn->errorCode(); |
|
|
|
|
1149
|
|
|
} |
1150
|
|
|
|
1151
|
|
|
/** |
1152
|
|
|
* Fetches extended error information associated with the last database operation. |
1153
|
|
|
* |
1154
|
|
|
* @return array The last error information. |
1155
|
|
|
*/ |
1156
|
|
|
public function errorInfo() |
1157
|
|
|
{ |
1158
|
|
|
$this->connect(); |
1159
|
|
|
|
1160
|
|
|
return $this->_conn->errorInfo(); |
1161
|
|
|
} |
1162
|
|
|
|
1163
|
|
|
/** |
1164
|
|
|
* Returns the ID of the last inserted row, or the last value from a sequence object, |
1165
|
|
|
* depending on the underlying driver. |
1166
|
|
|
* |
1167
|
|
|
* Note: This method may not return a meaningful or consistent result across different drivers, |
1168
|
|
|
* because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
1169
|
|
|
* columns or sequences. |
1170
|
|
|
* |
1171
|
|
|
* @param string|null $seqName Name of the sequence object from which the ID should be returned. |
1172
|
|
|
* |
1173
|
|
|
* @return string A string representation of the last inserted ID. |
1174
|
|
|
*/ |
1175
|
92 |
|
public function lastInsertId($seqName = null) |
1176
|
|
|
{ |
1177
|
92 |
|
$this->connect(); |
1178
|
|
|
|
1179
|
92 |
|
return $this->_conn->lastInsertId($seqName); |
1180
|
|
|
} |
1181
|
|
|
|
1182
|
|
|
/** |
1183
|
|
|
* Executes a function in a transaction. |
1184
|
|
|
* |
1185
|
|
|
* The function gets passed this Connection instance as an (optional) parameter. |
1186
|
|
|
* |
1187
|
|
|
* If an exception occurs during execution of the function or transaction commit, |
1188
|
|
|
* the transaction is rolled back and the exception re-thrown. |
1189
|
|
|
* |
1190
|
|
|
* @param \Closure $func The function to execute transactionally. |
1191
|
|
|
* |
1192
|
|
|
* @return mixed The value returned by $func |
1193
|
|
|
* |
1194
|
|
|
* @throws Exception |
1195
|
|
|
* @throws Throwable |
1196
|
|
|
*/ |
1197
|
72 |
|
public function transactional(Closure $func) |
1198
|
|
|
{ |
1199
|
72 |
|
$this->beginTransaction(); |
1200
|
|
|
try { |
1201
|
72 |
|
$res = $func($this); |
1202
|
36 |
|
$this->commit(); |
1203
|
36 |
|
return $res; |
1204
|
36 |
|
} catch (Exception $e) { |
1205
|
18 |
|
$this->rollBack(); |
1206
|
18 |
|
throw $e; |
1207
|
18 |
|
} catch (Throwable $e) { |
1208
|
18 |
|
$this->rollBack(); |
1209
|
18 |
|
throw $e; |
1210
|
|
|
} |
1211
|
|
|
} |
1212
|
|
|
|
1213
|
|
|
/** |
1214
|
|
|
* Sets if nested transactions should use savepoints. |
1215
|
|
|
* |
1216
|
|
|
* @param bool $nestTransactionsWithSavepoints |
1217
|
|
|
* |
1218
|
|
|
* @return void |
1219
|
|
|
* |
1220
|
|
|
* @throws \Doctrine\DBAL\ConnectionException |
1221
|
|
|
*/ |
1222
|
36 |
|
public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints) |
1223
|
|
|
{ |
1224
|
36 |
|
if ($this->_transactionNestingLevel > 0) { |
1225
|
36 |
|
throw ConnectionException::mayNotAlterNestedTransactionWithSavepointsInTransaction(); |
1226
|
|
|
} |
1227
|
|
|
|
1228
|
18 |
|
if ( ! $this->getDatabasePlatform()->supportsSavepoints()) { |
1229
|
|
|
throw ConnectionException::savepointsNotSupported(); |
1230
|
|
|
} |
1231
|
|
|
|
1232
|
18 |
|
$this->_nestTransactionsWithSavepoints = (bool) $nestTransactionsWithSavepoints; |
1233
|
18 |
|
} |
1234
|
|
|
|
1235
|
|
|
/** |
1236
|
|
|
* Gets if nested transactions should use savepoints. |
1237
|
|
|
* |
1238
|
|
|
* @return bool |
1239
|
|
|
*/ |
1240
|
18 |
|
public function getNestTransactionsWithSavepoints() |
1241
|
|
|
{ |
1242
|
18 |
|
return $this->_nestTransactionsWithSavepoints; |
1243
|
|
|
} |
1244
|
|
|
|
1245
|
|
|
/** |
1246
|
|
|
* Returns the savepoint name to use for nested transactions are false if they are not supported |
1247
|
|
|
* "savepointFormat" parameter is not set |
1248
|
|
|
* |
1249
|
|
|
* @return mixed A string with the savepoint name or false. |
1250
|
|
|
*/ |
1251
|
18 |
|
protected function _getNestedTransactionSavePointName() |
1252
|
|
|
{ |
1253
|
18 |
|
return 'DOCTRINE2_SAVEPOINT_'.$this->_transactionNestingLevel; |
1254
|
|
|
} |
1255
|
|
|
|
1256
|
|
|
/** |
1257
|
|
|
* Starts a transaction by suspending auto-commit mode. |
1258
|
|
|
* |
1259
|
|
|
* @return void |
1260
|
|
|
*/ |
1261
|
367 |
|
public function beginTransaction() |
1262
|
|
|
{ |
1263
|
367 |
|
$this->connect(); |
1264
|
|
|
|
1265
|
367 |
|
++$this->_transactionNestingLevel; |
1266
|
|
|
|
1267
|
367 |
|
$logger = $this->_config->getSQLLogger(); |
1268
|
|
|
|
1269
|
367 |
|
if ($this->_transactionNestingLevel == 1) { |
1270
|
367 |
|
if ($logger) { |
1271
|
268 |
|
$logger->startQuery('"START TRANSACTION"'); |
1272
|
|
|
} |
1273
|
367 |
|
$this->_conn->beginTransaction(); |
1274
|
367 |
|
if ($logger) { |
1275
|
367 |
|
$logger->stopQuery(); |
1276
|
|
|
} |
1277
|
54 |
|
} elseif ($this->_nestTransactionsWithSavepoints) { |
1278
|
18 |
|
if ($logger) { |
1279
|
18 |
|
$logger->startQuery('"SAVEPOINT"'); |
1280
|
|
|
} |
1281
|
18 |
|
$this->createSavepoint($this->_getNestedTransactionSavePointName()); |
1282
|
18 |
|
if ($logger) { |
1283
|
18 |
|
$logger->stopQuery(); |
1284
|
|
|
} |
1285
|
|
|
} |
1286
|
367 |
|
} |
1287
|
|
|
|
1288
|
|
|
/** |
1289
|
|
|
* Commits the current transaction. |
1290
|
|
|
* |
1291
|
|
|
* @return void |
1292
|
|
|
* |
1293
|
|
|
* @throws \Doctrine\DBAL\ConnectionException If the commit failed due to no active transaction or |
1294
|
|
|
* because the transaction was marked for rollback only. |
1295
|
|
|
*/ |
1296
|
205 |
|
public function commit() |
1297
|
|
|
{ |
1298
|
205 |
|
if ($this->_transactionNestingLevel == 0) { |
1299
|
18 |
|
throw ConnectionException::noActiveTransaction(); |
1300
|
|
|
} |
1301
|
187 |
|
if ($this->_isRollbackOnly) { |
1302
|
36 |
|
throw ConnectionException::commitFailedRollbackOnly(); |
1303
|
|
|
} |
1304
|
|
|
|
1305
|
151 |
|
$this->connect(); |
1306
|
|
|
|
1307
|
151 |
|
$logger = $this->_config->getSQLLogger(); |
1308
|
|
|
|
1309
|
151 |
|
if ($this->_transactionNestingLevel == 1) { |
1310
|
151 |
|
if ($logger) { |
1311
|
106 |
|
$logger->startQuery('"COMMIT"'); |
1312
|
|
|
} |
1313
|
151 |
|
$this->_conn->commit(); |
1314
|
151 |
|
if ($logger) { |
1315
|
151 |
|
$logger->stopQuery(); |
1316
|
|
|
} |
1317
|
36 |
|
} elseif ($this->_nestTransactionsWithSavepoints) { |
1318
|
18 |
|
if ($logger) { |
1319
|
18 |
|
$logger->startQuery('"RELEASE SAVEPOINT"'); |
1320
|
|
|
} |
1321
|
18 |
|
$this->releaseSavepoint($this->_getNestedTransactionSavePointName()); |
1322
|
18 |
|
if ($logger) { |
1323
|
18 |
|
$logger->stopQuery(); |
1324
|
|
|
} |
1325
|
|
|
} |
1326
|
|
|
|
1327
|
151 |
|
--$this->_transactionNestingLevel; |
1328
|
|
|
|
1329
|
151 |
|
if (false === $this->autoCommit && 0 === $this->_transactionNestingLevel) { |
1330
|
36 |
|
$this->beginTransaction(); |
1331
|
|
|
} |
1332
|
151 |
|
} |
1333
|
|
|
|
1334
|
|
|
/** |
1335
|
|
|
* Commits all current nesting transactions. |
1336
|
|
|
*/ |
1337
|
18 |
|
private function commitAll() |
1338
|
|
|
{ |
1339
|
18 |
|
while (0 !== $this->_transactionNestingLevel) { |
1340
|
18 |
|
if (false === $this->autoCommit && 1 === $this->_transactionNestingLevel) { |
1341
|
|
|
// When in no auto-commit mode, the last nesting commit immediately starts a new transaction. |
1342
|
|
|
// Therefore we need to do the final commit here and then leave to avoid an infinite loop. |
1343
|
18 |
|
$this->commit(); |
1344
|
|
|
|
1345
|
18 |
|
return; |
1346
|
|
|
} |
1347
|
|
|
|
1348
|
18 |
|
$this->commit(); |
1349
|
|
|
} |
1350
|
18 |
|
} |
1351
|
|
|
|
1352
|
|
|
/** |
1353
|
|
|
* Cancels any database changes done during the current transaction. |
1354
|
|
|
* |
1355
|
|
|
* @throws \Doctrine\DBAL\ConnectionException If the rollback operation failed. |
1356
|
|
|
*/ |
1357
|
233 |
|
public function rollBack() |
1358
|
|
|
{ |
1359
|
233 |
|
if ($this->_transactionNestingLevel == 0) { |
1360
|
18 |
|
throw ConnectionException::noActiveTransaction(); |
1361
|
|
|
} |
1362
|
|
|
|
1363
|
215 |
|
$this->connect(); |
1364
|
|
|
|
1365
|
215 |
|
$logger = $this->_config->getSQLLogger(); |
1366
|
|
|
|
1367
|
215 |
|
if ($this->_transactionNestingLevel == 1) { |
1368
|
197 |
|
if ($logger) { |
1369
|
179 |
|
$logger->startQuery('"ROLLBACK"'); |
1370
|
|
|
} |
1371
|
197 |
|
$this->_transactionNestingLevel = 0; |
1372
|
197 |
|
$this->_conn->rollBack(); |
1373
|
197 |
|
$this->_isRollbackOnly = false; |
1374
|
197 |
|
if ($logger) { |
1375
|
179 |
|
$logger->stopQuery(); |
1376
|
|
|
} |
1377
|
|
|
|
1378
|
197 |
|
if (false === $this->autoCommit) { |
1379
|
197 |
|
$this->beginTransaction(); |
1380
|
|
|
} |
1381
|
36 |
|
} elseif ($this->_nestTransactionsWithSavepoints) { |
1382
|
18 |
|
if ($logger) { |
1383
|
18 |
|
$logger->startQuery('"ROLLBACK TO SAVEPOINT"'); |
1384
|
|
|
} |
1385
|
18 |
|
$this->rollbackSavepoint($this->_getNestedTransactionSavePointName()); |
1386
|
18 |
|
--$this->_transactionNestingLevel; |
1387
|
18 |
|
if ($logger) { |
1388
|
18 |
|
$logger->stopQuery(); |
1389
|
|
|
} |
1390
|
|
|
} else { |
1391
|
18 |
|
$this->_isRollbackOnly = true; |
1392
|
18 |
|
--$this->_transactionNestingLevel; |
1393
|
|
|
} |
1394
|
215 |
|
} |
1395
|
|
|
|
1396
|
|
|
/** |
1397
|
|
|
* Creates a new savepoint. |
1398
|
|
|
* |
1399
|
|
|
* @param string $savepoint The name of the savepoint to create. |
1400
|
|
|
* |
1401
|
|
|
* @return void |
1402
|
|
|
* |
1403
|
|
|
* @throws \Doctrine\DBAL\ConnectionException |
1404
|
|
|
*/ |
1405
|
18 |
|
public function createSavepoint($savepoint) |
1406
|
|
|
{ |
1407
|
18 |
|
if ( ! $this->getDatabasePlatform()->supportsSavepoints()) { |
1408
|
|
|
throw ConnectionException::savepointsNotSupported(); |
1409
|
|
|
} |
1410
|
|
|
|
1411
|
18 |
|
$this->_conn->exec($this->platform->createSavePoint($savepoint)); |
1412
|
18 |
|
} |
1413
|
|
|
|
1414
|
|
|
/** |
1415
|
|
|
* Releases the given savepoint. |
1416
|
|
|
* |
1417
|
|
|
* @param string $savepoint The name of the savepoint to release. |
1418
|
|
|
* |
1419
|
|
|
* @return void |
1420
|
|
|
* |
1421
|
|
|
* @throws \Doctrine\DBAL\ConnectionException |
1422
|
|
|
*/ |
1423
|
18 |
|
public function releaseSavepoint($savepoint) |
1424
|
|
|
{ |
1425
|
18 |
|
if ( ! $this->getDatabasePlatform()->supportsSavepoints()) { |
1426
|
|
|
throw ConnectionException::savepointsNotSupported(); |
1427
|
|
|
} |
1428
|
|
|
|
1429
|
18 |
|
if ($this->platform->supportsReleaseSavepoints()) { |
1430
|
16 |
|
$this->_conn->exec($this->platform->releaseSavePoint($savepoint)); |
1431
|
|
|
} |
1432
|
18 |
|
} |
1433
|
|
|
|
1434
|
|
|
/** |
1435
|
|
|
* Rolls back to the given savepoint. |
1436
|
|
|
* |
1437
|
|
|
* @param string $savepoint The name of the savepoint to rollback to. |
1438
|
|
|
* |
1439
|
|
|
* @return void |
1440
|
|
|
* |
1441
|
|
|
* @throws \Doctrine\DBAL\ConnectionException |
1442
|
|
|
*/ |
1443
|
18 |
|
public function rollbackSavepoint($savepoint) |
1444
|
|
|
{ |
1445
|
18 |
|
if ( ! $this->getDatabasePlatform()->supportsSavepoints()) { |
1446
|
|
|
throw ConnectionException::savepointsNotSupported(); |
1447
|
|
|
} |
1448
|
|
|
|
1449
|
18 |
|
$this->_conn->exec($this->platform->rollbackSavePoint($savepoint)); |
1450
|
18 |
|
} |
1451
|
|
|
|
1452
|
|
|
/** |
1453
|
|
|
* Gets the wrapped driver connection. |
1454
|
|
|
* |
1455
|
|
|
* @return \Doctrine\DBAL\Driver\Connection |
1456
|
|
|
*/ |
1457
|
895 |
|
public function getWrappedConnection() |
1458
|
|
|
{ |
1459
|
895 |
|
$this->connect(); |
1460
|
|
|
|
1461
|
895 |
|
return $this->_conn; |
1462
|
|
|
} |
1463
|
|
|
|
1464
|
|
|
/** |
1465
|
|
|
* Gets the SchemaManager that can be used to inspect or change the |
1466
|
|
|
* database schema through the connection. |
1467
|
|
|
* |
1468
|
|
|
* @return \Doctrine\DBAL\Schema\AbstractSchemaManager |
1469
|
|
|
*/ |
1470
|
3034 |
|
public function getSchemaManager() |
1471
|
|
|
{ |
1472
|
3034 |
|
if ( ! $this->_schemaManager) { |
1473
|
197 |
|
$this->_schemaManager = $this->_driver->getSchemaManager($this); |
1474
|
|
|
} |
1475
|
|
|
|
1476
|
3034 |
|
return $this->_schemaManager; |
1477
|
|
|
} |
1478
|
|
|
|
1479
|
|
|
/** |
1480
|
|
|
* Marks the current transaction so that the only possible |
1481
|
|
|
* outcome for the transaction to be rolled back. |
1482
|
|
|
* |
1483
|
|
|
* @return void |
1484
|
|
|
* |
1485
|
|
|
* @throws \Doctrine\DBAL\ConnectionException If no transaction is active. |
1486
|
|
|
*/ |
1487
|
36 |
|
public function setRollbackOnly() |
1488
|
|
|
{ |
1489
|
36 |
|
if ($this->_transactionNestingLevel == 0) { |
1490
|
18 |
|
throw ConnectionException::noActiveTransaction(); |
1491
|
|
|
} |
1492
|
18 |
|
$this->_isRollbackOnly = true; |
1493
|
18 |
|
} |
1494
|
|
|
|
1495
|
|
|
/** |
1496
|
|
|
* Checks whether the current transaction is marked for rollback only. |
1497
|
|
|
* |
1498
|
|
|
* @return bool |
1499
|
|
|
* |
1500
|
|
|
* @throws \Doctrine\DBAL\ConnectionException If no transaction is active. |
1501
|
|
|
*/ |
1502
|
54 |
|
public function isRollbackOnly() |
1503
|
|
|
{ |
1504
|
54 |
|
if ($this->_transactionNestingLevel == 0) { |
1505
|
18 |
|
throw ConnectionException::noActiveTransaction(); |
1506
|
|
|
} |
1507
|
|
|
|
1508
|
36 |
|
return $this->_isRollbackOnly; |
1509
|
|
|
} |
1510
|
|
|
|
1511
|
|
|
/** |
1512
|
|
|
* Converts a given value to its database representation according to the conversion |
1513
|
|
|
* rules of a specific DBAL mapping type. |
1514
|
|
|
* |
1515
|
|
|
* @param mixed $value The value to convert. |
1516
|
|
|
* @param string $type The name of the DBAL mapping type. |
1517
|
|
|
* |
1518
|
|
|
* @return mixed The converted value. |
1519
|
|
|
*/ |
1520
|
|
|
public function convertToDatabaseValue($value, $type) |
1521
|
|
|
{ |
1522
|
|
|
return Type::getType($type)->convertToDatabaseValue($value, $this->getDatabasePlatform()); |
1523
|
|
|
} |
1524
|
|
|
|
1525
|
|
|
/** |
1526
|
|
|
* Converts a given value to its PHP representation according to the conversion |
1527
|
|
|
* rules of a specific DBAL mapping type. |
1528
|
|
|
* |
1529
|
|
|
* @param mixed $value The value to convert. |
1530
|
|
|
* @param string $type The name of the DBAL mapping type. |
1531
|
|
|
* |
1532
|
|
|
* @return mixed The converted type. |
1533
|
|
|
*/ |
1534
|
|
|
public function convertToPHPValue($value, $type) |
1535
|
|
|
{ |
1536
|
|
|
return Type::getType($type)->convertToPHPValue($value, $this->getDatabasePlatform()); |
1537
|
|
|
} |
1538
|
|
|
|
1539
|
|
|
/** |
1540
|
|
|
* Binds a set of parameters, some or all of which are typed with a PDO binding type |
1541
|
|
|
* or DBAL mapping type, to a given statement. |
1542
|
|
|
* |
1543
|
|
|
* @param \Doctrine\DBAL\Driver\Statement $stmt The statement to bind the values to. |
1544
|
|
|
* @param array $params The map/list of named/positional parameters. |
1545
|
|
|
* @param array $types The parameter types (PDO binding types or DBAL mapping types). |
1546
|
|
|
* |
1547
|
|
|
* @return void |
1548
|
|
|
* |
1549
|
|
|
* @internal Duck-typing used on the $stmt parameter to support driver statements as well as |
1550
|
|
|
* raw PDOStatement instances. |
1551
|
|
|
*/ |
1552
|
489 |
|
private function _bindTypedValues($stmt, array $params, array $types) |
1553
|
|
|
{ |
1554
|
|
|
// Check whether parameters are positional or named. Mixing is not allowed, just like in PDO. |
1555
|
489 |
|
if (is_int(key($params))) { |
1556
|
|
|
// Positional parameters |
1557
|
489 |
|
$typeOffset = array_key_exists(0, $types) ? -1 : 0; |
1558
|
489 |
|
$bindIndex = 1; |
1559
|
489 |
|
foreach ($params as $value) { |
1560
|
489 |
|
$typeIndex = $bindIndex + $typeOffset; |
1561
|
489 |
|
if (isset($types[$typeIndex])) { |
1562
|
489 |
|
$type = $types[$typeIndex]; |
1563
|
489 |
|
list($value, $bindingType) = $this->getBindingInfo($value, $type); |
1564
|
489 |
|
$stmt->bindValue($bindIndex, $value, $bindingType); |
1565
|
|
|
} else { |
1566
|
18 |
|
$stmt->bindValue($bindIndex, $value); |
1567
|
|
|
} |
1568
|
489 |
|
++$bindIndex; |
1569
|
|
|
} |
1570
|
|
|
} else { |
1571
|
|
|
// Named parameters |
1572
|
|
|
foreach ($params as $name => $value) { |
1573
|
|
|
if (isset($types[$name])) { |
1574
|
|
|
$type = $types[$name]; |
1575
|
|
|
list($value, $bindingType) = $this->getBindingInfo($value, $type); |
1576
|
|
|
$stmt->bindValue($name, $value, $bindingType); |
1577
|
|
|
} else { |
1578
|
|
|
$stmt->bindValue($name, $value); |
1579
|
|
|
} |
1580
|
|
|
} |
1581
|
|
|
} |
1582
|
489 |
|
} |
1583
|
|
|
|
1584
|
|
|
/** |
1585
|
|
|
* Gets the binding type of a given type. The given type can be a PDO or DBAL mapping type. |
1586
|
|
|
* |
1587
|
|
|
* @param mixed $value The value to bind. |
1588
|
|
|
* @param mixed $type The type to bind (PDO or DBAL). |
1589
|
|
|
* |
1590
|
|
|
* @return array [0] => the (escaped) value, [1] => the binding type. |
1591
|
|
|
*/ |
1592
|
569 |
|
private function getBindingInfo($value, $type) |
1593
|
|
|
{ |
1594
|
569 |
|
if (is_string($type)) { |
1595
|
216 |
|
$type = Type::getType($type); |
1596
|
|
|
} |
1597
|
569 |
|
if ($type instanceof Type) { |
1598
|
216 |
|
$value = $type->convertToDatabaseValue($value, $this->getDatabasePlatform()); |
1599
|
216 |
|
$bindingType = $type->getBindingType(); |
1600
|
|
|
} else { |
1601
|
461 |
|
$bindingType = $type; |
1602
|
|
|
} |
1603
|
|
|
|
1604
|
569 |
|
return [$value, $bindingType]; |
1605
|
|
|
} |
1606
|
|
|
|
1607
|
|
|
/** |
1608
|
|
|
* Resolves the parameters to a format which can be displayed. |
1609
|
|
|
* |
1610
|
|
|
* @internal This is a purely internal method. If you rely on this method, you are advised to |
1611
|
|
|
* copy/paste the code as this method may change, or be removed without prior notice. |
1612
|
|
|
* |
1613
|
|
|
* @param array $params |
1614
|
|
|
* @param array $types |
1615
|
|
|
* |
1616
|
|
|
* @return array |
1617
|
|
|
*/ |
1618
|
2249 |
|
public function resolveParams(array $params, array $types) |
1619
|
|
|
{ |
1620
|
2249 |
|
$resolvedParams = []; |
1621
|
|
|
|
1622
|
|
|
// Check whether parameters are positional or named. Mixing is not allowed, just like in PDO. |
1623
|
2249 |
|
if (is_int(key($params))) { |
1624
|
|
|
// Positional parameters |
1625
|
165 |
|
$typeOffset = array_key_exists(0, $types) ? -1 : 0; |
1626
|
165 |
|
$bindIndex = 1; |
1627
|
165 |
|
foreach ($params as $value) { |
1628
|
165 |
|
$typeIndex = $bindIndex + $typeOffset; |
1629
|
165 |
|
if (isset($types[$typeIndex])) { |
1630
|
|
|
$type = $types[$typeIndex]; |
1631
|
|
|
list($value,) = $this->getBindingInfo($value, $type); |
1632
|
|
|
$resolvedParams[$bindIndex] = $value; |
1633
|
|
|
} else { |
1634
|
165 |
|
$resolvedParams[$bindIndex] = $value; |
1635
|
|
|
} |
1636
|
165 |
|
++$bindIndex; |
1637
|
|
|
} |
1638
|
|
|
} else { |
1639
|
|
|
// Named parameters |
1640
|
2084 |
|
foreach ($params as $name => $value) { |
1641
|
|
|
if (isset($types[$name])) { |
1642
|
|
|
$type = $types[$name]; |
1643
|
|
|
list($value,) = $this->getBindingInfo($value, $type); |
1644
|
|
|
$resolvedParams[$name] = $value; |
1645
|
|
|
} else { |
1646
|
|
|
$resolvedParams[$name] = $value; |
1647
|
|
|
} |
1648
|
|
|
} |
1649
|
|
|
} |
1650
|
|
|
|
1651
|
2249 |
|
return $resolvedParams; |
1652
|
|
|
} |
1653
|
|
|
|
1654
|
|
|
/** |
1655
|
|
|
* Creates a new instance of a SQL query builder. |
1656
|
|
|
* |
1657
|
|
|
* @return \Doctrine\DBAL\Query\QueryBuilder |
1658
|
|
|
*/ |
1659
|
|
|
public function createQueryBuilder() |
1660
|
|
|
{ |
1661
|
|
|
return new Query\QueryBuilder($this); |
1662
|
|
|
} |
1663
|
|
|
|
1664
|
|
|
/** |
1665
|
|
|
* Ping the server |
1666
|
|
|
* |
1667
|
|
|
* When the server is not available the method returns FALSE. |
1668
|
|
|
* It is responsibility of the developer to handle this case |
1669
|
|
|
* and abort the request or reconnect manually: |
1670
|
|
|
* |
1671
|
|
|
* @example |
1672
|
|
|
* |
1673
|
|
|
* if ($conn->ping() === false) { |
1674
|
|
|
* $conn->close(); |
1675
|
|
|
* $conn->connect(); |
1676
|
|
|
* } |
1677
|
|
|
* |
1678
|
|
|
* It is undefined if the underlying driver attempts to reconnect |
1679
|
|
|
* or disconnect when the connection is not available anymore |
1680
|
|
|
* as long it returns TRUE when a reconnect succeeded and |
1681
|
|
|
* FALSE when the connection was dropped. |
1682
|
|
|
* |
1683
|
|
|
* @return bool |
1684
|
|
|
*/ |
1685
|
18 |
|
public function ping() |
1686
|
|
|
{ |
1687
|
18 |
|
$this->connect(); |
1688
|
|
|
|
1689
|
18 |
|
if ($this->_conn instanceof PingableConnection) { |
1690
|
3 |
|
return $this->_conn->ping(); |
1691
|
|
|
} |
1692
|
|
|
|
1693
|
|
|
try { |
1694
|
15 |
|
$this->query($this->getDatabasePlatform()->getDummySelectSQL()); |
1695
|
|
|
|
1696
|
15 |
|
return true; |
1697
|
|
|
} catch (DBALException $e) { |
1698
|
|
|
return false; |
1699
|
|
|
} |
1700
|
|
|
} |
1701
|
|
|
} |
1702
|
|
|
|