Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Expand Down
16 changes: 14 additions & 2 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

return $params['dbname'];
}

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

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

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

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

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

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

/**
* {@inheritDoc}
*/
Expand Down
13 changes: 13 additions & 0 deletions lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
morozov marked this conversation as resolved.
Show resolved Hide resolved
* as an indicator of an implicitly selected database.
*
* @see \Doctrine\DBAL\Connection::getDatabase()
*/
public function getCurrentDatabaseExpression() : string
{
return "''";
}

/**
* {@inheritDoc}
*/
Expand Down
17 changes: 0 additions & 17 deletions tests/Doctrine/Tests/DBAL/Driver/AbstractDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
30 changes: 0 additions & 30 deletions tests/Doctrine/Tests/DBAL/Driver/AbstractMySQLDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Loading