generate_query push

This commit is contained in:
Frédéric CAMPO 2025-02-12 00:11:23 +01:00
parent 9e1521e373
commit 4b4d39257a
Signed by: llowin
GPG Key ID: 19EACF0560706F99
1 changed files with 75 additions and 0 deletions

View File

@ -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<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)
}
}
pub fn get_data() -> Vec<InnerStats> {
//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!()
}
}