diff options
| author | Yuvi Panda <yuvipanda@gmail.com> | 2021-05-09 13:03:56 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-09 08:33:56 +0100 |
| commit | 19bd00f62005d07fc22ef72558e1102a7bb13b03 (patch) | |
| tree | 818aaf786755e37aec2dcfa40ea938f42edc72db /atuin-client/src/database.rs | |
| parent | Retain the query entered into the TUI (#76) (diff) | |
| download | atuin-19bd00f62005d07fc22ef72558e1102a7bb13b03.zip | |
Support fulltext search of commands (#75)
Diffstat (limited to '')
| -rw-r--r-- | atuin-client/src/database.rs | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/atuin-client/src/database.rs b/atuin-client/src/database.rs index e56a8df0..160c6054 100644 --- a/atuin-client/src/database.rs +++ b/atuin-client/src/database.rs @@ -13,6 +13,7 @@ use sqlx::sqlite::{ use sqlx::Row; use super::history::History; +use super::settings::SearchMode; #[async_trait] pub trait Database { @@ -34,7 +35,12 @@ pub trait Database { async fn last(&self) -> Result<History>; async fn before(&self, timestamp: chrono::DateTime<Utc>, count: i64) -> Result<Vec<History>>; - async fn search(&self, limit: Option<i64>, query: &str) -> Result<Vec<History>>; + async fn search( + &self, + limit: Option<i64>, + search_mode: SearchMode, + query: &str, + ) -> Result<Vec<History>>; async fn query_history(&self, query: &str) -> Result<Vec<History>>; } @@ -185,7 +191,7 @@ impl Database for Sqlite { // inject the unique check if unique { "where timestamp = ( - select max(timestamp) from history + select max(timestamp) from history where h.command = history.command )" } else { @@ -268,16 +274,26 @@ impl Database for Sqlite { Ok(res.0) } - async fn search(&self, limit: Option<i64>, query: &str) -> Result<Vec<History>> { + async fn search( + &self, + limit: Option<i64>, + search_mode: SearchMode, + query: &str, + ) -> Result<Vec<History>> { let query = query.to_string().replace("*", "%"); // allow wildcard char let limit = limit.map_or("".to_owned(), |l| format!("limit {}", l)); + let query = match search_mode { + SearchMode::Prefix => query, + SearchMode::FullText => format!("%{}", query), + }; + let res = sqlx::query( format!( "select * from history h - where command like ?1 || '%' + where command like ?1 || '%' and timestamp = ( - select max(timestamp) from history + select max(timestamp) from history where h.command = history.command ) order by timestamp desc {}", |
