Adding new field filter to serialize_vec_from_table and correcting all associated declarations and usages

This commit is contained in:
Frédéric CAMPO 2025-02-18 22:43:20 +01:00
parent d9ab10c0a7
commit c9c6cbe40e
Signed by: llowin
GPG Key ID: 19EACF0560706F99
4 changed files with 10 additions and 36 deletions

View File

@ -11,11 +11,12 @@ use serde::de::DeserializeOwned;
pub fn get_data<T>(
conn: &rusqlite::Connection,
table_name: &str,
filter: Option<&str>,
) -> Result<Vec<T>, serde_rusqlite::Error>
where
T: DeserializeOwned + more_fields::FieldsAsSlice,
{
let mut query_result = conn.prepare(&generate_select_query::<T>(table_name, None))?;
let mut query_result = conn.prepare(&generate_select_query::<T>(table_name, filter))?;
Ok(serde_rusqlite::from_rows::<T>(query_result.query([])?)
.filter_map(|s| s.ok())
.collect())
@ -106,7 +107,7 @@ mod test {
let filename = "test.db";
let conn = mock_set_up(filename);
let result = conn
.serialize_vec_from_table::<TestStruct>("TestStruct")
.serialize_vec_from_table::<TestStruct>("TestStruct", None)
.unwrap();
tear_down(filename, conn.conn);
assert_eq!(

View File

@ -164,17 +164,11 @@ pub trait RecipeDatabase {
where
Self: std::marker::Sized;
/// Serialize a vector of objects of the given type in the specified table of the underlying database
/// Serialize a vector of objects of the given type in the specified table with an optional filter of the underlying database
fn serialize_vec_from_table<T: DeserializeOwned + more_fields::FieldsAsSlice>(
&self,
table: &str,
) -> std::result::Result<Vec<T>, FatalEquipementError>;
/// Serialize a specified vector of objects of the given type with an additionnal restriction
fn serialize_vec_from_table_with_query<T: DeserializeOwned>(
&self,
table: &str,
query: &str,
filter: Option<&str>,
) -> std::result::Result<Vec<T>, FatalEquipementError>;
/// Initialize an empty database & table

View File

@ -22,18 +22,9 @@ impl RecipeDatabase for EquipementDatabase {
fn serialize_vec_from_table<T: DeserializeOwned + more_fields::FieldsAsSlice>(
&self,
table: &str,
filter: Option<&str>,
) -> Result<Vec<T>, crate::error::FatalEquipementError> {
Ok(get_data(&self.conn, table)?)
}
fn serialize_vec_from_table_with_query<T: DeserializeOwned>(
&self,
table: &str,
query: &str,
) -> Result<Vec<T>, crate::error::FatalEquipementError> {
let _ = table;
let _ = query;
todo!()
Ok(get_data(&self.conn, table, filter)?)
}
/// Checks if database exists, and returns a rusqlite::Connection. If not, opens database in memory.
@ -48,7 +39,6 @@ impl RecipeDatabase for EquipementDatabase {
) -> Result<(), crate::error::EquipementError> {
// A vector of <attribute, type>
let _attr_map = T::attributes_slice();
let _ = table;
todo!()
}

View File

@ -23,18 +23,10 @@ impl RecipeDatabase for DatabaseMock {
fn serialize_vec_from_table<T>(
&self,
table: &str,
filter: Option<&str>,
) -> std::result::Result<Vec<T>, crate::error::FatalEquipementError> {
let _ = table;
unimplemented!()
}
fn serialize_vec_from_table_with_query<T>(
&self,
table: &str,
query: &str,
) -> std::result::Result<Vec<T>, FatalEquipementError> {
let _ = table;
let _ = query;
let _ = filter;
unimplemented!()
}
@ -95,10 +87,7 @@ impl Scenario for CrossRecipeScenario {
fn generate_refined_recipes(&self) -> Self::FinalListType {
let _all_parts = &self
.database
.serialize_vec_from_table_with_query::<InnerEquipementStats>(
"bob",
"WHERE CATEGOTY == 'Bobitty'",
);
.serialize_vec_from_table::<InnerEquipementStats>("bob", Some("CATEGOTY == 'Bobitty'"));
todo!()
}