Skip to content

Commit

Permalink
Remove ServerInfoAwareConnection#requiresQueryForServerVersion() as a…
Browse files Browse the repository at this point in the history
…n implementation detail

Testing the implementations of this method requires partial mocking of the implementing class which makes it impossible to make them `final` (doctrine#3590).

Additionally, this API breaks the encapsulation of the driver layer: instead of exposing the fact of whether the connection will perform a query to detect the server version, the driver should just instantiate a platform corresponding to a connection.

The rationale behind introducing this method (doctrine#487) is really questionable:

> This is also required for drivers that cannot return the database server version without an additional query (performance reasons).

1. There's no evidence that an underlying driver that exposes the server version via its API doesn't make a request of any kind to the server.
2. For an application that works with any realistic database, a query like `SELECT VERSION()` wouldn't be a performance bottleneck.
3. Even if it was, it's always possible to specify the platform version upfront. Otherwise, the current logic of falling back to a default platform may cause undefined behavior of the application (we don't test the compatibility of the lowest level of the DBAL platform with all supported server versions). Remember, “If it doesn’t work, it doesn’t matter how fast it doesn’t work.”
  • Loading branch information
morozov committed Jun 27, 2020
1 parent 9d45766 commit b98abf0
Show file tree
Hide file tree
Showing 14 changed files with 5 additions and 191 deletions.
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Upgrade to 3.0

## BC BREAK: `ServerInfoAwareConnection::requiresQueryForServerVersion()` is removed.

The `ServerInfoAwareConnection::requiresQueryForServerVersion()` method has been removed as an implementation detail which is the same for all supported drivers.

## BC BREAK Changes in driver exceptions

1. The `Doctrine\DBAL\Driver\DriverException::getErrorCode()` method is removed. In order to obtain the driver error code, please use `::getCode()` or `::getSQLState()`.
Expand Down
2 changes: 1 addition & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ private function getServerVersion()
$connection = $this->getWrappedConnection();

// Automatic platform version detection.
if ($connection instanceof ServerInfoAwareConnection && ! $connection->requiresQueryForServerVersion()) {
if ($connection instanceof ServerInfoAwareConnection) {
return $connection->getServerVersion();
}

Expand Down
8 changes: 0 additions & 8 deletions src/Driver/IBMDB2/DB2Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@ public function getServerVersion()
return $serverInfo->DBMS_VER;
}

/**
* {@inheritdoc}
*/
public function requiresQueryForServerVersion()
{
return false;
}

public function prepare(string $sql): DriverStatement
{
$stmt = @db2_prepare($this->conn, $sql);
Expand Down
8 changes: 0 additions & 8 deletions src/Driver/Mysqli/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,6 @@ public function getServerVersion()
return $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
}

/**
* {@inheritdoc}
*/
public function requiresQueryForServerVersion()
{
return false;
}

public function prepare(string $sql): DriverStatement
{
return new Statement($this->conn, $sql);
Expand Down
8 changes: 0 additions & 8 deletions src/Driver/OCI8/OCI8Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,6 @@ public function getServerVersion()
return $matches[1];
}

/**
* {@inheritdoc}
*/
public function requiresQueryForServerVersion()
{
return false;
}

public function prepare(string $sql): DriverStatement
{
return new Statement($this->dbh, $sql, $this->executionMode);
Expand Down
8 changes: 0 additions & 8 deletions src/Driver/PDOConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,6 @@ public function lastInsertId($name = null)
}
}

/**
* {@inheritdoc}
*/
public function requiresQueryForServerVersion()
{
return false;
}

/**
* Creates a wrapped statement
*/
Expand Down
8 changes: 0 additions & 8 deletions src/Driver/SQLSrv/SQLSrvConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ public function getServerVersion()
return $serverInfo['SQLServerVersion'];
}

/**
* {@inheritdoc}
*/
public function requiresQueryForServerVersion()
{
return false;
}

public function prepare(string $sql): DriverStatement
{
return new Statement($this->conn, $sql, $this->lastInsertId);
Expand Down
7 changes: 0 additions & 7 deletions src/Driver/ServerInfoAwareConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,4 @@ interface ServerInfoAwareConnection extends Connection
* @return string
*/
public function getServerVersion();

/**
* Checks whether a query is required to retrieve the database server version.
*
* @return bool True if a query is required to retrieve the database server version, false otherwise.
*/
public function requiresQueryForServerVersion();
}
4 changes: 0 additions & 4 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,10 +661,6 @@ public function testPlatformDetectionIsTriggerOnlyOnceOnRetrievingPlatform(): vo
->method('connect')
->will(self::returnValue($driverConnectionMock));

$driverConnectionMock->expects(self::once())
->method('requiresQueryForServerVersion')
->will(self::returnValue(false));

$driverConnectionMock->expects(self::once())
->method('getServerVersion')
->will(self::returnValue('6.6.6'));
Expand Down
37 changes: 0 additions & 37 deletions tests/Driver/IBMDB2/DB2ConnectionTest.php

This file was deleted.

23 changes: 0 additions & 23 deletions tests/Driver/Mysqli/MysqliConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,19 @@

use Doctrine\DBAL\Driver\Mysqli\Driver;
use Doctrine\DBAL\Driver\Mysqli\HostRequired;
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use PHPUnit\Framework\MockObject\MockObject;

use function extension_loaded;

class MysqliConnectionTest extends FunctionalTestCase
{
/**
* The mysqli driver connection mock under test.
*
* @var MysqliConnection|MockObject
*/
private $connectionMock;

protected function setUp(): void
{
if (! extension_loaded('mysqli')) {
self::markTestSkipped('mysqli is not installed.');
}

parent::setUp();

if (! $this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
$this->markTestSkipped('MySQL only test.');
}

$this->connectionMock = $this->getMockBuilder(MysqliConnection::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
}

public function testDoesNotRequireQueryForServerVersion(): void
{
self::assertFalse($this->connectionMock->requiresQueryForServerVersion());
}

public function testHostnameIsRequiredForPersistentConnection(): void
Expand Down
37 changes: 0 additions & 37 deletions tests/Driver/OCI8/OCI8ConnectionTest.php

This file was deleted.

37 changes: 0 additions & 37 deletions tests/Driver/SQLSrv/SQLSrvConnectionTest.php

This file was deleted.

5 changes: 0 additions & 5 deletions tests/Functional/Driver/PDO/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ protected function tearDown(): void
parent::tearDown();
}

public function testDoesNotRequireQueryForServerVersion(): void
{
self::assertFalse($this->driverConnection->requiresQueryForServerVersion());
}

public function testThrowsWrappedExceptionOnConstruct(): void
{
$this->expectException(Exception::class);
Expand Down

0 comments on commit b98abf0

Please sign in to comment.