120 lines
3.3 KiB
Rust
120 lines
3.3 KiB
Rust
// Workspace: weaponry
|
|
// File: weaponry/src/database.rs
|
|
//! Database handiling module
|
|
//! Implements the basics deserialisation from a database to the weaponry components
|
|
//! Should dump whole database in a vec of structs from weaponry_structs
|
|
#![allow(dead_code)]
|
|
|
|
use crate::{error::EquipementError, interfaces::RecipeDatabase};
|
|
use rusqlite::{self, Connection};
|
|
use serde::de::DeserializeOwned;
|
|
|
|
/// Struct for database connection.
|
|
pub struct EquipementDatabase {
|
|
/// Connection parameter for rusqlite
|
|
pub conn: rusqlite::Connection,
|
|
}
|
|
|
|
impl EquipementDatabase {
|
|
/// Public function to get data from db. Returns the result as a vec with structs
|
|
pub fn get_data_from_fields<T>(
|
|
&self,
|
|
fields: &[&str],
|
|
table_name: &str,
|
|
) -> Result<Vec<T>, serde_rusqlite::Error>
|
|
where
|
|
T: DeserializeOwned,
|
|
{
|
|
let mut query_result = self.conn.prepare(&generate_query(fields, table_name))?;
|
|
Ok(serde_rusqlite::from_rows::<T>(query_result.query([])?)
|
|
.filter_map(|s| s.ok())
|
|
.collect())
|
|
}
|
|
}
|
|
|
|
impl RecipeDatabase for EquipementDatabase {
|
|
fn serialize_vec_from_table<T>(table: &str) -> Result<Vec<T>, crate::error::EquipementError> {
|
|
todo!()
|
|
}
|
|
|
|
fn serialize_vec_from_table_with_query<T>(
|
|
table: &str,
|
|
query: &str,
|
|
) -> Result<Vec<T>, crate::error::EquipementError> {
|
|
todo!()
|
|
}
|
|
|
|
/// Checks if database exists, and returns a rusqlite::Connection. If not, opens database in memory.
|
|
fn new(filename: &str) -> Result<Self, EquipementError> {
|
|
Ok(EquipementDatabase {
|
|
conn: Connection::open(filename)?,
|
|
})
|
|
}
|
|
}
|
|
|
|
/// Generate a SELECT query with fields generated from a struct. Returns a String
|
|
#[inline]
|
|
fn generate_query(fields: &[&str], table_name: &str) -> String {
|
|
format!("SELECT {} FROM {};", fields.join(", "), table_name)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
use pretty_assertions::assert_eq;
|
|
use serde::Deserialize;
|
|
use struct_field_names_as_array::FieldNamesAsArray;
|
|
|
|
#[derive(FieldNamesAsArray, Deserialize, Debug)]
|
|
struct TestStruct {
|
|
id: usize,
|
|
name: String,
|
|
}
|
|
|
|
fn mock_set_up() -> EquipementDatabase {
|
|
let equip_db = EquipementDatabase {
|
|
conn: Connection::open_in_memory().unwrap(),
|
|
};
|
|
equip_db.conn.execute("CREATE TABLE IF NOT EXISTS TestStruct (id INTEGER UNIQUE, name TEXT, PRIMARY KEY('id' AUTOINCREMENT));",()).unwrap();
|
|
equip_db
|
|
.conn
|
|
.execute(
|
|
"INSERT INTO TestStruct (id, name) VALUES (1, 'dupont'), (2, 'durand');",
|
|
(),
|
|
)
|
|
.unwrap();
|
|
equip_db
|
|
}
|
|
|
|
#[test]
|
|
fn test_generate_query() {
|
|
assert_eq!(
|
|
generate_query(&TestStruct::FIELD_NAMES_AS_ARRAY, "TestStruct"),
|
|
"SELECT id, name FROM TestStruct;"
|
|
)
|
|
}
|
|
|
|
#[ignore]
|
|
#[test]
|
|
fn test_initialize_database_conn() {
|
|
todo!()
|
|
}
|
|
|
|
#[ignore]
|
|
#[test]
|
|
fn test_generate_table_from_struct() {
|
|
todo!()
|
|
}
|
|
|
|
#[ignore]
|
|
#[test]
|
|
fn test_get_data() {
|
|
let conn = mock_set_up();
|
|
let result = conn
|
|
.get_data_from_fields::<TestStruct>(&TestStruct::FIELD_NAMES_AS_ARRAY, "TestStruct")
|
|
.unwrap();
|
|
println!("{:?}", result);
|
|
assert!(result.is_empty())
|
|
}
|
|
}
|