diff options
| author | Ellie Huxtable <e@elm.sh> | 2020-10-05 20:36:15 +0100 |
|---|---|---|
| committer | Ellie Huxtable <e@elm.sh> | 2020-10-05 20:38:34 +0100 |
| commit | 9917d6c1e2a743d5666247d8e87c9791721c213e (patch) | |
| tree | 088df4af1041d53bd1faa38d5542200689be57a1 /src/local/database.rs | |
| parent | Use bundled sqlite (diff) | |
| parent | Merge pull request #2 from conradludgate/main (diff) | |
| download | atuin-9917d6c1e2a743d5666247d8e87c9791721c213e.zip | |
Fix merge
...I forgot to push. oops.
Diffstat (limited to 'src/local/database.rs')
| -rw-r--r-- | src/local/database.rs | 46 |
1 files changed, 25 insertions, 21 deletions
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<SqliteDatabase> { - let path = shellexpand::full(path)?; +impl SqliteDatabase { + pub fn new(path: impl AsRef<Path>) -> Result<SqliteDatabase> { 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)?, |
