From 28287a63031ec44fdda0eea99d757a7047df5a41 Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Mon, 5 Oct 2020 17:20:48 +0100 Subject: feat: use directories project data dir chore: clean up some things --- src/local/database.rs | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) (limited to 'src/local/database.rs') diff --git a/src/local/database.rs b/src/local/database.rs index b84bf99c..b94a6445 100644 --- a/src/local/database.rs +++ b/src/local/database.rs @@ -1,12 +1,11 @@ use std::path::Path; use eyre::Result; -use shellexpand; -use rusqlite::{params, Connection}; use rusqlite::NO_PARAMS; +use rusqlite::{params, Connection}; -use super::history::History; +use crate::History; pub trait Database { fn save(&self, h: History) -> Result<()>; @@ -19,23 +18,25 @@ pub struct SqliteDatabase { conn: Connection, } -impl SqliteDatabase{ - pub fn new(path: &str) -> Result { - let path = shellexpand::full(path)?; +impl SqliteDatabase { + pub fn new(path: impl AsRef) -> Result { let path = path.as_ref(); - debug!("opening sqlite database at {:?}", path); - let create = !Path::new(path).exists(); + let create = !path.exists(); + if create { + if let Some(dir) = path.parent() { + std::fs::create_dir_all(dir)?; + } + } + let conn = Connection::open(path)?; if create { Self::setup_db(&conn)?; } - Ok(SqliteDatabase{ - conn: conn, - }) + Ok(SqliteDatabase { conn }) } fn setup_db(conn: &Connection) -> Result<()> { @@ -43,11 +44,11 @@ impl SqliteDatabase{ conn.execute( "create table if not exists history ( - id integer primary key, - timestamp integer not null, - command text not null, - cwd text not null - )", + id integer primary key, + timestamp integer not null, + command text not null, + cwd text not null + )", NO_PARAMS, )?; @@ -61,19 +62,22 @@ impl Database for SqliteDatabase { self.conn.execute( "insert into history ( - timestamp, + timestamp, command, cwd - ) values (?1, ?2, ?3)", - params![h.timestamp, h.command, h.cwd])?; + ) values (?1, ?2, ?3)", + params![h.timestamp, h.command, h.cwd], + )?; Ok(()) } fn list(&self) -> Result<()> { debug!("listing history"); - - let mut stmt = self.conn.prepare("SELECT timestamp, command, cwd FROM history")?; + + let mut stmt = self + .conn + .prepare("SELECT timestamp, command, cwd FROM history")?; let history_iter = stmt.query_map(params![], |row| { Ok(History { timestamp: row.get(0)?, -- cgit v1.3.1