Skip to content

Commit

Permalink
feat: introduce RegExp struct for regex patterns and update import ha…
Browse files Browse the repository at this point in the history
…ndlers to use it
  • Loading branch information
Kremilly committed Dec 7, 2024
1 parent af160da commit 52122b2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/consts/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod global;
pub mod global;
pub mod regexp;
9 changes: 9 additions & 0 deletions src/consts/regexp.rs
Original file line number Diff line number Diff line change
@@ -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)?;";

}
3 changes: 2 additions & 1 deletion src/engine/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::{
};

use crate::{
consts::regexp::RegExp,
engine::connection::Connection,
helpers::import_handlers::ImportHandlers,

Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/engine/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::{
};

use crate::{
consts::regexp::RegExp,
engine::connection::Connection,
helpers::import_handlers::ImportHandlers,

Expand Down Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions src/helpers/import_handlers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use regex::Regex;

use crate::consts::regexp::RegExp;

pub struct ImportHandlers {
dbname: String,
dump_content: String,
Expand All @@ -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: &regex::Captures| {
let db_name = if &caps[2] != self.dbname {
Expand Down

0 comments on commit 52122b2

Please sign in to comment.