Skip to content

Commit e9e9fbe

Browse files
committed
Use the MySQL-on-SQLite driver API
Create WP_MySQL_On_SQLite through SQLiteDriverFactory when AST mode and the class are available, while preserving older driver fallbacks. Handle PDO-style query results in export and table listing, and add integration coverage for both commands.
1 parent b5bc658 commit e9e9fbe

8 files changed

Lines changed: 125 additions & 35 deletions

File tree

‎AGENTS.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Imports a MySQL dump file into the SQLite database. Parses SQL statements using
1111
**Parameters:**
1212

1313
- `<file>` (required), Path to the SQL dump file. Use `-` to read from STDIN.
14-
- `[--enable-ast-driver]` Use the new AST-based `WP_SQLite_Driver` instead of the legacy `WP_SQLite_Translator` for full MySQL compatibility.
14+
- `[--enable-ast-driver]` Use the new AST-based `WP_MySQL_On_SQLite` driver instead of the legacy `WP_SQLite_Translator` for full MySQL compatibility. Older integration plugin versions fall back to `WP_SQLite_Driver`.
1515

1616
### `wp sqlite export [<file>]`
1717

@@ -38,7 +38,7 @@ Lists all user tables in the SQLite database, excluding internal/system tables.
3838

3939
- PHP >=7.4, WP-CLI package (`wp-cli/wp-cli ^2.5`)
4040
- WordPress SQLite Database Integration plugin, translation layer between MySQL queries and SQLite
41-
- Two driver modes: legacy `WP_SQLite_Translator` and new AST-based `WP_SQLite_Driver` (via `--enable-ast-driver`)
41+
- Two driver modes: legacy `WP_SQLite_Translator` and new AST-based `WP_MySQL_On_SQLite` (via `--enable-ast-driver`), with a `WP_SQLite_Driver` compatibility fallback
4242

4343
## Directory Structure
4444

@@ -105,13 +105,13 @@ Runs lint, phpcs, phpunit, and behat sequentially.
105105
- `command.php` is the entry point, it registers `wp sqlite` as a WP-CLI command pointing to `SQLite_Command`
106106
- `SQLite_Command` delegates to `Import`, `Export`, and `Tables` classes
107107
- `SQLiteDatabaseIntegrationLoader::load_plugin()` bootstraps the WordPress SQLite Database Integration plugin, which MUST be loaded before any database operations
108-
- `SQLiteDriverFactory::create_driver()` creates the appropriate driver based on whether the AST driver is enabled
108+
- `SQLiteDriverFactory::create_driver()` creates the PDO-based `WP_MySQL_On_SQLite` driver when AST mode and the class are available, with compatibility fallbacks for older integration plugin versions and the legacy translator
109109
- The import parser (`Import::parse_statements()`) is a streaming SQL parser using PHP generators, it handles quotes, comments, escape sequences, and multi-line statements character by character
110110

111111
## Common Pitfalls
112112

113113
- **MUST load the SQLite plugin first:** All database operations require `SQLiteDatabaseIntegrationLoader::load_plugin()` to be called before creating a driver. The driver depends on classes from the WordPress SQLite Database Integration plugin.
114-
- **Two driver APIs:** The legacy `WP_SQLite_Translator` and new `WP_SQLite_Driver` (AST) have different class names. Use `SQLiteDriverFactory::create_driver()` to get the correct one, do not instantiate drivers directly.
114+
- **Multiple driver APIs:** Always use `SQLiteDriverFactory::create_driver()`. It selects the PDO-based `WP_MySQL_On_SQLite` API when available and preserves compatibility with older AST drivers and the legacy translator.
115115
- **SQL parser edge cases:** The `Import::parse_statements()` method handles escape sequences, nested quotes, and multi-line comments manually. Changes to this parser MUST be tested with the existing Behat scenarios in `features/sqlite-import.feature`.
116116
- **Encoding handling:** Import attempts UTF-8 conversion as a fallback when queries fail. This is intentional error recovery, not a bug.
117117
- **`@when after_wp_config_load`:** All command methods use this WP-CLI hook annotation. New commands MUST include it to ensure WordPress config is loaded before execution.

‎features/sqlite-export.feature‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ Feature: WP-CLI SQLite Export Command
2525
INSERT INTO
2626
"""
2727

28+
@require-sqlite
29+
Scenario: Successfully export the entire SQLite database with the new driver
30+
When I run `wp sqlite --enable-ast-driver export test_export_new_driver.sql`
31+
Then STDOUT should contain:
32+
"""
33+
Success: Exporting database...
34+
Success: Export complete. File written to test_export_new_driver.sql
35+
"""
36+
And the file "test_export_new_driver.sql" should exist
37+
And the file "test_export_new_driver.sql" should contain:
38+
"""
39+
CREATE TABLE
40+
"""
41+
And the file "test_export_new_driver.sql" should contain:
42+
"""
43+
INSERT INTO
44+
"""
45+
2846
@require-sqlite
2947
Scenario: Export specific tables
3048
When I run `wp sqlite export test_export_specific.sql --tables=wp_posts,wp_users`

‎features/sqlite-tables.feature‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ Feature: WP-CLI SQLite Tables Command
2525
wp_users
2626
"""
2727

28+
@require-sqlite
29+
Scenario: Successfully list the tables in the SQLite database with the new driver
30+
When I run `wp sqlite --enable-ast-driver tables`
31+
Then STDOUT should contain:
32+
"""
33+
wp_commentmeta
34+
wp_comments
35+
wp_links
36+
wp_options
37+
wp_postmeta
38+
wp_posts
39+
wp_term_relationships
40+
wp_term_taxonomy
41+
wp_termmeta
42+
wp_terms
43+
wp_usermeta
44+
wp_users
45+
"""
46+
2847
@require-sqlite
2948
Scenario: Successfully list the tables in the SQLite database in a CSV format
3049
When I run `wp sqlite tables --format=csv`

‎phpstan/stubs.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public function query( string $sql ) {}
2626
public function get_connection(): WP_SQLite_Connection {}
2727
}
2828

29+
class WP_MySQL_On_SQLite extends PDO {} // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO -- Stub for the plugin's PDO subclass.
30+
2931
class WP_SQLite_Translator {
3032
public function __construct() {}
3133
public function query( string $sql ) {}

‎src/Export.php‎

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
use Exception;
55
use PDO;
66
use WP_CLI;
7+
use WP_MySQL_On_SQLite;
78
use WP_SQLite_Driver;
89
use WP_SQLite_Translator;
910

1011
class Export {
1112
/**
1213
* The SQLite driver instance.
1314
*
14-
* @var WP_SQLite_Driver|WP_SQLite_Translator
15+
* @var WP_MySQL_On_SQLite|WP_SQLite_Driver|WP_SQLite_Translator
1516
*/
1617
protected $driver;
1718

@@ -85,8 +86,7 @@ protected function close_output_stream( $handle ) {
8586
protected function write_sql_statements( $handle ) {
8687
$include_tables = $this->get_include_tables();
8788
$exclude_tables = $this->get_exclude_tables();
88-
foreach ( $this->driver->query( 'SHOW TABLES' ) as $row ) {
89-
$table_name = array_values( (array) $row )[0];
89+
foreach ( $this->get_table_names() as $table_name ) {
9090

9191
// Skip tables that are not in the include_tables list if the list is defined
9292
if ( ! empty( $include_tables ) && ! in_array( $table_name, $include_tables, true ) ) {
@@ -111,6 +111,24 @@ protected function write_sql_statements( $handle ) {
111111
fwrite( $handle, sprintf( '-- Dump completed on %s', gmdate( 'c' ) ) );
112112
}
113113

114+
/**
115+
* Get the database table names.
116+
*
117+
* @return string[]
118+
*/
119+
protected function get_table_names() {
120+
$result = $this->driver->query( 'SHOW TABLES' );
121+
if ( $this->driver instanceof PDO ) {
122+
return $result->fetchAll( PDO::FETCH_COLUMN );
123+
}
124+
125+
$tables = array();
126+
foreach ( $result as $row ) {
127+
$tables[] = array_values( (array) $row )[0];
128+
}
129+
return $tables;
130+
}
131+
114132
/**
115133
* Write the create statement for a table to the output stream.
116134
*
@@ -158,6 +176,13 @@ protected function write_insert_statements( $handle, $table_name ) {
158176
* @throws Exception
159177
*/
160178
protected function get_create_statement( $table_name ) {
179+
if ( $this->driver instanceof PDO ) {
180+
$table_name = '`' . str_replace( '`', '``', $table_name ) . '`';
181+
$create = $this->driver->query( 'SHOW CREATE TABLE ' . $table_name );
182+
$sql = $create->fetchColumn( 1 );
183+
return rtrim( $sql, ';' ) . ";\n";
184+
}
185+
161186
$table_name = $this->driver instanceof WP_SQLite_Driver
162187
? $this->driver->get_connection()->quote_identifier( $table_name )
163188
: $table_name;
@@ -176,12 +201,17 @@ protected function get_create_statement( $table_name ) {
176201
* @return \Generator
177202
*/
178203
protected function get_insert_statements( $table_name ) {
179-
$pdo = $this->get_pdo();
180-
$stmt = $pdo->prepare( 'SELECT * FROM ' . $table_name );
181-
$stmt->execute();
204+
if ( $this->driver instanceof PDO ) {
205+
$quoted_table_name = '`' . str_replace( '`', '``', $table_name ) . '`';
206+
$stmt = $this->driver->query( 'SELECT * FROM ' . $quoted_table_name );
207+
} else {
208+
$pdo = $this->get_pdo();
209+
$stmt = $pdo->prepare( 'SELECT * FROM ' . $table_name );
210+
$stmt->execute();
211+
}
182212
// phpcs:ignore
183213
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT ) ) {
184-
yield sprintf( 'INSERT INTO `%1s` VALUES (%2s);', $table_name, $this->escape_values( $pdo, $row ) );
214+
yield sprintf( 'INSERT INTO `%1s` VALUES (%2s);', $table_name, $this->escape_values( $row ) );
185215
}
186216
}
187217

@@ -228,13 +258,11 @@ protected function get_include_tables() {
228258
/**
229259
* Escape values for insert statement
230260
*
231-
* @param PDO $pdo
232261
* @param $values
233262
*
234263
* @return string
235264
*/
236-
protected function escape_values( PDO $pdo, $values ) {
237-
// Get a mysql PDO instance
265+
protected function escape_values( $values ) {
238266
$escaped_values = [];
239267
foreach ( $values as $value ) {
240268
if ( is_null( $value ) ) {
@@ -292,9 +320,14 @@ protected function get_dump_comment( $comment ) {
292320
* @return bool
293321
*/
294322
protected function table_has_records( $table_name ) {
295-
$pdo = $this->get_pdo();
296-
$stmt = $pdo->prepare( 'SELECT COUNT(*) FROM ' . $table_name );
297-
$stmt->execute();
323+
if ( $this->driver instanceof PDO ) {
324+
$table_name = '`' . str_replace( '`', '``', $table_name ) . '`';
325+
$stmt = $this->driver->query( 'SELECT COUNT(*) FROM ' . $table_name );
326+
} else {
327+
$pdo = $this->get_pdo();
328+
$stmt = $pdo->prepare( 'SELECT COUNT(*) FROM ' . $table_name );
329+
$stmt->execute();
330+
}
298331
return $stmt->fetchColumn() > 0;
299332
}
300333

‎src/Import.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
use Exception;
55
use Generator;
66
use WP_CLI;
7+
use WP_MySQL_On_SQLite;
8+
use WP_SQLite_Driver;
79
use WP_SQLite_Translator;
810

911
class Import {
1012
/**
1113
* The SQLite driver instance.
1214
*
13-
* @var WP_SQLite_Driver|WP_SQLite_Translator
15+
* @var WP_MySQL_On_SQLite|WP_SQLite_Driver|WP_SQLite_Translator
1416
*/
1517
protected $driver;
1618

‎src/SQLiteDriverFactory.php‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Automattic\WP_CLI\SQLite;
44

5+
use PDO;
6+
use WP_MySQL_On_SQLite;
57
use WP_SQLite_Connection;
68
use WP_SQLite_Driver;
79
use WP_SQLite_Translator;
@@ -10,25 +12,32 @@ class SQLiteDriverFactory {
1012
/**
1113
* Create an instance of the SQLite driver.
1214
*
13-
* @return WP_SQLite_Driver|WP_SQLite_Translator
15+
* @return WP_MySQL_On_SQLite|WP_SQLite_Driver|WP_SQLite_Translator
1416
*/
1517
public static function create_driver() {
1618
$new_driver_enabled = defined( 'WP_SQLITE_AST_DRIVER' ) && WP_SQLITE_AST_DRIVER;
19+
if ( ! $new_driver_enabled ) {
20+
return new WP_SQLite_Translator();
21+
}
22+
23+
$db_name = defined( 'DB_NAME' ) && '' !== DB_NAME ? DB_NAME : 'database_name_here';
1724

18-
if ( $new_driver_enabled ) {
19-
$connection = new WP_SQLite_Connection(
20-
array(
21-
'path' => FQDB,
22-
)
25+
if ( class_exists( WP_MySQL_On_SQLite::class ) ) {
26+
$database_dsn = sprintf(
27+
'mysql-on-sqlite:path=%s;dbname=%s',
28+
str_replace( ';', ';;', FQDB ),
29+
str_replace( ';', ';;', $db_name )
2330
);
24-
if ( defined( 'DB_NAME' ) && '' !== DB_NAME ) {
25-
$db_name = DB_NAME;
26-
} else {
27-
$db_name = 'database_name_here';
28-
}
29-
return new WP_SQLite_Driver( $connection, $db_name );
31+
$driver = new WP_MySQL_On_SQLite( $database_dsn );
32+
$driver->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
33+
return $driver;
3034
}
3135

32-
return new WP_SQLite_Translator();
36+
$connection = new WP_SQLite_Connection(
37+
array(
38+
'path' => FQDB,
39+
)
40+
);
41+
return new WP_SQLite_Driver( $connection, $db_name );
3342
}
3443
}

‎src/Tables.php‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
namespace Automattic\WP_CLI\SQLite;
44

5+
use PDO;
56
use WP_CLI;
7+
use WP_MySQL_On_SQLite;
68
use WP_SQLite_Driver;
79
use WP_SQLite_Translator;
810

911
class Tables {
1012
/**
1113
* The SQLite driver instance.
1214
*
13-
* @var WP_SQLite_Driver|WP_SQLite_Translator
15+
* @var WP_MySQL_On_SQLite|WP_SQLite_Driver|WP_SQLite_Translator
1416
*/
1517
protected $driver;
1618

@@ -27,9 +29,14 @@ public function __construct() {
2729
*/
2830
public function run( $assoc_args = [] ) {
2931
// Get all tables
30-
$tables = array();
31-
foreach ( $this->driver->query( 'SHOW TABLES' ) as $row ) {
32-
$tables[] = array_values( (array) $row )[0];
32+
$result = $this->driver->query( 'SHOW TABLES' );
33+
if ( $this->driver instanceof PDO ) {
34+
$tables = $result->fetchAll( PDO::FETCH_COLUMN );
35+
} else {
36+
$tables = array();
37+
foreach ( $result as $row ) {
38+
$tables[] = array_values( (array) $row )[0];
39+
}
3340
}
3441

3542
// With the legacy driver, we need to exclude system tables

0 commit comments

Comments
 (0)