Driver.php 4.32 KB
Newer Older
1
<?php
Benjamin Morel's avatar
Benjamin Morel committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the MIT license. For more information, see
 * <http://www.doctrine-project.org>.
 */
19

20 21
namespace Doctrine\DBAL\Driver\PDOPgSql;

22
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
Steve Müller's avatar
Steve Müller committed
23
use Doctrine\DBAL\Driver\PDOConnection;
24 25
use Doctrine\DBAL\DBALException;
use PDOException;
26
use PDO;
27 28 29 30 31 32

/**
 * Driver that connects through pdo_pgsql.
 *
 * @since 2.0
 */
33
class Driver extends AbstractPostgreSQLDriver
34
{
romanb's avatar
romanb committed
35
    /**
Benjamin Morel's avatar
Benjamin Morel committed
36
     * {@inheritdoc}
romanb's avatar
romanb committed
37
     */
38
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
39
    {
40
        try {
41
            $pdo = new PDOConnection(
42 43 44 45 46
                $this->_constructPdoDsn($params),
                $username,
                $password,
                $driverOptions
            );
47

48
            if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES')
49 50 51 52 53 54 55
                && (! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES])
                    || true === $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES]
                )
            ) {
                $pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
            }

56 57
            /* defining client_encoding via SET NAMES to avoid inconsistent DSN support
             * - the 'client_encoding' connection param only works with postgres >= 9.1
rocksfrow's avatar
rocksfrow committed
58
             * - passing client_encoding via the 'options' param breaks pgbouncer support
59
             */
60 61 62 63
            if (isset($params['charset'])) {
              $pdo->query('SET NAMES \''.$params['charset'].'\'');
            }

64
            return $pdo;
65
        } catch (PDOException $e) {
66 67
            throw DBALException::driverException($this, $e);
        }
68
    }
69

70 71 72
    /**
     * Constructs the Postgres PDO DSN.
     *
Benjamin Morel's avatar
Benjamin Morel committed
73 74
     * @param array $params
     *
romanb's avatar
romanb committed
75
     * @return string The DSN.
76 77 78
     */
    private function _constructPdoDsn(array $params)
    {
79
        $dsn = 'pgsql:';
80

81
        if (isset($params['host']) && $params['host'] != '') {
82
            $dsn .= 'host=' . $params['host'] . ';';
83
        }
84

85
        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'] . ';';
Steve Müller's avatar
Steve Müller committed
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 109 110 111 112 113 114 115 116 117 118 119
        if (isset($params['sslcert'])) {
            $dsn .= 'sslcert=' . $params['sslcert'] . ';';
        }

        if (isset($params['sslkey'])) {
            $dsn .= 'sslkey=' . $params['sslkey'] . ';';
        }

        if (isset($params['sslcrl'])) {
            $dsn .= 'sslcrl=' . $params['sslcrl'] . ';';
        }

120
        if (isset($params['application_name'])) {
121
            $dsn .= 'application_name=' . $params['application_name'] . ';';
122 123
        }

124
        return $dsn;
125
    }
126

Benjamin Morel's avatar
Benjamin Morel committed
127 128 129
    /**
     * {@inheritdoc}
     */
130 131 132 133
    public function getName()
    {
        return 'pdo_pgsql';
    }
gnoMii's avatar
gnoMii committed
134
}