Skip to content

Commit d426211

Browse files
authored
Merge pull request #10 from Automattic/new-sqlite-driver
Add support for the new SQLite driver
2 parents 45f05f5 + 957e06a commit d426211

11 files changed

Lines changed: 203 additions & 61 deletions

‎README.md‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ $ wp sqlite import <file>
2020
<file>
2121
The path to the MySQL compatible dump file to import. When passing `-` as the file argument, the SQL commands are read from standard input.
2222

23+
[--enable-ast-driver]
24+
Enables new AST driver for full MySQL compatibility.
25+
2326
### wp sqlite export
2427

2528
Exports an SQLite database to a MySQL compatible file.
@@ -42,6 +45,9 @@ $ wp sqlite export [<file>] [--tables=<tables>] [--exclude-tables] [--porcelain]
4245
[--porcelain]
4346
Output filename for the exported database.
4447

48+
[--enable-ast-driver]
49+
Enables new AST driver for full MySQL compatibility.
50+
4551
### wp sqlite tables
4652

4753
Lists the SQLite database tables.
@@ -61,23 +67,26 @@ $ wp sqlite tables [--format=<list|csv>]
6167
- csv
6268
---
6369

70+
[--enable-ast-driver]
71+
Enables new AST driver for full MySQL compatibility.
72+
6473
**EXAMPLES**
6574

6675
```
6776
# List all tables
6877
$ wp sqlite tables
69-
wp_users
70-
wp_usermeta
71-
wp_termmeta
72-
wp_terms
73-
wp_term_taxonomy
74-
wp_term_relationships
7578
wp_commentmeta
7679
wp_comments
7780
wp_links
7881
wp_options
7982
wp_postmeta
8083
wp_posts
84+
wp_term_relationships
85+
wp_term_taxonomy
86+
wp_termmeta
87+
wp_terms
88+
wp_usermeta
89+
wp_users
8190
8291
* List all tables in CSV format
8392
$ wp sqlite tables --format=csv

‎features/pass.feature‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Feature: Always Passing Test
2-
Scenario: Evaluate true expression
2+
Scenario: Evaluate true expression
33
Given a WP install
44

55
When I run `wp eval "var_export(true);"`
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@ Feature: WP-CLI SQLite Tables Command
1111
When I run `wp sqlite tables`
1212
Then STDOUT should contain:
1313
"""
14-
wp_users
15-
wp_usermeta
16-
wp_termmeta
17-
wp_terms
18-
wp_term_taxonomy
19-
wp_term_relationships
2014
wp_commentmeta
2115
wp_comments
2216
wp_links
2317
wp_options
2418
wp_postmeta
2519
wp_posts
20+
wp_term_relationships
21+
wp_term_taxonomy
22+
wp_termmeta
23+
wp_terms
24+
wp_usermeta
25+
wp_users
2626
"""
2727

2828
@require-sqlite
2929
Scenario: Successfully list the tables in the SQLite database in a CSV format
3030
When I run `wp sqlite tables --format=csv`
3131
Then STDOUT should contain:
3232
"""
33-
wp_users,wp_usermeta,wp_termmeta,wp_terms,wp_term_taxonomy,wp_term_relationships,wp_commentmeta,wp_comments,wp_links,wp_options,wp_postmeta,wp_posts
33+
wp_commentmeta,wp_comments,wp_links,wp_options,wp_postmeta,wp_posts,wp_term_relationships,wp_term_taxonomy,wp_termmeta,wp_terms,wp_usermeta,wp_users
3434
"""
3535

3636
@require-sqlite
3737
Scenario: Successfully list the tables in the SQLite database in a CSV format
3838
When I run `wp sqlite tables --format=json`
3939
Then STDOUT should contain:
4040
"""
41-
["wp_users","wp_usermeta","wp_termmeta","wp_terms","wp_term_taxonomy","wp_term_relationships","wp_commentmeta","wp_comments","wp_links","wp_options","wp_postmeta","wp_posts"]
41+
["wp_commentmeta","wp_comments","wp_links","wp_options","wp_postmeta","wp_posts","wp_term_relationships","wp_term_taxonomy","wp_termmeta","wp_terms","wp_usermeta","wp_users"]
4242
"""

‎src/Export.php‎

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@
44
use Exception;
55
use PDO;
66
use WP_CLI;
7+
use WP_SQLite_Driver;
78
use WP_SQLite_Translator;
89

910
class Export {
11+
/**
12+
* The SQLite driver instance.
13+
*
14+
* @var WP_SQLite_Driver|WP_SQLite_Translator
15+
*/
16+
protected $driver;
1017

11-
protected $translator;
1218
protected $args = array();
1319
protected $is_stdout = false;
1420

1521
public function __construct() {
1622
SQLiteDatabaseIntegrationLoader::load_plugin();
17-
$this->translator = new WP_SQLite_Translator();
23+
$this->driver = SQLiteDriverFactory::create_driver();
1824
}
1925

2026
/**
@@ -79,19 +85,27 @@ protected function close_output_stream( $handle ) {
7985
protected function write_sql_statements( $handle ) {
8086
$include_tables = $this->get_include_tables();
8187
$exclude_tables = $this->get_exclude_tables();
82-
foreach ( $this->translator->query( 'SHOW TABLES' ) as $table ) {
88+
foreach ( $this->driver->query( 'SHOW TABLES' ) as $row ) {
89+
$table_name = array_values( (array) $row )[0];
90+
8391
// Skip tables that are not in the include_tables list if the list is defined
84-
if ( ! empty( $include_tables ) && ! in_array( $table->name, $include_tables, true ) ) {
92+
if ( ! empty( $include_tables ) && ! in_array( $table_name, $include_tables, true ) ) {
93+
continue;
94+
}
95+
96+
// Skip internal tables used by the new SQLite driver
97+
// (This is only needed when exporting with the legacy driver.)
98+
if ( 0 === strpos( $table_name, '_wp_sqlite_' ) ) {
8599
continue;
86100
}
87101

88102
// Skip tables that are in the exclude_tables list
89-
if ( in_array( $table->name, $exclude_tables, true ) ) {
103+
if ( in_array( $table_name, $exclude_tables, true ) ) {
90104
continue;
91105
}
92106

93-
$this->write_create_table_statement( $handle, $table->name );
94-
$this->write_insert_statements( $handle, $table->name );
107+
$this->write_create_table_statement( $handle, $table_name );
108+
$this->write_insert_statements( $handle, $table_name );
95109
}
96110

97111
fwrite( $handle, sprintf( '-- Dump completed on %s', gmdate( 'c' ) ) );
@@ -144,8 +158,14 @@ protected function write_insert_statements( $handle, $table_name ) {
144158
* @throws Exception
145159
*/
146160
protected function get_create_statement( $table_name ) {
147-
$create = $this->translator->query( 'SHOW CREATE TABLE ' . $table_name );
148-
return $create[0]->{'Create Table'} . "\n";
161+
$table_name = $this->driver instanceof WP_SQLite_Driver
162+
? $this->driver->get_connection()->quote_identifier( $table_name )
163+
: $table_name;
164+
165+
$create = $this->driver->query( 'SHOW CREATE TABLE ' . $table_name );
166+
$sql = $create[0]->{'Create Table'};
167+
$sql = rtrim( $sql, ';' ); // The old SQLite driver appends a semicolon.
168+
return $sql . ";\n";
149169
}
150170

151171
/**
@@ -156,7 +176,7 @@ protected function get_create_statement( $table_name ) {
156176
* @return \Generator
157177
*/
158178
protected function get_insert_statements( $table_name ) {
159-
$pdo = $this->translator->get_pdo();
179+
$pdo = $this->get_pdo();
160180
$stmt = $pdo->prepare( 'SELECT * FROM ' . $table_name );
161181
$stmt->execute();
162182
// phpcs:ignore
@@ -175,6 +195,8 @@ protected function get_exclude_tables() {
175195
return array_merge(
176196
$exclude_tables,
177197
[
198+
// This list is only needed when exporting with the legacy driver.
199+
// In the new SQLite driver, SHOW TABLES never returns these tables.
178200
'_mysql_data_types_cache',
179201
'sqlite_master',
180202
'sqlite_sequence',
@@ -235,7 +257,7 @@ protected function escape_values( PDO $pdo, $values ) {
235257
* @return string
236258
*/
237259
protected function escape_string( $value ) {
238-
$pdo = $this->translator->get_pdo();
260+
$pdo = $this->get_pdo();
239261
return addcslashes( $pdo->quote( $value ), "\\\n" );
240262
}
241263

@@ -261,9 +283,21 @@ protected function get_dump_comment( $comment ) {
261283
* @return bool
262284
*/
263285
protected function table_has_records( $table_name ) {
264-
$pdo = $this->translator->get_pdo();
286+
$pdo = $this->get_pdo();
265287
$stmt = $pdo->prepare( 'SELECT COUNT(*) FROM ' . $table_name );
266288
$stmt->execute();
267289
return $stmt->fetchColumn() > 0;
268290
}
291+
292+
/**
293+
* Get the PDO instance.
294+
*
295+
* @return PDO
296+
*/
297+
protected function get_pdo() {
298+
if ( $this->driver instanceof WP_SQLite_Translator ) {
299+
return $this->driver->get_pdo();
300+
}
301+
return $this->driver->get_connection()->get_pdo();
302+
}
269303
}

‎src/Import.php‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
use WP_SQLite_Translator;
88

99
class Import {
10+
/**
11+
* The SQLite driver instance.
12+
*
13+
* @var WP_SQLite_Driver|WP_SQLite_Translator
14+
*/
15+
protected $driver;
1016

11-
protected $translator;
1217
protected $args;
1318

1419
public function __construct() {
1520
SQLiteDatabaseIntegrationLoader::load_plugin();
16-
$this->translator = new WP_SQLite_Translator();
21+
$this->driver = SQLiteDriverFactory::create_driver();
1722
}
1823

1924
/**
@@ -46,10 +51,10 @@ public function run( $sql_file_path, $args ) {
4651
*/
4752
protected function execute_statements( $import_file ) {
4853
foreach ( $this->parse_statements( $import_file ) as $statement ) {
49-
$result = $this->translator->query( $statement );
54+
$result = $this->driver->query( $statement );
5055
if ( false === $result ) {
5156
WP_CLI::warning( 'Could not execute statement: ' . $statement );
52-
echo $this->translator->get_error_message();
57+
echo $this->driver->get_error_message();
5358
}
5459
}
5560
}

‎src/SQLiteDatabaseIntegrationLoader.php‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,32 @@ public static function load_plugin() {
8585
// We also need to selectively load the necessary classes from the plugin.
8686
require_once $plugin_directory . '/php-polyfills.php';
8787
require_once $plugin_directory . '/constants.php';
88-
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-lexer.php';
89-
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-query-rewriter.php';
90-
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-translator.php';
91-
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-token.php';
92-
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
88+
89+
$new_driver_enabled = defined( 'WP_SQLITE_AST_DRIVER' ) && WP_SQLITE_AST_DRIVER;
90+
91+
if ( $new_driver_enabled ) {
92+
require_once $plugin_directory . '/version.php';
93+
require_once $plugin_directory . '/wp-includes/parser/class-wp-parser-grammar.php';
94+
require_once $plugin_directory . '/wp-includes/parser/class-wp-parser.php';
95+
require_once $plugin_directory . '/wp-includes/parser/class-wp-parser-node.php';
96+
require_once $plugin_directory . '/wp-includes/parser/class-wp-parser-token.php';
97+
require_once $plugin_directory . '/wp-includes/mysql/class-wp-mysql-token.php';
98+
require_once $plugin_directory . '/wp-includes/mysql/class-wp-mysql-lexer.php';
99+
require_once $plugin_directory . '/wp-includes/mysql/class-wp-mysql-parser.php';
100+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
101+
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-connection.php';
102+
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-configurator.php';
103+
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
104+
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
105+
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';
106+
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-exception.php';
107+
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-reconstructor.php';
108+
} else {
109+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-lexer.php';
110+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-query-rewriter.php';
111+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-translator.php';
112+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-token.php';
113+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
114+
}
93115
}
94116
}

‎src/SQLiteDriverFactory.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Automattic\WP_CLI\SQLite;
4+
5+
use WP_SQLite_Connection;
6+
use WP_SQLite_Driver;
7+
use WP_SQLite_Translator;
8+
9+
class SQLiteDriverFactory {
10+
/**
11+
* Create an instance of the SQLite driver.
12+
*
13+
* @return WP_SQLite_Driver|WP_SQLite_Translator
14+
*/
15+
public static function create_driver() {
16+
$new_driver_enabled = defined( 'WP_SQLITE_AST_DRIVER' ) && WP_SQLITE_AST_DRIVER;
17+
18+
if ( $new_driver_enabled ) {
19+
$connection = new WP_SQLite_Connection(
20+
array(
21+
'path' => FQDB,
22+
)
23+
);
24+
return new WP_SQLite_Driver( $connection, DB_NAME );
25+
}
26+
27+
return new WP_SQLite_Translator();
28+
}
29+
}

0 commit comments

Comments
 (0)