Skip to content

Commit

Permalink
refactor: extract database creation query to MySqlQueriesBuilders for…
Browse files Browse the repository at this point in the history
… improved code organization
  • Loading branch information
Kremilly committed Jan 7, 2025
1 parent 5959f19 commit 9e00a19
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/handlers/import_handlers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use regex::Regex;

use crate::constants::regexp::RegExp;
use crate::{
constants::regexp::RegExp,
handlers::queries_builders::MySqlQueriesBuilders,
};

pub struct ImportHandlers {
dbname: String,
Expand All @@ -27,7 +30,7 @@ impl ImportHandlers {
caps[2].to_string()
};

format!("CREATE DATABASE IF NOT EXISTS `{}`;", db_name)
MySqlQueriesBuilders.create_database_not_exists(&db_name)
});

let dump_content = use_db_regex.replace_all(&content, |caps: &regex::Captures| {
Expand Down
4 changes: 4 additions & 0 deletions src/handlers/queries_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ impl MySqlQueriesBuilders {
format!("DROP TABLE IF EXISTS `{}`;", table)
}

pub fn create_database_not_exists(&self, dbname: &str) -> String {
format!("CREATE DATABASE IF NOT EXISTS `{}`;", dbname)
}

pub fn create_database(&self, dbname: &str) -> Result<(String, String), String> {
let create_db = format!("CREATE DATABASE IF NOT EXISTS `{}`;\n", dbname);
let use_db = format!("USE `{}`;", dbname);
Expand Down

0 comments on commit 9e00a19

Please sign in to comment.