Started to wrap sqlite connection into a factory.
This commit is contained in:
1 parent
5ff946af8f
commit
7b716c1115
2 files changed
+42
No files matched your search
+40
@@ -1,5 +1,7 @@
|
||||
use std::io;
|
||||
|
||||
use rusqlite::{Connection, OpenFlags};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
SqlError(rusqlite::Error),
|
||||
@@ -12,3 +14,41 @@ pub enum Error {
|
||||
}
|
||||
|
||||
pub type CustomResult<T, E = Error> = Result<T, E>;
|
||||
|
||||
trait SQLiteConnectionFactory {
|
||||
fn connection(&self) -> Connection;
|
||||
fn readonly(&self) -> Connection;
|
||||
}
|
||||
|
||||
pub struct PathSQLiteConnectionFactory {
|
||||
path: String,
|
||||
}
|
||||
|
||||
impl PathSQLiteConnectionFactory {
|
||||
pub fn new(path: String) -> PathSQLiteConnectionFactory {
|
||||
PathSQLiteConnectionFactory { path }
|
||||
}
|
||||
}
|
||||
|
||||
impl SQLiteConnectionFactory for PathSQLiteConnectionFactory {
|
||||
fn connection(&self) -> Connection {
|
||||
Connection::open(&self.path).unwrap()
|
||||
}
|
||||
|
||||
fn readonly(&self) -> Connection {
|
||||
Connection::open_with_flags(&self.path, OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct InMemorySQLiteConnectionFactory{}
|
||||
|
||||
impl SQLiteConnectionFactory for InMemorySQLiteConnectionFactory {
|
||||
fn connection(&self) -> Connection {
|
||||
Connection::open_in_memory().unwrap()
|
||||
}
|
||||
|
||||
fn readonly(&self) -> Connection {
|
||||
Connection::open_in_memory_with_flags(OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user