1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
mod auto_increment;
mod base;
mod mutable;
use std::sync::{Mutex, MutexGuard};
use {
crate::Result,
serde::{Deserialize, Serialize},
std::fmt::Debug,
thiserror::Error,
};
pub use {auto_increment::AutoIncrement, base::DBBase, mutable::DBMut};
#[derive(Error, Serialize, Debug, PartialEq)]
pub enum DatabaseError {
#[error("this database has not yet implemented this method")]
Unimplemented,
#[error("tried to connect to an unknown database")]
UnknownConnection,
#[error("table not found")]
TableNotFound,
}
#[derive(Serialize, Deserialize)]
pub enum Connection {
Unknown,
#[cfg(feature = "memory-database")]
Memory,
#[cfg(feature = "sled-database")]
Sled(String),
#[cfg(feature = "csv-database")]
CSV(String, crate::CSVSettings),
#[cfg(feature = "sheet-database")]
Sheet(String),
#[cfg(feature = "odbc-database")]
ODBC(String),
}
impl Default for Connection {
fn default() -> Self {
Connection::Unknown
}
}
impl TryFrom<Connection> for Database {
type Error = crate::Error;
fn try_from(connection: Connection) -> Result<Database> {
use Connection::*;
let database: Mutex<Box<DatabaseInner>> = Mutex::new(match &connection {
#[cfg(feature = "memory-database")]
Memory => Box::new(crate::MemoryDatabase::new()),
#[cfg(feature = "sled-database")]
Sled(path) => Box::new(crate::SledDatabase::new(path)?),
#[cfg(feature = "csv-database")]
CSV(path, settings) => Box::new(crate::CSVDatabase::new_with_settings(
path,
settings.clone(),
)?),
#[cfg(feature = "sheet-database")]
Sheet(path) => Box::new(crate::SheetDatabase::new(path)?),
#[cfg(feature = "odbc-database")]
ODBC(connection_string) => Box::new(crate::ODBCDatabase::new(connection_string)?),
Unknown => return Err(DatabaseError::UnknownConnection.into()),
});
Ok(Database {
database,
source_connection: connection,
})
}
}
pub struct Database {
source_connection: Connection,
database: Mutex<Box<DatabaseInner>>,
}
impl Database {
pub fn new(database: Box<DatabaseInner>) -> Self {
let database = Mutex::new(database);
Self {
database,
source_connection: Connection::default(),
}
}
pub fn get(&self) -> MutexGuard<Box<DatabaseInner>> {
self.database
.lock()
.expect("Unreachable: Database wasn't replaced!")
}
pub fn get_mut(&mut self) -> &mut Box<DatabaseInner> {
self.database
.get_mut()
.expect("Unreachable: Database wasn't replaced!")
}
pub fn into_source(self) -> Connection {
self.source_connection
}
pub fn from_source(connection: Connection) -> Result<Self> {
connection.try_into()
}
}
pub type DatabaseInner = dyn DBFull;
pub trait DBFull: DBBase + DBMut + AutoIncrement {}