Skip to content

Commit

Permalink
Refactoring changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenpf committed Jul 19, 2024
1 parent 6543dd9 commit 0c81605
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
72 changes: 53 additions & 19 deletions src/WP_SQLite_Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
*
Expand Down

0 comments on commit 0c81605

Please sign in to comment.