Skip to content
This repository was archived by the owner on Sep 11, 2026. It is now read-only.

Commit 2f49fd5

Browse files
authored
Simplify the MySQL-on-SQLite driver API (WordPress#449)
## Summary Use the PDO-compatible `WP_MySQL_On_SQLite` class directly in both the WordPress plugin and MySQL proxy, while retaining `WP_SQLite_Driver` temporarily for backward compatibility. This includes: - Rename `WP_PDO_MySQL_On_SQLite` to `WP_MySQL_On_SQLite` and align implementation, test, tooling, and CI. - Adapt legacy SQL behavior tests and production consumers to the `PDOStatement` query API. This should be reviewed and merged together with Automattic/wp-cli-sqlite-command#33. ## Motivation Using the PDO-compatible driver directly makes it the primary public entry point with a clear, PDO-defined API. The legacy `WP_SQLite_Driver` constructor and result API remain available only as a temporary backward-compatibility layer. ## Developer impact The `WP_SQLite_Driver` API remains available for backward compatibility, but new consumers should construct `WP_MySQL_On_SQLite` with its DSN and options, and consume `query()` results as `PDOStatement` objects. Consumers using `WP_PDO_MySQL_On_SQLite` must update the class name to `WP_MySQL_On_SQLite`.
1 parent 11c2143 commit 2f49fd5

22 files changed

Lines changed: 567 additions & 411 deletions

‎.github/workflows/wp-tests-phpunit-native-extension-setup.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ if ( ! ( $parser_ast instanceof WP_MySQL_Native_Parser_Node ) ) {
149149
wp_sqlite_native_parser_verification_fail( 'Native parser did not produce a native-backed AST in the WordPress PHP test container.' );
150150
}
151151
152-
$driver = new WP_PDO_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp;' );
152+
$driver = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp;' );
153153
$parser = $driver->create_parser( 'SELECT 1' );
154154
wp_sqlite_assert_native_parser_delegate( $parser, 'WordPress PHP test container SQLite driver did not create a native parser delegate.' );
155155
$parser->next_query();

‎packages/mysql-on-sqlite/src/load.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@
4141
require_once __DIR__ . '/sqlite/class-wp-sqlite-information-schema-exception.php';
4242
require_once __DIR__ . '/sqlite/class-wp-sqlite-information-schema-reconstructor.php';
4343
require_once __DIR__ . '/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
44-
require_once __DIR__ . '/sqlite/class-wp-pdo-mysql-on-sqlite.php';
44+
require_once __DIR__ . '/sqlite/class-wp-mysql-on-sqlite.php';
4545
require_once __DIR__ . '/sqlite/class-wp-pdo-proxy-statement.php';

packages/mysql-on-sqlite/src/sqlite/class-wp-pdo-mysql-on-sqlite.php renamed to packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* The driver requires PDO with the SQLite driver, and the PCRE engine.
1616
*/
17-
class WP_PDO_MySQL_On_SQLite extends PDO {
17+
class WP_MySQL_On_SQLite extends PDO {
1818
/**
1919
* The path to the MySQL SQL grammar file.
2020
*/
@@ -632,8 +632,17 @@ class WP_PDO_MySQL_On_SQLite extends PDO {
632632
*
633633
* Set up an SQLite connection and the MySQL-on-SQLite driver.
634634
*
635-
* @param WP_SQLite_Connection $connection A SQLite database connection.
636-
* @param string $db_name The database name.
635+
* @param string $dsn MySQL-on-SQLite DSN containing the SQLite path and database name.
636+
* @param string|null $username Optional. Ignored by this driver.
637+
* @param string|null $password Optional. Ignored by this driver.
638+
* @param array $options {
639+
* Optional driver options.
640+
*
641+
* @type int $mysql_version Optional. MySQL version to emulate. Default 80038.
642+
* @type PDO|null $pdo Optional. Existing SQLite PDO connection.
643+
* @type string|null $journal_mode Optional. SQLite journal mode. Default 'WAL'.
644+
* @type string|int|null $synchronous Optional. SQLite synchronous setting.
645+
* }
637646
*
638647
* @throws WP_SQLite_Driver_Exception When the driver initialization fails.
639648
*/
@@ -789,11 +798,12 @@ function ( string $sql, array $params ) {
789798
*
790799
* A single MySQL query can be translated into zero or more SQLite queries.
791800
*
792-
* @param string $query Full SQL statement string.
793-
* @param int $fetch_mode PDO fetch mode. Default is PDO::FETCH_OBJ.
794-
* @param array ...$fetch_mode_args Additional fetch mode arguments.
801+
* @param string $query Full SQL statement string.
802+
* @param int|null $fetch_mode Optional. PDO fetch mode. Defaults to the configured
803+
* PDO::ATTR_DEFAULT_FETCH_MODE.
804+
* @param mixed ...$fetch_mode_args Additional fetch mode arguments.
795805
*
796-
* @return mixed Return value, depending on the query type.
806+
* @return PDOStatement|false PDO statement, or false when the fetch mode is invalid on PHP < 8.1.
797807
*
798808
* @throws WP_SQLite_Driver_Exception When the query execution fails.
799809
*/
@@ -1013,15 +1023,6 @@ public function beginTransaction(): bool {
10131023
return true;
10141024
}
10151025

1016-
/**
1017-
* A temporary alias for back compatibility.
1018-
*
1019-
* @see self::beginTransaction()
1020-
*/
1021-
public function begin_transaction(): void {
1022-
$this->beginTransaction();
1023-
}
1024-
10251026
/**
10261027
* PDO API: Commit a transaction.
10271028
*
@@ -4211,7 +4212,7 @@ private function translate_query_expression( WP_Parser_Node $node ): string {
42114212
* When the ORDER BY clause is present, we need to disambiguate the item
42124213
* list and make sure they don't cause an "ambiguous column name" error.
42134214
*
4214-
* @see WP_SQLite_Driver::disambiguate_item()
4215+
* @see WP_MySQL_On_SQLite::disambiguate_item()
42154216
*/
42164217
$disambiguated_order_list = array();
42174218
$order_clause = $node->get_first_child_node( 'orderClause' );
@@ -4279,7 +4280,7 @@ private function translate_query_specification( WP_Parser_Node $node ): string {
42794280
* When the GROUP BY or HAVING clause is present, we need to disambiguate
42804281
* the items to ensure they don't cause an "ambiguous column name" error.
42814282
*
4282-
* @see WP_SQLite_Driver::disambiguate_item()
4283+
* @see WP_MySQL_On_SQLite::disambiguate_item()
42834284
*/
42844285
$group_by_clause = null;
42854286
$having_clause = null;
@@ -5788,7 +5789,7 @@ private function unnest_parenthesized_expression( WP_Parser_Node $node ): WP_Par
57885789
* consider column references in forms like "db.table.column".
57895790
*
57905791
* @param array $disambiguation_map The SELECT item disambiguation map (column name => array of select items).
5791-
* @see WP_SQLite_Driver::create_select_item_disambiguation_map()
5792+
* @see WP_MySQL_On_SQLite::create_select_item_disambiguation_map()
57925793
* @param WP_Parser_Node $expr The expression AST node or subnode.
57935794
* @return string|null The disambiguated and translated expression;
57945795
* null when the expression cannot be disambiguated.
@@ -5827,7 +5828,7 @@ private function disambiguate_item( array $disambiguation_map, WP_Parser_Node $e
58275828
* Create a SELECT item disambiguation map from a SELECT item list for use
58285829
* with the ORDER BY, GROUP BY, and HAVING clause disambiguation algorithm.
58295830
*
5830-
* @see WP_SQLite_Driver::disambiguate_item()
5831+
* @see WP_MySQL_On_SQLite::disambiguate_item()
58315832
*
58325833
* @param WP_Parser_Node $select_item_list The "selectItemList" AST node.
58335834
* @return array The SELECT item disambiguation map (column name => array of select items).

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class WP_SQLite_Configurator {
1414
/**
1515
* The SQLite driver instance.
1616
*
17-
* @var WP_PDO_MySQL_On_SQLite
17+
* @var WP_MySQL_On_SQLite
1818
*/
1919
private $driver;
2020

@@ -35,11 +35,11 @@ class WP_SQLite_Configurator {
3535
/**
3636
* Constructor.
3737
*
38-
* @param WP_PDO_MySQL_On_SQLite $driver The SQLite driver instance.
38+
* @param WP_MySQL_On_SQLite $driver The SQLite driver instance.
3939
* @param WP_SQLite_Information_Schema_Builder $schema_builder The information schema builder instance.
4040
*/
4141
public function __construct(
42-
WP_PDO_MySQL_On_SQLite $driver,
42+
WP_MySQL_On_SQLite $driver,
4343
WP_SQLite_Information_Schema_Builder $schema_builder
4444
) {
4545
$this->driver = $driver;
@@ -100,7 +100,7 @@ private function ensure_global_variables_table(): void {
100100
sprintf(
101101
'CREATE TABLE IF NOT EXISTS %s (name TEXT PRIMARY KEY, value TEXT)',
102102
$this->driver->get_connection()->quote_identifier(
103-
WP_PDO_MySQL_On_SQLite::GLOBAL_VARIABLES_TABLE_NAME
103+
WP_MySQL_On_SQLite::GLOBAL_VARIABLES_TABLE_NAME
104104
)
105105
)
106106
);
@@ -260,11 +260,11 @@ private function save_current_driver_version(): void {
260260
sprintf(
261261
'INSERT INTO %s (name, value) VALUES (?, ?) ON CONFLICT(name) DO UPDATE SET value = ?',
262262
$this->driver->get_connection()->quote_identifier(
263-
WP_PDO_MySQL_On_SQLite::GLOBAL_VARIABLES_TABLE_NAME
263+
WP_MySQL_On_SQLite::GLOBAL_VARIABLES_TABLE_NAME
264264
)
265265
),
266266
array(
267-
WP_PDO_MySQL_On_SQLite::DRIVER_VERSION_VARIABLE_NAME,
267+
WP_MySQL_On_SQLite::DRIVER_VERSION_VARIABLE_NAME,
268268
SQLITE_DRIVER_VERSION,
269269
SQLITE_DRIVER_VERSION,
270270
)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ class WP_SQLite_Driver_Exception extends PDOException {
44
/**
55
* The SQLite driver that originated the exception.
66
*
7-
* @var WP_PDO_MySQL_On_SQLite
7+
* @var WP_MySQL_On_SQLite
88
*/
99
private $driver;
1010

1111
/**
1212
* Constructor.
1313
*
14-
* @param WP_PDO_MySQL_On_SQLite $driver The SQLite driver that originated the exception.
14+
* @param WP_MySQL_On_SQLite $driver The SQLite driver that originated the exception.
1515
* @param string $message The exception message.
1616
* @param int|string $code The exception code. In PDO, it can be a string with value of SQLSTATE.
1717
* @param Throwable|null $previous The previous throwable used for the exception chaining.
1818
*/
1919
public function __construct(
20-
WP_PDO_MySQL_On_SQLite $driver,
20+
WP_MySQL_On_SQLite $driver,
2121
string $message,
2222
$code = 0,
2323
?Throwable $previous = null
@@ -27,7 +27,7 @@ public function __construct(
2727
$this->driver = $driver;
2828
}
2929

30-
public function getDriver(): WP_PDO_MySQL_On_SQLite {
30+
public function getDriver(): WP_MySQL_On_SQLite {
3131
return $this->driver;
3232
}
3333
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88
/**
99
* For back compatibility with dependencies that use their own loader scripts
10-
* (e.g., WP CLI SQLite Command), ensure the new PDO-based classes are loaded.
10+
* (e.g., WP CLI SQLite Command), ensure the PDO-based classes are loaded.
1111
*/
12-
require_once __DIR__ . '/class-wp-pdo-mysql-on-sqlite.php';
12+
require_once __DIR__ . '/class-wp-mysql-on-sqlite.php';
1313
require_once __DIR__ . '/class-wp-pdo-proxy-statement.php';
1414

1515
/**
16-
* Deprecated: A proxy of the WP_PDO_MySQL_On_SQLite class preserving legacy API.
16+
* Deprecated: A proxy of the WP_MySQL_On_SQLite class preserving the legacy API.
1717
*
18-
* This is a temporary class to preserve the legacy API for easier transition
19-
* to the new PDO-based API, developed in the "WP_PDO_MySQL_On_SQLite" class.
18+
* This class temporarily preserves the legacy constructor and result API while
19+
* consumers transition to the PDO-based WP_MySQL_On_SQLite API.
2020
*/
2121
class WP_SQLite_Driver {
2222
/**
@@ -38,7 +38,7 @@ class WP_SQLite_Driver {
3838
/**
3939
* The MySQL-on-SQLite driver instance.
4040
*
41-
* @var WP_PDO_MySQL_On_SQLite
41+
* @var WP_MySQL_On_SQLite
4242
*/
4343
private $mysql_on_sqlite_driver;
4444

@@ -64,7 +64,7 @@ public function __construct(
6464
string $database,
6565
int $mysql_version = 80038
6666
) {
67-
$this->mysql_on_sqlite_driver = new WP_PDO_MySQL_On_SQLite(
67+
$this->mysql_on_sqlite_driver = new WP_MySQL_On_SQLite(
6868
sprintf( 'mysql-on-sqlite:dbname=%s', $database ),
6969
null,
7070
null,
@@ -230,7 +230,7 @@ public function execute_sqlite_query( string $sql, array $params = array() ): PD
230230
* Begin a new transaction or nested transaction.
231231
*/
232232
public function beginTransaction(): void { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
233-
$this->mysql_on_sqlite_driver->begin_transaction();
233+
$this->mysql_on_sqlite_driver->beginTransaction();
234234
}
235235

236236
/**

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class WP_SQLite_Information_Schema_Reconstructor {
1919
/**
2020
* The SQLite driver instance.
2121
*
22-
* @var WP_PDO_MySQL_On_SQLite
22+
* @var WP_MySQL_On_SQLite
2323
*/
2424
private $driver;
2525

@@ -40,7 +40,7 @@ class WP_SQLite_Information_Schema_Reconstructor {
4040
/**
4141
* Constructor.
4242
*
43-
* @param WP_PDO_MySQL_On_SQLite $driver The SQLite driver instance.
43+
* @param WP_MySQL_On_SQLite $driver The SQLite driver instance.
4444
* @param WP_SQLite_Information_Schema_Builder $schema_builder The information schema builder instance.
4545
*/
4646
public function __construct(
@@ -137,7 +137,7 @@ private function get_sqlite_table_names(): array {
137137
array(
138138
'_mysql_data_types_cache',
139139
'sqlite\_%',
140-
str_replace( '_', '\_', WP_PDO_MySQL_On_SQLite::RESERVED_PREFIX ) . '%',
140+
str_replace( '_', '\_', WP_MySQL_On_SQLite::RESERVED_PREFIX ) . '%',
141141
)
142142
)->fetchAll( PDO::FETCH_COLUMN );
143143
}
@@ -763,9 +763,9 @@ private function get_mysql_column_type( string $column_type ): string {
763763
/**
764764
* Format a MySQL UTF-8 string literal for output in a CREATE TABLE statement.
765765
*
766-
* See WP_PDO_MySQL_On_SQLite::quote_mysql_utf8_string_literal().
766+
* See WP_MySQL_On_SQLite::quote_mysql_utf8_string_literal().
767767
*
768-
* TODO: This is a copy of WP_PDO_MySQL_On_SQLite::quote_mysql_utf8_string_literal().
768+
* TODO: This is a copy of WP_MySQL_On_SQLite::quote_mysql_utf8_string_literal().
769769
* We may consider extracing it to reusable MySQL helpers.
770770
*
771771
* @param string $utf8_literal The UTF-8 string literal to escape.

packages/mysql-on-sqlite/tests/WP_SQLite_Driver_Concurrency_Tests.php renamed to packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Concurrency_Tests.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* Tests for concurrent access to the same SQLite database file.
77
*/
8-
class WP_SQLite_Driver_Concurrency_Tests extends TestCase {
8+
class WP_MySQL_On_SQLite_Concurrency_Tests extends TestCase {
99
/**
1010
* Path to the temporary SQLite database file used in file-based tests.
1111
*
@@ -109,7 +109,7 @@ public function testSetQueryOpensReadOnlyTransaction(): void {
109109
public function testSetQuerySucceedsWhileAnotherConnectionHoldsWriteLock(): void {
110110
// Connection A: set up the database and hold a write transaction.
111111
$conn_a = new WP_SQLite_Connection( array( 'path' => $this->db_path ) );
112-
$driver_a = new WP_SQLite_Driver( $conn_a, 'wp' );
112+
$driver_a = $this->create_driver( $conn_a );
113113
$driver_a->query( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
114114
$driver_a->query( "INSERT INTO t VALUES (1, 'Alice')" );
115115

@@ -124,13 +124,13 @@ public function testSetQuerySucceedsWhileAnotherConnectionHoldsWriteLock(): void
124124
'timeout' => 0,
125125
)
126126
);
127-
$driver_b = new WP_SQLite_Driver( $conn_b, 'wp' );
127+
$driver_b = $this->create_driver( $conn_b );
128128
$conn_b->get_pdo()->setAttribute( PDO::ATTR_TIMEOUT, 0 );
129129

130130
// SET writes nothing, so it must not contend for the write lock.
131131
$result = $driver_b->query( "SET SESSION sql_mode = 'NO_ENGINE_SUBSTITUTION'" );
132132

133-
$this->assertSame( 0, $result );
133+
$this->assertSame( 0, $result->rowCount() );
134134
} finally {
135135
$conn_a->get_pdo()->exec( 'ROLLBACK' );
136136
}
@@ -139,7 +139,7 @@ public function testSetQuerySucceedsWhileAnotherConnectionHoldsWriteLock(): void
139139
private function assertReadOnlyQuerySucceedsUnderWriteLock( string $query ): void {
140140
// Connection A: set up the database.
141141
$conn_a = new WP_SQLite_Connection( array( 'path' => $this->db_path ) );
142-
$driver_a = new WP_SQLite_Driver( $conn_a, 'wp' );
142+
$driver_a = $this->create_driver( $conn_a );
143143
$driver_a->query( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
144144
$driver_a->query( "INSERT INTO t VALUES (1, 'Alice')" );
145145

@@ -154,22 +154,33 @@ private function assertReadOnlyQuerySucceedsUnderWriteLock( string $query ): voi
154154
'timeout' => 0,
155155
)
156156
);
157-
$driver_b = new WP_SQLite_Driver( $conn_b, 'wp' );
157+
$driver_b = $this->create_driver( $conn_b );
158158
$conn_b->get_pdo()->setAttribute( PDO::ATTR_TIMEOUT, 0 );
159159

160160
$result = $driver_b->query( $query );
161161

162-
$this->assertIsArray( $result );
163-
$this->assertNotEmpty( $result );
162+
$this->assertNotEmpty( $result->fetchAll() );
164163
} finally {
165164
$conn_a->get_pdo()->exec( 'ROLLBACK' );
166165
}
167166
}
168167

169-
private function create_in_memory_driver(): WP_SQLite_Driver {
168+
private function create_in_memory_driver(): WP_MySQL_On_SQLite {
170169
$pdo_class = PHP_VERSION_ID >= 80400 ? PDO\SQLite::class : PDO::class;
171170
$pdo = new $pdo_class( 'sqlite::memory:' );
172171
$connection = new WP_SQLite_Connection( array( 'pdo' => $pdo ) );
173-
return new WP_SQLite_Driver( $connection, 'wp' );
172+
return $this->create_driver( $connection );
173+
}
174+
175+
private function create_driver( WP_SQLite_Connection $connection ): WP_MySQL_On_SQLite {
176+
return new WP_MySQL_On_SQLite(
177+
'mysql-on-sqlite:dbname=wp',
178+
null,
179+
null,
180+
array(
181+
'pdo' => $connection->get_pdo(),
182+
'journal_mode' => $connection->query( 'PRAGMA journal_mode' )->fetchColumn(),
183+
)
184+
);
174185
}
175186
}

0 commit comments

Comments
 (0)