diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-01-05 23:52:45 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-01-05 23:52:45 +0100 |
commit | 26255357b7d444e0de0fc42b13c5f80868deeb44 (patch) | |
tree | f056cee9af9f29701a05d033b4a7636a8575d901 | |
parent | 2916debd5536274959bfe903e8bb30307deb45be (diff) |
Switch implementation to more builder-style interface for query building
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | lib/domain/libimagmail/src/store.rs | 99 |
1 files changed, 87 insertions, 12 deletions
diff --git a/lib/domain/libimagmail/src/store.rs b/lib/domain/libimagmail/src/store.rs index 86e4c6c3..049e5fbc 100644 --- a/lib/domain/libimagmail/src/store.rs +++ b/lib/domain/libimagmail/src/store.rs @@ -22,6 +22,7 @@ use std::borrow::Cow; use failure::Fallible as Result; use toml_query::insert::TomlValueInsertExt; use toml::Value; +use notmuch_rs::Sort as NotmuchSorting; use notmuch_rs::Query; use libimagstore::store::Store; @@ -45,6 +46,7 @@ impl<'a> MailStore<'a> for Store { } } +#[derive(Debug)] pub struct MailStoreWithConnection<'a> { store: &'a Store, connection: &'a NotmuchConnection, @@ -71,20 +73,14 @@ impl<'a> MailStoreWithConnection<'a> { Ok(new_entries) } - pub fn query(&self, q: &str) -> Result<Vec<FileLockEntry<'a>>> { - self.connection - .execute(|db| { - let query = db.create_query(q)?; - self.execute_query(query) - }) + pub fn query(&'a self, q: &str) -> Result<Vec<FileLockEntry<'a>>> { + let r = self.build_query(q).execute()?; + Ok(r) } - pub fn query_atomic(&self, q: &str) -> Result<Vec<FileLockEntry<'a>>> { - self.connection - .execute_atomic(|db| { - let query = db.create_query(q)?; - self.execute_query(query) - }) + pub fn query_atomic(&'a self, q: &str) -> Result<Vec<FileLockEntry<'a>>> { + let r = self.build_query(q).atomic(true).execute()?; + Ok(r) } fn execute_query<'d>(&self, q: Query<'d>) -> Result<Vec<FileLockEntry<'a>>> { @@ -96,6 +92,10 @@ impl<'a> MailStoreWithConnection<'a> { .collect::<Result<Vec<_>>>() } + pub fn build_query<'q>(&'a self, q: &'q str) -> QueryBuilder<'q, 'a> { + QueryBuilder::new(self, q) + } + pub fn get_mail_by_id(&self, id: &str) -> Result<Option<FileLockEntry>> { self.get_entry_for_id(Cow::from(id)) } @@ -118,3 +118,78 @@ impl<'a> MailStoreWithConnection<'a> { } } + +#[derive(Debug)] +pub struct QueryBuilder<'q, 's> { + store: &'s MailStoreWithConnection<'s>, + query: &'q str, + sorting: Sorting, + atomic: bool, +} + +#[derive(Debug)] +pub enum Sorting { + OldestFirst, + NewestFirst, + MessageId, + Unsorted, +} + +impl Default for Sorting { + fn default() -> Self { + Sorting::Unsorted + } +} + +impl Into<NotmuchSorting> for Sorting { + fn into(self) -> NotmuchSorting { + match self { + Sorting::OldestFirst => NotmuchSorting::OldestFirst, + Sorting::NewestFirst => NotmuchSorting::NewestFirst, + Sorting::MessageId => NotmuchSorting::MessageID, + Sorting::Unsorted => NotmuchSorting::Unsorted, + } + } +} + +impl<'q, 's> QueryBuilder<'q, 's> { + fn new(store: &'s MailStoreWithConnection, query: &'q str) -> Self { + QueryBuilder { + store, + query, + sorting: Sorting::default(), + atomic: false, + } + } + + pub fn sorted(mut self, sorting: Sorting) -> Self { + self.sorting = sorting; + self + } + + pub fn atomic(mut self, atomic: bool) -> Self { + self.atomic = atomic; + self + } + + pub fn execute(self) -> Result<Vec<FileLockEntry<'s>>> { + if self.atomic { + self.store + .connection + .execute_atomic(|db| { + let query = db.create_query(self.query)?; + query.set_sort(self.sorting.into()); + self.store.execute_query(query) + }) + } else { + self.store + .connection + .execute(|db| { + let query = db.create_query(self.query)?; + query.set_sort(self.sorting.into()); + self.store.execute_query(query) + }) + } + } +} + |