1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\DBAL\Driver\PDOPgSql; |
||
6 | |||
7 | use Doctrine\DBAL\DBALException; |
||
8 | use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver; |
||
9 | use Doctrine\DBAL\Driver\Connection; |
||
10 | use Doctrine\DBAL\Driver\PDOConnection; |
||
11 | use Doctrine\DBAL\Driver\PDOException; |
||
12 | use PDO; |
||
13 | use function defined; |
||
14 | |||
15 | /** |
||
16 | * Driver that connects through pdo_pgsql. |
||
17 | */ |
||
18 | final class Driver extends AbstractPostgreSQLDriver |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | 200 | public function connect( |
|
24 | array $params, |
||
25 | string $username = '', |
||
26 | string $password = '', |
||
27 | array $driverOptions = [] |
||
28 | ) : Connection { |
||
29 | 200 | if (! empty($params['persistent'])) { |
|
30 | 5 | $driverOptions[PDO::ATTR_PERSISTENT] = true; |
|
31 | } |
||
32 | |||
33 | try { |
||
34 | 200 | $connection = new PDOConnection( |
|
35 | 200 | $this->constructPdoDsn($params), |
|
36 | $username, |
||
37 | $password, |
||
38 | $driverOptions |
||
39 | ); |
||
40 | |||
41 | 190 | if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES') |
|
42 | 190 | && (! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES]) |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
43 | 190 | || $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true |
|
44 | ) |
||
45 | ) { |
||
46 | 185 | $connection->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true); |
|
47 | } |
||
48 | |||
49 | /* defining client_encoding via SET NAMES to avoid inconsistent DSN support |
||
50 | * - the 'client_encoding' connection param only works with postgres >= 9.1 |
||
51 | * - passing client_encoding via the 'options' param breaks pgbouncer support |
||
52 | */ |
||
53 | 190 | if (isset($params['charset'])) { |
|
54 | 10 | $connection->exec('SET NAMES \'' . $params['charset'] . '\''); |
|
55 | } |
||
56 | |||
57 | 190 | return $connection; |
|
58 | 10 | } catch (PDOException $e) { |
|
59 | 10 | throw DBALException::driverException($this, $e); |
|
60 | } |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Constructs the Postgres PDO DSN. |
||
65 | * |
||
66 | * @param mixed[] $params |
||
67 | * |
||
68 | * @return string The DSN. |
||
69 | */ |
||
70 | 200 | private function constructPdoDsn(array $params) : string |
|
71 | { |
||
72 | 200 | $dsn = 'pgsql:'; |
|
73 | |||
74 | 200 | if (isset($params['host']) && $params['host'] !== '') { |
|
75 | 200 | $dsn .= 'host=' . $params['host'] . ';'; |
|
76 | } |
||
77 | |||
78 | 200 | if (isset($params['port']) && $params['port'] !== '') { |
|
79 | 200 | $dsn .= 'port=' . $params['port'] . ';'; |
|
80 | } |
||
81 | |||
82 | 200 | if (isset($params['dbname'])) { |
|
83 | 170 | $dsn .= 'dbname=' . $params['dbname'] . ';'; |
|
84 | 45 | } elseif (isset($params['default_dbname'])) { |
|
85 | 5 | $dsn .= 'dbname=' . $params['default_dbname'] . ';'; |
|
86 | } else { |
||
87 | // Used for temporary connections to allow operations like dropping the database currently connected to. |
||
88 | // Connecting without an explicit database does not work, therefore "postgres" database is used |
||
89 | // as it is mostly present in every server setup. |
||
90 | 40 | $dsn .= 'dbname=postgres;'; |
|
91 | } |
||
92 | |||
93 | 200 | if (isset($params['sslmode'])) { |
|
94 | $dsn .= 'sslmode=' . $params['sslmode'] . ';'; |
||
95 | } |
||
96 | |||
97 | 200 | if (isset($params['sslrootcert'])) { |
|
98 | $dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';'; |
||
99 | } |
||
100 | |||
101 | 200 | if (isset($params['sslcert'])) { |
|
102 | $dsn .= 'sslcert=' . $params['sslcert'] . ';'; |
||
103 | } |
||
104 | |||
105 | 200 | if (isset($params['sslkey'])) { |
|
106 | $dsn .= 'sslkey=' . $params['sslkey'] . ';'; |
||
107 | } |
||
108 | |||
109 | 200 | if (isset($params['sslcrl'])) { |
|
110 | $dsn .= 'sslcrl=' . $params['sslcrl'] . ';'; |
||
111 | } |
||
112 | |||
113 | 200 | if (isset($params['application_name'])) { |
|
114 | 5 | $dsn .= 'application_name=' . $params['application_name'] . ';'; |
|
115 | } |
||
116 | |||
117 | 200 | return $dsn; |
|
118 | } |
||
119 | } |
||
120 |