From 283fb830c93d50cbdc97ea950785cb8054c31589 Mon Sep 17 00:00:00 2001 From: mjovanc Date: Sat, 23 Nov 2024 17:01:41 +0100 Subject: [PATCH] Cleanup --- njord/src/column.rs | 13 +++++++------ njord/src/mariadb/delete.rs | 1 - njord/src/mariadb/insert.rs | 6 +++--- njord/src/mariadb/select.rs | 5 ++--- njord/src/mariadb/update.rs | 6 ++---- njord/src/mariadb/util.rs | 3 +-- njord/src/mssql/delete.rs | 5 ++--- njord/src/mssql/insert.rs | 6 +++--- njord/src/mssql/mod.rs | 3 ++- njord/src/mssql/update.rs | 5 ++--- njord/src/mssql/util.rs | 2 +- njord/src/mysql/delete.rs | 5 ++--- njord/src/mysql/insert.rs | 4 ++-- njord/src/mysql/select.rs | 1 - njord/src/mysql/update.rs | 5 ++--- njord/src/mysql/util.rs | 2 +- njord/src/oracle/delete.rs | 5 ++--- njord/src/oracle/insert.rs | 4 ++-- njord/src/oracle/mod.rs | 4 ++-- njord/src/oracle/select.rs | 4 ++-- njord/src/oracle/update.rs | 6 ++---- njord/src/oracle/util.rs | 2 +- njord/src/query.rs | 2 +- njord/src/sqlite/delete.rs | 5 ++--- njord/src/sqlite/insert.rs | 4 ++-- njord/src/sqlite/update.rs | 6 ++---- njord/src/sqlite/util.rs | 2 +- 27 files changed, 51 insertions(+), 65 deletions(-) diff --git a/njord/src/column.rs b/njord/src/column.rs index 0147f0ff..7767d61a 100644 --- a/njord/src/column.rs +++ b/njord/src/column.rs @@ -3,12 +3,13 @@ use crate::query::QueryBuilder; /// Define the enum to represent a column as either a String or SelectQueryBuilder #[derive(Clone)] pub enum Column<'a> { + /// Column as a String Text(String), - // Subquery with alias + /// Subquery with Alias SubQuery(Box + 'a>, String), } -// Implement the build method to convert the enum to a string +/// Implement the build method to convert the enum to a string impl<'a> Column<'a> { /// Helper function to convert the columns to a string pub fn build(&self) -> String { @@ -21,21 +22,21 @@ impl<'a> Column<'a> { } } -// Implementation of fmt::Display for Column +/// Implementation of fmt::Display for Column impl<'a> std::fmt::Display for Column<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.build()) } } -// Implementation of PartialEq for Column +/// Implementation of PartialEq for Column impl<'a> PartialEq for Column<'a> { fn eq(&self, other: &Self) -> bool { self.build() == other.build() } } -// Implementation of PartialEq for Column +/// Implementation of PartialEq for Column impl<'a> PartialEq for Column<'a> { fn eq(&self, other: &String) -> bool { match self { @@ -45,7 +46,7 @@ impl<'a> PartialEq for Column<'a> { } } -// Implementation of PartialEq<&str> for Column +/// Implementation of PartialEq<&str> for Column impl<'a> PartialEq<&str> for Column<'a> { fn eq(&self, other: &&str) -> bool { match self { diff --git a/njord/src/mariadb/delete.rs b/njord/src/mariadb/delete.rs index 65f699f1..f6bbaa9e 100644 --- a/njord/src/mariadb/delete.rs +++ b/njord/src/mariadb/delete.rs @@ -158,7 +158,6 @@ impl<'a, T: Table + Default> DeleteQueryBuilder<'a, T> { info!("{}", query); - // Execute SQL let _ = conn.query_drop(&query.to_string()); Ok(()) diff --git a/njord/src/mariadb/insert.rs b/njord/src/mariadb/insert.rs index 35f90654..2cdab829 100644 --- a/njord/src/mariadb/insert.rs +++ b/njord/src/mariadb/insert.rs @@ -96,7 +96,7 @@ pub fn into<'a, T: Table + Default>( subquery: Box + 'a>, ) -> Result { let statement = generate_insert_into_statement::(columns, subquery); - let sql = statement.unwrap(); + let sql = statement?; // FIXME: Convert to transaction let _ = conn.query_drop(&sql); @@ -104,7 +104,7 @@ pub fn into<'a, T: Table + Default>( info!("Inserted into table, done."); // FIXME: Return the number of rows affected - return Ok(sql); + Ok(sql) } /// Generates an SQL INSERT INTO statement for a given subquery. @@ -197,7 +197,7 @@ fn generate_statement(table_row: &T, first_statement: bool) -> Result< format!("({})", values_str) }; - debug!("{}", sql); // For debugging purposes + debug!("{}", sql); Ok(sql) } diff --git a/njord/src/mariadb/select.rs b/njord/src/mariadb/select.rs index 7f91d677..1ac424ce 100644 --- a/njord/src/mariadb/select.rs +++ b/njord/src/mariadb/select.rs @@ -40,7 +40,7 @@ use crate::{ }; use std::{collections::HashMap, sync::Arc}; -use log::info; +use log::{debug, info}; use mysql::prelude::*; use mysql::{Error, PooledConn, Value}; @@ -360,7 +360,7 @@ impl<'a, T: Table + Default> SelectQueryBuilder<'a, T> { pub fn build(&mut self, conn: &mut PooledConn) -> Result, Error> { let final_query = self.build_query(); - info!("{}", final_query); + debug!("{}", final_query); raw_execute(&final_query, conn) } @@ -426,7 +426,6 @@ pub fn raw_execute(sql: &str, conn: &mut PooledConn) -> Resu instance.set_column_value(column.name_str().as_ref(), &column_value_str); } - // Move `instance` to the `results` only after it is fully set up results.push(instance); } diff --git a/njord/src/mariadb/update.rs b/njord/src/mariadb/update.rs index 944e3e97..a54e6dc5 100644 --- a/njord/src/mariadb/update.rs +++ b/njord/src/mariadb/update.rs @@ -39,7 +39,7 @@ use crate::{ }, }; -use log::info; +use log::{debug, info}; use mysql::{prelude::Queryable, PooledConn}; use crate::table::Table; @@ -186,7 +186,6 @@ impl<'a, T: Table + Default> UpdateQueryBuilder<'a, T> { }; set_fields.push(format!("{} = {}", column, formatted_value)); } else { - // Handle the case when the column doesn't exist in the table eprintln!("Column '{}' does not exist in the table", column); } } @@ -217,9 +216,8 @@ impl<'a, T: Table + Default> UpdateQueryBuilder<'a, T> { format!("{} {}", limit_str, offset_str), ); - info!("{}", query); + debug!("{}", query); - // Prepare SQL statement let _ = conn.query_drop(query.as_str()); Ok(()) diff --git a/njord/src/mariadb/util.rs b/njord/src/mariadb/util.rs index 76b0df07..2ea4e009 100644 --- a/njord/src/mariadb/util.rs +++ b/njord/src/mariadb/util.rs @@ -102,7 +102,7 @@ pub fn generate_order_by_str(order_by: &Option, String>>) -> String::new() }; - return order_by_str; + order_by_str } /// Generates an SQL LIMIT clause string based on the provided limit count. @@ -183,7 +183,6 @@ mod tests { let condition = Condition::Eq("age".to_string(), Value::Literal("30".to_string())); let _result = generate_where_condition_str(Some(condition)); // TODO: need to fix this later // assert_eq!(result, format!("WHERE {}", condition.build())); - // Test when condition is None let result = generate_where_condition_str(None); assert_eq!(result, ""); diff --git a/njord/src/mssql/delete.rs b/njord/src/mssql/delete.rs index 36016cc4..76a35607 100644 --- a/njord/src/mssql/delete.rs +++ b/njord/src/mssql/delete.rs @@ -34,7 +34,7 @@ use crate::{ mssql::util::{generate_where_condition_str, remove_quotes_and_backslashes}, }; -use log::info; +use log::{debug, info}; use crate::table::Table; @@ -111,9 +111,8 @@ impl<'a, T: Table + Default> DeleteQueryBuilder<'a, T> { // Construct the query based on defined variables above let query = format!("DELETE FROM {} {}", table_name_str, where_condition_str,); - info!("{}", query); + debug!("{}", query); - // Execute SQL match conn.client.execute(&query, &[]).await { Ok(_) => Ok(()), Err(err) => Err(err.to_string()), diff --git a/njord/src/mssql/insert.rs b/njord/src/mssql/insert.rs index 0b232e1c..19ac2867 100644 --- a/njord/src/mssql/insert.rs +++ b/njord/src/mssql/insert.rs @@ -70,10 +70,10 @@ pub async fn insert( debug!("{}", joined_statements); match conn.client.query(&joined_statements, &[]).await { - Ok(_) => return Ok("Inserted into table, done.".to_string()), + Ok(_) => Ok("Inserted into table, done.".to_string()), Err(err) => { eprintln!("Error: {}", err); - return Err(MSSQLError::InvalidQuery); + Err(MSSQLError::InvalidQuery) } } } @@ -102,7 +102,7 @@ pub fn into<'a, T: Table + Default>( info!("Inserted into table, done."); // FIXME: Return the number of rows affected - return Ok(sql); + Ok(sql) } /// Generates an SQL INSERT INTO statement for a given subquery. diff --git a/njord/src/mssql/mod.rs b/njord/src/mssql/mod.rs index c52894f3..5919a46c 100644 --- a/njord/src/mssql/mod.rs +++ b/njord/src/mssql/mod.rs @@ -76,7 +76,8 @@ pub async fn open(connection_string: &str) -> Result { return Err(err); } }; - return Ok(Connection { client }); + + Ok(Connection { client }) } /// Executes a raw SQL query and returns a vector of table rows. diff --git a/njord/src/mssql/update.rs b/njord/src/mssql/update.rs index 69288655..2ba60277 100644 --- a/njord/src/mssql/update.rs +++ b/njord/src/mssql/update.rs @@ -36,7 +36,7 @@ use crate::{ mssql::util::{generate_where_condition_str, remove_quotes_and_backslashes}, }; -use log::info; +use log::{debug, info}; use crate::table::Table; @@ -170,9 +170,8 @@ impl<'a, T: Table + Default> UpdateQueryBuilder<'a, T> { table_name_str, set, where_condition_str, ); - info!("{}", query); + debug!("{}", query); - // Prepare SQL statement match self.conn.client.execute(query.as_str(), &[]).await { Ok(_) => Ok("Success!".to_string()), Err(_) => Err("Could not execute...".to_string()), diff --git a/njord/src/mssql/util.rs b/njord/src/mssql/util.rs index 1b68e57e..ea21da53 100644 --- a/njord/src/mssql/util.rs +++ b/njord/src/mssql/util.rs @@ -102,7 +102,7 @@ pub fn generate_order_by_str(order_by: &Option, String>>) -> String::new() }; - return order_by_str; + order_by_str } /// Generates an SQL HAVING clause string based on the provided group by flag and condition. diff --git a/njord/src/mysql/delete.rs b/njord/src/mysql/delete.rs index 816a9e1b..f76a1fd0 100644 --- a/njord/src/mysql/delete.rs +++ b/njord/src/mysql/delete.rs @@ -39,7 +39,7 @@ use crate::{ }, }; -use log::info; +use log::{debug, info}; use mysql::{prelude::Queryable, PooledConn}; use crate::table::Table; @@ -156,9 +156,8 @@ impl<'a, T: Table + Default> DeleteQueryBuilder<'a, T> { format!("{} {}", limit_str, offset_str), ); - info!("{}", query); + debug!("{}", query); - // Execute SQL let _ = conn.query_drop(&query.to_string()); Ok(()) diff --git a/njord/src/mysql/insert.rs b/njord/src/mysql/insert.rs index fba655c7..3affaefc 100644 --- a/njord/src/mysql/insert.rs +++ b/njord/src/mysql/insert.rs @@ -104,7 +104,7 @@ pub fn into<'a, T: Table + Default>( info!("Inserted into table, done."); // FIXME: Return the number of rows affected - return Ok(sql); + Ok(sql) } /// Generates an SQL INSERT INTO statement for a given subquery. @@ -197,7 +197,7 @@ fn generate_statement(table_row: &T, first_statement: bool) -> Result< format!("({})", values_str) }; - debug!("{}", sql); // For debugging purposes + debug!("{}", sql); Ok(sql) } diff --git a/njord/src/mysql/select.rs b/njord/src/mysql/select.rs index d3c5c05c..a5b5dc24 100644 --- a/njord/src/mysql/select.rs +++ b/njord/src/mysql/select.rs @@ -426,7 +426,6 @@ pub fn raw_execute(sql: &str, conn: &mut PooledConn) -> Resu instance.set_column_value(column.name_str().as_ref(), &column_value_str); } - // Move `instance` to the `results` only after it is fully set up results.push(instance); } diff --git a/njord/src/mysql/update.rs b/njord/src/mysql/update.rs index 4281f9ad..8df117d1 100644 --- a/njord/src/mysql/update.rs +++ b/njord/src/mysql/update.rs @@ -39,7 +39,7 @@ use crate::{ }, }; -use log::info; +use log::{debug, info}; use mysql::{prelude::Queryable, PooledConn}; use crate::table::Table; @@ -217,9 +217,8 @@ impl<'a, T: Table + Default> UpdateQueryBuilder<'a, T> { format!("{} {}", limit_str, offset_str), ); - info!("{}", query); + debug!("{}", query); - // Prepare SQL statement let _ = conn.query_drop(query.as_str()); Ok(()) diff --git a/njord/src/mysql/util.rs b/njord/src/mysql/util.rs index 76b0df07..6a89505b 100644 --- a/njord/src/mysql/util.rs +++ b/njord/src/mysql/util.rs @@ -102,7 +102,7 @@ pub fn generate_order_by_str(order_by: &Option, String>>) -> String::new() }; - return order_by_str; + order_by_str } /// Generates an SQL LIMIT clause string based on the provided limit count. diff --git a/njord/src/oracle/delete.rs b/njord/src/oracle/delete.rs index b164f3a5..e486a06c 100644 --- a/njord/src/oracle/delete.rs +++ b/njord/src/oracle/delete.rs @@ -39,7 +39,7 @@ use crate::{ }, }; -use log::info; +use log::{debug, info}; use oracle::Connection; use crate::table::Table; @@ -156,9 +156,8 @@ impl<'a, T: Table + Default> DeleteQueryBuilder<'a, T> { format!("{} {}", limit_str, offset_str), ); - info!("{}", query); + debug!("{}", query); - // Execute SQL let _ = conn.execute(&query, &[]); Ok(()) diff --git a/njord/src/oracle/insert.rs b/njord/src/oracle/insert.rs index 0ccf6be0..9e458148 100644 --- a/njord/src/oracle/insert.rs +++ b/njord/src/oracle/insert.rs @@ -107,7 +107,7 @@ pub fn into<'a, T: Table + Default>( info!("Inserted into table, done."); // FIXME: Return the number of rows affected - return Ok(sql); + Ok(sql) } /// Generates an SQL INSERT INTO statement for a given subquery. @@ -200,7 +200,7 @@ fn generate_statement(table_row: &T, first_statement: bool) -> Result< format!("({})", values_str) }; - debug!("{}", sql); // For debugging purposes + debug!("{}", sql); Ok(sql) } diff --git a/njord/src/oracle/mod.rs b/njord/src/oracle/mod.rs index f951c230..9c20d090 100644 --- a/njord/src/oracle/mod.rs +++ b/njord/src/oracle/mod.rs @@ -64,12 +64,12 @@ pub fn open(username: &str, password: &str, connect_string: &str) -> Result { println!("Successfully connected to Oracle database"); - return Ok(conn); + Ok(conn) } Err(err) => { eprintln!("Error: {}", err); - return Err(err); + Err(err) } } } diff --git a/njord/src/oracle/select.rs b/njord/src/oracle/select.rs index 946ef45d..d7ef0834 100644 --- a/njord/src/oracle/select.rs +++ b/njord/src/oracle/select.rs @@ -40,7 +40,7 @@ use crate::{ }; use std::{collections::HashMap, sync::Arc}; -use log::info; +use log::{debug, info}; use oracle::{Connection, Error}; use crate::table::Table; @@ -332,7 +332,7 @@ impl<'a, T: Table + Default> SelectQueryBuilder<'a, T> { pub fn build(self, conn: &Connection) -> Result, Error> { let final_query = self.build_query(); - info!("{}", final_query); + debug!("{}", final_query); raw_execute(&final_query, conn) } diff --git a/njord/src/oracle/update.rs b/njord/src/oracle/update.rs index 97682993..8b777865 100644 --- a/njord/src/oracle/update.rs +++ b/njord/src/oracle/update.rs @@ -39,7 +39,7 @@ use crate::{ }, }; -use log::info; +use log::{debug, info}; use oracle::Connection; use crate::table::Table; @@ -186,7 +186,6 @@ impl<'a, T: Table + Default> UpdateQueryBuilder<'a, T> { }; set_fields.push(format!("{} = {}", column, formatted_value)); } else { - // Handle the case when the column doesn't exist in the table eprintln!("Column '{}' does not exist in the table", column); } } @@ -217,9 +216,8 @@ impl<'a, T: Table + Default> UpdateQueryBuilder<'a, T> { format!("{} {}", limit_str, offset_str), ); - info!("{}", query); + debug!("{}", query); - // Prepare SQL statement let _ = conn.execute(query.as_str(), &[]); Ok(()) diff --git a/njord/src/oracle/util.rs b/njord/src/oracle/util.rs index 76b0df07..6a89505b 100644 --- a/njord/src/oracle/util.rs +++ b/njord/src/oracle/util.rs @@ -102,7 +102,7 @@ pub fn generate_order_by_str(order_by: &Option, String>>) -> String::new() }; - return order_by_str; + order_by_str } /// Generates an SQL LIMIT clause string based on the provided limit count. diff --git a/njord/src/query.rs b/njord/src/query.rs index e5868f7f..69340c4d 100644 --- a/njord/src/query.rs +++ b/njord/src/query.rs @@ -36,7 +36,7 @@ pub trait QueryBuilder<'a>: QueryBuilderClone<'a> { fn to_sql(&self) -> String; } -// A helper trait to enable cloning of `Box` +/// A helper trait to enable cloning of `Box` pub trait QueryBuilderClone<'a> { fn clone_box(&self) -> Box + 'a>; } diff --git a/njord/src/sqlite/delete.rs b/njord/src/sqlite/delete.rs index dd5633e7..4742658f 100644 --- a/njord/src/sqlite/delete.rs +++ b/njord/src/sqlite/delete.rs @@ -39,7 +39,7 @@ use crate::{ use rusqlite::{Connection, Result}; -use log::info; +use log::{debug, info}; use crate::table::Table; @@ -159,9 +159,8 @@ impl<'a, T: Table + Default> DeleteQueryBuilder<'a, T> { format!("{} {}", limit_str, offset_str), ); - info!("{}", query); + debug!("{}", query); - // Execute SQL let _ = conn.execute(&query.to_string(), []); Ok(()) diff --git a/njord/src/sqlite/insert.rs b/njord/src/sqlite/insert.rs index 2b9ea898..da33edbc 100644 --- a/njord/src/sqlite/insert.rs +++ b/njord/src/sqlite/insert.rs @@ -98,7 +98,7 @@ pub fn into<'a, T: Table + Default>( info!("Inserted into table, done."); // FIXME: Return the number of rows affected - return Ok(sql); + Ok(sql) } /// Generates an SQL INSERT INTO statement for a given subquery. @@ -191,7 +191,7 @@ fn generate_statement(table_row: &T, first_statement: bool) -> Result< format!("({})", values_str) }; - debug!("{}", sql); // For debugging purposes + debug!("{}", sql); Ok(sql) } diff --git a/njord/src/sqlite/update.rs b/njord/src/sqlite/update.rs index 74e7b302..c55ab087 100644 --- a/njord/src/sqlite/update.rs +++ b/njord/src/sqlite/update.rs @@ -39,7 +39,7 @@ use crate::{ use rusqlite::{Connection, Result}; -use log::info; +use log::{debug, info}; use crate::table::Table; @@ -186,7 +186,6 @@ impl<'a, T: Table + Default> UpdateQueryBuilder<'a, T> { }; set_fields.push(format!("{} = {}", column, formatted_value)); } else { - // Handle the case when the column doesn't exist in the table eprintln!("Column '{}' does not exist in the table", column); } } @@ -217,9 +216,8 @@ impl<'a, T: Table + Default> UpdateQueryBuilder<'a, T> { format!("{} {}", limit_str, offset_str), ); - info!("{}", query); + debug!("{}", query); - // Prepare SQL statement match conn.prepare(query.as_str()) { Ok(_) => println!("Success!"), Err(_) => eprintln!("Could not execute..."), diff --git a/njord/src/sqlite/util.rs b/njord/src/sqlite/util.rs index 9fa894c7..c94425b6 100644 --- a/njord/src/sqlite/util.rs +++ b/njord/src/sqlite/util.rs @@ -100,7 +100,7 @@ pub fn generate_order_by_str(order_by: &Option, String>>) -> String::new() }; - return order_by_str; + order_by_str } /// Generates an SQL LIMIT clause string based on the provided limit count.