use std::path::Path; use anyhow::Result; use rusqlite::{Connection, ToSql}; pub struct DataBase { sql: Connection, } impl DataBase { pub async fn new(path: impl AsRef) -> Result { let me = Self { sql: Connection::open(path)?, }; me.generate_tables()?; Ok(me) } fn generate_tables(&self) -> Result<()> { self.sql.execute( "CREATE TABLE IF NOT EXISTS data ( id INTEGER PRIMARY KEY, device TEXT NOT NULL, time BIGINT NOT NULL, watts REAL NOT NULL )", [], )?; Ok(()) } pub fn write(&self, device_name: &str, time: u64, watts: f32) -> Result<()> { let params: &[&dyn ToSql] = &[&device_name, &time, &watts]; self.sql.execute( "INSERT INTO data (device, time, watts) VALUES (?1, ?2, ?3)", params, )?; Ok(()) } } #[cfg(test)] mod test { use std::fs; use anyhow::Result; use super::DataBase; #[tokio::test] async fn test_connection() -> Result<()> { DataBase::new("connection_test.db").await?; fs::remove_file("connection_test.db")?; Ok(()) } #[tokio::test] async fn test_write() -> Result<()> { let db = DataBase::new("write_test.db").await?; db.write("test", 0, 5.5)?; fs::remove_file("write_test.db")?; Ok(()) } }