Completed
Push — develop ( a4b136...55bd22 )
by Sergei
21s queued 14s
created

Driver   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 80.48%

Importance

Changes 0
Metric Value
wmc 21
eloc 42
dl 0
loc 104
ccs 33
cts 41
cp 0.8048
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
F _constructPdoDsn() 0 48 13
B connect() 0 33 7
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 240
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
23
    {
24
        if (! empty($params['persistent'])) {
25 240
            $driverOptions[PDO::ATTR_PERSISTENT] = true;
26 240
        }
27
28
        try {
29
            $connection = new PDOConnection(
30
                $this->_constructPdoDsn($params),
31
                $username,
32 240
                $password,
33 240
                $driverOptions
34 240
            );
35
36
            if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES')
37 240
                && (! 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
                    || $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
39
                )
40
            ) {
41
                $connection->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
42
            }
43
44 240
            /* defining client_encoding via SET NAMES to avoid inconsistent DSN support
45 78
             * - the 'client_encoding' connection param only works with postgres >= 9.1
46
             * - passing client_encoding via the 'options' param breaks pgbouncer support
47
             */
48 240
            if (isset($params['charset'])) {
49 138
                $connection->exec('SET NAMES \'' . $params['charset'] . '\'');
50 138
            }
51
52
            return $connection;
53
        } catch (PDOException $e) {
54
            throw DBALException::driverException($this, $e);
55
        }
56
    }
57
58
    /**
59
     * Constructs the Postgres PDO DSN.
60
     *
61 240
     * @param mixed[] $params
62
     *
63 240
     * @return string The DSN.
64
     */
65 240
    private function _constructPdoDsn(array $params)
66 240
    {
67
        $dsn = 'pgsql:';
68
69 240
        if (isset($params['host']) && $params['host'] !== '') {
70 240
            $dsn .= 'host=' . $params['host'] . ';';
71
        }
72
73 240
        if (isset($params['port']) && $params['port'] !== '') {
74 240
            $dsn .= 'port=' . $params['port'] . ';';
75 240
        }
76 108
77
        if (isset($params['dbname'])) {
78
            $dsn .= 'dbname=' . $params['dbname'] . ';';
79
        } elseif (isset($params['default_dbname'])) {
80
            $dsn .= 'dbname=' . $params['default_dbname'] . ';';
81 240
        } 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 240
            // as it is mostly present in every server setup.
85
            $dsn .= 'dbname=postgres;';
86
        }
87
88 240
        if (isset($params['sslmode'])) {
89
            $dsn .= 'sslmode=' . $params['sslmode'] . ';';
90
        }
91
92 240
        if (isset($params['sslrootcert'])) {
93
            $dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';
94
        }
95
96 240
        if (isset($params['sslcert'])) {
97
            $dsn .= 'sslcert=' . $params['sslcert'] . ';';
98
        }
99
100 240
        if (isset($params['sslkey'])) {
101
            $dsn .= 'sslkey=' . $params['sslkey'] . ';';
102
        }
103
104 240
        if (isset($params['sslcrl'])) {
105 96
            $dsn .= 'sslcrl=' . $params['sslcrl'] . ';';
106
        }
107
108 240
        if (isset($params['application_name'])) {
109
            $dsn .= 'application_name=' . $params['application_name'] . ';';
110
        }
111
112
        return $dsn;
113
    }
114 261
115
    /**
116 261
     * {@inheritdoc}
117
     */
118
    public function getName()
119
    {
120
        return 'pdo_pgsql';
121
    }
122
}
123