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

parent 36bb4e58
# 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: `Doctrine\DBAL\Driver::getName()` removed
The `Doctrine\DBAL\Driver::getName()` has been removed.
......
......@@ -197,13 +197,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
* @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()
{
return $this->_driver->getDatabase($this);
$platform = $this->getDatabasePlatform();
$query = $platform->getDummySelectSQL($platform->getCurrentDatabaseExpression());
$database = $this->query($query)->fetchOne();
assert(is_string($database) || $database === null);
return $database;
}
/**
......
......@@ -39,13 +39,4 @@ interface Driver
* @return AbstractSchemaManager
*/
public function getSchemaManager(Connection $conn);
/**
* Gets the name of the database connected to for this driver.
*
* @deprecated Use Connection::getDatabase() instead.
*
* @return string The name of the database.
*/
public function getDatabase(Connection $conn);
}
......@@ -12,18 +12,6 @@ use Doctrine\DBAL\Schema\DB2SchemaManager;
*/
abstract class AbstractDB2Driver implements Driver
{
/**
* {@inheritdoc}
*
* @deprecated Use Connection::getDatabase() instead.
*/
public function getDatabase(Connection $conn)
{
$params = $conn->getParams();
return $params['dbname'];
}
/**
* {@inheritdoc}
*/
......
......@@ -194,18 +194,6 @@ abstract class AbstractMySQLDriver implements ExceptionConverterDriver, VersionA
return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch'];
}
/**
* {@inheritdoc}
*
* @deprecated Use Connection::getDatabase() instead.
*/
public function getDatabase(Connection $conn)
{
$params = $conn->getParams();
return $params['dbname'] ?? $conn->query('SELECT DATABASE()')->fetchOne();
}
/**
* {@inheritdoc}
*
......
......@@ -57,18 +57,6 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver
return new Exception\DriverException($message, $exception);
}
/**
* {@inheritdoc}
*
* @deprecated Use Connection::getDatabase() instead.
*/
public function getDatabase(Connection $conn)
{
$params = $conn->getParams();
return $params['user'];
}
/**
* {@inheritdoc}
*/
......
......@@ -102,18 +102,6 @@ abstract class AbstractPostgreSQLDriver implements ExceptionConverterDriver, Ver
return new PostgreSQL94Platform();
}
/**
* {@inheritdoc}
*
* @deprecated Use Connection::getDatabase() instead.
*/
public function getDatabase(Connection $conn)
{
$params = $conn->getParams();
return $params['dbname'] ?? $conn->query('SELECT CURRENT_DATABASE()')->fetchOne();
}
/**
* {@inheritdoc}
*/
......
......@@ -12,18 +12,6 @@ use Doctrine\DBAL\Schema\SQLServerSchemaManager;
*/
abstract class AbstractSQLServerDriver implements Driver
{
/**
* {@inheritdoc}
*
* @deprecated Use Connection::getDatabase() instead.
*/
public function getDatabase(Connection $conn)
{
$params = $conn->getParams();
return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchOne();
}
/**
* {@inheritdoc}
*/
......
......@@ -77,18 +77,6 @@ abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver
return new Exception\DriverException($message, $exception);
}
/**
* {@inheritdoc}
*
* @deprecated Use Connection::getDatabase() instead.
*/
public function getDatabase(Connection $conn)
{
$params = $conn->getParams();
return $params['path'] ?? null;
}
/**
* {@inheritdoc}
*/
......
......@@ -1284,6 +1284,11 @@ abstract class AbstractPlatform
return '(' . $value1 . ' | ' . $value2 . ')';
}
/**
* Returns the SQL expression which represents the currently selected database.
*/
abstract public function getCurrentDatabaseExpression(): string;
/**
* Returns the FOR UPDATE expression.
*
......
......@@ -840,6 +840,11 @@ class DB2Platform extends AbstractPlatform
return 'SUBSTR(' . $value . ', ' . $from . ', ' . $length . ')';
}
public function getCurrentDatabaseExpression(): string
{
return 'CURRENT_USER';
}
/**
* {@inheritDoc}
*/
......
......@@ -127,6 +127,11 @@ class MySqlPlatform extends AbstractPlatform
return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
}
public function getCurrentDatabaseExpression(): string
{
return 'DATABASE()';
}
/**
* {@inheritDoc}
*/
......
......@@ -161,6 +161,11 @@ class OraclePlatform extends AbstractPlatform
return 'BITAND(' . $value1 . ', ' . $value2 . ')';
}
public function getCurrentDatabaseExpression(): string
{
return "SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')";
}
/**
* {@inheritDoc}
*/
......
......@@ -139,6 +139,11 @@ class PostgreSQL94Platform extends AbstractPlatform
return '(DATE(' . $date1 . ')-DATE(' . $date2 . '))';
}
public function getCurrentDatabaseExpression(): string
{
return 'CURRENT_DATABASE()';
}
/**
* {@inheritDoc}
*/
......
......@@ -1203,6 +1203,11 @@ SQL
return 'LEN(' . $column . ')';
}
public function getCurrentDatabaseExpression(): string
{
return 'DB_NAME()';
}
/**
* {@inheritDoc}
*/
......
......@@ -162,6 +162,19 @@ class SqlitePlatform extends AbstractPlatform
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}
*/
......
......@@ -1072,7 +1072,13 @@ abstract class AbstractSchemaManager
*/
public function getSchemaSearchPaths()
{
return [$this->_conn->getDatabase()];
$database = $this->_conn->getDatabase();
if ($database !== null) {
return [$database];
}
return [];
}
/**
......
......@@ -142,23 +142,6 @@ abstract class AbstractDriverTest extends TestCase
$this->driver->createDatabasePlatformForVersion('foo');
}
public function testReturnsDatabaseName(): void
{
$params = [
'user' => 'foo',
'password' => 'bar',
'dbname' => 'baz',
];
$connection = $this->getConnectionMock();
$connection->expects(self::once())
->method('getParams')
->will(self::returnValue($params));
self::assertSame($params['dbname'], $this->driver->getDatabase($connection));
}
public function testReturnsDatabasePlatform(): void
{
self::assertEquals($this->createPlatform(), $this->driver->getDatabasePlatform());
......
......@@ -5,7 +5,6 @@ namespace Doctrine\DBAL\Tests\Driver;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MariaDb1027Platform;
use Doctrine\DBAL\Platforms\MySQL57Platform;
......@@ -16,35 +15,6 @@ use Doctrine\DBAL\Schema\MySqlSchemaManager;
class AbstractMySQLDriverTest extends AbstractDriverTest
{
public function testReturnsDatabaseName(): void
{
parent::testReturnsDatabaseName();
$database = 'bloo';
$params = [
'user' => 'foo',
'password' => 'bar',
];
$result = $this->createMock(Result::class);
$result->expects(self::once())
->method('fetchOne')
->will(self::returnValue($database));
$connection = $this->getConnectionMock();
$connection->expects(self::once())
->method('getParams')
->will(self::returnValue($params));
$connection->expects(self::once())
->method('query')
->will(self::returnValue($result));
self::assertSame($database, $this->driver->getDatabase($connection));
}
protected function createDriver(): Driver
{
return $this->getMockForAbstractClass(AbstractMySQLDriver::class);
......
......@@ -12,42 +12,6 @@ use Doctrine\DBAL\Schema\OracleSchemaManager;
class AbstractOracleDriverTest extends AbstractDriverTest
{
public function testReturnsDatabaseName(): void
{
$params = [
'user' => 'foo',
'password' => 'bar',
'dbname' => 'baz',
];
$connection = $this->getConnectionMock();
$connection->expects(self::once())
->method('getParams')
->will(self::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(self::once())
->method('getParams')
->will(self::returnValue($params));
self::assertSame($params['user'], $this->driver->getDatabase($connection));
}
protected function createDriver(): Driver
{
return $this->getMockForAbstractClass(AbstractOracleDriver::class);
......
......@@ -5,7 +5,6 @@ namespace Doctrine\DBAL\Tests\Driver;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
......@@ -14,35 +13,6 @@ use Doctrine\DBAL\Schema\PostgreSqlSchemaManager;
class AbstractPostgreSQLDriverTest extends AbstractDriverTest
{
public function testReturnsDatabaseName(): void
{
parent::testReturnsDatabaseName();
$database = 'bloo';
$params = [
'user' => 'foo',
'password' => 'bar',
];
$result = $this->createMock(Result::class);
$result->expects(self::once())
->method('fetchOne')
->will(self::returnValue($database));
$connection = $this->getConnectionMock();
$connection->expects(self::once())
->method('getParams')
->will(self::returnValue($params));
$connection->expects(self::once())
->method('query')
->will(self::returnValue($result));
self::assertSame($database, $this->driver->getDatabase($connection));
}
protected function createDriver(): Driver
{
return $this->getMockForAbstractClass(AbstractPostgreSQLDriver::class);
......
......@@ -12,24 +12,6 @@ use Doctrine\DBAL\Schema\SqliteSchemaManager;
class AbstractSQLiteDriverTest extends AbstractDriverTest
{
public function testReturnsDatabaseName(): void
{
$params = [
'user' => 'foo',
'password' => 'bar',
'dbname' => 'baz',
'path' => 'bloo',
];
$connection = $this->getConnectionMock();
$connection->expects(self::once())
->method('getParams')
->will(self::returnValue($params));
self::assertSame($params['path'], $this->driver->getDatabase($connection));
}
protected function createDriver(): Driver
{
return $this->getMockForAbstractClass(AbstractSQLiteDriver::class);
......
......@@ -53,7 +53,7 @@ abstract class AbstractDriverTest extends FunctionalTestCase
self::assertSame(
static::getDatabaseNameForConnectionWithoutDatabaseNameParameter(),
$this->driver->getDatabase($connection)
$connection->getDatabase()
);
}
......
......@@ -48,7 +48,7 @@ class DriverTest extends AbstractDriverTest
self::assertSame(
$expectedDatabaseName,
$this->driver->getDatabase($connection)
$connection->getDatabase()
);
}
......
......@@ -25,6 +25,11 @@ class DriverTest extends AbstractDriverTest
self::markTestSkipped('pdo_sqlite only test.');
}
public function testReturnsDatabaseNameWithoutDatabaseNameParameter(): void
{
self::markTestSkipped('SQLite does not support the concept of a database.');
}
protected function createDriver(): DriverInterface
{
return new Driver();
......
......@@ -8,6 +8,7 @@ use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\MySqlSchemaManager;
......@@ -74,11 +75,15 @@ class MySqlInheritCharsetTest extends TestCase
private function getTableOptionsForOverride(array $overrideOptions = []): array
{
$eventManager = new EventManager();
$driverMock = $this->createMock(Driver::class);
$platform = new MySqlPlatform();
$connOptions = array_merge(['platform' => $platform], $overrideOptions);
$conn = new Connection($connOptions, $driverMock, new Configuration(), $eventManager);
$manager = new MySqlSchemaManager($conn, $platform);
$driverMock = $this->createMock(Driver::class);
$driverMock->method('connect')
->willReturn($this->createMock(DriverConnection::class));
$platform = new MySqlPlatform();
$connOptions = array_merge(['platform' => $platform], $overrideOptions);
$conn = new Connection($connOptions, $driverMock, new Configuration(), $eventManager);
$manager = new MySqlSchemaManager($conn, $platform);
$schemaConfig = $manager->createSchemaConfig();
......
......@@ -42,7 +42,7 @@ class MySqlSchemaManagerTest extends TestCase
public function testCompositeForeignKeys(): void
{
$this->conn->expects(self::once())->method('fetchAllAssociative')->will(self::returnValue($this->getFKDefinition()));
$fkeys = $this->manager->listTableForeignKeys('dummy');
$fkeys = $this->manager->listTableForeignKeys('dummy', 'dummy');
self::assertCount(1, $fkeys, 'Table has to have one foreign key.');
self::assertInstanceOf(ForeignKeyConstraint::class, $fkeys[0]);
......
......@@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Tests;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use PHPUnit\Framework\Assert;
use function array_keys;
......@@ -87,7 +88,12 @@ class TestUtil
$platform = $privConn->getDatabasePlatform();
if ($platform->supportsCreateDropDatabase()) {
$dbname = $testConn->getDatabase();
if (! $platform instanceof OraclePlatform) {
$dbname = $testConnParams['dbname'];
} else {
$dbname = $testConnParams['user'];
}
$testConn->close();
$privConn->getSchemaManager()->dropAndCreateDatabase($dbname);
......
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