Started to wrap sqlite connection into a factory.

This commit is contained in:
didier amyot committed 2023-02-11 09:58:34 -05:00
1 parent 5ff946af8f
commit 7b716c1115
2 files changed
+42

No files matched your search

@@ -1,3 +1,5 @@
PRAGMA journal_mode=WAL;
CREATE TABLE IF NOT EXISTS users CREATE TABLE IF NOT EXISTS users
( (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
+40
View File
@@ -1,5 +1,7 @@
use std::io; use std::io;
use rusqlite::{Connection, OpenFlags};
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
SqlError(rusqlite::Error), SqlError(rusqlite::Error),
@@ -12,3 +14,41 @@ pub enum Error {
} }
pub type CustomResult<T, E = Error> = Result<T, E>; 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()
}
}