Skip to content

Commit cd7da68

Browse files
Leverage PHP 8.4 PDO classes, fix PHP 8.5 deprecation (#7132)
| Q | A |------------- | ----------- | Type | bug | Fixed issues | - https://wiki.php.net/rfc/pdo_driver_specific_subclasses https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_driver_specific_pdo_constants_and_methods Co-authored-by: Alexander M. Turek <[email protected]>
1 parent df919f8 commit cd7da68

File tree

7 files changed

+99
-12
lines changed

7 files changed

+99
-12
lines changed

src/Driver/PDO/MySQL/Driver.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
66
use Doctrine\DBAL\Driver\PDO\Connection;
77
use Doctrine\DBAL\Driver\PDO\Exception;
8+
use Doctrine\DBAL\Driver\PDO\PDOConnect;
89
use PDO;
910
use PDOException;
1011
use SensitiveParameter;
1112

1213
final class Driver extends AbstractMySQLDriver
1314
{
15+
use PDOConnect;
16+
1417
/**
1518
* {@inheritDoc}
1619
*
@@ -30,7 +33,7 @@ public function connect(
3033
unset($safeParams['password'], $safeParams['url']);
3134

3235
try {
33-
$pdo = new PDO(
36+
$pdo = $this->doConnect(
3437
$this->constructPdoDsn($safeParams),
3538
$params['user'] ?? '',
3639
$params['password'] ?? '',

src/Driver/PDO/OCI/Driver.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
use Doctrine\DBAL\Driver\AbstractOracleDriver;
66
use Doctrine\DBAL\Driver\PDO\Connection;
77
use Doctrine\DBAL\Driver\PDO\Exception;
8+
use Doctrine\DBAL\Driver\PDO\PDOConnect;
89
use PDO;
910
use PDOException;
1011
use SensitiveParameter;
1112

1213
final class Driver extends AbstractOracleDriver
1314
{
15+
use PDOConnect;
16+
1417
/**
1518
* {@inheritDoc}
1619
*
@@ -30,7 +33,7 @@ public function connect(
3033
unset($safeParams['password'], $safeParams['url']);
3134

3235
try {
33-
$pdo = new PDO(
36+
$pdo = $this->doConnect(
3437
$this->constructPdoDsn($params),
3538
$params['user'] ?? '',
3639
$params['password'] ?? '',

src/Driver/PDO/PDOConnect.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
/** @param array<int, mixed> $options */
15+
private function doConnect(
16+
string $dsn,
17+
string $username,
18+
string $password,
19+
array $options
20+
): PDO {
21+
if (PHP_VERSION_ID < 80400) {
22+
return new PDO($dsn, $username, $password, $options);
23+
}
24+
25+
return PDO::connect($dsn, $username, $password, $options);
26+
}
27+
}

src/Driver/PDO/PgSQL/Driver.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
66
use Doctrine\DBAL\Driver\PDO\Connection;
77
use Doctrine\DBAL\Driver\PDO\Exception;
8+
use Doctrine\DBAL\Driver\PDO\PDOConnect;
89
use Doctrine\Deprecations\Deprecation;
910
use PDO;
11+
use Pdo\Pgsql;
1012
use PDOException;
1113
use SensitiveParameter;
1214

15+
use const PHP_VERSION_ID;
16+
1317
final class Driver extends AbstractPostgreSQLDriver
1418
{
19+
use PDOConnect;
20+
1521
/**
1622
* {@inheritDoc}
1723
*
@@ -31,7 +37,7 @@ public function connect(
3137
unset($safeParams['password'], $safeParams['url']);
3238

3339
try {
34-
$pdo = new PDO(
40+
$pdo = $this->doConnect(
3541
$this->constructPdoDsn($safeParams),
3642
$params['user'] ?? '',
3743
$params['password'] ?? '',
@@ -41,11 +47,11 @@ public function connect(
4147
throw Exception::new($exception);
4248
}
4349

44-
if (
45-
! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES])
46-
|| $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
47-
) {
48-
$pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
50+
$disablePreparesAttr = PHP_VERSION_ID >= 80400
51+
? Pgsql::ATTR_DISABLE_PREPARES
52+
: PDO::PGSQL_ATTR_DISABLE_PREPARES;
53+
if ($driverOptions[$disablePreparesAttr] ?? true) {
54+
$pdo->setAttribute($disablePreparesAttr, true);
4955
}
5056

5157
$connection = new Connection($pdo);

src/Driver/PDO/SQLSrv/Driver.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\DBAL\Driver\Exception;
88
use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection;
99
use Doctrine\DBAL\Driver\PDO\Exception as PDOException;
10+
use Doctrine\DBAL\Driver\PDO\PDOConnect;
1011
use PDO;
1112
use SensitiveParameter;
1213

@@ -15,6 +16,8 @@
1516

1617
final class Driver extends AbstractSQLServerDriver
1718
{
19+
use PDOConnect;
20+
1821
/**
1922
* {@inheritDoc}
2023
*
@@ -44,7 +47,7 @@ public function connect(
4447
unset($safeParams['password'], $safeParams['url']);
4548

4649
try {
47-
$pdo = new PDO(
50+
$pdo = $this->doConnect(
4851
$this->constructDsn($safeParams, $dsnOptions),
4952
$params['user'] ?? '',
5053
$params['password'] ?? '',

src/Driver/PDO/SQLite/Driver.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
use Doctrine\DBAL\Driver\API\SQLite\UserDefinedFunctions;
77
use Doctrine\DBAL\Driver\PDO\Connection;
88
use Doctrine\DBAL\Driver\PDO\Exception;
9+
use Doctrine\DBAL\Driver\PDO\PDOConnect;
910
use Doctrine\Deprecations\Deprecation;
10-
use PDO;
11+
use Pdo\Sqlite;
1112
use PDOException;
1213
use SensitiveParameter;
1314

1415
use function array_intersect_key;
1516

1617
final class Driver extends AbstractSQLiteDriver
1718
{
19+
use PDOConnect;
20+
1821
/**
1922
* {@inheritDoc}
2023
*
@@ -40,7 +43,7 @@ public function connect(
4043
}
4144

4245
try {
43-
$pdo = new PDO(
46+
$pdo = $this->doConnect(
4447
$this->constructPdoDsn(array_intersect_key($params, ['path' => true, 'memory' => true])),
4548
$params['user'] ?? '',
4649
$params['password'] ?? '',
@@ -51,7 +54,7 @@ public function connect(
5154
}
5255

5356
UserDefinedFunctions::register(
54-
[$pdo, 'sqliteCreateFunction'],
57+
$pdo instanceof Sqlite ? [$pdo, 'createFunction'] : [$pdo, 'sqliteCreateFunction'],
5558
$userDefinedFunctions,
5659
);
5760

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Pgsql;
11+
use Pdo\Sqlite;
12+
13+
/** @requires PHP 8.4 */
14+
final class PDOSubclassTest extends FunctionalTestCase
15+
{
16+
public function testMySQLSubclass(): void
17+
{
18+
if (! TestUtil::isDriverOneOf('pdo_mysql')) {
19+
self::markTestSkipped('This test requires the pdo_mysql driver.');
20+
}
21+
22+
self::assertInstanceOf(Mysql::class, $this->connection->getNativeConnection());
23+
}
24+
25+
public function testPgSQLSubclass(): void
26+
{
27+
if (! TestUtil::isDriverOneOf('pdo_pgsql')) {
28+
self::markTestSkipped('This test requires the pdo_pgsql driver.');
29+
}
30+
31+
self::assertInstanceOf(Pgsql::class, $this->connection->getNativeConnection());
32+
}
33+
34+
public function testSQLiteSubclass(): void
35+
{
36+
if (! TestUtil::isDriverOneOf('pdo_sqlite')) {
37+
self::markTestSkipped('This test requires the pdo_sqlite driver.');
38+
}
39+
40+
self::assertInstanceOf(Sqlite::class, $this->connection->getNativeConnection());
41+
}
42+
}

0 commit comments

Comments
 (0)