diff options
author | Ellie Huxtable <ellie@elliehuxtable.com> | 2023-03-08 23:45:14 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-08 23:45:14 +0000 |
commit | b91d4f4806a0cf3059db08f6dcfc4c1bb4cf992c (patch) | |
tree | fe3ed96922a75a7f811652526b22d9e1396b79c9 /atuin-client/src/database.rs | |
parent | afd1113b3b0301795f3557feb94f7d7f953c5f9c (diff) |
Fix before/after combined with limit (#770)
* Fix before/after combined with limit
Mixing filters done in Rust with filters done in SQL is _no bueno_.
Been meaning to do this for a while anyways. Search params are getting a
bit fat but oh well!
* Make an excuse for a big function sig
* Do options map_or not if
* Fix tests
Diffstat (limited to 'atuin-client/src/database.rs')
-rw-r--r-- | atuin-client/src/database.rs | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/atuin-client/src/database.rs b/atuin-client/src/database.rs index d0e696c45..d19a0b74e 100644 --- a/atuin-client/src/database.rs +++ b/atuin-client/src/database.rs @@ -71,13 +71,19 @@ pub trait Database: Send + Sync { async fn last(&self) -> Result<History>; async fn before(&self, timestamp: chrono::DateTime<Utc>, count: i64) -> Result<Vec<History>>; + // Yes I know, it's a lot. + // Could maybe break it down to a searchparams struct or smth but that feels a little... pointless. + // Been debating maybe a DSL for search? eg "before:time limit:1 the query" + #[allow(clippy::too_many_arguments)] async fn search( &self, - limit: Option<i64>, search_mode: SearchMode, filter: FilterMode, context: &Context, query: &str, + limit: Option<i64>, + before: Option<i64>, + after: Option<i64>, ) -> Result<Vec<History>>; async fn query_history(&self, query: &str) -> Result<Vec<History>>; @@ -385,11 +391,13 @@ impl Database for Sqlite { async fn search( &self, - limit: Option<i64>, search_mode: SearchMode, filter: FilterMode, context: &Context, query: &str, + limit: Option<i64>, + before: Option<i64>, + after: Option<i64>, ) -> Result<Vec<History>> { let mut sql = SqlBuilder::select_from("history"); @@ -401,6 +409,14 @@ impl Database for Sqlite { sql.limit(limit); } + if let Some(after) = after { + sql.and_where_gt("timestamp", after); + } + + if let Some(before) = before { + sql.and_where_lt("timestamp", before); + } + match filter { FilterMode::Global => &mut sql, FilterMode::Host => sql.and_where_eq("hostname", quote(&context.hostname)), @@ -498,7 +514,9 @@ mod test { cwd: "/home/ellie".to_string(), }; - let results = db.search(None, mode, filter_mode, &context, query).await?; + let results = db + .search(mode, filter_mode, &context, query, None, None, None) + .await?; assert_eq!( results.len(), @@ -701,7 +719,15 @@ mod test { } let start = Instant::now(); let _results = db - .search(None, SearchMode::Fuzzy, FilterMode::Global, &context, "") + .search( + SearchMode::Fuzzy, + FilterMode::Global, + &context, + "", + None, + None, + None, + ) .await .unwrap(); let duration = start.elapsed(); |