diff --git a/weaponry/src/database.rs b/weaponry/src/database.rs index 1ac040d..ba0e30d 100644 --- a/weaponry/src/database.rs +++ b/weaponry/src/database.rs @@ -2,3 +2,78 @@ // 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)] +#![allow(unused_imports)] +use std::ops::Deref; + +use crate::weaponry_structs::equipement_structs::InnerStats; +use rusqlite::{self, Connection, Result, params}; +use struct_field_names_as_array::FieldNamesAsArray; + +fn generate_query(fields: &[&str], table_name: &str) -> String { + let mut query = String::new(); + query.push_str("SELECT"); + for item in fields.iter() { + query.push_str(" "); + query.push_str(*item); + query.push_str(","); + } + query.pop(); + query.push_str(format!(" FROM {};", table_name).as_str()); + query +} + +/// Checks if database exists, and returns a rusqlite::Connection. If not, panics while insulting you. You deserved it. +fn initialize_database_conn(filename: Option) -> Result { + if let Some(text) = filename { + let conn = Connection::open(text)?; + Ok(conn) + } else { + panic!("No filename, you big oaf."); + //let conn = Connection::open_in_memory()?; + //Ok(conn) + } +} + +pub fn get_data() -> Vec { + //let query = generate_query(&InnerStats::FIELD_NAMES_AS_ARRAY, "InnerStats"); + todo!() +} + +#[cfg(test)] +mod test { + use std::any::type_name; + + use pretty_assertions::assert_eq; + + use super::*; + #[derive(FieldNamesAsArray)] + struct TestStruct { + id: usize, + name: String, + } + + #[test] + fn test_generate_query() { + assert_eq!( + generate_query(&TestStruct::FIELD_NAMES_AS_ARRAY, "TestStruct"), + "SELECT id, name FROM TestStruct;" + ) + } + + #[test] + fn test_initialize_database() { + todo!() + } + + #[test] + fn test_generate_table_from_struct() { + todo!() + } + + #[test] + fn test_get_data() { + todo!() + } +}