Failed Conditions
Pull Request — develop (#3553)
by Sergei
100:55 queued 35:51
created

Driver::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
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 164
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
23
    {
24 164
        if (! empty($params['persistent'])) {
25 92
            $driverOptions[PDO::ATTR_PERSISTENT] = true;
26
        }
27
28
        try {
29 164
            $connection = new PDOConnection(
30 164
                $this->_constructPdoDsn($params),
31
                $username,
32
                $password,
33
                $driverOptions
34
            );
35
36 164
            if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES')
37 164
                && (! 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 164
                    || $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
39
                )
40
            ) {
41 164
                $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 164
            if (isset($params['charset'])) {
49 52
                $connection->exec('SET NAMES \'' . $params['charset'] . '\'');
50
            }
51
52 164
            return $connection;
53 96
        } catch (PDOException $e) {
54 96
            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 164
    private function _constructPdoDsn(array $params)
66
    {
67 164
        $dsn = 'pgsql:';
68
69 164
        if (isset($params['host']) && $params['host'] !== '') {
70 164
            $dsn .= 'host=' . $params['host'] . ';';
71
        }
72
73 164
        if (isset($params['port']) && $params['port'] !== '') {
74 164
            $dsn .= 'port=' . $params['port'] . ';';
75
        }
76
77 164
        if (isset($params['dbname'])) {
78 164
            $dsn .= 'dbname=' . $params['dbname'] . ';';
79 164
        } elseif (isset($params['default_dbname'])) {
80 72
            $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 164
            $dsn .= 'dbname=postgres;';
86
        }
87
88 164
        if (isset($params['sslmode'])) {
89
            $dsn .= 'sslmode=' . $params['sslmode'] . ';';
90
        }
91
92 164
        if (isset($params['sslrootcert'])) {
93
            $dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';
94
        }
95
96 164
        if (isset($params['sslcert'])) {
97
            $dsn .= 'sslcert=' . $params['sslcert'] . ';';
98
        }
99
100 164
        if (isset($params['sslkey'])) {
101
            $dsn .= 'sslkey=' . $params['sslkey'] . ';';
102
        }
103
104 164
        if (isset($params['sslcrl'])) {
105
            $dsn .= 'sslcrl=' . $params['sslcrl'] . ';';
106
        }
107
108 164
        if (isset($params['application_name'])) {
109 64
            $dsn .= 'application_name=' . $params['application_name'] . ';';
110
        }
111
112 164
        return $dsn;
113
    }
114
}
115