Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactoring
  • Loading branch information
jeroenpf committed Jul 25, 2024
commit 46863b86fcc312eb74ec08c9307997d4fc0fe061
33 changes: 25 additions & 8 deletions src/SQLite/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,54 @@ class Import extends Base {
protected $translator;
protected $args;

public function __construct() {
$this->load_dependencies();
$this->translator = new WP_SQLite_Translator();
}

/**
* Execute the import command for SQLite.
*
* @throws Exception
* @param string $sql_file_path The path to the SQL dump file.
* @param array $args The arguments passed to the command.
*
* @return void
*/
public function run( $sql_file_path, $args ) {
$this->args = $args;
$this->check_arguments( $args );
$this->load_dependencies();
$translator = new WP_SQLite_Translator();

$is_stdin = '-' === $sql_file_path;
$import_file = $is_stdin ? 'php://stdin' : $sql_file_path;

$this->execute_statements( $import_file );

$imported_from = $is_stdin ? 'STDIN' : $sql_file_path;
WP_CLI::success( sprintf( "Imported from '%s'.", $imported_from ) );
}

/**
* Execute SQL statements from an SQL dump file.
*
* @param $import_file
*
* @return void
* @throws Exception
*/
protected function execute_statements( $import_file ) {
foreach ( $this->parse_statements( $import_file ) as $statement ) {
$result = $translator->query( $statement );
$result = $this->translator->query( $statement );
if ( false === $result ) {
WP_CLI::warning( 'Could not execute statement: ' . $statement );
}
}

$imported_from = $is_stdin ? 'STDIN' : $sql_file_path;
WP_CLI::success( sprintf( "Imported from '%s'.", $imported_from ) );
}

/**
* Parse SQL statements from an SQL dump file.
* @param string $sql_file_path The path to the SQL dump file.
*
* @return Generator A generator that yields SQL statements.
* @throws Exception
*/
public function parse_statements( $sql_file_path ) {

Expand Down