From 7294e5944c2e5620c47d1ab014e217c5ee05b3a6 Mon Sep 17 00:00:00 2001 From: Mroik Date: Wed, 1 Apr 2026 00:49:31 +0200 Subject: Implement User model The mailing list will need to save the data of the subscribers for them to receive the emails. Add User model with its DB interactions. Signed-off-by: Mroik --- src/database.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src/database.rs') diff --git a/src/database.rs b/src/database.rs index a1d0a18..de77f95 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,22 +1,22 @@ use anyhow::Result; use rusqlite::{Connection, Transaction, config::DbConfig}; -const DB_NAME: &str = "database.sqlite"; +pub const DB_NAME: &str = "database.sqlite"; const DB_VERSION: i64 = 0; -struct Database { +pub struct Database { conn: Connection, } impl Database { - fn new() -> Result { + pub fn new(db_file: &str) -> Result { let mut init = false; - if !std::fs::exists(DB_NAME)? { + if !std::fs::exists(db_file)? { init = true; } let mut db = Database { - conn: Connection::open(DB_NAME)?, + conn: Connection::open(db_file)?, }; if init { db.initialize()?; @@ -35,7 +35,7 @@ impl Database { tx.execute(q, ())?; q = "INSERT INTO version VALUES (?)"; - tx.execute(q, &[&DB_VERSION])?; + tx.execute(q, [&DB_VERSION])?; q = "CREATE TABLE user ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -61,7 +61,10 @@ impl Database { Ok(()) } - fn execute(&mut self, q: Ex) -> Result> where Ex: DBExecutable { + pub fn execute(&mut self, q: Ex) -> Result> + where + Ex: DBExecutable, + { let tx = self.conn.transaction()?; let ris = q.execute(&tx)?; tx.commit()?; @@ -74,7 +77,8 @@ pub trait DBExecutable { fn execute(&self, tx: &Transaction) -> Result>; } +#[derive(Debug)] pub enum QueryResult { Empty, - Vec(Vec) + Vec(Vec), } -- cgit v1.3