From 0e89311f3b345d46d404c0f2d246590efb6b8715 Mon Sep 17 00:00:00 2001 From: giuseppe-g-gelardi Date: Thu, 12 Dec 2024 23:46:57 -0500 Subject: [PATCH] cleanup --- src/database.rs | 8 +- src/query.rs | 349 +----------------------------------------------- 2 files changed, 5 insertions(+), 352 deletions(-) diff --git a/src/database.rs b/src/database.rs index 095c192..7e823ef 100644 --- a/src/database.rs +++ b/src/database.rs @@ -279,9 +279,7 @@ mod tests { tables: vec![], }; - db.save_to_file() - .await - .expect("Failed to save database"); + db.save_to_file().await.expect("Failed to save database"); let loaded_db = Database::load_from_file(&db_path) .await .expect("Failed to load database"); @@ -301,9 +299,7 @@ mod tests { tables: vec![], }; - db.save_to_file() - .await - .expect("Failed to save database"); + db.save_to_file().await.expect("Failed to save database"); let loaded_db = Database::load_from_file(&db_path) .await diff --git a/src/query.rs b/src/query.rs index b9893b7..e389659 100644 --- a/src/query.rs +++ b/src/query.rs @@ -74,16 +74,12 @@ impl Query { Operation::Read => self.execute_select(table, key, value), Operation::Update => { let result = self.execute_update(table, key, value); - db.save_to_file() - .await - .map_err(DatabaseError::SaveError)?; + db.save_to_file().await.map_err(DatabaseError::SaveError)?; result } Operation::Delete => { let result = self.execute_delete(table, key, value); - db.save_to_file() - .await - .map_err(DatabaseError::SaveError)?; + db.save_to_file().await.map_err(DatabaseError::SaveError)?; result } Operation::Create => unreachable!(), @@ -114,9 +110,7 @@ impl Query { if let Some(row_data) = self.row_data.clone() { table.columns.validate(row_data.clone())?; table.rows.push(Row::new(row_data)); - db.save_to_file() - .await - .map_err(DatabaseError::SaveError)?; + db.save_to_file().await.map_err(DatabaseError::SaveError)?; Ok(()) } else { Err(DatabaseError::InvalidData( @@ -523,341 +517,4 @@ mod tests { assert!(deleted_record.is_none(), "Expected record to be deleted"); assert!(rows.is_empty(), "Expected all records to be deleted"); } - - // // #[cfg(feature = "async")] - // #[tokio::test] - // async fn test_query_where_eq_match() { - // let mut db = setup_temp_db().await; - // - // let test_data = TestData { - // id: "1".to_string(), - // name: "Alice".to_string(), - // }; - // - // db.add_row() - // .from("TestTable") - // .data_from_struct(test_data.clone()) - // .execute_add() - // .await - // .expect("Failed to add row"); - // - // let result: Option = db - // .get_rows() - // .from("TestTable") - // .where_eq("id", "1") - // .await - // .unwrap(); - // - // assert_eq!(result, Some(test_data), "Expected matching record"); - // } - // - // // #[cfg(feature = "async")] - // #[tokio::test] - // async fn test_query_execute_update() { - // let mut db = setup_temp_db().await; - // - // let test_data = TestData { - // id: "1".to_string(), - // name: "Alice".to_string(), - // }; - // - // db.add_row() - // .from("TestTable") - // .data_from_struct(test_data.clone()) - // .execute_add() - // .await - // .expect("Failed to add row"); - // - // let update_data = json!({ "name": "Updated Alice" }); - // - // let result = db - // .update_row() - // .from("TestTable") - // .data(update_data) - // .where_eq::("id", "1") - // .await - // .unwrap(); - // - // assert!( - // result.is_some(), - // "Expected update to return the updated record" - // ); - // assert_eq!( - // result.unwrap().name, - // "Updated Alice", - // "Name was not updated" - // ); - // } - // - // // #[cfg(feature = "async")] - // #[tokio::test] - // async fn test_query_all() { - // let mut db = setup_temp_db().await; - // - // let test_data1 = TestData { - // id: "1".to_string(), - // name: "Alice".to_string(), - // }; - // let test_data2 = TestData { - // id: "2".to_string(), - // name: "Bob".to_string(), - // }; - // - // db.add_row() - // .from("TestTable") - // .data_from_struct(test_data1.clone()) - // .execute_add() - // .await - // .expect("Failed to add row 1"); - // - // db.add_row() - // .from("TestTable") - // .data_from_struct(test_data2.clone()) - // .execute_add() - // .await - // .expect("Failed to add row 2"); - // - // let rows: Vec = db.get_rows().from("TestTable").all().await; - // - // assert_eq!(rows.len(), 2); - // assert!(rows.contains(&test_data1)); - // assert!(rows.contains(&test_data2)); - // } - // - // // #[cfg(feature = "async")] - // #[tokio::test] - // async fn test_query_execute_delete() { - // let mut db = setup_temp_db().await; - // - // let test_data = TestData { - // id: "1".to_string(), - // name: "Alice".to_string(), - // }; - // - // db.add_row() - // .from("TestTable") - // .data_from_struct(test_data.clone()) - // .execute_add() - // .await - // .expect("Failed to add row"); - // - // let result = db - // .get_single() - // .from("TestTable") - // .where_eq::("id", "1") - // .await - // .unwrap(); - // - // assert!(result.is_some(), "Expected record to exist before deletion"); - // - // let _ = db - // .delete_single() - // .from("TestTable") - // .where_eq::("id", "1") - // .await - // .unwrap(); - // - // let rows: Vec = db.get_rows().from("TestTable").all().await; - // let deleted_record = db - // .get_single() - // .from("TestTable") - // .where_eq::("id", "1") - // .await - // .unwrap(); - // - // assert!(deleted_record.is_none(), "Expected record to be deleted"); - // assert!(rows.is_empty(), "Expected all records to be deleted"); - // } } - -// pub fn where_eq( -// self, -// key: &str, -// value: &str, -// ) -> Result, DatabaseError> { -// // Load the database -// let mut db = Database::load_from_file(&self.db_file_name) -// .map_err(|e| DatabaseError::LoadError(e))?; -// -// // Clone table_name to avoid moving self -// let table_name = self -// .table_name -// .clone() -// .ok_or_else(|| DatabaseError::TableNotFound("Table name not specified.".to_string()))?; -// -// // Find the index of the table -// let table_index = db -// .tables -// .iter() -// .position(|t| t.name == table_name) -// .ok_or_else(|| { -// DatabaseError::TableNotFound(format!("Table '{}' not found.", table_name)) -// })?; -// -// // Borrow the table by index -// let table = &mut db.tables[table_index]; -// -// // consider thiserror for the error handling for these operations -// match self.operation { -// Operation::Read => self.execute_select(table, key, value), -// Operation::Update => { -// let result = self.execute_update(table, key, value); -// if let Err(e) = db.save_to_file().map_err(DatabaseError::SaveError) { -// tracing::error!("Failed to save database: {}", e); -// return Err(e); -// } -// result -// } -// Operation::Delete => { -// let result = self.execute_delete(table, key, value); -// if let Err(e) = db.save_to_file().map_err(DatabaseError::SaveError) { -// tracing::error!("Failed to save database: {}", e); -// return Err(e); -// } -// result -// } -// Operation::Create => unreachable!(), -// } -// } -// -// -// -// -// -// -// -// pub fn execute_add(self) -> Result<(), DatabaseError> { -// let mut db = -// Database::load_from_file(&self.db_file_name).map_err(DatabaseError::LoadError)?; -// -// let table_name = self -// .table_name -// .clone() -// .ok_or_else(|| DatabaseError::InvalidData("Table name not specified.".to_string()))?; -// -// // Find the table -// let table = db -// .tables -// .iter_mut() -// .find(|t| t.name == table_name) -// .ok_or_else(|| DatabaseError::TableNotFound(table_name.clone()))?; -// -// // Validate and add the row -// if let Some(row_data) = self.row_data { -// table.columns.validate(row_data.clone())?; // optional schema validation -// table.rows.push(Row::new(row_data)); -// -// db.save_to_file().map_err(DatabaseError::SaveError)?; -// tracing::info!("Row added successfully to '{}'.", table_name); -// Ok(()) -// } else { -// tracing::error!("No data provided for the new row."); -// Err(DatabaseError::InvalidData( -// "No data provided for the new row.".to_string(), -// )) -// } -// } -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// pub fn all(&self) -> Vec { -// let db = Database::load_from_file(&self.db_file_name).unwrap_or_else(|e| { -// tracing::error!("Failed to load database from file: {}", e); -// Database { -// name: String::new(), -// file_name: self.db_file_name.clone(), -// tables: Vec::new(), -// } -// }); -// -// if let Some(table_name) = &self.table_name { -// if let Some(table) = db.tables.iter().find(|t| t.name == *table_name) { -// table -// .rows -// .iter() -// .filter_map(|row| serde_json::from_value(row.data.clone()).ok()) -// .collect() -// } else { -// tracing::error!("Table {} not found", table_name); -// Vec::new() -// } -// } else { -// tracing::error!("Table name not provided"); -// Vec::new() -// } -// } -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// fn execute_update( -// &self, -// table: &mut Table, -// key: &str, -// value: &str, -// ) -> Result, DatabaseError> { -// for row in &mut table.rows { -// if let Some(field_value) = row.data.get(key) { -// if field_value.as_str() == Some(value) { -// if let Some(update_data) = &self.update_data { -// if let Value::Object(update_map) = update_data { -// if let Value::Object(row_map) = &mut row.data { -// for (k, v) in update_map { -// row_map.insert(k.clone(), v.clone()); -// } -// } else { -// return Err(DatabaseError::InvalidData( -// "Row data is not a JSON object.".to_string(), -// )); -// } -// -// tracing::info!("Record updated successfully."); -// return serde_json::from_value(row.data.clone()).map(Some).map_err( -// |e| { -// DatabaseError::InvalidData(format!( -// "Deserialization error: {}", -// e -// )) -// }, -// ); -// } else { -// return Err(DatabaseError::InvalidData( -// "Invalid update data format.".to_string(), -// )); -// } -// } else { -// return Err(DatabaseError::InvalidData( -// "No update data provided.".to_string(), -// )); -// } -// } -// } -// } -// -// Ok(None) // No matching record found -// }