diff options
| author | Mroik <mroik@delayed.space> | 2026-04-01 00:49:31 +0200 |
|---|---|---|
| committer | Mroik <mroik@delayed.space> | 2026-04-13 06:55:04 +0200 |
| commit | 7294e5944c2e5620c47d1ab014e217c5ee05b3a6 (patch) | |
| tree | 7cff945aef488565a18d7129e3af278a9332b596 /src/database.rs | |
| parent | 8f8fd10dc2b185ca0a8e8908229c4d4bbefd70b7 (diff) | |
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 <mroik@delayed.space>
Diffstat (limited to 'src/database.rs')
| -rw-r--r-- | src/database.rs | 20 |
1 files changed, 12 insertions, 8 deletions
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<Self> { + pub fn new(db_file: &str) -> Result<Self> { 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<Ex>(&mut self, q: Ex) -> Result<QueryResult<Ex::T>> where Ex: DBExecutable { + pub fn execute<Ex>(&mut self, q: Ex) -> Result<QueryResult<Ex::T>> + 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<QueryResult<Self::T>>; } +#[derive(Debug)] pub enum QueryResult<T> { Empty, - Vec(Vec<T>) + Vec(Vec<T>), } |
