Skip to content

Commit

Permalink
Cleanup (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjovanc authored Nov 23, 2024
2 parents 87d3425 + 283fb83 commit 70b8437
Show file tree
Hide file tree
Showing 27 changed files with 51 additions and 65 deletions.
13 changes: 7 additions & 6 deletions njord/src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn QueryBuilder<'a> + '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 {
Expand All @@ -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<String> for Column
/// Implementation of PartialEq<String> for Column
impl<'a> PartialEq<String> for Column<'a> {
fn eq(&self, other: &String) -> bool {
match self {
Expand All @@ -45,7 +46,7 @@ impl<'a> PartialEq<String> 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 {
Expand Down
1 change: 0 additions & 1 deletion njord/src/mariadb/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ impl<'a, T: Table + Default> DeleteQueryBuilder<'a, T> {

info!("{}", query);

// Execute SQL
let _ = conn.query_drop(&query.to_string());

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions njord/src/mariadb/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ pub fn into<'a, T: Table + Default>(
subquery: Box<dyn QueryBuilder<'a> + 'a>,
) -> Result<String, RusqliteError> {
let statement = generate_insert_into_statement::<T>(columns, subquery);
let sql = statement.unwrap();
let sql = statement?;

// FIXME: Convert to transaction
let _ = conn.query_drop(&sql);

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.
Expand Down Expand Up @@ -197,7 +197,7 @@ fn generate_statement<T: Table>(table_row: &T, first_statement: bool) -> Result<
format!("({})", values_str)
};

debug!("{}", sql); // For debugging purposes
debug!("{}", sql);

Ok(sql)
}
5 changes: 2 additions & 3 deletions njord/src/mariadb/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -360,7 +360,7 @@ impl<'a, T: Table + Default> SelectQueryBuilder<'a, T> {
pub fn build(&mut self, conn: &mut PooledConn) -> Result<Vec<T>, Error> {
let final_query = self.build_query();

info!("{}", final_query);
debug!("{}", final_query);

raw_execute(&final_query, conn)
}
Expand Down Expand Up @@ -426,7 +426,6 @@ pub fn raw_execute<T: Table + Default>(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);
}

Expand Down
6 changes: 2 additions & 4 deletions njord/src/mariadb/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::{
},
};

use log::info;
use log::{debug, info};
use mysql::{prelude::Queryable, PooledConn};

use crate::table::Table;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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(())
Expand Down
3 changes: 1 addition & 2 deletions njord/src/mariadb/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub fn generate_order_by_str(order_by: &Option<HashMap<Vec<String>, String>>) ->
String::new()
};

return order_by_str;
order_by_str
}

/// Generates an SQL LIMIT clause string based on the provided limit count.
Expand Down Expand Up @@ -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, "");
Expand Down
5 changes: 2 additions & 3 deletions njord/src/mssql/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()),
Expand Down
6 changes: 3 additions & 3 deletions njord/src/mssql/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ pub async fn insert<T: Table>(
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)
}
}
}
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion njord/src/mssql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ pub async fn open(connection_string: &str) -> Result<Connection, Error> {
return Err(err);
}
};
return Ok(Connection { client });

Ok(Connection { client })
}

/// Executes a raw SQL query and returns a vector of table rows.
Expand Down
5 changes: 2 additions & 3 deletions njord/src/mssql/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion njord/src/mssql/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub fn generate_order_by_str(order_by: &Option<HashMap<Vec<String>, 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.
Expand Down
5 changes: 2 additions & 3 deletions njord/src/mysql/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::{
},
};

use log::info;
use log::{debug, info};
use mysql::{prelude::Queryable, PooledConn};

use crate::table::Table;
Expand Down Expand Up @@ -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(())
Expand Down
4 changes: 2 additions & 2 deletions njord/src/mysql/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -197,7 +197,7 @@ fn generate_statement<T: Table>(table_row: &T, first_statement: bool) -> Result<
format!("({})", values_str)
};

debug!("{}", sql); // For debugging purposes
debug!("{}", sql);

Ok(sql)
}
1 change: 0 additions & 1 deletion njord/src/mysql/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ pub fn raw_execute<T: Table + Default>(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);
}

Expand Down
5 changes: 2 additions & 3 deletions njord/src/mysql/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::{
},
};

use log::info;
use log::{debug, info};
use mysql::{prelude::Queryable, PooledConn};

use crate::table::Table;
Expand Down Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion njord/src/mysql/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub fn generate_order_by_str(order_by: &Option<HashMap<Vec<String>, String>>) ->
String::new()
};

return order_by_str;
order_by_str
}

/// Generates an SQL LIMIT clause string based on the provided limit count.
Expand Down
5 changes: 2 additions & 3 deletions njord/src/oracle/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::{
},
};

use log::info;
use log::{debug, info};
use oracle::Connection;

use crate::table::Table;
Expand Down Expand Up @@ -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(())
Expand Down
4 changes: 2 additions & 2 deletions njord/src/oracle/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -200,7 +200,7 @@ fn generate_statement<T: Table>(table_row: &T, first_statement: bool) -> Result<
format!("({})", values_str)
};

debug!("{}", sql); // For debugging purposes
debug!("{}", sql);

Ok(sql)
}
4 changes: 2 additions & 2 deletions njord/src/oracle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ pub fn open(username: &str, password: &str, connect_string: &str) -> Result<Conn
Ok(conn) => {
println!("Successfully connected to Oracle database");

return Ok(conn);
Ok(conn)
}
Err(err) => {
eprintln!("Error: {}", err);

return Err(err);
Err(err)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions njord/src/oracle/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -332,7 +332,7 @@ impl<'a, T: Table + Default> SelectQueryBuilder<'a, T> {
pub fn build(self, conn: &Connection) -> Result<Vec<T>, Error> {
let final_query = self.build_query();

info!("{}", final_query);
debug!("{}", final_query);

raw_execute(&final_query, conn)
}
Expand Down
Loading

0 comments on commit 70b8437

Please sign in to comment.