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
|
222 |
|
public function connect( |
24
|
|
|
array $params, |
25
|
|
|
string $username = '', |
26
|
|
|
string $password = '', |
27
|
|
|
array $driverOptions = [] |
28
|
|
|
) : Connection { |
29
|
222 |
|
if (! empty($params['persistent'])) { |
30
|
126 |
|
$driverOptions[PDO::ATTR_PERSISTENT] = true; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
try { |
34
|
222 |
|
$connection = new PDOConnection( |
35
|
222 |
|
$this->constructPdoDsn($params), |
36
|
|
|
$username, |
37
|
|
|
$password, |
38
|
|
|
$driverOptions |
39
|
|
|
); |
40
|
|
|
|
41
|
222 |
|
if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES') |
42
|
222 |
|
&& (! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES]) |
|
|
|
|
43
|
222 |
|
|| $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true |
44
|
|
|
) |
45
|
|
|
) { |
46
|
222 |
|
$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
|
222 |
|
if (isset($params['charset'])) { |
54
|
72 |
|
$connection->exec('SET NAMES \'' . $params['charset'] . '\''); |
55
|
|
|
} |
56
|
|
|
|
57
|
222 |
|
return $connection; |
58
|
132 |
|
} catch (PDOException $e) { |
59
|
132 |
|
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
|
222 |
|
private function constructPdoDsn(array $params) : string |
71
|
|
|
{ |
72
|
222 |
|
$dsn = 'pgsql:'; |
73
|
|
|
|
74
|
222 |
|
if (isset($params['host']) && $params['host'] !== '') { |
75
|
222 |
|
$dsn .= 'host=' . $params['host'] . ';'; |
76
|
|
|
} |
77
|
|
|
|
78
|
222 |
|
if (isset($params['port']) && $params['port'] !== '') { |
79
|
222 |
|
$dsn .= 'port=' . $params['port'] . ';'; |
80
|
|
|
} |
81
|
|
|
|
82
|
222 |
|
if (isset($params['dbname'])) { |
83
|
222 |
|
$dsn .= 'dbname=' . $params['dbname'] . ';'; |
84
|
222 |
|
} elseif (isset($params['default_dbname'])) { |
85
|
102 |
|
$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
|
222 |
|
$dsn .= 'dbname=postgres;'; |
91
|
|
|
} |
92
|
|
|
|
93
|
222 |
|
if (isset($params['sslmode'])) { |
94
|
|
|
$dsn .= 'sslmode=' . $params['sslmode'] . ';'; |
95
|
|
|
} |
96
|
|
|
|
97
|
222 |
|
if (isset($params['sslrootcert'])) { |
98
|
|
|
$dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';'; |
99
|
|
|
} |
100
|
|
|
|
101
|
222 |
|
if (isset($params['sslcert'])) { |
102
|
|
|
$dsn .= 'sslcert=' . $params['sslcert'] . ';'; |
103
|
|
|
} |
104
|
|
|
|
105
|
222 |
|
if (isset($params['sslkey'])) { |
106
|
|
|
$dsn .= 'sslkey=' . $params['sslkey'] . ';'; |
107
|
|
|
} |
108
|
|
|
|
109
|
222 |
|
if (isset($params['sslcrl'])) { |
110
|
|
|
$dsn .= 'sslcrl=' . $params['sslcrl'] . ';'; |
111
|
|
|
} |
112
|
|
|
|
113
|
222 |
|
if (isset($params['application_name'])) { |
114
|
90 |
|
$dsn .= 'application_name=' . $params['application_name'] . ';'; |
115
|
|
|
} |
116
|
|
|
|
117
|
222 |
|
return $dsn; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|