From 52122b217e3513bb80c02cdfaa7e7dc02e28af41 Mon Sep 17 00:00:00 2001 From: Kremilly Date: Sat, 7 Dec 2024 00:51:12 -0300 Subject: [PATCH] feat: introduce RegExp struct for regex patterns and update import handlers to use it --- src/consts/mod.rs | 3 ++- src/consts/regexp.rs | 9 +++++++++ src/engine/import.rs | 3 ++- src/engine/transfer.rs | 3 ++- src/helpers/import_handlers.rs | 6 ++++-- 5 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 src/consts/regexp.rs diff --git a/src/consts/mod.rs b/src/consts/mod.rs index 4daaf7b..7d05e2e 100644 --- a/src/consts/mod.rs +++ b/src/consts/mod.rs @@ -1 +1,2 @@ -pub mod global; \ No newline at end of file +pub mod global; +pub mod regexp; \ No newline at end of file diff --git a/src/consts/regexp.rs b/src/consts/regexp.rs new file mode 100644 index 0000000..b01b1b7 --- /dev/null +++ b/src/consts/regexp.rs @@ -0,0 +1,9 @@ +pub struct RegExp; + +impl RegExp { + + pub const USE_CASE: &'static str = r"(?i)(USE\s+`?)(\w+)(`?)"; + pub const CREATE_TABLE: &'static str = r"(?i)CREATE TABLE\s+`?(\w+)`?"; + pub const CREATE_DATABASE_CASES: &'static str = r"(?i)CREATE DATABASE\s+(`?)(\w+)(`?)\s*(IF NOT EXISTS)?;"; + +} diff --git a/src/engine/import.rs b/src/engine/import.rs index b1aa1d9..7867eec 100644 --- a/src/engine/import.rs +++ b/src/engine/import.rs @@ -22,6 +22,7 @@ use std::{ }; use crate::{ + consts::regexp::RegExp, engine::connection::Connection, helpers::import_handlers::ImportHandlers, @@ -98,7 +99,7 @@ impl Import { let dump_content = ImportHandlers::new(&self.dbname, &dump_content).check_db_name(); - let create_table_regex = Regex::new(r"(?i)CREATE TABLE\s+`?(\w+)`?").unwrap(); + let create_table_regex = Regex::new(RegExp::CREATE_TABLE).unwrap(); for statement in dump_content.split(';') { let trimmed = statement.trim(); diff --git a/src/engine/transfer.rs b/src/engine/transfer.rs index bf4e7e1..3261a45 100644 --- a/src/engine/transfer.rs +++ b/src/engine/transfer.rs @@ -22,6 +22,7 @@ use std::{ }; use crate::{ + consts::regexp::RegExp, engine::connection::Connection, helpers::import_handlers::ImportHandlers, @@ -106,7 +107,7 @@ impl Transfer { let dump_content = ImportHandlers::new(&self.dbname, &dump_content).check_db_name(); - let create_table_regex = Regex::new(r"(?i)CREATE TABLE\s+`?(\w+)`?").unwrap(); + let create_table_regex = Regex::new(RegExp::CREATE_TABLE).unwrap(); for statement in dump_content.split(';') { let trimmed = statement.trim(); diff --git a/src/helpers/import_handlers.rs b/src/helpers/import_handlers.rs index 8e18990..02eb335 100644 --- a/src/helpers/import_handlers.rs +++ b/src/helpers/import_handlers.rs @@ -1,5 +1,7 @@ use regex::Regex; +use crate::consts::regexp::RegExp; + pub struct ImportHandlers { dbname: String, dump_content: String, @@ -15,8 +17,8 @@ impl ImportHandlers { } pub fn check_db_name(&self) -> String { - let db_regex = Regex::new(r"(?i)CREATE DATABASE\s+(`?)(\w+)(`?)\s*(IF NOT EXISTS)?;").unwrap(); - let use_db_regex = Regex::new(r"(?i)(USE\s+`?)(\w+)(`?)").unwrap(); + let use_db_regex = Regex::new(RegExp::USE_CASE).unwrap(); + let db_regex = Regex::new(RegExp::CREATE_DATABASE_CASES).unwrap(); let content = db_regex.replace_all(&self.dump_content, |caps: ®ex::Captures| { let db_name = if &caps[2] != self.dbname {