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