diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/Driver/PgSQL/Driver.php')
-rw-r--r-- | vendor/doctrine/dbal/src/Driver/PgSQL/Driver.php | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Driver/PgSQL/Driver.php b/vendor/doctrine/dbal/src/Driver/PgSQL/Driver.php new file mode 100644 index 0000000..0d5d605 --- /dev/null +++ b/vendor/doctrine/dbal/src/Driver/PgSQL/Driver.php | |||
@@ -0,0 +1,88 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL\Driver\PgSQL; | ||
6 | |||
7 | use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver; | ||
8 | use ErrorException; | ||
9 | use SensitiveParameter; | ||
10 | |||
11 | use function addslashes; | ||
12 | use function array_filter; | ||
13 | use function array_keys; | ||
14 | use function array_map; | ||
15 | use function array_slice; | ||
16 | use function array_values; | ||
17 | use function func_get_args; | ||
18 | use function implode; | ||
19 | use function pg_connect; | ||
20 | use function restore_error_handler; | ||
21 | use function set_error_handler; | ||
22 | use function sprintf; | ||
23 | |||
24 | use const PGSQL_CONNECT_FORCE_NEW; | ||
25 | |||
26 | final class Driver extends AbstractPostgreSQLDriver | ||
27 | { | ||
28 | /** {@inheritDoc} */ | ||
29 | public function connect( | ||
30 | #[SensitiveParameter] | ||
31 | array $params, | ||
32 | ): Connection { | ||
33 | set_error_handler( | ||
34 | static function (int $severity, string $message): never { | ||
35 | throw new ErrorException($message, 0, $severity, ...array_slice(func_get_args(), 2, 2)); | ||
36 | }, | ||
37 | ); | ||
38 | |||
39 | try { | ||
40 | $connection = pg_connect($this->constructConnectionString($params), PGSQL_CONNECT_FORCE_NEW); | ||
41 | } catch (ErrorException $e) { | ||
42 | throw new Exception($e->getMessage(), '08006', 0, $e); | ||
43 | } finally { | ||
44 | restore_error_handler(); | ||
45 | } | ||
46 | |||
47 | if ($connection === false) { | ||
48 | throw new Exception('Unable to connect to Postgres server.'); | ||
49 | } | ||
50 | |||
51 | $driverConnection = new Connection($connection); | ||
52 | |||
53 | if (isset($params['application_name'])) { | ||
54 | $driverConnection->exec('SET application_name = ' . $driverConnection->quote($params['application_name'])); | ||
55 | } | ||
56 | |||
57 | return $driverConnection; | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * Constructs the Postgres connection string | ||
62 | * | ||
63 | * @param array<string, mixed> $params | ||
64 | */ | ||
65 | private function constructConnectionString( | ||
66 | #[SensitiveParameter] | ||
67 | array $params, | ||
68 | ): string { | ||
69 | $components = array_filter( | ||
70 | [ | ||
71 | 'host' => $params['host'] ?? null, | ||
72 | 'port' => $params['port'] ?? null, | ||
73 | 'dbname' => $params['dbname'] ?? 'postgres', | ||
74 | 'user' => $params['user'] ?? null, | ||
75 | 'password' => $params['password'] ?? null, | ||
76 | 'sslmode' => $params['sslmode'] ?? null, | ||
77 | 'gssencmode' => $params['gssencmode'] ?? null, | ||
78 | ], | ||
79 | static fn (int|string|null $value) => $value !== '' && $value !== null, | ||
80 | ); | ||
81 | |||
82 | return implode(' ', array_map( | ||
83 | static fn (int|string $value, string $key) => sprintf("%s='%s'", $key, addslashes((string) $value)), | ||
84 | array_values($components), | ||
85 | array_keys($components), | ||
86 | )); | ||
87 | } | ||
88 | } | ||