Get database abstraction opening working.

This commit is contained in:
abbie 2024-11-24 12:55:53 +00:00
parent 4ca612f1f9
commit 1c89aec6ff
Signed by: threeoh6000
GPG key ID: 801FE4AD456E922C
4 changed files with 66 additions and 12 deletions

View file

@ -1,2 +1,44 @@
// Database abstraction. // Database abstraction.
use crate::schema::DatabaseCredentials;
use std::fmt;
mod sqlite; mod sqlite;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum DbSource {
NONE,
SQLITE,
}
#[non_exhaustive]
pub struct DbAbstraction {
source: DbSource,
sqlite: Option<sqlite::Database>,
}
impl DbAbstraction {
pub fn new() -> DbAbstraction {
return DbAbstraction { source: DbSource::NONE, sqlite: None };
}
pub fn open(creds: DatabaseCredentials, source: DbSource) -> Result<DbAbstraction, String> {
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)
}
}

View file

@ -1,18 +1,7 @@
// SQLite to Calamari DB abstraction. // SQLite to Calamari DB abstraction.
use crate::schema::DatabaseCredentials;
use std::path::PathBuf; use std::path::PathBuf;
pub struct DatabaseCredentials {
pub server: Option<String>,
pub username: Option<String>,
pub password: Option<String>,
}
impl DatabaseCredentials {
pub fn new() -> DatabaseCredentials {
return DatabaseCredentials { server: None, username: None, password: None };
}
}
pub struct Database { pub struct Database {
connection: sqlite::Connection, connection: sqlite::Connection,
} }

View file

@ -3,5 +3,13 @@ mod schema;
mod db; mod db;
fn main() { 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());
}
} }

View file

@ -2,6 +2,21 @@
use uuid::Uuid; use uuid::Uuid;
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Debug, Clone)]
pub struct DatabaseCredentials {
pub server: Option<String>,
pub username: Option<String>,
pub password: Option<String>,
}
impl DatabaseCredentials {
pub fn new() -> DatabaseCredentials {
return DatabaseCredentials { server: None, username: None, password: None };
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Device { pub struct Device {
pub uuid: Uuid, pub uuid: Uuid,