Skip to content

Commit

Permalink
refactor: replace QueriesBuilders with MySqlQueriesBuilders for impro…
Browse files Browse the repository at this point in the history
…ved SQL query handling
  • Loading branch information
Kremilly committed Jan 1, 2025
1 parent 90f3657 commit 9a47294
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 27 deletions.
9 changes: 3 additions & 6 deletions src/core/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ use mysql::{

use crate::{
utils::file::FileUtils,
helpers::configs::Configs,
ui::success_alerts::SuccessAlerts,
handlers::export_handlers::ExportHandlers,
sql_queries::mysql_queries_builders::MySqlQueriesBuilders,

core::{
encrypt::Encrypt,
connection::Connection,
},

helpers::{
configs::Configs,
queries_builders::QueriesBuilders,
},
};

Expand Down Expand Up @@ -86,7 +83,7 @@ impl Export {
export_handlers.comments_header(writer.as_write())?;
export_handlers.write_create_new_database(writer.as_write())?;

let tables: Vec<String> = conn.query(QueriesBuilders.show_tables())?;
let tables: Vec<String> = conn.query(MySqlQueriesBuilders.show_tables())?;
let ignore_tables = Configs.list("exports", "ignore_tables").unwrap_or_default();

for table in tables {
Expand Down
19 changes: 8 additions & 11 deletions src/handlers/export_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ use mysql::{

use crate::{
utils::date::Date,
helpers::configs::Configs,
handlers::html_handlers::HTMLHandlers,

helpers::{
configs::Configs,
queries_builders::QueriesBuilders,
},
sql_queries::mysql_queries_builders::MySqlQueriesBuilders,
};

pub enum Writer {
Expand Down Expand Up @@ -102,7 +99,7 @@ impl ExportHandlers {

pub fn write_create_new_database(&self, writer: &mut dyn Write) -> Result<(), Box<dyn Error>> {
if self.database_if_not_exists {
let queries = QueriesBuilders.create_database(&self.dbname)?;
let queries = MySqlQueriesBuilders.create_database(&self.dbname)?;

write!(writer, "{}", queries.0)?;
writeln!(writer, "{}", queries.1)?;
Expand All @@ -115,7 +112,7 @@ impl ExportHandlers {
pub fn write_inserts_for_table(&self, table: &str, conn: &mut PooledConn, writer: &mut dyn Write) -> Result<(), Box<dyn Error>> {
if self.dump_data {
let rows: Vec<Row> = conn.query(
QueriesBuilders.select(table, None, None)
MySqlQueriesBuilders.select(table, None, None)
)?;

if rows.is_empty() {
Expand All @@ -135,9 +132,9 @@ impl ExportHandlers {
}).collect();

let line = if self.insert_ignore_into {
QueriesBuilders.insert_into(table, values, true)
MySqlQueriesBuilders.insert_into(table, values, true)
} else {
QueriesBuilders.insert_into(table, values, false)
MySqlQueriesBuilders.insert_into(table, values, false)
};

writeln!(writer, "{}", line)?;
Expand All @@ -152,10 +149,10 @@ impl ExportHandlers {
writeln!(writer, "-- Exporting the table: `{}`", table)?;

if self.drop_table_if_exists {
writeln!(writer, "{}", QueriesBuilders.drop_table(table))?;
writeln!(writer, "{}", MySqlQueriesBuilders.drop_table(table))?;
}

let row: Row = conn.query_first(QueriesBuilders.show_create_table(table))?.unwrap();
let row: Row = conn.query_first(MySqlQueriesBuilders.show_create_table(table))?.unwrap();
let create_table: String = row.get(1).expect("Error retrieving CREATE TABLE");
writeln!(writer, "{};\n", create_table)?;

Expand Down
3 changes: 1 addition & 2 deletions src/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod env;
pub mod configs;
pub mod queries_builders;
pub mod configs;
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod handlers;
mod args_cli;
mod dump_sync;
mod constants;
mod sql_queries;

use std::error::Error;
use crate::dump_sync::DumpSync;
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/scan_xss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
core::connection::Connection,
plugins::reports_xss::ReportsXSS,
handlers::scan_handlers::ScanHandlers,
helpers::queries_builders::QueriesBuilders,
sql_queries::mysql_queries_builders::MySqlQueriesBuilders,
};

pub struct ScanXSS {
Expand Down Expand Up @@ -90,7 +90,7 @@ impl ScanXSS {

let mut detections = Vec::new();

let query = QueriesBuilders.select(&self.table, self.offset.map(|o| o as usize), self.limit.map(|l| l as usize));
let query = MySqlQueriesBuilders.select(&self.table, self.offset.map(|o| o as usize), self.limit.map(|l| l as usize));
let rows: Vec<Row> = conn.query(query)?;

for (row_index, row) in rows.iter().enumerate() {
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use mysql::{
use crate::{
core::connection::Connection,
ui::schema_alerts::SchemaAlerts,
helpers::queries_builders::QueriesBuilders,
sql_queries::mysql_queries_builders::MySqlQueriesBuilders,
};

#[derive(Serialize, Debug)]
Expand Down Expand Up @@ -81,12 +81,12 @@ impl Schema {
}.create_pool()?;

let mut conn = pool.get_conn()?;
let tables: Vec<String> = conn.query(QueriesBuilders.get_table_names())?;
let tables: Vec<String> = conn.query(MySqlQueriesBuilders.get_table_names())?;

let mut schema = Vec::new();

for table in tables {
let columns: Vec<(String, String, String, String)> = conn.query(QueriesBuilders.table_info(&table))?;
let columns: Vec<(String, String, String, String)> = conn.query(MySqlQueriesBuilders.table_info(&table))?;

let column_data: Vec<Column> = columns
.iter()
Expand All @@ -98,7 +98,7 @@ impl Schema {
})
.collect();

let foreign_keys: Vec<(String, String, String)> = conn.query(QueriesBuilders::foreign_key_info(&table))?;
let foreign_keys: Vec<(String, String, String)> = conn.query(MySqlQueriesBuilders::foreign_key_info(&table))?;

let foreign_key_data: Vec<ForeignKey> = foreign_keys
.iter()
Expand Down
1 change: 1 addition & 0 deletions src/sql_queries/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod mysql_queries_builders;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub struct QueriesBuilders;
pub struct MySqlQueriesBuilders;

impl QueriesBuilders {
impl MySqlQueriesBuilders {

pub fn show_tables(&self) -> String {
"SHOW TABLES".to_string()
Expand Down

0 comments on commit 9a47294

Please sign in to comment.