You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
657 B
Rust

use rusqlite::Connection;
use std::path::PathBuf;
static DB_INIT_QUERY: &'static str = include_str!("init.sql");
pub struct DbHandle {
conn: Connection,
}
impl DbHandle {
pub fn new(db_path: &Option<String>) -> DbHandle {
let path: PathBuf = db_path
.as_ref()
.map(|path: &String| PathBuf::from(path))
.unwrap_or_else(|| dirs::home_dir().unwrap().join(".foil").to_path_buf());
let mut conn: Connection = Connection::open(path).unwrap();
let tx = conn.transaction().unwrap();
tx.execute_batch(DB_INIT_QUERY).unwrap();
tx.commit();
DbHandle { conn: conn }
}
}