Get database abstraction opening working.
This commit is contained in:
parent
4ca612f1f9
commit
1c89aec6ff
4 changed files with 66 additions and 12 deletions
|
@ -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<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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,7 @@
|
|||
// SQLite to Calamari DB abstraction.
|
||||
use crate::schema::DatabaseCredentials;
|
||||
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 {
|
||||
connection: sqlite::Connection,
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,21 @@
|
|||
use uuid::Uuid;
|
||||
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)]
|
||||
pub struct Device {
|
||||
pub uuid: Uuid,
|
||||
|
|
Loading…
Reference in a new issue