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

Remove getWrappedConnection() and add getNativeConnection() to the interface #5044

Merged
merged 1 commit into from
Nov 28, 2021
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
9 changes: 9 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ awareness about deprecated code.
The wrapper-level `Connection::getWrappedConnection()` method has been removed. The `Connection::connect()` method
has been made `protected` and now must return the underlying driver-level connection.

## BC BREAK: Added `getNativeConnection()` to driver connections and removed old accessors

Driver and middleware connections must implement `getNativeConnection()` now. This new method replaces several accessors
that have been removed:

* `Doctrine\DBAL\Driver\PDO\Connection::getWrappedConnection()`
* `Doctrine\DBAL\Driver\PDO\SQLSrv\Connection::getWrappedConnection()`
* `Doctrine\DBAL\Driver\Mysqli\Connection::getWrappedResourceHandle()`

## BC BREAK: Removed `SQLLogger` and its implementations.

The `SQLLogger` interface and its implementations `DebugStack` and `LoggerChain` have been removed.
Expand Down
4 changes: 0 additions & 4 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ parameters:
paths:
- src/Driver/Mysqli/Result.php

# Type check for legacy implementations of the Connection interface
# TODO: remove in 4.0.0
- "~Call to function method_exists\\(\\) with Doctrine\\\\DBAL\\\\Driver\\\\Connection and 'getNativeConnection' will always evaluate to true\\.~"

# These properties are accessed via var_dump()
-
message: '~Property Doctrine\\DBAL\\Tests\\Tools\\TestAsset\\.*::\$.* is never read, only written\.~'
Expand Down
3 changes: 0 additions & 3 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@
See https://github.com/doctrine/dbal/pull/4317
-->
<file name="tests/Functional/LegacyAPITest.php"/>

<!-- TODO: remove in 4.0.0 -->
<referencedMethod name="Doctrine\DBAL\Driver\PDO\Connection::getWrappedConnection"/>
</errorLevel>
</DeprecatedMethod>
<DocblockTypeContradiction>
Expand Down
17 changes: 3 additions & 14 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,15 @@
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\SQL\Parser;
use Doctrine\DBAL\Types\Type;
use LogicException;
use Throwable;
use Traversable;

use function assert;
use function count;
use function get_class;
use function implode;
use function is_int;
use function is_string;
use function key;
use function method_exists;
use function sprintf;

/**
* A database abstraction-level connection that implements features like events, transaction isolation levels,
Expand Down Expand Up @@ -1242,22 +1238,15 @@ public function rollbackSavepoint(string $savepoint): void
}

/**
* Provides access to the native database connection.
*
* @return resource|object
*
* @throws Exception
*/
public function getNativeConnection()
{
$connection = $this->connect();

if (! method_exists($connection, 'getNativeConnection')) {
throw new LogicException(sprintf(
'The driver connection %s does not support accessing the native connection.',
get_class($connection)
));
}

return $connection->getNativeConnection();
return $this->connect()->getNativeConnection();
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/Driver/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
/**
* Connection interface.
* Driver connections must implement this interface.
*
* @method resource|object getNativeConnection()
*/
interface Connection extends ServerVersionProvider
{
Expand Down Expand Up @@ -83,4 +81,11 @@ public function commit(): void;
* @throws Exception
*/
public function rollBack(): void;

/**
* Provides access to the native database connection.
*
* @return resource|object
*/
public function getNativeConnection();
}
14 changes: 1 addition & 13 deletions src/Driver/Middleware/AbstractConnectionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement;
use LogicException;

use function get_class;
use function method_exists;
use function sprintf;

abstract class AbstractConnectionMiddleware implements Connection
{
Expand Down Expand Up @@ -71,17 +66,10 @@ public function getServerVersion(): string
}

/**
* @return resource|object
* {@inheritdoc}
*/
public function getNativeConnection()
{
if (! method_exists($this->wrappedConnection, 'getNativeConnection')) {
throw new LogicException(sprintf(
'The driver connection %s does not support accessing the native connection.',
get_class($this->wrappedConnection)
));
}

return $this->wrappedConnection->getNativeConnection();
}
}
20 changes: 0 additions & 20 deletions src/Driver/Mysqli/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionError;
use Doctrine\Deprecations\Deprecation;
use mysqli;
use mysqli_sql_exception;

Expand All @@ -31,25 +30,6 @@ public function __construct(mysqli $connection)
$this->connection = $connection;
}

/**
* Retrieves mysqli native resource handle.
*
* Could be used if part of your application is not using DBAL.
*
* @deprecated Call {@see getNativeConnection()} instead.
*/
public function getWrappedResourceHandle(): mysqli
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/5037',
'%s is deprecated, call getNativeConnection() instead.',
__METHOD__
);

return $this->getNativeConnection();
}

/**
* {@inheritdoc}
*
Expand Down
16 changes: 0 additions & 16 deletions src/Driver/PDO/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\Exception\IdentityColumnsNotSupported;
use Doctrine\DBAL\Driver\Exception\NoIdentityValue;
use Doctrine\Deprecations\Deprecation;
use PDO;
use PDOException;
use PDOStatement;
Expand Down Expand Up @@ -135,19 +134,4 @@ public function getNativeConnection(): PDO
{
return $this->connection;
}

/**
* @deprecated Call {@see getNativeConnection()} instead.
*/
public function getWrappedConnection(): PDO
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/5037',
'%s is deprecated, call getNativeConnection() instead.',
__METHOD__
);

return $this->getNativeConnection();
}
}
16 changes: 0 additions & 16 deletions src/Driver/PDO/SQLSrv/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection;
use Doctrine\DBAL\Driver\PDO\Result;
use Doctrine\Deprecations\Deprecation;
use PDO;

final class Connection implements ConnectionInterface
Expand Down Expand Up @@ -73,19 +72,4 @@ public function getNativeConnection(): PDO
{
return $this->connection->getNativeConnection();
}

/**
* @deprecated Call {@see getNativeConnection()} instead.
*/
public function getWrappedConnection(): PDO
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/5037',
'%s is deprecated, call getNativeConnection() instead.',
__METHOD__
);

return $this->connection->getWrappedConnection();
}
}
11 changes: 1 addition & 10 deletions src/Portability/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
use LogicException;
use PDO;

use function method_exists;

use const CASE_LOWER;
use const CASE_UPPER;

Expand Down Expand Up @@ -45,13 +42,7 @@ public function connect(array $params): ConnectionInterface
$case = 0;

if ($this->case !== 0 && ($portability & Connection::PORTABILITY_FIX_CASE) !== 0) {
$nativeConnection = null;
if (method_exists($connection, 'getNativeConnection')) {
try {
$nativeConnection = $connection->getNativeConnection();
} catch (LogicException $e) {
}
}
$nativeConnection = $connection->getNativeConnection();

if ($nativeConnection instanceof PDO) {
$portability &= ~Connection::PORTABILITY_FIX_CASE;
Expand Down
20 changes: 1 addition & 19 deletions tests/Driver/Middleware/AbstractConnectionMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement;
use LogicException;
use PHPUnit\Framework\TestCase;

final class AbstractConnectionMiddlewareTest extends TestCase
Expand Down Expand Up @@ -66,33 +65,16 @@ public function testGetNativeConnection(): void
$nativeConnection = new class () {
};

$connection = $this->createMock(NativeDriverConnection::class);
$connection = $this->createMock(Connection::class);
$connection->method('getNativeConnection')
->willReturn($nativeConnection);

self::assertSame($nativeConnection, $this->createMiddleware($connection)->getNativeConnection());
}

public function testGetNativeConnectionFailsWithLegacyConnection(): void
{
$connection = $this->createMock(Connection::class);
$middleware = $this->createMiddleware($connection);

$this->expectException(LogicException::class);
$middleware->getNativeConnection();
}

private function createMiddleware(Connection $connection): AbstractConnectionMiddleware
{
return new class ($connection) extends AbstractConnectionMiddleware {
};
}
}

interface NativeDriverConnection extends Connection
{
/**
* @return object|resource
*/
public function getNativeConnection();
}
10 changes: 1 addition & 9 deletions tests/Portability/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testGetNativeConnection(): void
$nativeConnection = new class () {
};

$driverConnection = $this->createMock(NativeDriverConnection::class);
$driverConnection = $this->createMock(ConnectionInterface::class);
$driverConnection->method('getNativeConnection')
->willReturn($nativeConnection);

Expand All @@ -37,11 +37,3 @@ public function testGetNativeConnection(): void
self::assertSame($nativeConnection, $connection->getNativeConnection());
}
}

interface NativeDriverConnection extends ConnectionInterface
{
/**
* @return object|resource
*/
public function getNativeConnection();
}