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