diff --git a/UPGRADE.md b/UPGRADE.md index 56573f9b86c..6ecce0dd10e 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,11 @@ # 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 - 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. diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index 6966fca5468..42eebbb81f8 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -205,11 +205,23 @@ public function getParams() : array } /** - * 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 { - 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; } /** diff --git a/lib/Doctrine/DBAL/Driver.php b/lib/Doctrine/DBAL/Driver.php index 0cfc18cbba1..000ebf947f1 100644 --- a/lib/Doctrine/DBAL/Driver.php +++ b/lib/Doctrine/DBAL/Driver.php @@ -44,11 +44,4 @@ public function getDatabasePlatform() : AbstractPlatform; * database schema of the platform this driver connects to. */ 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; } diff --git a/lib/Doctrine/DBAL/Driver/AbstractDB2Driver.php b/lib/Doctrine/DBAL/Driver/AbstractDB2Driver.php index c98c376f10f..db2df68073f 100644 --- a/lib/Doctrine/DBAL/Driver/AbstractDB2Driver.php +++ b/lib/Doctrine/DBAL/Driver/AbstractDB2Driver.php @@ -16,16 +16,6 @@ */ abstract class AbstractDB2Driver implements Driver { - /** - * {@inheritdoc} - */ - public function getDatabase(Connection $conn) : ?string - { - $params = $conn->getParams(); - - return $params['dbname']; - } - /** * {@inheritdoc} */ diff --git a/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php b/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php index fada4ed9e4a..31854c517ad 100644 --- a/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php +++ b/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php @@ -193,16 +193,6 @@ private function getMariaDbMysqlVersionNumber(string $versionString) : string 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} * diff --git a/lib/Doctrine/DBAL/Driver/AbstractOracleDriver.php b/lib/Doctrine/DBAL/Driver/AbstractOracleDriver.php index 17e04bbf937..000268638b1 100644 --- a/lib/Doctrine/DBAL/Driver/AbstractOracleDriver.php +++ b/lib/Doctrine/DBAL/Driver/AbstractOracleDriver.php @@ -63,16 +63,6 @@ public function convertException(string $message, DriverExceptionInterface $exce return new DriverException($message, $exception); } - /** - * {@inheritdoc} - */ - public function getDatabase(Connection $conn) : ?string - { - $params = $conn->getParams(); - - return $params['user']; - } - /** * {@inheritdoc} */ diff --git a/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php b/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php index 933837e166c..c1052779f9d 100644 --- a/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php +++ b/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php @@ -107,16 +107,6 @@ public function createDatabasePlatformForVersion(string $version) : AbstractPlat } } - /** - * {@inheritdoc} - */ - public function getDatabase(Connection $conn) : ?string - { - $params = $conn->getParams(); - - return $params['dbname'] ?? $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn(); - } - /** * {@inheritdoc} */ diff --git a/lib/Doctrine/DBAL/Driver/AbstractSQLAnywhereDriver.php b/lib/Doctrine/DBAL/Driver/AbstractSQLAnywhereDriver.php index 248c0cfbd2f..59c418911bb 100644 --- a/lib/Doctrine/DBAL/Driver/AbstractSQLAnywhereDriver.php +++ b/lib/Doctrine/DBAL/Driver/AbstractSQLAnywhereDriver.php @@ -89,16 +89,6 @@ public function createDatabasePlatformForVersion(string $version) : AbstractPlat } } - /** - * {@inheritdoc} - */ - public function getDatabase(Connection $conn) : ?string - { - $params = $conn->getParams(); - - return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchColumn(); - } - /** * {@inheritdoc} */ diff --git a/lib/Doctrine/DBAL/Driver/AbstractSQLServerDriver.php b/lib/Doctrine/DBAL/Driver/AbstractSQLServerDriver.php index 4fd6d1442a4..5475eadee83 100644 --- a/lib/Doctrine/DBAL/Driver/AbstractSQLServerDriver.php +++ b/lib/Doctrine/DBAL/Driver/AbstractSQLServerDriver.php @@ -51,16 +51,6 @@ public function createDatabasePlatformForVersion(string $version) : AbstractPlat } } - /** - * {@inheritdoc} - */ - public function getDatabase(Connection $conn) : ?string - { - $params = $conn->getParams(); - - return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchColumn(); - } - /** * {@inheritdoc} */ diff --git a/lib/Doctrine/DBAL/Driver/AbstractSQLiteDriver.php b/lib/Doctrine/DBAL/Driver/AbstractSQLiteDriver.php index 760e9a0ca33..4b7c42cb285 100644 --- a/lib/Doctrine/DBAL/Driver/AbstractSQLiteDriver.php +++ b/lib/Doctrine/DBAL/Driver/AbstractSQLiteDriver.php @@ -76,16 +76,6 @@ public function convertException(string $message, DriverExceptionInterface $exce return new DriverException($message, $exception); } - /** - * {@inheritdoc} - */ - public function getDatabase(Connection $conn) : ?string - { - $params = $conn->getParams(); - - return $params['path'] ?? ''; - } - /** * {@inheritdoc} */ diff --git a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php index ca62e2aaa8c..43aa50f4d60 100644 --- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php @@ -1112,6 +1112,11 @@ public function getBitOrComparisonExpression(string $value1, string $value2) : s return '(' . $value1 . ' | ' . $value2 . ')'; } + /** + * Returns the SQL expression which represents the currently selected database. + */ + abstract public function getCurrentDatabaseExpression() : string; + /** * Returns the FOR UPDATE expression. */ diff --git a/lib/Doctrine/DBAL/Platforms/DB2Platform.php b/lib/Doctrine/DBAL/Platforms/DB2Platform.php index 30d001b31c1..cc9551c2c61 100644 --- a/lib/Doctrine/DBAL/Platforms/DB2Platform.php +++ b/lib/Doctrine/DBAL/Platforms/DB2Platform.php @@ -794,6 +794,14 @@ public function getSubstringExpression(string $string, string $start, ?string $l return sprintf('SUBSTR(%s, %s, %s)', $string, $start, $length); } + /** + * {@inheritDoc} + */ + public function getCurrentDatabaseExpression() : string + { + return 'CURRENT_USER'; + } + /** * {@inheritDoc} */ diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index b4af5b275e4..7a65de5ed03 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -117,6 +117,14 @@ public function getDateDiffExpression(string $date1, string $date2) : string return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')'; } + /** + * {@inheritDoc} + */ + public function getCurrentDatabaseExpression() : string + { + return 'DATABASE()'; + } + /** * {@inheritDoc} */ diff --git a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php index b520c3ea370..4046970dbe2 100644 --- a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php @@ -145,6 +145,14 @@ public function getBitAndComparisonExpression(string $value1, string $value2) : return 'BITAND(' . $value1 . ', ' . $value2 . ')'; } + /** + * {@inheritDoc} + */ + public function getCurrentDatabaseExpression() : string + { + return "SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')"; + } + /** * {@inheritDoc} */ diff --git a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php index 111d55df212..800b28a2b5b 100644 --- a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php @@ -125,6 +125,14 @@ public function getDateDiffExpression(string $date1, string $date2) : string return '(DATE(' . $date1 . ')-DATE(' . $date2 . '))'; } + /** + * {@inheritDoc} + */ + public function getCurrentDatabaseExpression() : string + { + return 'CURRENT_DATABASE()'; + } + /** * {@inheritDoc} */ diff --git a/lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php b/lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php index 935fe988bf5..c4b03dfa665 100644 --- a/lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php @@ -1124,6 +1124,14 @@ public function getTrimExpression(string $str, int $mode = TrimMode::UNSPECIFIED } } + /** + * {@inheritDoc} + */ + public function getCurrentDatabaseExpression() : string + { + return 'DB_NAME()'; + } + /** * {@inheritdoc} */ diff --git a/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php b/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php index 55be1a273dd..cac586ef4c6 100644 --- a/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php @@ -1097,6 +1097,14 @@ public function getLengthExpression(string $string) : string return 'LEN(' . $string . ')'; } + /** + * {@inheritDoc} + */ + public function getCurrentDatabaseExpression() : string + { + return 'DB_NAME()'; + } + /** * {@inheritDoc} */ diff --git a/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php b/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php index 7cf1b7b3fbf..4e78a531106 100644 --- a/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php @@ -150,6 +150,19 @@ public function getDateDiffExpression(string $date1, string $date2) : string 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} */ diff --git a/tests/Doctrine/Tests/DBAL/Driver/AbstractDriverTest.php b/tests/Doctrine/Tests/DBAL/Driver/AbstractDriverTest.php index b7bafb55906..d9ab0cb96c5 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/AbstractDriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/AbstractDriverTest.php @@ -140,23 +140,6 @@ public function testThrowsExceptionOnCreatingDatabasePlatformsForInvalidVersion( $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 { self::assertEquals($this->createPlatform(), $this->driver->getDatabasePlatform()); diff --git a/tests/Doctrine/Tests/DBAL/Driver/AbstractMySQLDriverTest.php b/tests/Doctrine/Tests/DBAL/Driver/AbstractMySQLDriverTest.php index 2d831dbd41f..cf6ba7ead6c 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/AbstractMySQLDriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/AbstractMySQLDriverTest.php @@ -7,7 +7,6 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver\AbstractMySQLDriver; -use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\MariaDb1027Platform; use Doctrine\DBAL\Platforms\MySQL57Platform; @@ -18,35 +17,6 @@ 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 { return $this->getMockForAbstractClass(AbstractMySQLDriver::class); diff --git a/tests/Doctrine/Tests/DBAL/Driver/AbstractOracleDriverTest.php b/tests/Doctrine/Tests/DBAL/Driver/AbstractOracleDriverTest.php index f5862eb12de..59f824ac717 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/AbstractOracleDriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/AbstractOracleDriverTest.php @@ -14,42 +14,6 @@ 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 { return $this->getMockForAbstractClass(AbstractOracleDriver::class); diff --git a/tests/Doctrine/Tests/DBAL/Driver/AbstractPostgreSQLDriverTest.php b/tests/Doctrine/Tests/DBAL/Driver/AbstractPostgreSQLDriverTest.php index 9a698d92f4d..6e0ac2dd78c 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/AbstractPostgreSQLDriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/AbstractPostgreSQLDriverTest.php @@ -7,7 +7,6 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver; -use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\PostgreSQL100Platform; use Doctrine\DBAL\Platforms\PostgreSQL94Platform; @@ -17,35 +16,6 @@ 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 { return $this->getMockForAbstractClass(AbstractPostgreSQLDriver::class); diff --git a/tests/Doctrine/Tests/DBAL/Driver/AbstractSQLiteDriverTest.php b/tests/Doctrine/Tests/DBAL/Driver/AbstractSQLiteDriverTest.php index 92bd2ebd90c..1e009de5bd7 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/AbstractSQLiteDriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/AbstractSQLiteDriverTest.php @@ -14,24 +14,6 @@ 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 { return $this->getMockForAbstractClass(AbstractSQLiteDriver::class); diff --git a/tests/Doctrine/Tests/DBAL/Functional/Driver/AbstractDriverTest.php b/tests/Doctrine/Tests/DBAL/Functional/Driver/AbstractDriverTest.php index 992a9ba691c..c4daf96051b 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Driver/AbstractDriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Driver/AbstractDriverTest.php @@ -58,7 +58,7 @@ public function testReturnsDatabaseNameWithoutDatabaseNameParameter() : void self::assertSame( static::getDatabaseNameForConnectionWithoutDatabaseNameParameter(), - $this->driver->getDatabase($connection) + $connection->getDatabase() ); } diff --git a/tests/Doctrine/Tests/DBAL/Functional/Driver/PDOPgSql/DriverTest.php b/tests/Doctrine/Tests/DBAL/Functional/Driver/PDOPgSql/DriverTest.php index 2f06cfe3862..69c44e9631f 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Driver/PDOPgSql/DriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Driver/PDOPgSql/DriverTest.php @@ -49,7 +49,7 @@ public function testDatabaseParameters(?string $databaseName, ?string $defaultDa self::assertSame( $expectedDatabaseName, - $this->driver->getDatabase($connection) + $connection->getDatabase() ); } diff --git a/tests/Doctrine/Tests/DBAL/Functional/Driver/SQLAnywhere/DriverTest.php b/tests/Doctrine/Tests/DBAL/Functional/Driver/SQLAnywhere/DriverTest.php index 7ddb4b9c807..5904597e8f3 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Driver/SQLAnywhere/DriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Driver/SQLAnywhere/DriverTest.php @@ -41,7 +41,7 @@ public function testReturnsDatabaseNameWithoutDatabaseNameParameter() : void // SQL Anywhere has no "default" database. The name of the default database // is defined on server startup and therefore can be arbitrary. - self::assertIsString($this->driver->getDatabase($connection)); + self::assertIsString($connection->getDatabase()); } /** diff --git a/tests/Doctrine/Tests/TestUtil.php b/tests/Doctrine/Tests/TestUtil.php index 7f9dc8d4e1e..a009e2d7291 100644 --- a/tests/Doctrine/Tests/TestUtil.php +++ b/tests/Doctrine/Tests/TestUtil.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Platforms\OraclePlatform; use InvalidArgumentException; use PHPUnit\Framework\Assert; use function explode; @@ -99,7 +100,12 @@ private static function initializeDatabase() : void $platform = $tmpConn->getDatabasePlatform(); if ($platform->supportsCreateDropDatabase()) { - $dbname = $realConn->getDatabase(); + if (! $platform instanceof OraclePlatform) { + $dbname = $realDbParams['dbname']; + } else { + $dbname = $realDbParams['user']; + } + $realConn->close(); if ($dbname === null) {