diff options
Diffstat (limited to 'src/local')
| -rw-r--r-- | src/local/database.rs | 22 | ||||
| -rw-r--r-- | src/local/history.rs | 21 |
2 files changed, 41 insertions, 2 deletions
diff --git a/src/local/database.rs b/src/local/database.rs index 0c31566d..cba7142c 100644 --- a/src/local/database.rs +++ b/src/local/database.rs @@ -15,12 +15,17 @@ pub enum QueryParam { pub trait Database { fn save(&mut self, h: &History) -> Result<()>; fn save_bulk(&mut self, h: &[History]) -> Result<()>; + fn load(&self, id: &str) -> Result<History>; fn list(&self) -> Result<Vec<History>>; fn range(&self, from: chrono::DateTime<Utc>, to: chrono::DateTime<Utc>) -> Result<Vec<History>>; - fn update(&self, h: &History) -> Result<()>; + fn query(&self, query: &str, params: &[QueryParam]) -> Result<Vec<History>>; + fn update(&self, h: &History) -> Result<()>; + fn history_count(&self) -> Result<i64>; + + fn prefix_search(&self, query: &str) -> Result<Vec<History>>; } // Intended for use on a developer machine and not a sync server. @@ -199,6 +204,21 @@ impl Database for Sqlite { Ok(history_iter.filter_map(Result::ok).collect()) } + + 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())], + ) + } + + fn history_count(&self) -> Result<i64> { + let res: i64 = + self.conn + .query_row_and_then("select count(1) from history;", params![], |row| row.get(0))?; + + Ok(res) + } } fn history_from_sqlite_row( diff --git a/src/local/history.rs b/src/local/history.rs index 05600b80..0ca112bd 100644 --- a/src/local/history.rs +++ b/src/local/history.rs @@ -1,8 +1,9 @@ use std::env; +use std::hash::{Hash, Hasher}; use crate::command::uuid_v4; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct History { pub id: String, pub timestamp: i64, @@ -42,3 +43,21 @@ impl History { } } } + +impl PartialEq for History { + // for the sakes of listing unique history only, we do not care about + // anything else + // obviously this does not refer to the *same* item of history, but when + // we only render the command, it looks the same + fn eq(&self, other: &Self) -> bool { + self.command == other.command + } +} + +impl Eq for History {} + +impl Hash for History { + fn hash<H: Hasher>(&self, state: &mut H) { + self.command.hash(state); + } +} |
