|
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
|
273 |
|
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) |
|
21
|
|
|
{ |
|
22
|
|
|
try { |
|
23
|
273 |
|
$pdo = new PDOConnection( |
|
24
|
273 |
|
$this->_constructPdoDsn($params), |
|
25
|
273 |
|
$username, |
|
26
|
273 |
|
$password, |
|
27
|
273 |
|
$driverOptions |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
259 |
|
if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES') |
|
31
|
259 |
|
&& (! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES]) |
|
|
|
|
|
|
32
|
259 |
|
|| $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true |
|
33
|
|
|
) |
|
34
|
|
|
) { |
|
35
|
252 |
|
$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
|
259 |
|
if (isset($params['charset'])) { |
|
43
|
14 |
|
$pdo->exec('SET NAMES \'' . $params['charset'] . '\''); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
259 |
|
return $pdo; |
|
47
|
21 |
|
} catch (PDOException $e) { |
|
48
|
21 |
|
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
|
273 |
|
private function _constructPdoDsn(array $params) |
|
60
|
|
|
{ |
|
61
|
273 |
|
$dsn = 'pgsql:'; |
|
62
|
|
|
|
|
63
|
273 |
|
if (isset($params['host']) && $params['host'] !== '') { |
|
64
|
273 |
|
$dsn .= 'host=' . $params['host'] . ';'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
273 |
|
if (isset($params['port']) && $params['port'] !== '') { |
|
68
|
273 |
|
$dsn .= 'port=' . $params['port'] . ';'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
273 |
|
if (isset($params['dbname'])) { |
|
72
|
224 |
|
$dsn .= 'dbname=' . $params['dbname'] . ';'; |
|
73
|
196 |
|
} elseif (isset($params['default_dbname'])) { |
|
74
|
7 |
|
$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
|
189 |
|
$dsn .= 'dbname=postgres;'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
273 |
|
if (isset($params['sslmode'])) { |
|
83
|
|
|
$dsn .= 'sslmode=' . $params['sslmode'] . ';'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
273 |
|
if (isset($params['sslrootcert'])) { |
|
87
|
|
|
$dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';'; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
273 |
|
if (isset($params['sslcert'])) { |
|
91
|
|
|
$dsn .= 'sslcert=' . $params['sslcert'] . ';'; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
273 |
|
if (isset($params['sslkey'])) { |
|
95
|
|
|
$dsn .= 'sslkey=' . $params['sslkey'] . ';'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
273 |
|
if (isset($params['sslcrl'])) { |
|
99
|
|
|
$dsn .= 'sslcrl=' . $params['sslcrl'] . ';'; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
273 |
|
if (isset($params['application_name'])) { |
|
103
|
7 |
|
$dsn .= 'application_name=' . $params['application_name'] . ';'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
273 |
|
return $dsn; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
27 |
|
public function getName() |
|
113
|
|
|
{ |
|
114
|
27 |
|
return 'pdo_pgsql'; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|