Skip to content

Commit

Permalink
Merge pull request #2284 from kimhemsoe/pg_default_database_option
Browse files Browse the repository at this point in the history
Added parameter "default_dbname" to pdo_pgsql driver which can be use…
  • Loading branch information
deeky666 committed Jan 15, 2016
2 parents 5f0dfd2 + 253b602 commit 02e5b61
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/en/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ pdo\_pgsql
- ``dbname`` (string): Name of the database/schema to connect to.
- ``charset`` (string): The charset used when connecting to the
database.
- ``default_dbname`` (string): Override the default database (postgres)
to connect to.
- ``sslmode`` (string): Determines whether or with what priority
a SSL TCP/IP connection will be negotiated with the server.
See the list of available modes:
Expand Down
4 changes: 3 additions & 1 deletion lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ private function _constructPdoDsn(array $params)

if (isset($params['dbname'])) {
$dsn .= 'dbname=' . $params['dbname'] . ' ';
} elseif (isset($params['default_dbname'])) {
$dsn .= 'dbname=' . $params['default_dbname'] . ' ';
} else {
// Used for temporary connections to allow operations like dropping the database currently connected to.
// Connecting without an explicit database does not work, therefore "postgres" database is used
// as it is certainly present in every server setup.
// as it is mostly present in every server setup.
$dsn .= 'dbname=postgres' . ' ';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Doctrine\Tests\DBAL\Functional\Driver\PDOPgSql;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDOPgSql\Driver;
use Doctrine\Tests\DBAL\Functional\Driver\AbstractDriverTest;
use Doctrine\Tests\TestUtil;

class DriverTest extends AbstractDriverTest
{
Expand All @@ -20,6 +22,43 @@ protected function setUp()
}
}

/**
* @dataProvider getDatabaseParameter
*/
public function testDatabaseParameters($databaseName, $defaultDatabaseName, $expectedDatabaseName)
{
$params = $this->_conn->getParams();
$params['dbname'] = $databaseName;
$params['default_dbname'] = $defaultDatabaseName;

$connection = new Connection(
$params,
$this->_conn->getDriver(),
$this->_conn->getConfiguration(),
$this->_conn->getEventManager()
);

$this->assertSame(
$expectedDatabaseName,
$this->driver->getDatabase($connection)
);
}

public function getDatabaseParameter()
{
$params = TestUtil::getConnection()->getParams();
$realDatabaseName = $params['dbname'];
$dummyDatabaseName = $realDatabaseName . 'a';

return array(
// dbname, default_dbname, expected
array($realDatabaseName, null, $realDatabaseName),
array($realDatabaseName, $dummyDatabaseName, $realDatabaseName),
array(null, $realDatabaseName, $realDatabaseName),
array(null, null, $this->getDatabaseNameForConnectionWithoutDatabaseNameParameter()),
);
}

/**
* @group DBAL-1146
*/
Expand Down

0 comments on commit 02e5b61

Please sign in to comment.