Skip to content

Commit

Permalink
Leverage the new PDO subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Oct 10, 2024
1 parent e0f3674 commit 2e8db90
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 6 deletions.
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ awareness about deprecated code.

# Upgrade to 4.2

## Support for new PDO subclasses on PHP 8.4

On PHP 8.4, if you call `getNativeConnection()` on a connection established through one of the PDO drivers,
you will get an instance of the new PDO subclasses, e.g. `Pdo\Mysql` or `Pdo\Ppgsql` instead of just `PDO`.

## Minor BC break: incompatible query cache format

The query cache format has been changed to address the issue where a cached result with no rows would miss the metadata.
Expand Down
5 changes: 4 additions & 1 deletion src/Driver/PDO/MySQL/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\DBAL\Driver\PDO\Connection;
use Doctrine\DBAL\Driver\PDO\Exception;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use Doctrine\DBAL\Driver\PDO\PDOConnect;
use PDO;
use PDOException;
use SensitiveParameter;
Expand All @@ -16,6 +17,8 @@

final class Driver extends AbstractMySQLDriver
{
use PDOConnect;

/**
* {@inheritDoc}
*/
Expand All @@ -39,7 +42,7 @@ public function connect(
unset($safeParams['password']);

try {
$pdo = new PDO(
$pdo = $this->doConnect(
$this->constructPdoDsn($safeParams),
$params['user'] ?? '',
$params['password'] ?? '',
Expand Down
5 changes: 4 additions & 1 deletion src/Driver/PDO/OCI/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\DBAL\Driver\PDO\Connection;
use Doctrine\DBAL\Driver\PDO\Exception;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use Doctrine\DBAL\Driver\PDO\PDOConnect;
use PDO;
use PDOException;
use SensitiveParameter;
Expand All @@ -16,6 +17,8 @@

final class Driver extends AbstractOracleDriver
{
use PDOConnect;

/**
* {@inheritDoc}
*/
Expand All @@ -39,7 +42,7 @@ public function connect(
unset($safeParams['password']);

try {
$pdo = new PDO(
$pdo = $this->doConnect(
$this->constructPdoDsn($params),
$params['user'] ?? '',
$params['password'] ?? '',
Expand Down
26 changes: 26 additions & 0 deletions src/Driver/PDO/PDOConnect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Driver\PDO;

use PDO;

use const PHP_VERSION_ID;

/** @internal */
trait PDOConnect
{
private function doConnect(

Check failure on line 14 in src/Driver/PDO/PDOConnect.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Method Doctrine\DBAL\Driver\PDO\MySQL\Driver::doConnect() has parameter $options with no value type specified in iterable type array.

Check failure on line 14 in src/Driver/PDO/PDOConnect.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Method Doctrine\DBAL\Driver\PDO\OCI\Driver::doConnect() has parameter $options with no value type specified in iterable type array.

Check failure on line 14 in src/Driver/PDO/PDOConnect.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Method Doctrine\DBAL\Driver\PDO\PgSQL\Driver::doConnect() has parameter $options with no value type specified in iterable type array.

Check failure on line 14 in src/Driver/PDO/PDOConnect.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Method Doctrine\DBAL\Driver\PDO\SQLSrv\Driver::doConnect() has parameter $options with no value type specified in iterable type array.

Check failure on line 14 in src/Driver/PDO/PDOConnect.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Method Doctrine\DBAL\Driver\PDO\SQLite\Driver::doConnect() has parameter $options with no value type specified in iterable type array.
string $dsn,
string|null $username = null,
string|null $password = null,
array|null $options = null,
): PDO {
if (PHP_VERSION_ID < 80400) {
return new PDO($dsn, $username, $password, $options);
}

return PDO::connect($dsn, $username, $password, $options);

Check failure on line 24 in src/Driver/PDO/PDOConnect.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.3)

UndefinedMethod

src/Driver/PDO/PDOConnect.php:24:16: UndefinedMethod: Method PDO::connect does not exist (see https://psalm.dev/022)

Check failure on line 24 in src/Driver/PDO/PDOConnect.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Call to an undefined static method PDO::connect().

Check failure on line 24 in src/Driver/PDO/PDOConnect.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Call to an undefined static method PDO::connect().

Check failure on line 24 in src/Driver/PDO/PDOConnect.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Call to an undefined static method PDO::connect().

Check failure on line 24 in src/Driver/PDO/PDOConnect.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Call to an undefined static method PDO::connect().

Check failure on line 24 in src/Driver/PDO/PDOConnect.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Call to an undefined static method PDO::connect().
}
}
5 changes: 4 additions & 1 deletion src/Driver/PDO/PgSQL/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\DBAL\Driver\PDO\Connection;
use Doctrine\DBAL\Driver\PDO\Exception;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use Doctrine\DBAL\Driver\PDO\PDOConnect;
use PDO;
use PDOException;
use SensitiveParameter;
Expand All @@ -16,6 +17,8 @@

final class Driver extends AbstractPostgreSQLDriver
{
use PDOConnect;

/**
* {@inheritDoc}
*/
Expand All @@ -39,7 +42,7 @@ public function connect(
unset($safeParams['password']);

try {
$pdo = new PDO(
$pdo = $this->doConnect(
$this->constructPdoDsn($safeParams),
$params['user'] ?? '',
$params['password'] ?? '',
Expand Down
5 changes: 4 additions & 1 deletion src/Driver/PDO/SQLSrv/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection;
use Doctrine\DBAL\Driver\PDO\Exception as PDOException;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use Doctrine\DBAL\Driver\PDO\PDOConnect;
use PDO;
use SensitiveParameter;

Expand All @@ -19,6 +20,8 @@

final class Driver extends AbstractSQLServerDriver
{
use PDOConnect;

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -52,7 +55,7 @@ public function connect(
unset($safeParams['password']);

try {
$pdo = new PDO(
$pdo = $this->doConnect(
$this->constructDsn($safeParams, $dsnOptions),
$params['user'] ?? '',
$params['password'] ?? '',
Expand Down
6 changes: 4 additions & 2 deletions src/Driver/PDO/SQLite/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Doctrine\DBAL\Driver\PDO\Connection;
use Doctrine\DBAL\Driver\PDO\Exception;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use PDO;
use Doctrine\DBAL\Driver\PDO\PDOConnect;
use PDOException;
use SensitiveParameter;

Expand All @@ -17,6 +17,8 @@

final class Driver extends AbstractSQLiteDriver
{
use PDOConnect;

/**
* {@inheritDoc}
*/
Expand All @@ -31,7 +33,7 @@ public function connect(
}

try {
$pdo = new PDO(
$pdo = $this->doConnect(
$this->constructPdoDsn(array_intersect_key($params, ['path' => true, 'memory' => true])),
$params['user'] ?? '',
$params['password'] ?? '',
Expand Down
63 changes: 63 additions & 0 deletions tests/Functional/Driver/PDO/PDOSubclassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional\Driver\PDO;

use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use Pdo\Mysql;
use Pdo\Oci;
use Pdo\Pgsql;
use Pdo\Sqlite;
use Pdo\Sqlsrv;
use PHPUnit\Framework\Attributes\RequiresPhp;

#[RequiresPhp('8.4')]
final class PDOSubclassTest extends FunctionalTestCase
{
public function testMySQLSubclass(): void
{
if (! TestUtil::isDriverOneOf('pdo_mysql')) {
self::markTestSkipped('This test requires the pdo_mysql driver.');
}

self::assertInstanceOf(Mysql::class, $this->connection->getNativeConnection());

Check failure on line 25 in tests/Functional/Driver/PDO/PDOSubclassTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.3)

UndefinedClass

tests/Functional/Driver/PDO/PDOSubclassTest.php:25:32: UndefinedClass: Class, interface or enum named Pdo\Mysql does not exist (see https://psalm.dev/019)
}

public function testOCISubclass(): void
{
if (! TestUtil::isDriverOneOf('pdo_oci')) {
self::markTestSkipped('This test requires the pdo_oci driver.');
}

self::assertInstanceOf(Oci::class, $this->connection->getNativeConnection());

Check failure on line 34 in tests/Functional/Driver/PDO/PDOSubclassTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.3)

UndefinedClass

tests/Functional/Driver/PDO/PDOSubclassTest.php:34:32: UndefinedClass: Class, interface or enum named Pdo\Oci does not exist (see https://psalm.dev/019)
}

public function testPgSQLSubclass(): void
{
if (! TestUtil::isDriverOneOf('pdo_pgsql')) {
self::markTestSkipped('This test requires the pdo_pgsql driver.');
}

self::assertInstanceOf(Pgsql::class, $this->connection->getNativeConnection());

Check failure on line 43 in tests/Functional/Driver/PDO/PDOSubclassTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.3)

UndefinedClass

tests/Functional/Driver/PDO/PDOSubclassTest.php:43:32: UndefinedClass: Class, interface or enum named Pdo\Pgsql does not exist (see https://psalm.dev/019)
}

public function testSQLiteSubclass(): void
{
if (! TestUtil::isDriverOneOf('pdo_sqlite')) {
self::markTestSkipped('This test requires the pdo_sqlite driver.');
}

self::assertInstanceOf(Sqlite::class, $this->connection->getNativeConnection());

Check failure on line 52 in tests/Functional/Driver/PDO/PDOSubclassTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.3)

UndefinedClass

tests/Functional/Driver/PDO/PDOSubclassTest.php:52:32: UndefinedClass: Class, interface or enum named Pdo\Sqlite does not exist (see https://psalm.dev/019)
}

public function testSQLSrvSubclass(): void
{
if (! TestUtil::isDriverOneOf('pdo_sqlsrv')) {
self::markTestSkipped('This test requires the pdo_sqlsrv driver.');
}

self::assertInstanceOf(Sqlsrv::class, $this->connection->getNativeConnection());

Check failure on line 61 in tests/Functional/Driver/PDO/PDOSubclassTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.3)

UndefinedClass

tests/Functional/Driver/PDO/PDOSubclassTest.php:61:32: UndefinedClass: Class, interface or enum named Pdo\Sqlsrv does not exist (see https://psalm.dev/019)
}
}

0 comments on commit 2e8db90

Please sign in to comment.