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

3 4 5
namespace Doctrine\DBAL\Driver\PDOPgSql;

use Doctrine\DBAL\Platforms;
6 7 8 9 10 11

/**
 * Driver that connects through pdo_pgsql.
 *
 * @since 2.0
 */
12
class Driver implements \Doctrine\DBAL\Driver
13
{
romanb's avatar
romanb committed
14 15 16 17 18
    /**
     * Attempts to connect to the database and returns a driver connection on success.
     *
     * @return Doctrine\DBAL\Driver\Connection
     */
19 20
    public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
    {
21 22 23 24 25 26
        return new \Doctrine\DBAL\Driver\PDOConnection(
            $this->_constructPdoDsn($params),
            $username,
            $password,
            $driverOptions
        );
27
    }
28

29 30 31
    /**
     * Constructs the Postgres PDO DSN.
     *
romanb's avatar
romanb committed
32
     * @return string The DSN.
33 34 35
     */
    private function _constructPdoDsn(array $params)
    {
36
        $dsn = 'pgsql:';
37
        if (isset($params['host']) && $params['host'] != '') {
38 39
            $dsn .= 'host=' . $params['host'] . ' ';
        }
40
        if (isset($params['port']) && $params['port'] != '') {
41 42 43 44 45 46 47
            $dsn .= 'port=' . $params['port'] . ' ';
        }
        if (isset($params['dbname'])) {
            $dsn .= 'dbname=' . $params['dbname'] . ' ';
        }

        return $dsn;
48
    }
49

50 51
    public function getDatabasePlatform()
    {
52
        return new \Doctrine\DBAL\Platforms\PostgreSqlPlatform();
53
    }
54 55

    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
56
    {
57
        return new \Doctrine\DBAL\Schema\PostgreSqlSchemaManager($conn);
58
    }
59 60 61 62 63

    public function getName()
    {
        return 'pdo_pgsql';
    }
64 65 66 67 68 69

    public function getDatabase(\Doctrine\DBAL\Connection $conn)
    {
        $params = $conn->getParams();
        return $params['dbname'];
    }
gnoMii's avatar
gnoMii committed
70
}