Failed Conditions
Pull Request — 2.10 (#3803)
by Sergei
62:38
created

Driver::connect()   B

Complexity

Conditions 7
Paths 22

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 33
ccs 12
cts 12
cp 1
rs 8.8333
c 0
b 0
f 0
cc 7
nc 22
nop 4
crap 7
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 287
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
21
    {
22
        if (! empty($params['persistent'])) {
23 287
            $driverOptions[PDO::ATTR_PERSISTENT] = true;
24 287
        }
25
26
        try {
27
            $connection = new PDOConnection(
28
                $this->_constructPdoDsn($params),
29
                $username,
30 287
                $password,
31 287
                $driverOptions
32 287
            );
33
34
            if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES')
35 287
                && (! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES])
0 ignored issues
show
Bug introduced by
The constant PDO::PGSQL_ATTR_DISABLE_PREPARES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36
                    || $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
37
                )
38
            ) {
39
                $connection->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
40
            }
41
42 287
            /* defining client_encoding via SET NAMES to avoid inconsistent DSN support
43 91
             * - the 'client_encoding' connection param only works with postgres >= 9.1
44
             * - passing client_encoding via the 'options' param breaks pgbouncer support
45
             */
46 287
            if (isset($params['charset'])) {
47 161
                $connection->exec('SET NAMES \'' . $params['charset'] . '\'');
48 161
            }
49
50
            return $connection;
51
        } catch (PDOException $e) {
52
            throw DBALException::driverException($this, $e);
53
        }
54
    }
55
56
    /**
57
     * Constructs the Postgres PDO DSN.
58
     *
59 287
     * @param mixed[] $params
60
     *
61 287
     * @return string The DSN.
62
     */
63 287
    private function _constructPdoDsn(array $params)
64 287
    {
65
        $dsn = 'pgsql:';
66
67 287
        if (isset($params['host']) && $params['host'] !== '') {
68 287
            $dsn .= 'host=' . $params['host'] . ';';
69
        }
70
71 287
        if (isset($params['port']) && $params['port'] !== '') {
72 287
            $dsn .= 'port=' . $params['port'] . ';';
73 287
        }
74 126
75
        if (isset($params['dbname'])) {
76
            $dsn .= 'dbname=' . $params['dbname'] . ';';
77
        } elseif (isset($params['default_dbname'])) {
78
            $dsn .= 'dbname=' . $params['default_dbname'] . ';';
79 287
        } else {
80
            // Used for temporary connections to allow operations like dropping the database currently connected to.
81
            // Connecting without an explicit database does not work, therefore "postgres" database is used
82 287
            // as it is mostly present in every server setup.
83
            $dsn .= 'dbname=postgres;';
84
        }
85
86 287
        if (isset($params['sslmode'])) {
87
            $dsn .= 'sslmode=' . $params['sslmode'] . ';';
88
        }
89
90 287
        if (isset($params['sslrootcert'])) {
91
            $dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';
92
        }
93
94 287
        if (isset($params['sslcert'])) {
95
            $dsn .= 'sslcert=' . $params['sslcert'] . ';';
96
        }
97
98 287
        if (isset($params['sslkey'])) {
99
            $dsn .= 'sslkey=' . $params['sslkey'] . ';';
100
        }
101
102 287
        if (isset($params['sslcrl'])) {
103 112
            $dsn .= 'sslcrl=' . $params['sslcrl'] . ';';
104
        }
105
106 287
        if (isset($params['application_name'])) {
107
            $dsn .= 'application_name=' . $params['application_name'] . ';';
108
        }
109
110
        return $dsn;
111
    }
112
113
    /**
114 307
     * {@inheritdoc}
115
     *
116 307
     * @deprecated
117
     */
118
    public function getName()
119
    {
120
        return 'pdo_pgsql';
121
    }
122
}
123