| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Doctrine\DBAL\Driver\PDO\PgSQL;
|
|
|
4 |
|
|
|
5 |
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
|
|
|
6 |
use Doctrine\DBAL\Driver\PDO\Connection;
|
|
|
7 |
use Doctrine\DBAL\Driver\PDO\Exception;
|
|
|
8 |
use Doctrine\Deprecations\Deprecation;
|
|
|
9 |
use PDO;
|
|
|
10 |
use PDOException;
|
|
|
11 |
|
|
|
12 |
final class Driver extends AbstractPostgreSQLDriver
|
|
|
13 |
{
|
|
|
14 |
/**
|
|
|
15 |
* {@inheritdoc}
|
|
|
16 |
*
|
|
|
17 |
* @return Connection
|
|
|
18 |
*/
|
|
|
19 |
public function connect(array $params)
|
|
|
20 |
{
|
|
|
21 |
$driverOptions = $params['driverOptions'] ?? [];
|
|
|
22 |
|
|
|
23 |
if (! empty($params['persistent'])) {
|
|
|
24 |
$driverOptions[PDO::ATTR_PERSISTENT] = true;
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
try {
|
|
|
28 |
$pdo = new PDO(
|
|
|
29 |
$this->constructPdoDsn($params),
|
|
|
30 |
$params['user'] ?? '',
|
|
|
31 |
$params['password'] ?? '',
|
|
|
32 |
$driverOptions,
|
|
|
33 |
);
|
|
|
34 |
} catch (PDOException $exception) {
|
|
|
35 |
throw Exception::new($exception);
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
if (
|
|
|
39 |
! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES])
|
|
|
40 |
|| $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
|
|
|
41 |
) {
|
|
|
42 |
$pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
$connection = new Connection($pdo);
|
|
|
46 |
|
|
|
47 |
/* defining client_encoding via SET NAMES to avoid inconsistent DSN support
|
|
|
48 |
* - passing client_encoding via the 'options' param breaks pgbouncer support
|
|
|
49 |
*/
|
|
|
50 |
if (isset($params['charset'])) {
|
|
|
51 |
$connection->exec('SET NAMES \'' . $params['charset'] . '\'');
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
return $connection;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Constructs the Postgres PDO DSN.
|
|
|
59 |
*
|
|
|
60 |
* @param mixed[] $params
|
|
|
61 |
*/
|
|
|
62 |
private function constructPdoDsn(array $params): string
|
|
|
63 |
{
|
|
|
64 |
$dsn = 'pgsql:';
|
|
|
65 |
|
|
|
66 |
if (isset($params['host']) && $params['host'] !== '') {
|
|
|
67 |
$dsn .= 'host=' . $params['host'] . ';';
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
if (isset($params['port']) && $params['port'] !== '') {
|
|
|
71 |
$dsn .= 'port=' . $params['port'] . ';';
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
if (isset($params['dbname'])) {
|
|
|
75 |
$dsn .= 'dbname=' . $params['dbname'] . ';';
|
|
|
76 |
} elseif (isset($params['default_dbname'])) {
|
|
|
77 |
Deprecation::trigger(
|
|
|
78 |
'doctrine/dbal',
|
|
|
79 |
'https://github.com/doctrine/dbal/pull/5705',
|
|
|
80 |
'The "default_dbname" connection parameter is deprecated. Use "dbname" instead.',
|
|
|
81 |
);
|
|
|
82 |
|
|
|
83 |
$dsn .= 'dbname=' . $params['default_dbname'] . ';';
|
|
|
84 |
} else {
|
|
|
85 |
if (isset($params['user']) && $params['user'] !== 'postgres') {
|
|
|
86 |
Deprecation::trigger(
|
|
|
87 |
'doctrine/dbal',
|
|
|
88 |
'https://github.com/doctrine/dbal/pull/5705',
|
|
|
89 |
'Relying on the DBAL connecting to the "postgres" database by default is deprecated.'
|
|
|
90 |
. ' Unless you want to have the server determine the default database for the connection,'
|
|
|
91 |
. ' specify the database name explicitly.',
|
|
|
92 |
);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
// Used for temporary connections to allow operations like dropping the database currently connected to.
|
|
|
96 |
$dsn .= 'dbname=postgres;';
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
if (isset($params['sslmode'])) {
|
|
|
100 |
$dsn .= 'sslmode=' . $params['sslmode'] . ';';
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
if (isset($params['sslrootcert'])) {
|
|
|
104 |
$dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
if (isset($params['sslcert'])) {
|
|
|
108 |
$dsn .= 'sslcert=' . $params['sslcert'] . ';';
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
if (isset($params['sslkey'])) {
|
|
|
112 |
$dsn .= 'sslkey=' . $params['sslkey'] . ';';
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
if (isset($params['sslcrl'])) {
|
|
|
116 |
$dsn .= 'sslcrl=' . $params['sslcrl'] . ';';
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
if (isset($params['application_name'])) {
|
|
|
120 |
$dsn .= 'application_name=' . $params['application_name'] . ';';
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
return $dsn;
|
|
|
124 |
}
|
|
|
125 |
}
|