Going somewhere.

This commit is contained in:
didier amyot committed 2023-02-25 12:04:40 -05:00
1 parent 9f86d53d11
commit 0bbe716d8d
9 files changed
+160 -59

No files matched your search

+61 -7
View File
@@ -1,4 +1,5 @@
use std::io;
use std::path::PathBuf;
use rusqlite::{Connection, OpenFlags};
@@ -17,20 +18,73 @@ pub enum Error {
pub type CustomResult<T, E = Error> = Result<T, E>;
#[derive(Clone)]
struct PathSQLiteConnectionCreator {
path: PathBuf,
}
impl PathSQLiteConnectionCreator {
fn connection(&self) -> CustomResult<Connection> {
Connection::open(&self.path).map_err(Error::SqlError)
}
fn readonly(&self) -> CustomResult<Connection> {
let flags = OpenFlags::SQLITE_OPEN_READ_ONLY;
Connection::open_with_flags(&self.path, flags).map_err(Error::SqlError)
}
}
#[derive(Clone)]
struct EphemeralSQLiteConnectionCreator {}
// todo: fix this horrible implementation. All connections
// must share the same data.
impl EphemeralSQLiteConnectionCreator {
fn connection(&self) -> CustomResult<Connection> {
Connection::open_in_memory().map_err(Error::SqlError)
}
fn readonly(&self) -> CustomResult<Connection> {
let flags = OpenFlags::SQLITE_OPEN_READ_ONLY;
Connection::open_in_memory_with_flags(flags).map_err(Error::SqlError)
}
}
#[derive(Clone)]
enum SQLiteConnectionCreator {
Path(PathSQLiteConnectionCreator),
Ephemeral(EphemeralSQLiteConnectionCreator),
}
#[derive(Clone)]
pub struct SQLiteConnectionFactory {
path: String,
creator: SQLiteConnectionCreator,
}
impl SQLiteConnectionFactory {
pub fn new(path: String) -> SQLiteConnectionFactory {
SQLiteConnectionFactory { path }
pub fn new(path: PathBuf) -> SQLiteConnectionFactory {
SQLiteConnectionFactory {
creator: SQLiteConnectionCreator::Path(PathSQLiteConnectionCreator { path })
}
}
fn connection(&self) -> Connection {
Connection::open(&self.path).unwrap()
pub fn inMemory() -> SQLiteConnectionFactory {
SQLiteConnectionFactory {
creator: SQLiteConnectionCreator::Ephemeral(EphemeralSQLiteConnectionCreator {})
}
}
fn readonly(&self) -> Connection {
Connection::open_with_flags(&self.path, OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap()
pub fn connection(&self) -> CustomResult<Connection> {
match & self.creator {
SQLiteConnectionCreator::Path(c) => c.connection(),
SQLiteConnectionCreator::Ephemeral(c) => c.connection(),
}
}
pub fn readonly(&self) -> CustomResult<Connection> {
match & self.creator {
SQLiteConnectionCreator::Path(c) => c.readonly(),
SQLiteConnectionCreator::Ephemeral(c) => c.readonly(),
}
}
}