diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/command/history.rs | 13 | ||||
-rw-r--r-- | src/local/database.rs | 24 |
2 files changed, 11 insertions, 26 deletions
diff --git a/src/command/history.rs b/src/command/history.rs index af8aef7d..05aed4b9 100644 --- a/src/command/history.rs +++ b/src/command/history.rs @@ -3,7 +3,7 @@ use std::env; use eyre::Result; use structopt::StructOpt; -use crate::local::database::{Database, QueryParam}; +use crate::local::database::Database; use crate::local::history::History; #[derive(StructOpt)] @@ -96,12 +96,11 @@ impl Cmd { let history = match params { (false, false) => db.list()?, - (true, false) => db.query(QUERY_SESSION, &[QueryParam::Text(session)])?, - (false, true) => db.query(QUERY_DIR, &[QueryParam::Text(cwd)])?, - (true, true) => db.query( - QUERY_SESSION_DIR, - &[QueryParam::Text(cwd), QueryParam::Text(session)], - )?, + (true, false) => db.query(QUERY_SESSION, &[session.as_str()])?, + (false, true) => db.query(QUERY_DIR, &[cwd.as_str()])?, + (true, true) => { + db.query(QUERY_SESSION_DIR, &[cwd.as_str(), session.as_str()])? + } }; print_list(&history); diff --git a/src/local/database.rs b/src/local/database.rs index cba7142c..ad7078e5 100644 --- a/src/local/database.rs +++ b/src/local/database.rs @@ -4,14 +4,10 @@ use std::path::Path; use eyre::Result; use rusqlite::{params, Connection}; -use rusqlite::{Transaction, NO_PARAMS}; +use rusqlite::{Params, Transaction}; use super::history::History; -pub enum QueryParam { - Text(String), -} - pub trait Database { fn save(&mut self, h: &History) -> Result<()>; fn save_bulk(&mut self, h: &[History]) -> Result<()>; @@ -21,7 +17,7 @@ pub trait Database { fn range(&self, from: chrono::DateTime<Utc>, to: chrono::DateTime<Utc>) -> Result<Vec<History>>; - fn query(&self, query: &str, params: &[QueryParam]) -> Result<Vec<History>>; + fn query(&self, query: &str, params: impl Params) -> Result<Vec<History>>; fn update(&self, h: &History) -> Result<()>; fn history_count(&self) -> Result<i64>; @@ -71,7 +67,7 @@ impl Sqlite { unique(timestamp, cwd, command) )", - NO_PARAMS, + [], )?; Ok(()) @@ -105,16 +101,6 @@ impl Sqlite { } } -impl rusqlite::ToSql for QueryParam { - fn to_sql(&self) -> Result<rusqlite::types::ToSqlOutput<'_>, rusqlite::Error> { - use rusqlite::types::{ToSqlOutput, Value}; - - match self { - Self::Text(s) => Ok(ToSqlOutput::Owned(Value::Text(s.clone()))), - } - } -} - impl Database for Sqlite { fn save(&mut self, h: &History) -> Result<()> { debug!("saving history to sqlite"); @@ -197,7 +183,7 @@ impl Database for Sqlite { Ok(history_iter.filter_map(Result::ok).collect()) } - fn query(&self, query: &str, params: &[QueryParam]) -> Result<Vec<History>> { + fn query(&self, query: &str, params: impl Params) -> Result<Vec<History>> { let mut stmt = self.conn.prepare(query)?; let history_iter = stmt.query_map(params, |row| history_from_sqlite_row(None, row))?; @@ -208,7 +194,7 @@ impl Database for Sqlite { fn prefix_search(&self, query: &str) -> Result<Vec<History>> { self.query( "select * from history where command like ?1 || '%' order by timestamp asc", - &[QueryParam::Text(query.to_string())], + &[query], ) } |