Skip to content

Commit 2e8db90

Browse files
committed
Leverage the new PDO subclasses
1 parent e0f3674 commit 2e8db90

File tree

8 files changed

+114
-6
lines changed

8 files changed

+114
-6
lines changed

UPGRADE.md

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ awareness about deprecated code.
88

99
# Upgrade to 4.2
1010

11+
## Support for new PDO subclasses on PHP 8.4
12+
13+
On PHP 8.4, if you call `getNativeConnection()` on a connection established through one of the PDO drivers,
14+
you will get an instance of the new PDO subclasses, e.g. `Pdo\Mysql` or `Pdo\Ppgsql` instead of just `PDO`.
15+
1116
## Minor BC break: incompatible query cache format
1217

1318
The query cache format has been changed to address the issue where a cached result with no rows would miss the metadata.

src/Driver/PDO/MySQL/Driver.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\DBAL\Driver\PDO\Connection;
99
use Doctrine\DBAL\Driver\PDO\Exception;
1010
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
11+
use Doctrine\DBAL\Driver\PDO\PDOConnect;
1112
use PDO;
1213
use PDOException;
1314
use SensitiveParameter;
@@ -16,6 +17,8 @@
1617

1718
final class Driver extends AbstractMySQLDriver
1819
{
20+
use PDOConnect;
21+
1922
/**
2023
* {@inheritDoc}
2124
*/
@@ -39,7 +42,7 @@ public function connect(
3942
unset($safeParams['password']);
4043

4144
try {
42-
$pdo = new PDO(
45+
$pdo = $this->doConnect(
4346
$this->constructPdoDsn($safeParams),
4447
$params['user'] ?? '',
4548
$params['password'] ?? '',

src/Driver/PDO/OCI/Driver.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\DBAL\Driver\PDO\Connection;
99
use Doctrine\DBAL\Driver\PDO\Exception;
1010
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
11+
use Doctrine\DBAL\Driver\PDO\PDOConnect;
1112
use PDO;
1213
use PDOException;
1314
use SensitiveParameter;
@@ -16,6 +17,8 @@
1617

1718
final class Driver extends AbstractOracleDriver
1819
{
20+
use PDOConnect;
21+
1922
/**
2023
* {@inheritDoc}
2124
*/
@@ -39,7 +42,7 @@ public function connect(
3942
unset($safeParams['password']);
4043

4144
try {
42-
$pdo = new PDO(
45+
$pdo = $this->doConnect(
4346
$this->constructPdoDsn($params),
4447
$params['user'] ?? '',
4548
$params['password'] ?? '',

src/Driver/PDO/PDOConnect.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Driver\PDO;
6+
7+
use PDO;
8+
9+
use const PHP_VERSION_ID;
10+
11+
/** @internal */
12+
trait PDOConnect
13+
{
14+
private function doConnect(
15+
string $dsn,
16+
string|null $username = null,
17+
string|null $password = null,
18+
array|null $options = null,
19+
): PDO {
20+
if (PHP_VERSION_ID < 80400) {
21+
return new PDO($dsn, $username, $password, $options);
22+
}
23+
24+
return PDO::connect($dsn, $username, $password, $options);
25+
}
26+
}

src/Driver/PDO/PgSQL/Driver.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\DBAL\Driver\PDO\Connection;
99
use Doctrine\DBAL\Driver\PDO\Exception;
1010
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
11+
use Doctrine\DBAL\Driver\PDO\PDOConnect;
1112
use PDO;
1213
use PDOException;
1314
use SensitiveParameter;
@@ -16,6 +17,8 @@
1617

1718
final class Driver extends AbstractPostgreSQLDriver
1819
{
20+
use PDOConnect;
21+
1922
/**
2023
* {@inheritDoc}
2124
*/
@@ -39,7 +42,7 @@ public function connect(
3942
unset($safeParams['password']);
4043

4144
try {
42-
$pdo = new PDO(
45+
$pdo = $this->doConnect(
4346
$this->constructPdoDsn($safeParams),
4447
$params['user'] ?? '',
4548
$params['password'] ?? '',

src/Driver/PDO/SQLSrv/Driver.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection;
1111
use Doctrine\DBAL\Driver\PDO\Exception as PDOException;
1212
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
13+
use Doctrine\DBAL\Driver\PDO\PDOConnect;
1314
use PDO;
1415
use SensitiveParameter;
1516

@@ -19,6 +20,8 @@
1920

2021
final class Driver extends AbstractSQLServerDriver
2122
{
23+
use PDOConnect;
24+
2225
/**
2326
* {@inheritDoc}
2427
*/
@@ -52,7 +55,7 @@ public function connect(
5255
unset($safeParams['password']);
5356

5457
try {
55-
$pdo = new PDO(
58+
$pdo = $this->doConnect(
5659
$this->constructDsn($safeParams, $dsnOptions),
5760
$params['user'] ?? '',
5861
$params['password'] ?? '',

src/Driver/PDO/SQLite/Driver.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Doctrine\DBAL\Driver\PDO\Connection;
99
use Doctrine\DBAL\Driver\PDO\Exception;
1010
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
11-
use PDO;
11+
use Doctrine\DBAL\Driver\PDO\PDOConnect;
1212
use PDOException;
1313
use SensitiveParameter;
1414

@@ -17,6 +17,8 @@
1717

1818
final class Driver extends AbstractSQLiteDriver
1919
{
20+
use PDOConnect;
21+
2022
/**
2123
* {@inheritDoc}
2224
*/
@@ -31,7 +33,7 @@ public function connect(
3133
}
3234

3335
try {
34-
$pdo = new PDO(
36+
$pdo = $this->doConnect(
3537
$this->constructPdoDsn(array_intersect_key($params, ['path' => true, 'memory' => true])),
3638
$params['user'] ?? '',
3739
$params['password'] ?? '',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Tests\Functional\Driver\PDO;
6+
7+
use Doctrine\DBAL\Tests\FunctionalTestCase;
8+
use Doctrine\DBAL\Tests\TestUtil;
9+
use Pdo\Mysql;
10+
use Pdo\Oci;
11+
use Pdo\Pgsql;
12+
use Pdo\Sqlite;
13+
use Pdo\Sqlsrv;
14+
use PHPUnit\Framework\Attributes\RequiresPhp;
15+
16+
#[RequiresPhp('8.4')]
17+
final class PDOSubclassTest extends FunctionalTestCase
18+
{
19+
public function testMySQLSubclass(): void
20+
{
21+
if (! TestUtil::isDriverOneOf('pdo_mysql')) {
22+
self::markTestSkipped('This test requires the pdo_mysql driver.');
23+
}
24+
25+
self::assertInstanceOf(Mysql::class, $this->connection->getNativeConnection());
26+
}
27+
28+
public function testOCISubclass(): void
29+
{
30+
if (! TestUtil::isDriverOneOf('pdo_oci')) {
31+
self::markTestSkipped('This test requires the pdo_oci driver.');
32+
}
33+
34+
self::assertInstanceOf(Oci::class, $this->connection->getNativeConnection());
35+
}
36+
37+
public function testPgSQLSubclass(): void
38+
{
39+
if (! TestUtil::isDriverOneOf('pdo_pgsql')) {
40+
self::markTestSkipped('This test requires the pdo_pgsql driver.');
41+
}
42+
43+
self::assertInstanceOf(Pgsql::class, $this->connection->getNativeConnection());
44+
}
45+
46+
public function testSQLiteSubclass(): void
47+
{
48+
if (! TestUtil::isDriverOneOf('pdo_sqlite')) {
49+
self::markTestSkipped('This test requires the pdo_sqlite driver.');
50+
}
51+
52+
self::assertInstanceOf(Sqlite::class, $this->connection->getNativeConnection());
53+
}
54+
55+
public function testSQLSrvSubclass(): void
56+
{
57+
if (! TestUtil::isDriverOneOf('pdo_sqlsrv')) {
58+
self::markTestSkipped('This test requires the pdo_sqlsrv driver.');
59+
}
60+
61+
self::assertInstanceOf(Sqlsrv::class, $this->connection->getNativeConnection());
62+
}
63+
}

0 commit comments

Comments
 (0)