Creating a DB handle.

This commit is contained in:
Tom Alexander
2024-05-05 00:48:04 -04:00
parent 50458d9f6e
commit 1928d4fb0f
4 changed files with 45 additions and 2 deletions

19
src/db/init.rs Normal file
View File

@@ -0,0 +1,19 @@
use std::path::Path;
use rusqlite::Connection;
static DB_INIT_QUERY: &str = include_str!("../../migrations/01_init.sql");
pub struct DbHandle {
conn: Connection,
}
impl DbHandle {
pub fn new<P: AsRef<Path>>(db_path: P) -> Result<DbHandle, Box<dyn std::error::Error>> {
let mut conn: Connection = Connection::open(db_path).unwrap();
let tx = conn.transaction()?;
tx.execute_batch(DB_INIT_QUERY).unwrap();
tx.commit().unwrap();
Ok(DbHandle { conn })
}
}

3
src/db/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
mod init;
pub(crate) use init::DbHandle;

View File

@@ -1,3 +1,10 @@
fn main() {
println!("Hello, world!");
mod db;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let db_path = std::env::args()
.nth(1)
.ok_or("Pass path to sqlite DB in first parameter.")?;
let mut db_conn: db::DbHandle = db::DbHandle::new(db_path)?;
println!("Done.");
Ok(())
}