Removed Driver::getDatabase() in favor of Connection::getDatabase()

parent 71157f9a
# Upgrade to 3.0 # Upgrade to 3.0
## BC BREAK: Changes in obtaining the currently selected database name
- The `Doctrine\DBAL\Driver::getDatabase()` method has been removed. Please use `Doctrine\DBAL\Connection::getDatabase()` instead.
- `Doctrine\DBAL\Connection::getDatabase()` will always return the name of the database currently connected to, regardless of the configuration parameters and will initialize a database connection if it's not yet established.
- A call to `Doctrine\DBAL\Connection::getDatabase()`, when connected to an SQLite database, will no longer return the database file path.
## BC BREAK: Changes in handling string and binary columns ## BC BREAK: Changes in handling string and binary columns
- When generating schema DDL, DBAL no longer provides the default length for string and binary columns. The application may need to provide the column length if required by the target platform. - When generating schema DDL, DBAL no longer provides the default length for string and binary columns. The application may need to provide the column length if required by the target platform.
......
...@@ -205,11 +205,23 @@ class Connection implements DriverConnection ...@@ -205,11 +205,23 @@ class Connection implements DriverConnection
} }
/** /**
* Gets the name of the database this Connection is connected to. * Gets the name of the currently selected database.
*
* @return string|null The name of the database or NULL if a database is not selected.
* The platforms which don't support the concept of a database (e.g. embedded databases)
* must always return a string as an indicator of an implicitly selected database.
*
* @throws DBALException
*/ */
public function getDatabase() : ?string public function getDatabase() : ?string
{ {
return $this->_driver->getDatabase($this); $platform = $this->getDatabasePlatform();
$query = $platform->getDummySelectSQL($platform->getCurrentDatabaseExpression());
$database = $this->query($query)->fetchColumn();
assert(is_string($database) || $database === null);
return $database;
} }
/** /**
......
...@@ -44,11 +44,4 @@ interface Driver ...@@ -44,11 +44,4 @@ interface Driver
* database schema of the platform this driver connects to. * database schema of the platform this driver connects to.
*/ */
public function getSchemaManager(Connection $conn) : AbstractSchemaManager; public function getSchemaManager(Connection $conn) : AbstractSchemaManager;
/**
* Gets the name of the database connected to for this driver.
*
* @return string|null The name of the database or NULL if no database is currently selected.
*/
public function getDatabase(Connection $conn) : ?string;
} }
...@@ -16,16 +16,6 @@ use Doctrine\DBAL\Schema\DB2SchemaManager; ...@@ -16,16 +16,6 @@ use Doctrine\DBAL\Schema\DB2SchemaManager;
*/ */
abstract class AbstractDB2Driver implements Driver abstract class AbstractDB2Driver implements Driver
{ {
/**
* {@inheritdoc}
*/
public function getDatabase(Connection $conn) : ?string
{
$params = $conn->getParams();
return $params['dbname'];
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -193,16 +193,6 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, ...@@ -193,16 +193,6 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch']; return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch'];
} }
/**
* {@inheritdoc}
*/
public function getDatabase(Connection $conn) : ?string
{
$params = $conn->getParams();
return $params['dbname'] ?? $conn->query('SELECT DATABASE()')->fetchColumn();
}
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
......
...@@ -63,16 +63,6 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver ...@@ -63,16 +63,6 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver
return new DriverException($message, $exception); return new DriverException($message, $exception);
} }
/**
* {@inheritdoc}
*/
public function getDatabase(Connection $conn) : ?string
{
$params = $conn->getParams();
return $params['user'];
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -107,16 +107,6 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri ...@@ -107,16 +107,6 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri
} }
} }
/**
* {@inheritdoc}
*/
public function getDatabase(Connection $conn) : ?string
{
$params = $conn->getParams();
return $params['dbname'] ?? $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn();
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -89,16 +89,6 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr ...@@ -89,16 +89,6 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr
} }
} }
/**
* {@inheritdoc}
*/
public function getDatabase(Connection $conn) : ?string
{
$params = $conn->getParams();
return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchColumn();
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -51,16 +51,6 @@ abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDr ...@@ -51,16 +51,6 @@ abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDr
} }
} }
/**
* {@inheritdoc}
*/
public function getDatabase(Connection $conn) : ?string
{
$params = $conn->getParams();
return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchColumn();
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -76,16 +76,6 @@ abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver ...@@ -76,16 +76,6 @@ abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver
return new DriverException($message, $exception); return new DriverException($message, $exception);
} }
/**
* {@inheritdoc}
*/
public function getDatabase(Connection $conn) : ?string
{
$params = $conn->getParams();
return $params['path'] ?? '';
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -1112,6 +1112,11 @@ abstract class AbstractPlatform ...@@ -1112,6 +1112,11 @@ abstract class AbstractPlatform
return '(' . $value1 . ' | ' . $value2 . ')'; return '(' . $value1 . ' | ' . $value2 . ')';
} }
/**
* Returns the SQL expression which represents the currently selected database.
*/
abstract public function getCurrentDatabaseExpression() : string;
/** /**
* Returns the FOR UPDATE expression. * Returns the FOR UPDATE expression.
*/ */
......
...@@ -794,6 +794,14 @@ class DB2Platform extends AbstractPlatform ...@@ -794,6 +794,14 @@ class DB2Platform extends AbstractPlatform
return sprintf('SUBSTR(%s, %s, %s)', $string, $start, $length); return sprintf('SUBSTR(%s, %s, %s)', $string, $start, $length);
} }
/**
* {@inheritDoc}
*/
public function getCurrentDatabaseExpression() : string
{
return 'CURRENT_USER';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -117,6 +117,14 @@ class MySqlPlatform extends AbstractPlatform ...@@ -117,6 +117,14 @@ class MySqlPlatform extends AbstractPlatform
return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')'; return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
} }
/**
* {@inheritDoc}
*/
public function getCurrentDatabaseExpression() : string
{
return 'DATABASE()';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -145,6 +145,14 @@ class OraclePlatform extends AbstractPlatform ...@@ -145,6 +145,14 @@ class OraclePlatform extends AbstractPlatform
return 'BITAND(' . $value1 . ', ' . $value2 . ')'; return 'BITAND(' . $value1 . ', ' . $value2 . ')';
} }
/**
* {@inheritDoc}
*/
public function getCurrentDatabaseExpression() : string
{
return "SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')";
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -125,6 +125,14 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -125,6 +125,14 @@ class PostgreSqlPlatform extends AbstractPlatform
return '(DATE(' . $date1 . ')-DATE(' . $date2 . '))'; return '(DATE(' . $date1 . ')-DATE(' . $date2 . '))';
} }
/**
* {@inheritDoc}
*/
public function getCurrentDatabaseExpression() : string
{
return 'CURRENT_DATABASE()';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -1124,6 +1124,14 @@ SQL ...@@ -1124,6 +1124,14 @@ SQL
} }
} }
/**
* {@inheritDoc}
*/
public function getCurrentDatabaseExpression() : string
{
return 'DB_NAME()';
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -1102,6 +1102,14 @@ SQL ...@@ -1102,6 +1102,14 @@ SQL
return 'LEN(' . $string . ')'; return 'LEN(' . $string . ')';
} }
/**
* {@inheritDoc}
*/
public function getCurrentDatabaseExpression() : string
{
return 'DB_NAME()';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -151,6 +151,19 @@ class SqlitePlatform extends AbstractPlatform ...@@ -151,6 +151,19 @@ class SqlitePlatform extends AbstractPlatform
return sprintf("JULIANDAY(%s, 'start of day') - JULIANDAY(%s, 'start of day')", $date1, $date2); return sprintf("JULIANDAY(%s, 'start of day') - JULIANDAY(%s, 'start of day')", $date1, $date2);
} }
/**
* {@inheritDoc}
*
* The SQLite platform doesn't support the concept of a database, therefore, it always returns an empty string
* as an indicator of an implicitly selected database.
*
* @see \Doctrine\DBAL\Connection::getDatabase()
*/
public function getCurrentDatabaseExpression() : string
{
return "''";
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -140,23 +140,6 @@ abstract class AbstractDriverTest extends DbalTestCase ...@@ -140,23 +140,6 @@ abstract class AbstractDriverTest extends DbalTestCase
$this->driver->createDatabasePlatformForVersion('foo'); $this->driver->createDatabasePlatformForVersion('foo');
} }
public function testReturnsDatabaseName() : void
{
$params = [
'user' => 'foo',
'password' => 'bar',
'dbname' => 'baz',
];
$connection = $this->getConnectionMock();
$connection->expects($this->once())
->method('getParams')
->will($this->returnValue($params));
self::assertSame($params['dbname'], $this->driver->getDatabase($connection));
}
public function testReturnsDatabasePlatform() : void public function testReturnsDatabasePlatform() : void
{ {
self::assertEquals($this->createPlatform(), $this->driver->getDatabasePlatform()); self::assertEquals($this->createPlatform(), $this->driver->getDatabasePlatform());
......
...@@ -7,7 +7,6 @@ namespace Doctrine\Tests\DBAL\Driver; ...@@ -7,7 +7,6 @@ namespace Doctrine\Tests\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MariaDb1027Platform; use Doctrine\DBAL\Platforms\MariaDb1027Platform;
use Doctrine\DBAL\Platforms\MySQL57Platform; use Doctrine\DBAL\Platforms\MySQL57Platform;
...@@ -18,35 +17,6 @@ use Doctrine\DBAL\Schema\MySqlSchemaManager; ...@@ -18,35 +17,6 @@ use Doctrine\DBAL\Schema\MySqlSchemaManager;
class AbstractMySQLDriverTest extends AbstractDriverTest class AbstractMySQLDriverTest extends AbstractDriverTest
{ {
public function testReturnsDatabaseName() : void
{
parent::testReturnsDatabaseName();
$database = 'bloo';
$params = [
'user' => 'foo',
'password' => 'bar',
];
$statement = $this->createMock(ResultStatement::class);
$statement->expects($this->once())
->method('fetchColumn')
->will($this->returnValue($database));
$connection = $this->getConnectionMock();
$connection->expects($this->once())
->method('getParams')
->will($this->returnValue($params));
$connection->expects($this->once())
->method('query')
->will($this->returnValue($statement));
self::assertSame($database, $this->driver->getDatabase($connection));
}
protected function createDriver() : Driver protected function createDriver() : Driver
{ {
return $this->getMockForAbstractClass(AbstractMySQLDriver::class); return $this->getMockForAbstractClass(AbstractMySQLDriver::class);
......
...@@ -14,42 +14,6 @@ use Doctrine\DBAL\Schema\OracleSchemaManager; ...@@ -14,42 +14,6 @@ use Doctrine\DBAL\Schema\OracleSchemaManager;
class AbstractOracleDriverTest extends AbstractDriverTest class AbstractOracleDriverTest extends AbstractDriverTest
{ {
public function testReturnsDatabaseName() : void
{
$params = [
'user' => 'foo',
'password' => 'bar',
'dbname' => 'baz',
];
$connection = $this->getConnectionMock();
$connection->expects($this->once())
->method('getParams')
->will($this->returnValue($params));
self::assertSame($params['user'], $this->driver->getDatabase($connection));
}
public function testReturnsDatabaseNameWithConnectDescriptor() : void
{
$params = [
'user' => 'foo',
'password' => 'bar',
'connectionstring' => '(DESCRIPTION=' .
'(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))' .
'(CONNECT_DATA=(SERVICE_NAME=baz)))',
];
$connection = $this->getConnectionMock();
$connection->expects($this->once())
->method('getParams')
->will($this->returnValue($params));
self::assertSame($params['user'], $this->driver->getDatabase($connection));
}
protected function createDriver() : Driver protected function createDriver() : Driver
{ {
return $this->getMockForAbstractClass(AbstractOracleDriver::class); return $this->getMockForAbstractClass(AbstractOracleDriver::class);
......
...@@ -7,7 +7,6 @@ namespace Doctrine\Tests\DBAL\Driver; ...@@ -7,7 +7,6 @@ namespace Doctrine\Tests\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver; use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSQL100Platform; use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform; use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
...@@ -17,35 +16,6 @@ use Doctrine\DBAL\Schema\PostgreSqlSchemaManager; ...@@ -17,35 +16,6 @@ use Doctrine\DBAL\Schema\PostgreSqlSchemaManager;
class AbstractPostgreSQLDriverTest extends AbstractDriverTest class AbstractPostgreSQLDriverTest extends AbstractDriverTest
{ {
public function testReturnsDatabaseName() : void
{
parent::testReturnsDatabaseName();
$database = 'bloo';
$params = [
'user' => 'foo',
'password' => 'bar',
];
$statement = $this->createMock(ResultStatement::class);
$statement->expects($this->once())
->method('fetchColumn')
->will($this->returnValue($database));
$connection = $this->getConnectionMock();
$connection->expects($this->once())
->method('getParams')
->will($this->returnValue($params));
$connection->expects($this->once())
->method('query')
->will($this->returnValue($statement));
self::assertSame($database, $this->driver->getDatabase($connection));
}
protected function createDriver() : Driver protected function createDriver() : Driver
{ {
return $this->getMockForAbstractClass(AbstractPostgreSQLDriver::class); return $this->getMockForAbstractClass(AbstractPostgreSQLDriver::class);
......
...@@ -14,24 +14,6 @@ use Doctrine\DBAL\Schema\SqliteSchemaManager; ...@@ -14,24 +14,6 @@ use Doctrine\DBAL\Schema\SqliteSchemaManager;
class AbstractSQLiteDriverTest extends AbstractDriverTest class AbstractSQLiteDriverTest extends AbstractDriverTest
{ {
public function testReturnsDatabaseName() : void
{
$params = [
'user' => 'foo',
'password' => 'bar',
'dbname' => 'baz',
'path' => 'bloo',
];
$connection = $this->getConnectionMock();
$connection->expects($this->once())
->method('getParams')
->will($this->returnValue($params));
self::assertSame($params['path'], $this->driver->getDatabase($connection));
}
protected function createDriver() : Driver protected function createDriver() : Driver
{ {
return $this->getMockForAbstractClass(AbstractSQLiteDriver::class); return $this->getMockForAbstractClass(AbstractSQLiteDriver::class);
......
...@@ -58,7 +58,7 @@ abstract class AbstractDriverTest extends DbalFunctionalTestCase ...@@ -58,7 +58,7 @@ abstract class AbstractDriverTest extends DbalFunctionalTestCase
self::assertSame( self::assertSame(
static::getDatabaseNameForConnectionWithoutDatabaseNameParameter(), static::getDatabaseNameForConnectionWithoutDatabaseNameParameter(),
$this->driver->getDatabase($connection) $connection->getDatabase()
); );
} }
......
...@@ -49,7 +49,7 @@ class DriverTest extends AbstractDriverTest ...@@ -49,7 +49,7 @@ class DriverTest extends AbstractDriverTest
self::assertSame( self::assertSame(
$expectedDatabaseName, $expectedDatabaseName,
$this->driver->getDatabase($connection) $connection->getDatabase()
); );
} }
......
...@@ -41,7 +41,7 @@ class DriverTest extends AbstractDriverTest ...@@ -41,7 +41,7 @@ class DriverTest extends AbstractDriverTest
// SQL Anywhere has no "default" database. The name of the default database // SQL Anywhere has no "default" database. The name of the default database
// is defined on server startup and therefore can be arbitrary. // is defined on server startup and therefore can be arbitrary.
self::assertIsString($this->driver->getDatabase($connection)); self::assertIsString($connection->getDatabase());
} }
/** /**
......
...@@ -6,6 +6,7 @@ namespace Doctrine\Tests; ...@@ -6,6 +6,7 @@ namespace Doctrine\Tests;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Platforms\OraclePlatform;
use InvalidArgumentException; use InvalidArgumentException;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
use function explode; use function explode;
...@@ -99,7 +100,12 @@ class TestUtil ...@@ -99,7 +100,12 @@ class TestUtil
$platform = $tmpConn->getDatabasePlatform(); $platform = $tmpConn->getDatabasePlatform();
if ($platform->supportsCreateDropDatabase()) { if ($platform->supportsCreateDropDatabase()) {
$dbname = $realConn->getDatabase(); if (! $platform instanceof OraclePlatform) {
$dbname = $realDbParams['dbname'];
} else {
$dbname = $realDbParams['user'];
}
$realConn->close(); $realConn->close();
if ($dbname === null) { if ($dbname === null) {
......
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