Skip to content

Commit edf6971

Browse files
committed
Restore legacy SQLite driver wrapper
Keep WP_SQLite_Driver as a compatibility proxy over WP_MySQL_On_SQLite while consumers migrate to the PDO-style API.
1 parent d713a29 commit edf6971

3 files changed

Lines changed: 377 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@
4242
require_once __DIR__ . '/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
4343
require_once __DIR__ . '/sqlite/class-wp-mysql-on-sqlite.php';
4444
require_once __DIR__ . '/sqlite/class-wp-pdo-proxy-statement.php';
45+
require_once __DIR__ . '/sqlite/class-wp-sqlite-driver.php';
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
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+
* For back compatibility with dependencies that use their own loader scripts
10+
* (e.g., WP CLI SQLite Command), ensure the PDO-based classes are loaded.
11+
*/
12+
require_once __DIR__ . '/class-wp-mysql-on-sqlite.php';
13+
require_once __DIR__ . '/class-wp-pdo-proxy-statement.php';
14+
15+
/**
16+
* Deprecated: A proxy of the WP_MySQL_On_SQLite class preserving the legacy API.
17+
*
18+
* This class temporarily preserves the legacy constructor and result API while
19+
* consumers transition to the PDO-based WP_MySQL_On_SQLite API.
20+
*/
21+
class WP_SQLite_Driver {
22+
/**
23+
* The SQLite engine version.
24+
*
25+
* This is a mysqli-like property that is needed to avoid a PHP warning in
26+
* the WordPress health info. The "WP_Debug_Data::get_wp_database()" method
27+
* calls "$wpdb->dbh->client_info" - a mysqli-specific abstraction leak.
28+
*
29+
* @TODO: This should be fixed in WordPress core.
30+
*
31+
* See:
32+
* https://github.com/WordPress/wordpress-develop/blob/bcdca3f9925f1d3eca7b78d231837c0caf0c8c24/src/wp-admin/includes/class-wp-debug-data.php#L1579
33+
*
34+
* @var string
35+
*/
36+
public $client_info;
37+
38+
/**
39+
* The MySQL-on-SQLite driver instance.
40+
*
41+
* @var WP_MySQL_On_SQLite
42+
*/
43+
private $mysql_on_sqlite_driver;
44+
45+
/**
46+
* Results of the last emulated query.
47+
*
48+
* @var mixed
49+
*/
50+
private $last_result;
51+
52+
/**
53+
* Constructor.
54+
*
55+
* Set up an SQLite connection and the MySQL-on-SQLite driver.
56+
*
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.
60+
*
61+
* @throws WP_SQLite_Driver_Exception When the driver initialization fails.
62+
*/
63+
public function __construct(
64+
WP_SQLite_Connection $connection,
65+
string $database,
66+
int $mysql_version = 80038
67+
) {
68+
$this->mysql_on_sqlite_driver = new WP_MySQL_On_SQLite(
69+
sprintf( 'mysql-on-sqlite:dbname=%s', $database ),
70+
null,
71+
null,
72+
array(
73+
'mysql_version' => $mysql_version,
74+
'pdo' => $connection->get_pdo(),
75+
'journal_mode' => $connection->query( 'PRAGMA journal_mode' )->fetchColumn(),
76+
)
77+
);
78+
$this->main_db_name = $database;
79+
$this->client_info = $this->mysql_on_sqlite_driver->client_info;
80+
81+
$connection->get_pdo()->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
82+
}
83+
84+
/**
85+
* Get the SQLite connection instance.
86+
*
87+
* @return WP_SQLite_Connection
88+
*/
89+
public function get_connection(): WP_SQLite_Connection {
90+
return $this->mysql_on_sqlite_driver->get_connection();
91+
}
92+
93+
/**
94+
* Get the version of the SQLite engine.
95+
*
96+
* @return string SQLite engine version as a string.
97+
*/
98+
public function get_sqlite_version(): string {
99+
return $this->mysql_on_sqlite_driver->get_sqlite_version();
100+
}
101+
102+
/**
103+
* Get the SQLite driver version saved in the database.
104+
*
105+
* The saved driver version corresponds to the latest version of the SQLite
106+
* driver that was used to initialize and configure the SQLite database.
107+
*
108+
* @return string SQLite driver version as a string.
109+
* @throws PDOException When the query execution fails.
110+
*/
111+
public function get_saved_driver_version(): string {
112+
return $this->mysql_on_sqlite_driver->get_saved_driver_version();
113+
}
114+
115+
/**
116+
* Check if a specific SQL mode is active.
117+
*
118+
* @param string $mode The SQL mode to check.
119+
* @return bool True if the SQL mode is active, false otherwise.
120+
*/
121+
public function is_sql_mode_active( string $mode ): bool {
122+
return $this->mysql_on_sqlite_driver->is_sql_mode_active( $mode );
123+
}
124+
125+
/**
126+
* Get the last executed MySQL query.
127+
*
128+
* @return string|null
129+
*/
130+
public function get_last_mysql_query(): ?string {
131+
return $this->mysql_on_sqlite_driver->get_last_mysql_query();
132+
}
133+
134+
/**
135+
* Get SQLite queries executed for the last MySQL query.
136+
*
137+
* @return array{ sql: string, params: array }[]
138+
*/
139+
public function get_last_sqlite_queries(): array {
140+
return $this->mysql_on_sqlite_driver->get_last_sqlite_queries();
141+
}
142+
143+
/**
144+
* Get the auto-increment value generated for the last query.
145+
*
146+
* @return int|string
147+
*/
148+
public function get_insert_id() {
149+
return $this->mysql_on_sqlite_driver->get_insert_id();
150+
}
151+
152+
/**
153+
* @param string $query Full SQL statement string.
154+
* @param int $fetch_mode PDO fetch mode. Default is PDO::FETCH_OBJ.
155+
* @param mixed ...$fetch_mode_args Additional fetch mode arguments.
156+
*
157+
* @return mixed Return value, depending on the query type.
158+
*
159+
* @throws WP_SQLite_Driver_Exception When the query execution fails.
160+
*/
161+
public function query( string $query, $fetch_mode = PDO::FETCH_OBJ, ...$fetch_mode_args ) {
162+
$stmt = $this->mysql_on_sqlite_driver->query( $query, $fetch_mode, ...$fetch_mode_args );
163+
164+
if ( $stmt->columnCount() > 0 ) {
165+
$this->last_result = $stmt->fetchAll( $fetch_mode );
166+
} else {
167+
$this->last_result = $stmt->rowCount();
168+
}
169+
return $this->last_result;
170+
}
171+
172+
/**
173+
* Tokenize a MySQL query and initialize a parser.
174+
*
175+
* @param string $query The MySQL query to parse.
176+
* @return WP_MySQL_Parser A parser initialized for the MySQL query.
177+
*/
178+
public function create_parser( string $query ): WP_MySQL_Parser {
179+
return $this->mysql_on_sqlite_driver->create_parser( $query );
180+
}
181+
182+
/**
183+
* Get results of the last query.
184+
*
185+
* @return mixed
186+
*/
187+
public function get_query_results() {
188+
return $this->last_result;
189+
}
190+
191+
/**
192+
* Get return value of the last query() function call.
193+
*
194+
* @return mixed
195+
*/
196+
public function get_last_return_value() {
197+
return $this->last_result;
198+
}
199+
200+
/**
201+
* Get the number of columns returned by the last emulated query.
202+
*
203+
* @return int
204+
*/
205+
public function get_last_column_count(): int {
206+
return $this->mysql_on_sqlite_driver->get_last_column_count();
207+
}
208+
209+
/**
210+
* Get column metadata for results of the last emulated query.
211+
*
212+
* @return array
213+
*/
214+
public function get_last_column_meta(): array {
215+
return $this->mysql_on_sqlite_driver->get_last_column_meta();
216+
}
217+
218+
/**
219+
* Execute a query in SQLite.
220+
*
221+
* @param string $sql The query to execute.
222+
* @param array $params The query parameters.
223+
* @throws PDOException When the query execution fails.
224+
* @return PDOStatement The PDO statement object.
225+
*/
226+
public function execute_sqlite_query( string $sql, array $params = array() ): PDOStatement {
227+
return $this->mysql_on_sqlite_driver->execute_sqlite_query( $sql, $params );
228+
}
229+
230+
/**
231+
* A temporary alias for back compatibility.
232+
*
233+
* @see self::beginTransaction()
234+
*/
235+
public function begin_transaction(): void {
236+
$this->beginTransaction();
237+
}
238+
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+
246+
/**
247+
* Commit the current transaction or nested transaction.
248+
*/
249+
public function commit(): void {
250+
$this->mysql_on_sqlite_driver->commit();
251+
}
252+
253+
/**
254+
* Rollback the current transaction or nested transaction.
255+
*/
256+
public function rollback(): void {
257+
$this->mysql_on_sqlite_driver->rollBack();
258+
}
259+
260+
/**
261+
* Proxy also the private property "$main_db_name", as it is used in tests.
262+
*/
263+
public function __set( string $name, $value ): void {
264+
if ( 'main_db_name' === $name ) {
265+
$closure = function ( string $value ) {
266+
$this->main_db_name = $value;
267+
};
268+
$closure->call( $this->mysql_on_sqlite_driver, $value );
269+
}
270+
}
271+
272+
/**
273+
* Proxy also this private method, as it is used in tests.
274+
*/
275+
private function quote_mysql_utf8_string_literal( string $utf8_literal ): string {
276+
$closure = function ( string $utf8_literal ) {
277+
return $this->quote_mysql_utf8_string_literal( $utf8_literal );
278+
};
279+
return $closure->call( $this->mysql_on_sqlite_driver, $utf8_literal );
280+
}
281+
}
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_renamed_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_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+
}

0 commit comments

Comments
 (0)