2019-05-25 23:54:37 -04:00
|
|
|
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());
|
2019-05-26 00:09:56 -04:00
|
|
|
let mut conn: Connection = Connection::open(path).unwrap();
|
|
|
|
let tx = conn.transaction().unwrap();
|
|
|
|
tx.execute_batch(DB_INIT_QUERY).unwrap();
|
|
|
|
tx.commit();
|
2019-05-25 23:54:37 -04:00
|
|
|
DbHandle { conn: conn }
|
|
|
|
}
|
|
|
|
}
|