83 lines
2.2 KiB
Rust
83 lines
2.2 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::weaponry_structs::equipement_structs::InnerStats;
|
|
use rusqlite::{self, Connection, Result};
|
|
|
|
/// Generate a SELECT query with fields generated from a struct. Returns a String
|
|
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(' ');
|
|
query.push_str(item);
|
|
query.push(',');
|
|
}
|
|
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<String>) -> Result<Connection> {
|
|
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)
|
|
}
|
|
}
|
|
|
|
/// Public function to get data from db
|
|
pub fn get_data() -> Vec<InnerStats> {
|
|
/* let gen_query = generate_query(&InnerStats::FIELD_NAMES_AS_ARRAY, "InnerStats");
|
|
let db_conn = initialize_database_conn(Some("/tmp/data.sqlite3".to_owned())).unwrap();
|
|
let mut query_result = db_conn.prepare(&gen_query);*/
|
|
todo!()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use pretty_assertions::assert_eq;
|
|
use struct_field_names_as_array::FieldNamesAsArray;
|
|
|
|
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;"
|
|
)
|
|
}
|
|
|
|
#[ignore]
|
|
#[test]
|
|
fn test_initialize_database_conn() {
|
|
todo!()
|
|
}
|
|
|
|
#[ignore]
|
|
#[test]
|
|
fn test_generate_table_from_struct() {
|
|
todo!()
|
|
}
|
|
|
|
#[ignore]
|
|
#[test]
|
|
fn test_get_data() {
|
|
todo!()
|
|
}
|
|
}
|