From 0c8160538be28e67adc00000c92c573b1b5d688a Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Fri, 19 Jul 2024 17:53:33 +0700 Subject: [PATCH] Refactoring changes --- src/DB_Command.php | 2 +- src/WP_SQLite_Export.php | 72 +++++++++++++++++++++++++++++----------- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index ba1f2324..8c6b7d3d 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -599,7 +599,7 @@ public function export( $args, $assoc_args ) { if ( WP_SQLite_Export::get_sqlite_version() ) { $export = new WP_SQLite_Export(); - $export->run(); + $export->run( $result_file, $assoc_args ); return; } diff --git a/src/WP_SQLite_Export.php b/src/WP_SQLite_Export.php index 01263eaf..1a260385 100644 --- a/src/WP_SQLite_Export.php +++ b/src/WP_SQLite_Export.php @@ -3,19 +3,42 @@ class WP_SQLite_Export extends WP_SQLite_Base { - public function run() { - $this->load_dependencies(); - WP_CLI::line( 'Exporting database...' ); + private $unsupported_arguments = [ + 'fields', + 'include-tablespaces', + 'defaults', + 'db_user', + 'db_pass', + 'tables', + 'exclude-tables' + ]; - $translator = new WP_SQLite_Translator(); + /** + * Run the export command. + * + * @param string $result_file The file to write the exported data to. + * @param array $args The arguments passed to the command. + * + * @return void + * @throws Exception + */ + public function run( $result_file, $args ) { - $result = $translator->query('SHOW TABLES '); - $pdo = $translator->get_pdo(); + if ( array_intersect_key( $args, array_flip( $this->unsupported_arguments ) ) ) { + WP_CLI::error( + sprintf( + 'The following arguments are not supported by SQLite exports: %s', + implode( ', ', $this->unsupported_arguments ) + ) + ); + return; + } - // Stream into a file - $handle = fopen( 'export.sql', 'w' ); + $this->load_dependencies(); + $translator = new WP_SQLite_Translator(); + $handle = fopen( 'export.sql', 'w' ); - foreach ( $result as $table ) { + foreach ( $translator->query('SHOW TABLES') as $table ) { $ignore_tables = [ '_mysql_data_types_cache', @@ -27,25 +50,36 @@ public function run() { continue; } - $create = $translator->query('SHOW CREATE TABLE ' . $table->name ); - var_dump($create); fwrite($handle, "DROP TABLE IF EXISTS `" . $table->name ."`;\n"); - fwrite( $handle, $create[0]->{'Create Table'} . "\n" ); - - $stmt = $pdo->prepare('SELECT * FROM ' . $table->name ); - $stmt->execute(); - while ( $row = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT) ) { - // Process the row here - // Rows are fetched in batches from the server - $insert_statement = sprintf("INSERT INTO `%1s` VALUES (%2s);", $table->name, $this->escape_values( $pdo, $row )); + fwrite( $handle, $this->get_create_statement( $table, $translator ) . "\n" ); + foreach( $this->get_insert_statements( $table, $translator->get_pdo() ) as $insert_statement ) { fwrite( $handle, $insert_statement . "\n" ); } } + if ( $args['porcelain'] ) { + WP_CLI::line( $result_file ); + } else { + WP_CLI::line( 'Export complete. File written to ' . $result_file ); + } + fclose( $handle ); } + protected function get_create_statement( $table, $translator ) { + $create = $translator->query('SHOW CREATE TABLE ' . $table->name ); + return $create[0]->{'Create Table'}; + } + + protected function get_insert_statements( $table, $pdo ) { + $stmt = $pdo->prepare('SELECT * FROM ' . $table->name ); + $stmt->execute(); + while ( $row = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT) ) { + yield sprintf("INSERT INTO `%1s` VALUES (%2s);", $table->name, $this->escape_values( $pdo, $row )); + } + } + /** * Escape values for insert statement *