Skip to content

Commit c5b9cd1

Browse files
committed
Add support for the new SQLite driver
1 parent 45f05f5 commit c5b9cd1

5 files changed

Lines changed: 113 additions & 25 deletions

File tree

‎src/Export.php‎

Lines changed: 36 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,21 @@ 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 ) ) {
8593
continue;
8694
}
8795

8896
// Skip tables that are in the exclude_tables list
89-
if ( in_array( $table->name, $exclude_tables, true ) ) {
97+
if ( in_array( $table_name, $exclude_tables, true ) ) {
9098
continue;
9199
}
92100

93-
$this->write_create_table_statement( $handle, $table->name );
94-
$this->write_insert_statements( $handle, $table->name );
101+
$this->write_create_table_statement( $handle, $table_name );
102+
$this->write_insert_statements( $handle, $table_name );
95103
}
96104

97105
fwrite( $handle, sprintf( '-- Dump completed on %s', gmdate( 'c' ) ) );
@@ -144,8 +152,12 @@ protected function write_insert_statements( $handle, $table_name ) {
144152
* @throws Exception
145153
*/
146154
protected function get_create_statement( $table_name ) {
147-
$create = $this->translator->query( 'SHOW CREATE TABLE ' . $table_name );
148-
return $create[0]->{'Create Table'} . "\n";
155+
$table_name = $this->driver instanceof WP_SQLite_Driver
156+
? $this->driver->get_connection()->quote_identifier( $table_name )
157+
: $table_name;
158+
159+
$create = $this->driver->query( 'SHOW CREATE TABLE ' . $table_name );
160+
return $create[0]->{'Create Table'} . ";\n";
149161
}
150162

151163
/**
@@ -156,7 +168,7 @@ protected function get_create_statement( $table_name ) {
156168
* @return \Generator
157169
*/
158170
protected function get_insert_statements( $table_name ) {
159-
$pdo = $this->translator->get_pdo();
171+
$pdo = $this->get_pdo();
160172
$stmt = $pdo->prepare( 'SELECT * FROM ' . $table_name );
161173
$stmt->execute();
162174
// phpcs:ignore
@@ -235,7 +247,7 @@ protected function escape_values( PDO $pdo, $values ) {
235247
* @return string
236248
*/
237249
protected function escape_string( $value ) {
238-
$pdo = $this->translator->get_pdo();
250+
$pdo = $this->get_pdo();
239251
return addcslashes( $pdo->quote( $value ), "\\\n" );
240252
}
241253

@@ -261,9 +273,21 @@ protected function get_dump_comment( $comment ) {
261273
* @return bool
262274
*/
263275
protected function table_has_records( $table_name ) {
264-
$pdo = $this->translator->get_pdo();
276+
$pdo = $this->get_pdo();
265277
$stmt = $pdo->prepare( 'SELECT COUNT(*) FROM ' . $table_name );
266278
$stmt->execute();
267279
return $stmt->fetchColumn() > 0;
268280
}
281+
282+
/**
283+
* Get the PDO instance.
284+
*
285+
* @return PDO
286+
*/
287+
protected function get_pdo() {
288+
if ( $this->driver instanceof WP_SQLite_Translator ) {
289+
return $this->driver->get_pdo();
290+
}
291+
return $this->driver->get_connection()->get_pdo();
292+
}
269293
}

‎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: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,31 @@ 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_supported = class_exists( WP_SQLite_Driver::class );
90+
$new_driver_enabled = $new_driver_supported && defined( 'WP_SQLITE_AST_DRIVER' ) && WP_SQLITE_AST_DRIVER;
91+
92+
if ( $new_driver_enabled ) {
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-ast/class-wp-sqlite-connection.php';
101+
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-configurator.php';
102+
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
103+
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
104+
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';
105+
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-exception.php';
106+
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-reconstructor.php';
107+
} else {
108+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-lexer.php';
109+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-query-rewriter.php';
110+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-translator.php';
111+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-token.php';
112+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
113+
}
93114
}
94115
}

‎src/SQLiteDriverFactory.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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_supported = class_exists( WP_SQLite_Driver::class );
17+
$new_driver_enabled = $new_driver_supported && defined( 'WP_SQLITE_AST_DRIVER' ) && WP_SQLITE_AST_DRIVER;
18+
19+
if ( $new_driver_enabled ) {
20+
$connection = new WP_SQLite_Connection(
21+
array(
22+
'path' => FQDB,
23+
)
24+
);
25+
return new WP_SQLite_Driver( $connection, DB_NAME );
26+
}
27+
28+
return new WP_SQLite_Translator();
29+
}
30+
}

‎src/Tables.php‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44

55
use WP_CLI;
66
use PDO;
7+
use WP_SQLite_Driver;
78
use WP_SQLite_Translator;
89

910
class Tables {
10-
11-
protected $translator;
11+
/**
12+
* The SQLite driver instance.
13+
*
14+
* @var WP_SQLite_Driver|WP_SQLite_Translator
15+
*/
16+
protected $driver;
1217

1318
public function __construct() {
1419
SQLiteDatabaseIntegrationLoader::load_plugin();
15-
$this->translator = new WP_SQLite_Translator();
20+
$this->driver = SQLiteDriverFactory::create_driver();
1621
}
1722

1823
/**
@@ -21,7 +26,10 @@ public function __construct() {
2126
* @return PDO
2227
*/
2328
protected function get_pdo() {
24-
return $this->translator->get_pdo();
29+
if ( $this->driver instanceof WP_SQLite_Translator ) {
30+
return $this->driver->get_pdo();
31+
}
32+
return $this->driver->get_connection()->get_pdo();
2533
}
2634

2735
/**

0 commit comments

Comments
 (0)