DB: Add basic connection functionality for both Redis and sqlite backends
This commit is contained in:
parent
f5e282dc50
commit
fe49220b64
@ -5,7 +5,7 @@ mod redis;
|
||||
|
||||
pub trait DbBackend {
|
||||
/// Connects to the DB
|
||||
fn connect(&self) -> Result<(), String>;
|
||||
fn connect(&mut self) -> Result<(), String>;
|
||||
|
||||
/// Get the map object at a key
|
||||
fn get(&self, key: &str) -> Result<Map<String, String>, String>;
|
||||
@ -14,13 +14,13 @@ pub trait DbBackend {
|
||||
fn get_inner(&self, key: &str, inner_key: &str) -> Result<String, String>;
|
||||
|
||||
/// Set the map object at a key, returns the value you game it
|
||||
fn set(&self, key: &str, value: Map<String, String>) -> Result<Map<String, String>, String>;
|
||||
fn set(&mut self, key: &str, value: Map<String, String>) -> Result<Map<String, String>, String>;
|
||||
|
||||
/// Set the value inside a map object at a key, returns the value you game it
|
||||
fn set_inner(&self, key: &str, inner_key: &str, value: &str) -> Result<&str, String>;
|
||||
fn set_inner(&mut self, key: &str, inner_key: &str, value: &str) -> Result<&str, String>;
|
||||
|
||||
/// Disconnects from the DB
|
||||
fn disconnect(&self) -> Result<(), String>;
|
||||
fn disconnect(&mut self) -> Result<(), String>;
|
||||
}
|
||||
|
||||
pub fn get_backend_names() -> Vec<&'static str> {
|
||||
|
@ -1,19 +1,38 @@
|
||||
use std::iter::Map;
|
||||
use crate::db::DbBackend;
|
||||
use crate::log::{log, LogArea, LogType};
|
||||
|
||||
pub struct RedisBackend {
|
||||
|
||||
conn: Option<redis::Connection>
|
||||
}
|
||||
|
||||
impl RedisBackend {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
Self {
|
||||
conn: None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DbBackend for RedisBackend {
|
||||
fn connect(&self) -> Result<(), String> {
|
||||
todo!()
|
||||
fn connect(&mut self) -> Result<(), String> {
|
||||
// TODO: Make this URL configurable
|
||||
self.conn = match redis::Client::open("redis://127.0.0.1/").unwrap().get_connection() {
|
||||
Ok(conn) => Some(conn),
|
||||
Err(e) => {
|
||||
log(LogType::Error, LogArea::Db, &format!("Failed to connect to database: {}", e));
|
||||
None
|
||||
},
|
||||
};
|
||||
|
||||
if self.conn.is_some() {
|
||||
log(LogType::Info, LogArea::Db, "Connected to database");
|
||||
}
|
||||
else {
|
||||
return Err("Failed to connect to database".to_string());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get(&self, key: &str) -> Result<Map<String, String>, String> {
|
||||
@ -24,15 +43,17 @@ impl DbBackend for RedisBackend {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn set(&self, key: &str, value: Map<String, String>) -> Result<Map<String, String>, String> {
|
||||
fn set(&mut self, key: &str, value: Map<String, String>) -> Result<Map<String, String>, String> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn set_inner(&self, key: &str, inner_key: &str, value: &str) -> Result<&str, String> {
|
||||
fn set_inner(&mut self, key: &str, inner_key: &str, value: &str) -> Result<&str, String> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn disconnect(&self) -> Result<(), String> {
|
||||
todo!()
|
||||
fn disconnect(&mut self) -> Result<(), String> {
|
||||
log(LogType::Info, LogArea::Db, "Disconnecting from database");
|
||||
drop(self.conn.take());
|
||||
return Ok(())
|
||||
}
|
||||
}
|
@ -1,19 +1,42 @@
|
||||
use std::iter::Map;
|
||||
use crate::db::DbBackend;
|
||||
use crate::log::{log, LogArea, LogType};
|
||||
|
||||
pub struct SqliteBackend {
|
||||
|
||||
conn: Option<sqlite::Connection>,
|
||||
}
|
||||
|
||||
impl SqliteBackend {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
Self {
|
||||
conn: None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DbBackend for SqliteBackend {
|
||||
fn connect(&self) -> Result<(), String> {
|
||||
todo!()
|
||||
fn connect(&mut self) -> Result<(), String> {
|
||||
self.conn = match sqlite::open("db.sqlite3") {
|
||||
Ok(conn) => Some(conn),
|
||||
Err(e) => {
|
||||
log(LogType::Error, LogArea::Db, &format!("Failed to connect to database: {}", e));
|
||||
None
|
||||
},
|
||||
};
|
||||
|
||||
if self.conn.is_some() {
|
||||
log(LogType::Info, LogArea::Db, "Connected to database");
|
||||
}
|
||||
else {
|
||||
return Err("Failed to connect to database".to_string());
|
||||
}
|
||||
|
||||
if let Err(e) = self.conn.as_ref().unwrap().execute("CREATE TABLE IF NOT EXISTS nodes (id INTEGER PRIMARY KEY, ip_encrypted TEXT NOT NULL, timeout TEXT NOT NULL)") {
|
||||
log(LogType::Error, LogArea::Db, &format!("Failed to create nodes table: {}", e));
|
||||
return Err("Failed to create nodes table".to_string());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get(&self, key: &str) -> Result<Map<String, String>, String> {
|
||||
@ -24,15 +47,18 @@ impl DbBackend for SqliteBackend {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn set(&self, key: &str, value: Map<String, String>) -> Result<Map<String, String>, String> {
|
||||
fn set(&mut self, key: &str, value: Map<String, String>) -> Result<Map<String, String>, String> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn set_inner(&self, key: &str, inner_key: &str, value: &str) -> Result<&str, String> {
|
||||
fn set_inner(&mut self, key: &str, inner_key: &str, value: &str) -> Result<&str, String> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn disconnect(&self) -> Result<(), String> {
|
||||
todo!()
|
||||
fn disconnect(&mut self) -> Result<(), String> {
|
||||
log(LogType::Info, LogArea::Db, "Disconnecting from database");
|
||||
drop(self.conn.take());
|
||||
self.conn = None;
|
||||
Ok(())
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ mod db;
|
||||
mod log;
|
||||
|
||||
fn main() {
|
||||
db::get_backend("redis");
|
||||
println!("{}", db::get_backend_names().join(", "));
|
||||
let mut db = db::get_backend("sqlite");
|
||||
db.as_mut().connect().unwrap();
|
||||
db.as_mut().disconnect().unwrap();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user