Skip to content

Commit abec387

Browse files
committed
Adopt PDO-style SQLite driver API
Migrate the WordPress integration, MySQL proxy, tools, and tests to the PDO-style driver constructor and statement API.
1 parent 11c2143 commit abec387

15 files changed

Lines changed: 517 additions & 370 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
}
3636
require_once __DIR__ . '/sqlite/class-wp-sqlite-connection.php';
3737
require_once __DIR__ . '/sqlite/class-wp-sqlite-configurator.php';
38-
require_once __DIR__ . '/sqlite/class-wp-sqlite-driver.php';
3938
require_once __DIR__ . '/sqlite/class-wp-sqlite-driver-exception.php';
4039
require_once __DIR__ . '/sqlite/class-wp-sqlite-information-schema-builder.php';
4140
require_once __DIR__ . '/sqlite/class-wp-sqlite-information-schema-exception.php';
4241
require_once __DIR__ . '/sqlite/class-wp-sqlite-information-schema-reconstructor.php';
4342
require_once __DIR__ . '/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
4443
require_once __DIR__ . '/sqlite/class-wp-pdo-mysql-on-sqlite.php';
4544
require_once __DIR__ . '/sqlite/class-wp-pdo-proxy-statement.php';
45+
require_once __DIR__ . '/sqlite/class-wp-sqlite-driver.php';

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,15 +1013,6 @@ public function beginTransaction(): bool {
10131013
return true;
10141014
}
10151015

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-
10251016
/**
10261017
* PDO API: Commit a transaction.
10271018
*
@@ -4211,7 +4202,7 @@ private function translate_query_expression( WP_Parser_Node $node ): string {
42114202
* When the ORDER BY clause is present, we need to disambiguate the item
42124203
* list and make sure they don't cause an "ambiguous column name" error.
42134204
*
4214-
* @see WP_SQLite_Driver::disambiguate_item()
4205+
* @see WP_PDO_MySQL_On_SQLite::disambiguate_item()
42154206
*/
42164207
$disambiguated_order_list = array();
42174208
$order_clause = $node->get_first_child_node( 'orderClause' );
@@ -4279,7 +4270,7 @@ private function translate_query_specification( WP_Parser_Node $node ): string {
42794270
* When the GROUP BY or HAVING clause is present, we need to disambiguate
42804271
* the items to ensure they don't cause an "ambiguous column name" error.
42814272
*
4282-
* @see WP_SQLite_Driver::disambiguate_item()
4273+
* @see WP_PDO_MySQL_On_SQLite::disambiguate_item()
42834274
*/
42844275
$group_by_clause = null;
42854276
$having_clause = null;
@@ -5788,7 +5779,7 @@ private function unnest_parenthesized_expression( WP_Parser_Node $node ): WP_Par
57885779
* consider column references in forms like "db.table.column".
57895780
*
57905781
* @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()
5782+
* @see WP_PDO_MySQL_On_SQLite::create_select_item_disambiguation_map()
57925783
* @param WP_Parser_Node $expr The expression AST node or subnode.
57935784
* @return string|null The disambiguated and translated expression;
57945785
* null when the expression cannot be disambiguated.
@@ -5827,7 +5818,7 @@ private function disambiguate_item( array $disambiguation_map, WP_Parser_Node $e
58275818
* Create a SELECT item disambiguation map from a SELECT item list for use
58285819
* with the ORDER BY, GROUP BY, and HAVING clause disambiguation algorithm.
58295820
*
5830-
* @see WP_SQLite_Driver::disambiguate_item()
5821+
* @see WP_PDO_MySQL_On_SQLite::disambiguate_item()
58315822
*
58325823
* @param WP_Parser_Node $select_item_list The "selectItemList" AST node.
58335824
* @return array The SELECT item disambiguation map (column name => array of select items).

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

Lines changed: 17 additions & 16 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
*/
1212
require_once __DIR__ . '/class-wp-pdo-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_PDO_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_PDO_MySQL_On_SQLite API.
2020
*/
2121
class WP_SQLite_Driver {
2222
/**
@@ -54,8 +54,9 @@ class WP_SQLite_Driver {
5454
*
5555
* Set up an SQLite connection and the MySQL-on-SQLite driver.
5656
*
57-
* @param WP_SQLite_Connection $connection A SQLite database connection.
58-
* @param string $database The database name.
57+
* @param WP_SQLite_Connection $connection A SQLite database connection.
58+
* @param string $database The database name.
59+
* @param int $mysql_version Optional. MySQL version to emulate. Default 80038.
5960
*
6061
* @throws WP_SQLite_Driver_Exception When the driver initialization fails.
6162
*/
@@ -151,7 +152,7 @@ public function get_insert_id() {
151152
/**
152153
* @param string $query Full SQL statement string.
153154
* @param int $fetch_mode PDO fetch mode. Default is PDO::FETCH_OBJ.
154-
* @param array ...$fetch_mode_args Additional fetch mode arguments.
155+
* @param mixed ...$fetch_mode_args Additional fetch mode arguments.
155156
*
156157
* @return mixed Return value, depending on the query type.
157158
*
@@ -218,21 +219,14 @@ public function get_last_column_meta(): array {
218219
* Execute a query in SQLite.
219220
*
220221
* @param string $sql The query to execute.
221-
* @param array $params The query parameters.
222+
* @param array $params The query parameters.
222223
* @throws PDOException When the query execution fails.
223224
* @return PDOStatement The PDO statement object.
224225
*/
225226
public function execute_sqlite_query( string $sql, array $params = array() ): PDOStatement {
226227
return $this->mysql_on_sqlite_driver->execute_sqlite_query( $sql, $params );
227228
}
228229

229-
/**
230-
* Begin a new transaction or nested transaction.
231-
*/
232-
public function beginTransaction(): void { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
233-
$this->mysql_on_sqlite_driver->begin_transaction();
234-
}
235-
236230
/**
237231
* A temporary alias for back compatibility.
238232
*
@@ -242,6 +236,13 @@ public function begin_transaction(): void {
242236
$this->beginTransaction();
243237
}
244238

239+
/**
240+
* Begin a new transaction or nested transaction.
241+
*/
242+
public function beginTransaction(): void { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
243+
$this->mysql_on_sqlite_driver->beginTransaction();
244+
}
245+
245246
/**
246247
* Commit the current transaction or nested transaction.
247248
*/
@@ -253,7 +254,7 @@ public function commit(): void {
253254
* Rollback the current transaction or nested transaction.
254255
*/
255256
public function rollback(): void {
256-
$this->mysql_on_sqlite_driver->rollback();
257+
$this->mysql_on_sqlite_driver->rollBack();
257258
}
258259

259260
/**

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,13 @@ public function testExplicitJournalModeSurfacesFailureWhenWalIsUnavailable(): vo
179179
}
180180

181181
public function testDriverKeepsConfiguredJournalMode(): void {
182-
$connection = new WP_SQLite_Connection(
183-
array(
184-
'path' => $this->db_path,
185-
'journal_mode' => 'DELETE',
186-
)
182+
$driver = new WP_PDO_MySQL_On_SQLite(
183+
sprintf( 'mysql-on-sqlite:path=%s;dbname=wp', $this->db_path ),
184+
null,
185+
null,
186+
array( 'journal_mode' => 'DELETE' )
187187
);
188188

189-
$driver = new WP_SQLite_Driver( $connection, 'wp' );
190-
191189
$this->assertSame( 'delete', $this->get_journal_mode( $driver->get_connection() ) );
192190
}
193191

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
class WP_SQLite_Driver_Compatibility_Tests extends TestCase {
6+
/** @var WP_SQLite_Driver */
7+
private $driver;
8+
9+
/** @var PDO */
10+
private $sqlite;
11+
12+
public function setUp(): void {
13+
$pdo_class = PHP_VERSION_ID >= 80400 ? PDO\SQLite::class : PDO::class;
14+
$this->sqlite = new $pdo_class( 'sqlite::memory:' );
15+
$this->driver = new WP_SQLite_Driver(
16+
new WP_SQLite_Connection( array( 'pdo' => $this->sqlite ) ),
17+
'wp'
18+
);
19+
$this->driver->query( 'CREATE TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, value VARCHAR(255))' );
20+
}
21+
22+
public function test_wraps_pdo_driver(): void {
23+
$get_driver = Closure::bind(
24+
function () {
25+
return $this->mysql_on_sqlite_driver;
26+
},
27+
$this->driver,
28+
WP_SQLite_Driver::class
29+
);
30+
31+
$this->assertInstanceOf( WP_PDO_MySQL_On_SQLite::class, $get_driver() );
32+
$this->assertSame( $this->sqlite, $this->driver->get_connection()->get_pdo() );
33+
$this->assertSame( $this->driver->get_sqlite_version(), $this->driver->client_info );
34+
$this->assertSame( SQLITE_DRIVER_VERSION, $this->driver->get_saved_driver_version() );
35+
$this->assertTrue( $this->driver->is_sql_mode_active( 'STRICT_TRANS_TABLES' ) );
36+
}
37+
38+
public function test_preserves_legacy_query_results(): void {
39+
$this->assertSame( 1, $this->driver->query( "INSERT INTO t (value) VALUES ('first')" ) );
40+
$this->assertSame( 1, $this->driver->get_insert_id() );
41+
42+
$result = $this->driver->query( 'SELECT id, value FROM t', PDO::FETCH_ASSOC );
43+
$this->assertSame(
44+
array(
45+
array(
46+
'id' => '1',
47+
'value' => 'first',
48+
),
49+
),
50+
$result
51+
);
52+
$this->assertSame( $result, $this->driver->get_query_results() );
53+
$this->assertSame( $result, $this->driver->get_last_return_value() );
54+
$this->assertSame( 2, $this->driver->get_last_column_count() );
55+
$this->assertCount( 2, $this->driver->get_last_column_meta() );
56+
}
57+
58+
public function test_delegates_diagnostics_and_native_queries(): void {
59+
$this->driver->query( 'SELECT 1' );
60+
61+
$this->assertSame( 'SELECT 1', $this->driver->get_last_mysql_query() );
62+
$this->assertNotEmpty( $this->driver->get_last_sqlite_queries() );
63+
$this->assertInstanceOf( WP_MySQL_Parser::class, $this->driver->create_parser( 'SELECT 1' ) );
64+
$this->assertSame( '42', $this->driver->execute_sqlite_query( 'SELECT 42' )->fetchColumn() );
65+
}
66+
67+
public function test_preserves_transaction_method_aliases(): void {
68+
$this->driver->begin_transaction();
69+
$this->driver->query( "INSERT INTO t (value) VALUES ('rolled back')" );
70+
$this->driver->rollback();
71+
$this->assertSame( '0', $this->driver->query( 'SELECT COUNT(*) FROM t' )[0]->{'COUNT(*)'} );
72+
73+
$this->driver->beginTransaction();
74+
$this->driver->query( "INSERT INTO t (value) VALUES ('committed')" );
75+
$this->driver->commit();
76+
$this->assertSame( '1', $this->driver->query( 'SELECT COUNT(*) FROM t' )[0]->{'COUNT(*)'} );
77+
}
78+
79+
public function test_proxies_legacy_test_helpers(): void {
80+
$this->driver->main_db_name = 'wp_test_new';
81+
$result = $this->driver->query(
82+
'SELECT schema_name FROM information_schema.schemata ORDER BY schema_name'
83+
);
84+
$this->assertSame( 'wp_test_new', $result[1]->SCHEMA_NAME );
85+
86+
$quote = Closure::bind(
87+
function ( string $value ) {
88+
return $this->quote_mysql_utf8_string_literal( $value );
89+
},
90+
$this->driver,
91+
WP_SQLite_Driver::class
92+
);
93+
$this->assertSame( "'abc''xyz'", $quote( "abc'xyz" ) );
94+
}
95+
}

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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_PDO_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_PDO_MySQL_On_SQLite {
176+
return new WP_PDO_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)