Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php (1 issue)

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
28
/**
29
 * Driver that connects through pdo_pgsql.
30
 *
31
 * @since 2.0
32
 */
33
class Driver extends AbstractPostgreSQLDriver
34
{
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
39
    {
40
        try {
41
            $pdo = new PDOConnection(
42
                $this->_constructPdoDsn($params),
43
                $username,
44
                $password,
45
                $driverOptions
46
            );
47
48
            if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES')
49
                && (! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES])
50
                    || true === $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES]
51
                )
52
            ) {
53
                $pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
54
            }
55
56
            /* defining client_encoding via SET NAMES to avoid inconsistent DSN support
57
             * - the 'client_encoding' connection param only works with postgres >= 9.1
58
             * - passing client_encoding via the 'options' param breaks pgbouncer support
59
             */
60
            if (isset($params['charset'])) {
61
              $pdo->query('SET NAMES \''.$params['charset'].'\'');
62
            }
63
64
            return $pdo;
65
        } catch (PDOException $e) {
66
            throw DBALException::driverException($this, $e);
67
        }
68
    }
69
70
    /**
71
     * Constructs the Postgres PDO DSN.
72
     *
73
     * @param array $params
74
     *
75
     * @return string The DSN.
76
     */
77
    private function _constructPdoDsn(array $params)
78
    {
79
        $dsn = 'pgsql:';
80
81 View Code Duplication
        if (isset($params['host']) && $params['host'] != '') {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
            $dsn .= 'host=' . $params['host'] . ';';
83
        }
84
85 View Code Duplication
        if (isset($params['port']) && $params['port'] != '') {
86
            $dsn .= 'port=' . $params['port'] . ';';
87
        }
88
89
        if (isset($params['dbname'])) {
90
            $dsn .= 'dbname=' . $params['dbname'] . ';';
91
        } elseif (isset($params['default_dbname'])) {
92
            $dsn .= 'dbname=' . $params['default_dbname'] . ';';
93
        } else {
94
            // Used for temporary connections to allow operations like dropping the database currently connected to.
95
            // Connecting without an explicit database does not work, therefore "postgres" database is used
96
            // as it is mostly present in every server setup.
97
            $dsn .= 'dbname=postgres' . ';';
98
        }
99
100
        if (isset($params['sslmode'])) {
101
            $dsn .= 'sslmode=' . $params['sslmode'] . ';';
102
        }
103
104
        if (isset($params['sslrootcert'])) {
105
            $dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';
106
        }
107
108
        if (isset($params['sslcert'])) {
109
            $dsn .= 'sslcert=' . $params['sslcert'] . ';';
110
        }
111
112
        if (isset($params['sslkey'])) {
113
            $dsn .= 'sslkey=' . $params['sslkey'] . ';';
114
        }
115
116
        if (isset($params['sslcrl'])) {
117
            $dsn .= 'sslcrl=' . $params['sslcrl'] . ';';
118
        }
119
120
        if (isset($params['application_name'])) {
121
            $dsn .= 'application_name=' . $params['application_name'] . ';';
122
        }
123
124
        return $dsn;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 1
    public function getName()
131
    {
132 1
        return 'pdo_pgsql';
133
    }
134
}
135