From 1c89aec6fff0e4cddfbf29a6ab0080766f29fe50 Mon Sep 17 00:00:00 2001 From: threeoh6000 Date: Sun, 24 Nov 2024 12:55:53 +0000 Subject: [PATCH] Get database abstraction opening working. --- src/db/mod.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/db/sqlite.rs | 13 +------------ src/main.rs | 8 ++++++++ src/schema.rs | 15 +++++++++++++++ 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/src/db/mod.rs b/src/db/mod.rs index 270ffef..dcf45d8 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,2 +1,44 @@ // Database abstraction. +use crate::schema::DatabaseCredentials; +use std::fmt; mod sqlite; + +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub enum DbSource { + NONE, + SQLITE, +} + +#[non_exhaustive] +pub struct DbAbstraction { + source: DbSource, + sqlite: Option, +} + +impl DbAbstraction { + pub fn new() -> DbAbstraction { + return DbAbstraction { source: DbSource::NONE, sqlite: None }; + } + + pub fn open(creds: DatabaseCredentials, source: DbSource) -> Result { + if source == DbSource::SQLITE { + let result = sqlite::Database::open(creds); + if result.is_err() { + return result.map(|_x| DbAbstraction::new()); + } else { + let mut dba = DbAbstraction::new(); + dba.source = source; + dba.sqlite = Some(result.unwrap()); + return Ok(dba); + } + } + return Err("Invalid database source.".to_string()); + } +} + +impl fmt::Debug for DbAbstraction { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self.source) + } +} diff --git a/src/db/sqlite.rs b/src/db/sqlite.rs index 4c51fed..016b302 100644 --- a/src/db/sqlite.rs +++ b/src/db/sqlite.rs @@ -1,18 +1,7 @@ // SQLite to Calamari DB abstraction. +use crate::schema::DatabaseCredentials; use std::path::PathBuf; -pub struct DatabaseCredentials { - pub server: Option, - pub username: Option, - pub password: Option, -} - -impl DatabaseCredentials { - pub fn new() -> DatabaseCredentials { - return DatabaseCredentials { server: None, username: None, password: None }; - } -} - pub struct Database { connection: sqlite::Connection, } diff --git a/src/main.rs b/src/main.rs index 8db7ff3..259c491 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,5 +3,13 @@ mod schema; mod db; fn main() { + let mut creds = schema::DatabaseCredentials::new(); + creds.server = Some(":memory:".to_string()); + let db = db::DbAbstraction::open(creds.clone(), db::DbSource::SQLITE); + if db.is_ok() { + println!("Opened database with following credentials and source: {:?}, {:?}", creds, db); + } else { + println!("{}", db.unwrap_err()); + } } diff --git a/src/schema.rs b/src/schema.rs index ee56f04..76e683a 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -2,6 +2,21 @@ use uuid::Uuid; use std::path::PathBuf; +#[derive(Debug, Clone)] +pub struct DatabaseCredentials { + pub server: Option, + pub username: Option, + pub password: Option, +} + +impl DatabaseCredentials { + pub fn new() -> DatabaseCredentials { + return DatabaseCredentials { server: None, username: None, password: None }; + } +} + + + #[derive(Debug, Clone)] pub struct Device { pub uuid: Uuid,