Commit 02e5b61b authored by Steve Müller's avatar Steve Müller

Merge pull request #2284 from kimhemsoe/pg_default_database_option

Added parameter "default_dbname" to pdo_pgsql driver which can be use…
parents 5f0dfd29 253b602c
......@@ -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:
......
......@@ -88,10 +88,12 @@ class Driver extends AbstractPostgreSQLDriver
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' . ' ';
}
......
......@@ -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
{
......@@ -20,6 +22,43 @@ class DriverTest extends AbstractDriverTest
}
}
/**
* @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
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment