Driver.php 3.37 KB
Newer Older
1 2
<?php

3 4
namespace Doctrine\DBAL\Driver\PDOPgSql;

5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
Steve Müller's avatar
Steve Müller committed
7
use Doctrine\DBAL\Driver\PDOConnection;
8
use PDO;
9
use PDOException;
10
use function defined;
11 12 13 14

/**
 * Driver that connects through pdo_pgsql.
 */
15
class Driver extends AbstractPostgreSQLDriver
16
{
romanb's avatar
romanb committed
17
    /**
Benjamin Morel's avatar
Benjamin Morel committed
18
     * {@inheritdoc}
romanb's avatar
romanb committed
19
     */
20
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
21
    {
22
        try {
23
            $pdo = new PDOConnection(
24 25 26 27 28
                $this->_constructPdoDsn($params),
                $username,
                $password,
                $driverOptions
            );
29

30
            if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES')
31
                && (! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES])
32
                    || $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
33 34 35 36 37
                )
            ) {
                $pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
            }

38 39
            /* 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
40
             * - passing client_encoding via the 'options' param breaks pgbouncer support
41
             */
42
            if (isset($params['charset'])) {
43
                $pdo->exec('SET NAMES \'' . $params['charset'] . '\'');
44 45
            }

46
            return $pdo;
47
        } catch (PDOException $e) {
48 49
            throw DBALException::driverException($this, $e);
        }
50
    }
51

52 53 54
    /**
     * Constructs the Postgres PDO DSN.
     *
55
     * @param mixed[] $params
Benjamin Morel's avatar
Benjamin Morel committed
56
     *
romanb's avatar
romanb committed
57
     * @return string The DSN.
58 59 60
     */
    private function _constructPdoDsn(array $params)
    {
61
        $dsn = 'pgsql:';
62

63
        if (isset($params['host']) && $params['host'] !== '') {
64
            $dsn .= 'host=' . $params['host'] . ';';
65
        }
66

67
        if (isset($params['port']) && $params['port'] !== '') {
68
            $dsn .= 'port=' . $params['port'] . ';';
69
        }
70

71
        if (isset($params['dbname'])) {
72
            $dsn .= 'dbname=' . $params['dbname'] . ';';
73
        } elseif (isset($params['default_dbname'])) {
74
            $dsn .= 'dbname=' . $params['default_dbname'] . ';';
Steve Müller's avatar
Steve Müller committed
75
        } else {
76
            // Used for temporary connections to allow operations like dropping the database currently connected to.
77
            // Connecting without an explicit database does not work, therefore "postgres" database is used
78
            // as it is mostly present in every server setup.
79
            $dsn .= 'dbname=postgres;';
80
        }
81

82
        if (isset($params['sslmode'])) {
83
            $dsn .= 'sslmode=' . $params['sslmode'] . ';';
84 85
        }

86
        if (isset($params['sslrootcert'])) {
87
            $dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';
88 89
        }

90 91 92 93 94 95 96 97 98 99 100 101
        if (isset($params['sslcert'])) {
            $dsn .= 'sslcert=' . $params['sslcert'] . ';';
        }

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

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

102
        if (isset($params['application_name'])) {
103
            $dsn .= 'application_name=' . $params['application_name'] . ';';
104 105
        }

106
        return $dsn;
107
    }
108

Benjamin Morel's avatar
Benjamin Morel committed
109 110 111
    /**
     * {@inheritdoc}
     */
112 113 114 115
    public function getName()
    {
        return 'pdo_pgsql';
    }
gnoMii's avatar
gnoMii committed
116
}