|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Cycle ORM package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Cycle\Database\Driver; |
|
13
|
|
|
|
|
14
|
|
|
use Cycle\Database\Config\DriverConfig; |
|
15
|
|
|
use Cycle\Database\Config\PDOConnectionConfig; |
|
16
|
|
|
use Cycle\Database\Config\ProvidesSourceString; |
|
17
|
|
|
use Cycle\Database\Exception\DriverException; |
|
18
|
|
|
use Cycle\Database\Exception\ReadonlyConnectionException; |
|
19
|
|
|
use Cycle\Database\Exception\StatementException; |
|
20
|
|
|
use Cycle\Database\Injection\ParameterInterface; |
|
21
|
|
|
use Cycle\Database\NamedInterface; |
|
22
|
|
|
use Cycle\Database\Query\BuilderInterface; |
|
23
|
|
|
use Cycle\Database\Query\Interpolator; |
|
24
|
|
|
use Cycle\Database\StatementInterface; |
|
25
|
|
|
use PDO; |
|
26
|
|
|
use PDOStatement; |
|
27
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
28
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Provides low level abstraction at top of |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract class Driver implements DriverInterface, NamedInterface, LoggerAwareInterface |
|
34
|
|
|
{ |
|
35
|
|
|
use LoggerAwareTrait; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* DateTime format to be used to perform automatic conversion of DateTime objects. |
|
39
|
|
|
* |
|
40
|
|
|
* @var non-empty-string (Typehint required for overriding behaviour) |
|
41
|
|
|
*/ |
|
42
|
|
|
protected const DATETIME = 'Y-m-d H:i:s'; |
|
43
|
|
|
|
|
44
|
|
|
protected const DATETIME_MICROSECONDS = 'Y-m-d H:i:s.u'; |
|
45
|
|
|
|
|
46
|
|
|
protected ?\PDO $pdo = null; |
|
47
|
|
|
protected int $transactionLevel = 0; |
|
48
|
|
|
protected HandlerInterface $schemaHandler; |
|
49
|
|
|
protected BuilderInterface $queryBuilder; |
|
50
|
|
|
|
|
51
|
|
|
/** @var \PDOStatement[]|PDOStatementInterface[] */ |
|
52
|
|
|
protected array $queryCache = []; |
|
53
|
|
|
|
|
54
|
|
|
private ?string $name = null; |
|
55
|
|
|
private bool $useCache = true; |
|
56
|
76 |
|
|
|
57
|
|
|
protected function __construct( |
|
58
|
|
|
protected DriverConfig $config, |
|
59
|
|
|
HandlerInterface $schemaHandler, |
|
60
|
|
|
protected CompilerInterface $queryCompiler, |
|
61
|
|
|
BuilderInterface $queryBuilder, |
|
62
|
76 |
|
) { |
|
63
|
76 |
|
$this->useCache = $this->config->queryCache; |
|
64
|
|
|
|
|
65
|
76 |
|
$this->schemaHandler = $schemaHandler->withDriver($this); |
|
66
|
72 |
|
$this->queryBuilder = $queryBuilder->withDriver($this); |
|
67
|
|
|
|
|
68
|
|
|
if ($this->useCache && $queryCompiler instanceof CachingCompilerInterface) { |
|
69
|
76 |
|
$this->queryCompiler = new CompilerCache($queryCompiler); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
76 |
|
if ($this->config->readonlySchema) { |
|
73
|
|
|
$this->schemaHandler = new ReadonlyHandler($this->schemaHandler); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param non-empty-string $name |
|
|
|
|
|
|
79
|
20 |
|
* |
|
80
|
|
|
* @internal |
|
81
|
20 |
|
*/ |
|
82
|
20 |
|
public function withName(string $name): static |
|
83
|
|
|
{ |
|
84
|
20 |
|
$driver = clone $this; |
|
85
|
|
|
$driver->name = $name; |
|
86
|
|
|
|
|
87
|
6 |
|
return $driver; |
|
88
|
|
|
} |
|
89
|
6 |
|
|
|
90
|
|
|
public function getName(): string |
|
91
|
|
|
{ |
|
92
|
1954 |
|
return $this->name ?? throw new \RuntimeException('Driver name is not defined.'); |
|
93
|
|
|
} |
|
94
|
1954 |
|
|
|
95
|
|
|
public function isReadonly(): bool |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->config->readonly; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function withoutCache(): static |
|
101
|
|
|
{ |
|
102
|
|
|
if ($this->useCache === false) { |
|
103
|
|
|
// Cache already disabled |
|
104
|
|
|
return $this; |
|
105
|
8 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
$driver = clone $this; |
|
108
|
8 |
|
$driver->useCache = false; |
|
109
|
8 |
|
|
|
110
|
8 |
|
return $driver; |
|
111
|
8 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function clearCache(): void |
|
114
|
|
|
{ |
|
115
|
|
|
$this->queryCache = []; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get driver source database or file name. |
|
120
|
|
|
* |
|
121
|
|
|
* @psalm-return non-empty-string |
|
122
|
12 |
|
* |
|
123
|
|
|
* @throws DriverException |
|
124
|
12 |
|
*/ |
|
125
|
|
|
public function getSource(): string |
|
126
|
|
|
{ |
|
127
|
6 |
|
$config = $this->config->connection; |
|
128
|
6 |
|
|
|
129
|
6 |
|
return $config instanceof ProvidesSourceString ? $config->getSourceString() : '*'; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function getTimezone(): \DateTimeZone |
|
133
|
|
|
{ |
|
134
|
|
|
return new \DateTimeZone($this->config->timezone); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function getSchemaHandler(): HandlerInterface |
|
138
|
|
|
{ |
|
139
|
|
|
// do not allow to carry prepared statements between schema changes |
|
140
|
4 |
|
$this->clearCache(); |
|
141
|
4 |
|
|
|
142
|
|
|
return $this->schemaHandler; |
|
143
|
|
|
} |
|
144
|
12 |
|
|
|
145
|
|
|
public function getQueryCompiler(): CompilerInterface |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->queryCompiler; |
|
148
|
22 |
|
} |
|
149
|
|
|
|
|
150
|
22 |
|
public function getQueryBuilder(): BuilderInterface |
|
151
|
22 |
|
{ |
|
152
|
22 |
|
return $this->queryBuilder; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Force driver connection. |
|
157
|
|
|
* |
|
158
|
|
|
* @throws DriverException |
|
159
|
|
|
*/ |
|
160
|
|
|
public function connect(): void |
|
161
|
490 |
|
{ |
|
162
|
|
|
$this->pdo ??= $this->createPDO(); |
|
163
|
490 |
|
} |
|
164
|
|
|
|
|
165
|
490 |
|
/** |
|
166
|
|
|
* Check if driver already connected. |
|
167
|
|
|
*/ |
|
168
|
1950 |
|
public function isConnected(): bool |
|
169
|
|
|
{ |
|
170
|
1950 |
|
return $this->pdo !== null; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
3748 |
|
/** |
|
174
|
|
|
* Disconnect driver. |
|
175
|
|
|
*/ |
|
176
|
3748 |
|
public function disconnect(): void |
|
177
|
|
|
{ |
|
178
|
3748 |
|
try { |
|
179
|
|
|
$this->clearCache(); |
|
180
|
|
|
$this->pdo = null; |
|
181
|
2600 |
|
} catch (\Throwable $e) { |
|
182
|
|
|
// disconnect error |
|
183
|
2600 |
|
$this->logger?->error($e->getMessage()); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
2174 |
|
$this->transactionLevel = 0; |
|
187
|
|
|
} |
|
188
|
2174 |
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @psalm-return non-empty-string |
|
191
|
|
|
*/ |
|
192
|
|
|
public function quote(mixed $value, int $type = \PDO::PARAM_STR): string |
|
193
|
|
|
{ |
|
194
|
|
|
/** @since PHP 8.1 */ |
|
195
|
|
|
if ($value instanceof \BackedEnum) { |
|
|
|
|
|
|
196
|
70 |
|
$value = (string) $value->value; |
|
197
|
|
|
} |
|
198
|
70 |
|
|
|
199
|
70 |
|
if ($value instanceof \DateTimeInterface) { |
|
200
|
|
|
$value = $this->formatDatetime($value); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $this->getPDO()->quote($value, $type); |
|
204
|
24 |
|
} |
|
205
|
|
|
|
|
206
|
24 |
|
/** |
|
207
|
|
|
* Execute query and return query statement. |
|
208
|
|
|
* |
|
209
|
|
|
* @psalm-param non-empty-string $statement |
|
210
|
|
|
* |
|
211
|
|
|
* @throws StatementException |
|
212
|
10 |
|
*/ |
|
213
|
|
|
public function query(string $statement, array $parameters = []): StatementInterface |
|
214
|
|
|
{ |
|
215
|
10 |
|
return $this->statement($statement, $parameters); |
|
216
|
10 |
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Execute query and return number of affected rows. |
|
220
|
|
|
* |
|
221
|
|
|
* @psalm-param non-empty-string $query |
|
222
|
10 |
|
* |
|
223
|
10 |
|
* @throws StatementException |
|
224
|
|
|
* @throws ReadonlyConnectionException |
|
225
|
|
|
*/ |
|
226
|
|
|
public function execute(string $query, array $parameters = []): int |
|
227
|
|
|
{ |
|
228
|
1030 |
|
if ($this->isReadonly()) { |
|
229
|
|
|
throw ReadonlyConnectionException::onWriteStatementExecution(); |
|
230
|
1030 |
|
} |
|
231
|
32 |
|
|
|
232
|
|
|
return $this->statement($query, $parameters)->rowCount(); |
|
233
|
|
|
} |
|
234
|
1030 |
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Get id of last inserted row, this method must be called after insert query. Attention, |
|
237
|
|
|
* such functionality may not work in some DBMS property (Postgres). |
|
238
|
|
|
* |
|
239
|
|
|
* @param string|null $sequence Name of the sequence object from which the ID should be returned. |
|
240
|
|
|
* |
|
241
|
|
|
* @return mixed |
|
242
|
|
|
*/ |
|
243
|
|
|
public function lastInsertID(?string $sequence = null) |
|
244
|
3758 |
|
{ |
|
245
|
|
|
$result = $this->getPDO()->lastInsertId(); |
|
246
|
3758 |
|
$this->logger?->debug("Insert ID: {$result}"); |
|
247
|
|
|
|
|
248
|
|
|
return $result; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
public function getTransactionLevel(): int |
|
252
|
|
|
{ |
|
253
|
|
|
return $this->transactionLevel; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
1954 |
|
* Start SQL transaction with specified isolation level (not all DBMS support it). Nested |
|
258
|
|
|
* transactions are processed using savepoints. |
|
259
|
1954 |
|
* |
|
260
|
80 |
|
* @link http://en.wikipedia.org/wiki/Database_transaction |
|
261
|
|
|
* @link http://en.wikipedia.org/wiki/Isolation_(database_systems) |
|
262
|
|
|
* |
|
263
|
1954 |
|
*/ |
|
264
|
|
|
public function beginTransaction(?string $isolationLevel = null): bool |
|
265
|
|
|
{ |
|
266
|
|
|
++$this->transactionLevel; |
|
267
|
|
|
|
|
268
|
|
|
if ($this->transactionLevel === 1) { |
|
269
|
|
|
if ($isolationLevel !== null) { |
|
270
|
|
|
$this->setIsolationLevel($isolationLevel); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
$this->logger?->info('Begin transaction'); |
|
274
|
274 |
|
|
|
275
|
|
|
try { |
|
276
|
274 |
|
return $this->getPDO()->beginTransaction(); |
|
277
|
274 |
|
} catch (\Throwable $e) { |
|
278
|
|
|
$e = $this->mapException($e, 'BEGIN TRANSACTION'); |
|
279
|
274 |
|
|
|
280
|
|
|
if ( |
|
281
|
|
|
$e instanceof StatementException\ConnectionException |
|
282
|
6 |
|
&& $this->config->reconnect |
|
283
|
|
|
) { |
|
284
|
6 |
|
$this->disconnect(); |
|
285
|
|
|
|
|
286
|
|
|
try { |
|
287
|
|
|
$this->transactionLevel = 1; |
|
288
|
|
|
return $this->getPDO()->beginTransaction(); |
|
289
|
|
|
} catch (\Throwable $e) { |
|
290
|
|
|
$this->transactionLevel = 0; |
|
291
|
|
|
throw $this->mapException($e, 'BEGIN TRANSACTION'); |
|
292
|
|
|
} |
|
293
|
|
|
} else { |
|
294
|
|
|
$this->transactionLevel = 0; |
|
295
|
|
|
throw $e; |
|
296
|
164 |
|
} |
|
297
|
|
|
} |
|
298
|
164 |
|
} |
|
299
|
|
|
|
|
300
|
164 |
|
$this->createSavepoint($this->transactionLevel); |
|
301
|
164 |
|
|
|
302
|
24 |
|
return true; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
164 |
|
/** |
|
306
|
|
|
* Commit the active database transaction. |
|
307
|
|
|
* |
|
308
|
164 |
|
* @throws StatementException |
|
309
|
|
|
*/ |
|
310
|
|
|
public function commitTransaction(): bool |
|
311
|
|
|
{ |
|
312
|
|
|
// Check active transaction |
|
313
|
|
|
if (!$this->getPDO()->inTransaction()) { |
|
314
|
|
|
$this->logger?->warning( |
|
315
|
|
|
\sprintf( |
|
316
|
|
|
'Attempt to commit a transaction that has not yet begun. Transaction level: %d', |
|
317
|
|
|
$this->transactionLevel, |
|
318
|
|
|
), |
|
319
|
|
|
); |
|
320
|
|
|
|
|
321
|
|
|
if ($this->transactionLevel === 0) { |
|
322
|
|
|
return false; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
$this->transactionLevel = 0; |
|
326
|
|
|
return true; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
--$this->transactionLevel; |
|
330
|
|
|
|
|
331
|
24 |
|
if ($this->transactionLevel === 0) { |
|
332
|
|
|
$this->logger?->info('Commit transaction'); |
|
333
|
24 |
|
|
|
334
|
|
|
try { |
|
335
|
|
|
return $this->getPDO()->commit(); |
|
336
|
|
|
} catch (\Throwable $e) { |
|
337
|
|
|
throw $this->mapException($e, 'COMMIT TRANSACTION'); |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
192 |
|
$this->releaseSavepoint($this->transactionLevel + 1); |
|
342
|
|
|
|
|
343
|
|
|
return true; |
|
344
|
192 |
|
} |
|
345
|
30 |
|
|
|
346
|
30 |
|
/** |
|
347
|
30 |
|
* Rollback the active database transaction. |
|
348
|
30 |
|
* |
|
349
|
|
|
* @throws StatementException |
|
350
|
|
|
*/ |
|
351
|
|
|
public function rollbackTransaction(): bool |
|
352
|
30 |
|
{ |
|
353
|
2 |
|
// Check active transaction |
|
354
|
|
|
if (!$this->getPDO()->inTransaction()) { |
|
355
|
|
|
$this->logger?->warning( |
|
356
|
28 |
|
\sprintf( |
|
357
|
28 |
|
'Attempt to rollback a transaction that has not yet begun. Transaction level: %d', |
|
358
|
|
|
$this->transactionLevel, |
|
359
|
|
|
), |
|
360
|
162 |
|
); |
|
361
|
|
|
|
|
362
|
162 |
|
$this->transactionLevel = 0; |
|
363
|
162 |
|
return false; |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
162 |
|
--$this->transactionLevel; |
|
367
|
|
|
|
|
368
|
|
|
if ($this->transactionLevel === 0) { |
|
369
|
|
|
$this->logger?->info('Rollback transaction'); |
|
370
|
|
|
|
|
371
|
|
|
try { |
|
372
|
8 |
|
return $this->getPDO()->rollBack(); |
|
373
|
|
|
} catch (\Throwable $e) { |
|
374
|
8 |
|
throw $this->mapException($e, 'ROLLBACK TRANSACTION'); |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
$this->rollbackSavepoint($this->transactionLevel + 1); |
|
379
|
|
|
|
|
380
|
|
|
return true; |
|
381
|
|
|
} |
|
382
|
40 |
|
|
|
383
|
|
|
/** |
|
384
|
|
|
* @psalm-param non-empty-string $identifier |
|
385
|
40 |
|
*/ |
|
386
|
2 |
|
public function identifier(string $identifier): string |
|
387
|
2 |
|
{ |
|
388
|
2 |
|
return $this->queryCompiler->quoteIdentifier($identifier); |
|
389
|
2 |
|
} |
|
390
|
|
|
|
|
391
|
|
|
public function __debugInfo(): array |
|
392
|
|
|
{ |
|
393
|
2 |
|
return [ |
|
394
|
2 |
|
'connection' => $this->config->connection, |
|
395
|
|
|
'source' => $this->getSource(), |
|
396
|
|
|
'connected' => $this->isConnected(), |
|
397
|
38 |
|
'options' => $this->config, |
|
398
|
|
|
]; |
|
399
|
38 |
|
} |
|
400
|
24 |
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* Compatibility with deprecated methods. |
|
403
|
24 |
|
* |
|
404
|
|
|
* @psalm-param non-empty-string $name |
|
405
|
|
|
* |
|
406
|
|
|
* @deprecated this method will be removed in a future releases. |
|
407
|
|
|
*/ |
|
408
|
|
|
public function __call(string $name, array $arguments): mixed |
|
409
|
22 |
|
{ |
|
410
|
|
|
return match ($name) { |
|
411
|
22 |
|
'isProfiling' => true, |
|
412
|
|
|
'setProfiling' => null, |
|
413
|
|
|
'getSchema' => $this->getSchemaHandler()->getSchema( |
|
414
|
|
|
$arguments[0], |
|
415
|
|
|
$arguments[1] ?? null, |
|
416
|
|
|
), |
|
417
|
3460 |
|
'tableNames' => $this->getSchemaHandler()->getTableNames(), |
|
418
|
|
|
'hasTable' => $this->getSchemaHandler()->hasTable($arguments[0]), |
|
419
|
3460 |
|
'identifier' => $this->getQueryCompiler()->quoteIdentifier($arguments[0]), |
|
420
|
|
|
'eraseData' => $this->getSchemaHandler()->eraseTable( |
|
|
|
|
|
|
421
|
|
|
$this->getSchemaHandler()->getSchema($arguments[0]), |
|
422
|
|
|
), |
|
423
|
|
|
'insertQuery', |
|
424
|
|
|
'selectQuery', |
|
425
|
|
|
'updateQuery', |
|
426
|
|
|
'deleteQuery' => \call_user_func_array( |
|
427
|
|
|
[$this->queryBuilder, $name], |
|
428
|
|
|
$arguments, |
|
429
|
|
|
), |
|
430
|
3758 |
|
default => throw new DriverException("Undefined driver method `{$name}`"), |
|
431
|
|
|
}; |
|
432
|
3758 |
|
} |
|
433
|
|
|
|
|
434
|
|
|
public function __clone() |
|
435
|
3758 |
|
{ |
|
436
|
3758 |
|
$this->schemaHandler = $this->schemaHandler->withDriver($this); |
|
437
|
|
|
$this->queryBuilder = $this->queryBuilder->withDriver($this); |
|
438
|
3758 |
|
} |
|
439
|
36 |
|
|
|
440
|
36 |
|
/** |
|
441
|
|
|
* Disconnect and destruct. |
|
442
|
|
|
*/ |
|
443
|
36 |
|
public function __destruct() |
|
444
|
36 |
|
{ |
|
445
|
36 |
|
$this->disconnect(); |
|
446
|
|
|
} |
|
447
|
2 |
|
|
|
448
|
|
|
/** |
|
449
|
2 |
|
* Create instance of PDOStatement using provided SQL query and set of parameters and execute |
|
450
|
|
|
* it. Will attempt singular reconnect. |
|
451
|
|
|
* |
|
452
|
34 |
|
* @psalm-param non-empty-string $query |
|
453
|
|
|
* |
|
454
|
3758 |
|
* @throws StatementException |
|
455
|
3724 |
|
*/ |
|
456
|
3724 |
|
protected function statement(string $query, iterable $parameters = [], bool $retry = true): StatementInterface |
|
457
|
|
|
{ |
|
458
|
3724 |
|
$queryStart = \microtime(true); |
|
459
|
36 |
|
|
|
460
|
36 |
|
try { |
|
461
|
|
|
$statement = $this->bindParameters($this->prepare($query), $parameters); |
|
462
|
3724 |
|
$statement->execute(); |
|
463
|
|
|
|
|
464
|
|
|
return new Statement($statement); |
|
465
|
|
|
} catch (\Throwable $e) { |
|
466
|
|
|
$e = $this->mapException($e, Interpolator::interpolate($query, $parameters)); |
|
467
|
|
|
|
|
468
|
|
|
if ( |
|
469
|
|
|
$retry |
|
470
|
|
|
&& $this->transactionLevel === 0 |
|
471
|
3758 |
|
&& $e instanceof StatementException\ConnectionException |
|
472
|
|
|
) { |
|
473
|
3758 |
|
$this->disconnect(); |
|
474
|
1360 |
|
|
|
475
|
|
|
return $this->statement($query, $parameters, false); |
|
476
|
|
|
} |
|
477
|
3758 |
|
|
|
478
|
3758 |
|
throw $e; |
|
479
|
3758 |
|
} finally { |
|
480
|
|
|
if ($this->logger !== null) { |
|
481
|
|
|
$queryString = $this->config->options['logInterpolatedQueries'] |
|
482
|
3758 |
|
? Interpolator::interpolate($query, $parameters, $this->config->options) |
|
483
|
|
|
: $query; |
|
484
|
|
|
|
|
485
|
|
|
$contextParameters = $this->config->options['logQueryParameters'] |
|
486
|
|
|
? $parameters |
|
487
|
|
|
: []; |
|
488
|
2830 |
|
|
|
489
|
|
|
$context = $this->defineLoggerContext( |
|
490
|
2830 |
|
$queryStart, |
|
|
|
|
|
|
491
|
2830 |
|
$statement ?? null, |
|
492
|
1500 |
|
$contextParameters, |
|
493
|
12 |
|
); |
|
494
|
|
|
|
|
495
|
1500 |
|
if (isset($e)) { |
|
496
|
|
|
$this->logger->error($queryString, $context); |
|
497
|
|
|
$this->logger->alert($e->getMessage()); |
|
498
|
1500 |
|
} else { |
|
499
|
|
|
$this->logger->info($queryString, $context); |
|
500
|
1500 |
|
} |
|
501
|
282 |
|
} |
|
502
|
282 |
|
} |
|
503
|
|
|
} |
|
504
|
|
|
|
|
505
|
1500 |
|
/** |
|
506
|
6 |
|
* @psalm-param non-empty-string $query |
|
507
|
|
|
*/ |
|
508
|
|
|
protected function prepare(string $query): \PDOStatement|PDOStatementInterface |
|
509
|
|
|
{ |
|
510
|
1500 |
|
if ($this->useCache && isset($this->queryCache[$query])) { |
|
511
|
|
|
return $this->queryCache[$query]; |
|
512
|
|
|
} |
|
513
|
2830 |
|
|
|
514
|
|
|
$statement = $this->getPDO()->prepare($query); |
|
515
|
|
|
if ($this->useCache) { |
|
516
|
|
|
$this->queryCache[$query] = $statement; |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
return $statement; |
|
520
|
|
|
} |
|
521
|
|
|
|
|
522
|
40 |
|
/** |
|
523
|
|
|
* Bind parameters into statement. |
|
524
|
|
|
*/ |
|
525
|
40 |
|
protected function bindParameters( |
|
526
|
|
|
\PDOStatement|PDOStatementInterface $statement, |
|
527
|
|
|
iterable $parameters, |
|
528
|
|
|
): \PDOStatement|PDOStatementInterface { |
|
529
|
|
|
$index = 0; |
|
530
|
40 |
|
foreach ($parameters as $name => $parameter) { |
|
531
|
|
|
if (\is_string($name)) { |
|
532
|
|
|
$index = $name; |
|
533
|
|
|
} else { |
|
534
|
|
|
$index++; |
|
535
|
|
|
} |
|
536
|
|
|
|
|
537
|
|
|
$type = \PDO::PARAM_STR; |
|
538
|
|
|
|
|
539
|
|
|
if ($parameter instanceof ParameterInterface) { |
|
540
|
|
|
$type = $parameter->getType(); |
|
541
|
|
|
$parameter = $parameter->getValue(); |
|
542
|
|
|
} |
|
543
|
|
|
|
|
544
|
|
|
/** @since PHP 8.1 */ |
|
545
|
|
|
if ($parameter instanceof \BackedEnum) { |
|
546
|
|
|
$type = \PDO::PARAM_STR; |
|
547
|
|
|
$parameter = $parameter->value; |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
24 |
|
if ($parameter instanceof \DateTimeInterface) { |
|
551
|
|
|
$parameter = $this->formatDatetime($parameter); |
|
552
|
24 |
|
} |
|
553
|
24 |
|
|
|
554
|
24 |
|
// numeric, @see http://php.net/manual/en/pdostatement.bindparam.php |
|
555
|
|
|
$statement->bindValue($index, $parameter, $type); |
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
return $statement; |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
|
|
/** |
|
562
|
|
|
* Convert DateTime object into local database representation. Driver will automatically force |
|
563
|
24 |
|
* needed timezone. |
|
564
|
|
|
* |
|
565
|
24 |
|
* @throws DriverException |
|
566
|
|
|
*/ |
|
567
|
24 |
|
protected function formatDatetime(\DateTimeInterface $value): string |
|
568
|
24 |
|
{ |
|
569
|
|
|
try { |
|
570
|
|
|
$datetime = match (true) { |
|
571
|
|
|
$value instanceof \DateTimeImmutable => $value->setTimezone($this->getTimezone()), |
|
572
|
|
|
$value instanceof \DateTime => \DateTimeImmutable::createFromMutable($value) |
|
573
|
|
|
->setTimezone($this->getTimezone()), |
|
574
|
|
|
default => (new \DateTimeImmutable('now', $this->getTimezone()))->setTimestamp($value->getTimestamp()), |
|
575
|
|
|
}; |
|
576
|
|
|
} catch (\Throwable $e) { |
|
577
|
6 |
|
throw new DriverException($e->getMessage(), (int) $e->getCode(), $e); |
|
578
|
|
|
} |
|
579
|
6 |
|
|
|
580
|
|
|
return $datetime->format( |
|
581
|
6 |
|
$this->config->options['withDatetimeMicroseconds'] ? self::DATETIME_MICROSECONDS : self::DATETIME, |
|
582
|
6 |
|
); |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
/** |
|
586
|
|
|
* Convert PDO exception into query or integrity exception. |
|
587
|
|
|
* |
|
588
|
|
|
* @psalm-param non-empty-string $query |
|
589
|
|
|
*/ |
|
590
|
|
|
abstract protected function mapException( |
|
591
|
16 |
|
\Throwable $exception, |
|
592
|
|
|
string $query, |
|
593
|
16 |
|
): StatementException; |
|
594
|
|
|
|
|
595
|
16 |
|
/** |
|
596
|
16 |
|
* Set transaction isolation level, this feature may not be supported by specific database |
|
597
|
|
|
* driver. |
|
598
|
|
|
* |
|
599
|
|
|
* @psalm-param non-empty-string $level |
|
600
|
|
|
*/ |
|
601
|
62 |
|
protected function setIsolationLevel(string $level): void |
|
602
|
|
|
{ |
|
603
|
62 |
|
$this->logger?->info("Transaction isolation level '{$level}'"); |
|
604
|
|
|
$this->execute("SET TRANSACTION ISOLATION LEVEL {$level}"); |
|
605
|
62 |
|
} |
|
606
|
|
|
|
|
607
|
|
|
/** |
|
608
|
|
|
* Create nested transaction save point. |
|
609
|
|
|
* |
|
610
|
|
|
* @link http://en.wikipedia.org/wiki/Savepoint |
|
611
|
62 |
|
* |
|
612
|
62 |
|
* @param int $level Savepoint name/id, must not contain spaces and be valid database identifier. |
|
613
|
62 |
|
*/ |
|
614
|
62 |
|
protected function createSavepoint(int $level): void |
|
615
|
62 |
|
{ |
|
616
|
|
|
$this->logger?->info("Transaction: new savepoint 'SVP{$level}'"); |
|
617
|
|
|
|
|
618
|
|
|
$this->execute('SAVEPOINT ' . $this->identifier("SVP{$level}")); |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
/** |
|
622
|
|
|
* Commit/release savepoint. |
|
623
|
|
|
* |
|
624
|
3760 |
|
* @link http://en.wikipedia.org/wiki/Savepoint |
|
625
|
|
|
* |
|
626
|
3760 |
|
* @param int $level Savepoint name/id, must not contain spaces and be valid database identifier. |
|
627
|
28 |
|
*/ |
|
628
|
|
|
protected function releaseSavepoint(int $level): void |
|
629
|
|
|
{ |
|
630
|
3760 |
|
$this->logger?->info("Transaction: release savepoint 'SVP{$level}'"); |
|
631
|
|
|
|
|
632
|
|
|
$this->execute('RELEASE SAVEPOINT ' . $this->identifier("SVP{$level}")); |
|
633
|
|
|
} |
|
634
|
|
|
|
|
635
|
|
|
/** |
|
636
|
|
|
* Rollback savepoint. |
|
637
|
|
|
* |
|
638
|
|
|
* @link http://en.wikipedia.org/wiki/Savepoint |
|
639
|
3724 |
|
* |
|
640
|
|
|
* @param int $level Savepoint name/id, must not contain spaces and be valid database identifier. |
|
641
|
3724 |
|
*/ |
|
642
|
3724 |
|
protected function rollbackSavepoint(int $level): void |
|
643
|
|
|
{ |
|
644
|
3724 |
|
$this->logger?->info("Transaction: rollback savepoint 'SVP{$level}'"); |
|
645
|
3724 |
|
|
|
646
|
|
|
$this->execute('ROLLBACK TO SAVEPOINT ' . $this->identifier("SVP{$level}")); |
|
647
|
|
|
} |
|
648
|
3724 |
|
|
|
649
|
|
|
/** |
|
650
|
|
|
* Create instance of configured PDO class. |
|
651
|
|
|
*/ |
|
652
|
|
|
protected function createPDO(): \PDO|PDOInterface |
|
653
|
|
|
{ |
|
654
|
|
|
$connection = $this->config->connection; |
|
655
|
|
|
|
|
656
|
|
|
if (!$connection instanceof PDOConnectionConfig) { |
|
657
|
|
|
throw new \InvalidArgumentException( |
|
658
|
|
|
'Could not establish PDO connection using non-PDO configuration', |
|
659
|
|
|
); |
|
660
|
|
|
} |
|
661
|
|
|
|
|
662
|
|
|
return new \PDO( |
|
663
|
|
|
dsn: $connection->getDsn(), |
|
664
|
|
|
username: $connection->getUsername(), |
|
665
|
|
|
password: $connection->getPassword(), |
|
666
|
|
|
options: $connection->getOptions(), |
|
667
|
|
|
); |
|
668
|
|
|
} |
|
669
|
|
|
|
|
670
|
|
|
/** |
|
671
|
|
|
* Get associated PDO connection. Must automatically connect if such connection does not exists. |
|
672
|
|
|
* |
|
673
|
|
|
* @throws DriverException |
|
674
|
|
|
*/ |
|
675
|
|
|
protected function getPDO(): \PDO|PDOInterface |
|
676
|
|
|
{ |
|
677
|
|
|
if ($this->pdo === null) { |
|
678
|
|
|
$this->connect(); |
|
679
|
|
|
} |
|
680
|
|
|
|
|
681
|
|
|
return $this->pdo; |
|
|
|
|
|
|
682
|
|
|
} |
|
683
|
|
|
|
|
684
|
|
|
/** |
|
685
|
|
|
* Creating a context for logging |
|
686
|
|
|
* |
|
687
|
|
|
* @param float $queryStart Query start time |
|
688
|
|
|
* @param \PDOStatement|PDOStatementInterface|null $statement Statement object |
|
689
|
|
|
* @param iterable $parameters Query parameters |
|
690
|
|
|
* |
|
691
|
|
|
*/ |
|
692
|
|
|
protected function defineLoggerContext(float $queryStart, \PDOStatement|PDOStatementInterface|null $statement, iterable $parameters = []): array |
|
693
|
|
|
{ |
|
694
|
|
|
$context = [ |
|
695
|
|
|
'driver' => $this->getType(), |
|
696
|
|
|
'elapsed' => \microtime(true) - $queryStart, |
|
697
|
|
|
]; |
|
698
|
|
|
if ($statement !== null) { |
|
699
|
|
|
$context['rowCount'] = $statement->rowCount(); |
|
700
|
|
|
} |
|
701
|
|
|
|
|
702
|
|
|
foreach ($parameters as $parameter) { |
|
703
|
|
|
$context['parameters'][] = Interpolator::resolveValue($parameter, $this->config->options); |
|
704
|
|
|
} |
|
705
|
|
|
|
|
706
|
|
|
return $context; |
|
707
|
|
|
} |
|
708
|
|
|
} |
|
709
|
|
|
|