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