Skip to content

Commit 6446c5a

Browse files
committed
Implement PDO::connect()
Override PDO::connect() on PHP 8.4 and newer while loading an empty compatibility trait on older supported runtimes.
1 parent 6939616 commit 6446c5a

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,24 @@
1010
*
1111
* PDO uses $string as a parameter name, enable it:
1212
* phpcs:disable Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
13+
*
14+
* We conditionally define a trait for PHP-version-specific PDO methods:
15+
* phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
1316
*/
1417

18+
/*
19+
* The "PDO::connect()" method in PHP 8.4 uses a "static" return type declaration,
20+
* which PHP 7 cannot parse. Therefore, a conditional file import is needed.
21+
*/
22+
if ( PHP_VERSION_ID >= 80400 ) {
23+
require_once __DIR__ . '/trait-wp-mysql-on-sqlite-pdo-compat-php-84.php';
24+
} else {
25+
/**
26+
* @access private
27+
*/
28+
trait WP_MySQL_On_SQLite_PDO_Compat {}
29+
}
30+
1531
/**
1632
* SQLite driver for MySQL.
1733
*
@@ -22,6 +38,8 @@
2238
* The driver requires PDO with the SQLite driver, and the PCRE engine.
2339
*/
2440
class WP_MySQL_On_SQLite extends PDO {
41+
use WP_MySQL_On_SQLite_PDO_Compat;
42+
2543
/**
2644
* The path to the MySQL SQL grammar file.
2745
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* The SQLite driver uses PDO. Enable PDO function calls:
5+
* phpcs:disable WordPress.DB.RestrictedClasses.mysql__PDO
6+
*/
7+
8+
/**
9+
* PDO methods introduced in PHP 8.4.
10+
*
11+
* @access private
12+
*/
13+
trait WP_MySQL_On_SQLite_PDO_Compat {
14+
/**
15+
* Create a MySQL-on-SQLite PDO connection.
16+
*
17+
* @param string $dsn MySQL-on-SQLite data source name.
18+
* @param string|null $username Optional. Ignored by this driver.
19+
* @param string|null $password Optional. Ignored by this driver.
20+
* @param array|null $options Optional PDO and driver options.
21+
* @return static The new connection.
22+
*/
23+
public static function connect(
24+
string $dsn,
25+
?string $username = null,
26+
?string $password = null,
27+
?array $options = null
28+
): static {
29+
return new static( $dsn, $username, $password, $options );
30+
}
31+
}

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ public function test_connection(): void {
3434
$this->assertInstanceOf( PDO::class, $driver );
3535
}
3636

37+
public function test_static_connect(): void {
38+
if ( PHP_VERSION_ID < 80400 ) {
39+
$this->markTestSkipped( 'PDO::connect() requires PHP 8.4 or newer.' );
40+
}
41+
42+
$driver = WP_MySQL_On_SQLite::connect(
43+
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
44+
null,
45+
null,
46+
array( PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC )
47+
);
48+
49+
$this->assertInstanceOf( WP_MySQL_On_SQLite::class, $driver );
50+
$this->assertSame( array( 'value' => 1 ), $driver->query( 'SELECT 1 AS value' )->fetch() );
51+
}
52+
3753
public function test_constructor_accepts_null_options(): void {
3854
$driver = new WP_MySQL_On_SQLite(
3955
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',

0 commit comments

Comments
 (0)