Passed
Push — tests-better-coverage ( b00e66...9c1c22 )
by Michael
23:49
created

Driver::connect()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 15
cts 15
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 15
nc 11
nop 4
crap 6
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Driver\PDOPgSql;
21
22
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
23
use Doctrine\DBAL\Driver\PDOConnection;
24
use Doctrine\DBAL\DBALException;
25
use PDOException;
26
use PDO;
27
use function defined;
28
29
/**
30
 * Driver that connects through pdo_pgsql.
31
 *
32
 * @since 2.0
33
 */
34
class Driver extends AbstractPostgreSQLDriver
35
{
36
    /**
37
     * {@inheritdoc}
38
     */
39 234
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
40
    {
41
        try {
42 234
            $pdo = new PDOConnection(
43 234
                $this->_constructPdoDsn($params),
44 234
                $username,
45 234
                $password,
46 234
                $driverOptions
47
            );
48
49 222
            if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES')
50 222
                && (! 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...
51 222
                    || true === $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES]
52
                )
53
            ) {
54 216
                $pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
55
            }
56
57
            /* defining client_encoding via SET NAMES to avoid inconsistent DSN support
58
             * - the 'client_encoding' connection param only works with postgres >= 9.1
59
             * - passing client_encoding via the 'options' param breaks pgbouncer support
60
             */
61 222
            if (isset($params['charset'])) {
62 12
              $pdo->query('SET NAMES \''.$params['charset'].'\'');
63
            }
64
65 222
            return $pdo;
66 18
        } catch (PDOException $e) {
67 18
            throw DBALException::driverException($this, $e);
68
        }
69
    }
70
71
    /**
72
     * Constructs the Postgres PDO DSN.
73
     *
74
     * @param array $params
75
     *
76
     * @return string The DSN.
77
     */
78 234
    private function _constructPdoDsn(array $params)
79
    {
80 234
        $dsn = 'pgsql:';
81
82 234
        if (isset($params['host']) && $params['host'] != '') {
83 234
            $dsn .= 'host=' . $params['host'] . ';';
84
        }
85
86 234
        if (isset($params['port']) && $params['port'] != '') {
87 234
            $dsn .= 'port=' . $params['port'] . ';';
88
        }
89
90 234
        if (isset($params['dbname'])) {
91 192
            $dsn .= 'dbname=' . $params['dbname'] . ';';
92 168
        } elseif (isset($params['default_dbname'])) {
93 6
            $dsn .= 'dbname=' . $params['default_dbname'] . ';';
94
        } else {
95
            // Used for temporary connections to allow operations like dropping the database currently connected to.
96
            // Connecting without an explicit database does not work, therefore "postgres" database is used
97
            // as it is mostly present in every server setup.
98 162
            $dsn .= 'dbname=postgres' . ';';
99
        }
100
101 234
        if (isset($params['sslmode'])) {
102
            $dsn .= 'sslmode=' . $params['sslmode'] . ';';
103
        }
104
105 234
        if (isset($params['sslrootcert'])) {
106
            $dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';
107
        }
108
109 234
        if (isset($params['sslcert'])) {
110
            $dsn .= 'sslcert=' . $params['sslcert'] . ';';
111
        }
112
113 234
        if (isset($params['sslkey'])) {
114
            $dsn .= 'sslkey=' . $params['sslkey'] . ';';
115
        }
116
117 234
        if (isset($params['sslcrl'])) {
118
            $dsn .= 'sslcrl=' . $params['sslcrl'] . ';';
119
        }
120
121 234
        if (isset($params['application_name'])) {
122 6
            $dsn .= 'application_name=' . $params['application_name'] . ';';
123
        }
124
125 234
        return $dsn;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 41
    public function getName()
132
    {
133 41
        return 'pdo_pgsql';
134
    }
135
}
136