Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3606 from morozov/issues/3592
Browse files Browse the repository at this point in the history
Removed Driver::getDatabase() in favor of Connection::getDatabase()
morozov committed Jun 27, 2019

Verified

This commit was signed with the committer’s verified signature.
morozov Sergei Morozov
2 parents 3e19275 + 13f1612 commit 119891b
Showing 27 changed files with 96 additions and 214 deletions.
6 changes: 6 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 14 additions & 2 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
@@ -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;
}

/**
7 changes: 0 additions & 7 deletions lib/Doctrine/DBAL/Driver.php
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 0 additions & 10 deletions lib/Doctrine/DBAL/Driver/AbstractDB2Driver.php
Original file line number Diff line number Diff line change
@@ -16,16 +16,6 @@
*/
abstract class AbstractDB2Driver implements Driver
{
/**
* {@inheritdoc}
*/
public function getDatabase(Connection $conn) : ?string
{
$params = $conn->getParams();

return $params['dbname'];
}

/**
* {@inheritdoc}
*/
10 changes: 0 additions & 10 deletions lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php
Original file line number Diff line number Diff line change
@@ -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}
*
10 changes: 0 additions & 10 deletions lib/Doctrine/DBAL/Driver/AbstractOracleDriver.php
Original file line number Diff line number Diff line change
@@ -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}
*/
10 changes: 0 additions & 10 deletions lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php
Original file line number Diff line number Diff line change
@@ -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}
*/
10 changes: 0 additions & 10 deletions lib/Doctrine/DBAL/Driver/AbstractSQLAnywhereDriver.php
Original file line number Diff line number Diff line change
@@ -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}
*/
10 changes: 0 additions & 10 deletions lib/Doctrine/DBAL/Driver/AbstractSQLServerDriver.php
Original file line number Diff line number Diff line change
@@ -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}
*/
10 changes: 0 additions & 10 deletions lib/Doctrine/DBAL/Driver/AbstractSQLiteDriver.php
Original file line number Diff line number Diff line change
@@ -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}
*/
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
@@ -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.
*/
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Platforms/DB2Platform.php
Original file line number Diff line number Diff line change
@@ -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}
*/
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
@@ -117,6 +117,14 @@ public function getDateDiffExpression(string $date1, string $date2) : string
return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
}

/**
* {@inheritDoc}
*/
public function getCurrentDatabaseExpression() : string
{
return 'DATABASE()';
}

/**
* {@inheritDoc}
*/
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
@@ -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}
*/
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
@@ -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}
*/
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php
Original file line number Diff line number Diff line change
@@ -1124,6 +1124,14 @@ public function getTrimExpression(string $str, int $mode = TrimMode::UNSPECIFIED
}
}

/**
* {@inheritDoc}
*/
public function getCurrentDatabaseExpression() : string
{
return 'DB_NAME()';
}

/**
* {@inheritdoc}
*/
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
@@ -1097,6 +1097,14 @@ public function getLengthExpression(string $string) : string
return 'LEN(' . $string . ')';
}

/**
* {@inheritDoc}
*/
public function getCurrentDatabaseExpression() : string
{
return 'DB_NAME()';
}

/**
* {@inheritDoc}
*/
13 changes: 13 additions & 0 deletions lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
@@ -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}
*/
17 changes: 0 additions & 17 deletions tests/Doctrine/Tests/DBAL/Driver/AbstractDriverTest.php
Original file line number Diff line number Diff line change
@@ -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());
30 changes: 0 additions & 30 deletions tests/Doctrine/Tests/DBAL/Driver/AbstractMySQLDriverTest.php
Original file line number Diff line number Diff line change
@@ -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);
36 changes: 0 additions & 36 deletions tests/Doctrine/Tests/DBAL/Driver/AbstractOracleDriverTest.php
Original file line number Diff line number Diff line change
@@ -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);
30 changes: 0 additions & 30 deletions tests/Doctrine/Tests/DBAL/Driver/AbstractPostgreSQLDriverTest.php
Original file line number Diff line number Diff line change
@@ -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);
18 changes: 0 additions & 18 deletions tests/Doctrine/Tests/DBAL/Driver/AbstractSQLiteDriverTest.php
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ public function testReturnsDatabaseNameWithoutDatabaseNameParameter() : void

self::assertSame(
static::getDatabaseNameForConnectionWithoutDatabaseNameParameter(),
$this->driver->getDatabase($connection)
$connection->getDatabase()
);
}

Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ public function testDatabaseParameters(?string $databaseName, ?string $defaultDa

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

Original file line number Diff line number Diff line change
@@ -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());
}

/**
8 changes: 7 additions & 1 deletion tests/Doctrine/Tests/TestUtil.php
Original file line number Diff line number Diff line change
@@ -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) {

0 comments on commit 119891b

Please sign in to comment.