From 7b716c111565b340e9770536e56a398815756af5 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Sat, 11 Feb 2023 09:58:34 -0500 Subject: [PATCH] Started to wrap sqlite connection into a factory. --- .../migrations/sql/create_tables_001.sql | 2 + src/util.rs | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/resources/migrations/sql/create_tables_001.sql b/resources/migrations/sql/create_tables_001.sql index d95cf2f..98517c1 100644 --- a/resources/migrations/sql/create_tables_001.sql +++ b/resources/migrations/sql/create_tables_001.sql @@ -1,3 +1,5 @@ +PRAGMA journal_mode=WAL; + CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, diff --git a/src/util.rs b/src/util.rs index b84bdf0..4a44e03 100644 --- a/src/util.rs +++ b/src/util.rs @@ -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 = Result; + +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() + } +} \ No newline at end of file