Completed
Pull Request — develop (#3533)
by
unknown
16:31 queued 01:26
created

Driver::connect()   B

Complexity

Conditions 7
Paths 22

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.2694

Importance

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