/
PgsqlDriver.php
431 lines (331 loc) · 11.2 KB
/
PgsqlDriver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php declare(strict_types = 1);
namespace Nextras\Dbal\Drivers\Pgsql;
use DateInterval;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use Exception;
use Nextras\Dbal\Connection;
use Nextras\Dbal\Drivers\Exception\ConnectionException;
use Nextras\Dbal\Drivers\Exception\DriverException;
use Nextras\Dbal\Drivers\Exception\ForeignKeyConstraintViolationException;
use Nextras\Dbal\Drivers\Exception\NotNullConstraintViolationException;
use Nextras\Dbal\Drivers\Exception\QueryException;
use Nextras\Dbal\Drivers\Exception\UniqueConstraintViolationException;
use Nextras\Dbal\Drivers\IDriver;
use Nextras\Dbal\Exception\InvalidArgumentException;
use Nextras\Dbal\Exception\NotSupportedException;
use Nextras\Dbal\ILogger;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\Platforms\PostgreSqlPlatform;
use Nextras\Dbal\Result\Result;
use Nextras\Dbal\Utils\LoggerHelper;
use Nextras\Dbal\Utils\StrictObjectTrait;
use function is_string;
class PgsqlDriver implements IDriver
{
use StrictObjectTrait;
/** @var resource|null */
private $connection;
/** @var DateTimeZone Timezone for database connection. */
private $connectionTz;
/** @var ILogger */
private $logger;
/** @var int */
private $affectedRows = 0;
/** @var float */
private $timeTaken = 0.0;
public function __destruct()
{
$this->disconnect();
}
public function connect(array $params, ILogger $logger): void
{
static $knownKeys = [
'host',
'hostaddr',
'port',
'dbname',
'user',
'password',
'connect_timeout',
'options',
'sslmode',
'service',
];
$this->logger = $logger;
$params = $this->processConfig($params);
$connectionString = '';
foreach ($knownKeys as $key) {
if (isset($params[$key])) {
$connectionString .= $key . '=' . $params[$key] . ' ';
}
}
set_error_handler(function (int $code, string $message): bool {
restore_error_handler();
throw $this->createException($message, $code, null);
}, E_ALL);
$connection = pg_connect($connectionString, PGSQL_CONNECT_FORCE_NEW);
assert($connection !== false); // connection error is handled in error_handler
$this->connection = $connection;
restore_error_handler();
$this->connectionTz = new DateTimeZone($params['connectionTz']);
if (strpos($this->connectionTz->getName(), ':') !== false) {
$this->loggedQuery('SET TIME ZONE INTERVAL ' . pg_escape_literal($this->connectionTz->getName()) . ' HOUR TO MINUTE');
} else {
$this->loggedQuery('SET TIME ZONE ' . pg_escape_literal($this->connectionTz->getName()));
}
if (isset($params['searchPath'])) {
$schemas = array_map([$this, 'convertIdentifierToSql',], (array) $params['searchPath']);
$this->loggedQuery('SET search_path TO ' . implode(', ', $schemas));
}
}
public function disconnect(): void
{
if ($this->connection !== null) {
pg_close($this->connection);
$this->connection = null;
}
}
public function isConnected(): bool
{
return $this->connection !== null;
}
public function getResourceHandle()
{
return $this->connection;
}
public function query(string $query): Result
{
assert($this->connection !== null);
if (!pg_send_query($this->connection, $query)) {
throw $this->createException(pg_last_error($this->connection), 0, null);
}
$time = microtime(true);
$resource = pg_get_result($this->connection);
$this->timeTaken = microtime(true) - $time;
if ($resource === false) {
throw $this->createException(pg_last_error($this->connection), 0, null);
}
$state = pg_result_error_field($resource, PGSQL_DIAG_SQLSTATE);
if (is_string($state)) {
$error = pg_result_error($resource);
throw $this->createException($error !== false ? $error : 'Unknown error', 0, $state, $query);
}
$this->affectedRows = pg_affected_rows($resource);
return new Result(new PgsqlResultAdapter($resource), $this);
}
public function getLastInsertedId(?string $sequenceName = null)
{
if ($sequenceName === null) {
throw new InvalidArgumentException('PgsqlDriver requires to pass sequence name for getLastInsertedId() method.');
}
assert($this->connection !== null);
$sql = 'SELECT CURRVAL(' . pg_escape_literal($this->connection, $sequenceName) . ')';
return $this->loggedQuery($sql)->fetchField();
}
public function getAffectedRows(): int
{
return $this->affectedRows;
}
public function getQueryElapsedTime(): float
{
return $this->timeTaken;
}
public function createPlatform(Connection $connection): IPlatform
{
return new PostgreSqlPlatform($connection);
}
public function getServerVersion(): string
{
assert($this->connection !== null);
return pg_version($this->connection)['server'];
}
public function ping(): bool
{
assert($this->connection !== null);
return pg_ping($this->connection);
}
public function setTransactionIsolationLevel(int $level): void
{
static $levels = [
Connection::TRANSACTION_READ_UNCOMMITTED => 'READ UNCOMMITTED',
Connection::TRANSACTION_READ_COMMITTED => 'READ COMMITTED',
Connection::TRANSACTION_REPEATABLE_READ => 'REPEATABLE READ',
Connection::TRANSACTION_SERIALIZABLE => 'SERIALIZABLE',
];
if (!isset($levels[$level])) {
throw new NotSupportedException("Unsupported transaction level $level");
}
$this->loggedQuery("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL {$levels[$level]}");
}
public function beginTransaction(): void
{
$this->loggedQuery('START TRANSACTION');
}
public function commitTransaction(): void
{
$this->loggedQuery('COMMIT');
}
public function rollbackTransaction(): void
{
$this->loggedQuery('ROLLBACK');
}
public function createSavepoint(string $name): void
{
$this->loggedQuery('SAVEPOINT ' . $this->convertIdentifierToSql($name));
}
public function releaseSavepoint(string $name): void
{
$this->loggedQuery('RELEASE SAVEPOINT ' . $this->convertIdentifierToSql($name));
}
public function rollbackSavepoint(string $name): void
{
$this->loggedQuery('ROLLBACK TO SAVEPOINT ' . $this->convertIdentifierToSql($name));
}
public function convertToPhp(string $value, $nativeType)
{
static $trues = ['true', 't', 'yes', 'y', 'on', '1'];
if ($nativeType === 'bool') {
return in_array(strtolower($value), $trues, true);
} elseif ($nativeType === 'int8') {
// called only on 32bit
// hack for phpstan
/** @var int|float $numeric */
$numeric = $value;
return is_float($tmp = $numeric * 1) ? $numeric : $tmp;
} elseif ($nativeType === 'interval') {
return DateInterval::createFromDateString($value);
} elseif ($nativeType === 'bit' || $nativeType === 'varbit') {
return bindec($value);
} elseif ($nativeType === 'bytea') {
return pg_unescape_bytea($value);
} else {
throw new NotSupportedException("PgsqlDriver does not support '{$nativeType}' type conversion.");
}
}
public function convertStringToSql(string $value): string
{
assert($this->connection !== null);
return pg_escape_literal($this->connection, $value);
}
public function convertJsonToSql($value): string
{
$encoded = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new InvalidArgumentException('JSON Encode Error: ' . json_last_error_msg());
}
assert(is_string($encoded));
return $this->convertStringToSql($encoded);
}
public function convertLikeToSql(string $value, int $mode)
{
$value = strtr($value, [
"'" => "''",
'\\' => '\\\\',
'%' => '\\%',
'_' => '\\_',
]);
return ($mode <= 0 ? "'%" : "'") . $value . ($mode >= 0 ? "%'" : "'");
}
public function convertBoolToSql(bool $value): string
{
return $value ? 'TRUE' : 'FALSE';
}
public function convertIdentifierToSql(string $value): string
{
assert($this->connection !== null);
$parts = explode('.', $value);
foreach ($parts as &$part) {
if ($part !== '*') {
$part = pg_escape_identifier($this->connection, $part);
}
}
return implode('.', $parts);
}
public function convertDateTimeToSql(DateTimeInterface $value): string
{
assert($value instanceof DateTime || $value instanceof DateTimeImmutable);
if ($value->getTimezone()->getName() !== $this->connectionTz->getName()) {
if ($value instanceof DateTimeImmutable) {
$value = $value->setTimezone($this->connectionTz);
} else {
$value = clone $value;
$value->setTimezone($this->connectionTz);
}
}
return "'" . $value->format('Y-m-d H:i:s.u') . "'::timestamptz";
}
public function convertDateTimeSimpleToSql(DateTimeInterface $value): string
{
return "'" . $value->format('Y-m-d H:i:s.u') . "'::timestamp";
}
public function convertDateIntervalToSql(DateInterval $value): string
{
return $value->format('P%yY%mM%dDT%hH%iM%sS');
}
public function convertBlobToSql(string $value): string
{
assert($this->connection !== null);
return "'" . pg_escape_bytea($this->connection, $value) . "'";
}
public function modifyLimitQuery(string $query, ?int $limit, ?int $offset): string
{
if ($limit !== null) {
$query .= ' LIMIT ' . $limit;
}
if ($offset !== null) {
$query .= ' OFFSET ' . $offset;
}
return $query;
}
/**
* This method is based on Doctrine\DBAL project.
* @link www.doctrine-project.org
*/
protected function createException(string $error, int $errorNo, ?string $sqlState, ?string $query = null): Exception
{
// see codes at http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
if ($sqlState === '0A000' && strpos($error, 'truncate') !== false) {
// Foreign key constraint violations during a TRUNCATE operation
// are considered "feature not supported" in PostgreSQL.
return new ForeignKeyConstraintViolationException($error, $errorNo, $sqlState, null, $query);
} elseif ($sqlState === '23502') {
return new NotNullConstraintViolationException($error, $errorNo, $sqlState, null, $query);
} elseif ($sqlState === '23503') {
return new ForeignKeyConstraintViolationException($error, $errorNo, $sqlState, null, $query);
} elseif ($sqlState === '23505') {
return new UniqueConstraintViolationException($error, $errorNo, $sqlState, null, $query);
} elseif ($sqlState === null && stripos($error, 'pg_connect()') !== false) {
return new ConnectionException($error, $errorNo, $sqlState);
} elseif ($query !== null) {
return new QueryException($error, $errorNo, (string) $sqlState, null, $query);
} else {
return new DriverException($error, $errorNo, $sqlState);
}
}
protected function loggedQuery(string $sql): Result
{
return LoggerHelper::loggedQuery($this, $this->logger, $sql);
}
/**
* @phpstan-param array<string, mixed> $params
* @phpstan-return array<string, mixed>
*/
private function processConfig(array $params): array
{
if (!isset($params['database']) && isset($params['dbname'])) {
throw new InvalidArgumentException("You have passed 'dbname' key, did you mean 'database' key?");
}
$params['dbname'] = $params['database'] ?? null;
$params['user'] = $params['username'] ?? null;
unset($params['database'], $params['username']);
if (!isset($params['connectionTz']) || $params['connectionTz'] === IDriver::TIMEZONE_AUTO_PHP_NAME) {
$params['connectionTz'] = date_default_timezone_get();
} elseif ($params['connectionTz'] === IDriver::TIMEZONE_AUTO_PHP_OFFSET) {
$params['connectionTz'] = date('P');
}
return $params;
}
}