From 2c362d699106da4fbe81701664c438107f74f4fb Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 20 Mar 2020 15:35:57 +1000 Subject: [PATCH] search order --- rslib/src/config.rs | 27 +++++++++++++++++++++++++++ rslib/src/search/cards.rs | 32 +++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/rslib/src/config.rs b/rslib/src/config.rs index 9514d2cab..2224b68f3 100644 --- a/rslib/src/config.rs +++ b/rslib/src/config.rs @@ -16,4 +16,31 @@ pub struct Config { pub(crate) rollover: Option, pub(crate) creation_offset: Option, pub(crate) local_offset: Option, + #[serde(rename = "sortType")] + pub(crate) browser_sort_kind: SortKind, + #[serde(rename = "sortBackwards", default)] + pub(crate) browser_sort_reverse: bool, +} + +#[derive(Deserialize, PartialEq, Debug)] +#[serde(rename_all = "camelCase")] +pub enum SortKind { + #[serde(rename = "noteCrt")] + NoteCreation, + NoteMod, + #[serde(rename = "noteFld")] + NoteField, + CardMod, + CardReps, + CardDue, + CardEase, + CardLapses, + #[serde(rename = "cardIvl")] + CardInterval, +} + +impl Default for SortKind { + fn default() -> Self { + Self::NoteCreation + } } diff --git a/rslib/src/search/cards.rs b/rslib/src/search/cards.rs index 00a3f21e9..a0ab40562 100644 --- a/rslib/src/search/cards.rs +++ b/rslib/src/search/cards.rs @@ -2,7 +2,9 @@ // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html use super::{parser::Node, sqlwriter::node_to_sql}; +use crate::card::CardType; use crate::collection::RequestContext; +use crate::config::SortKind; use crate::err::Result; use crate::search::parser::parse; use crate::types::ObjID; @@ -13,11 +15,12 @@ pub(crate) fn search_cards<'a, 'b>( ) -> Result> { let top_node = Node::Group(parse(search)?); let (sql, args) = node_to_sql(req, &top_node)?; - - let sql = format!( - "select c.id from cards c, notes n where c.nid=n.id and {} order by c.id", + let mut sql = format!( + "select c.id from cards c, notes n where c.nid=n.id and {} order by ", sql ); + write_order(req, &mut sql)?; + let mut stmt = req.storage.db.prepare(&sql)?; let ids: Vec = stmt .query_map(&args, |row| row.get(0))? @@ -26,3 +29,26 @@ pub(crate) fn search_cards<'a, 'b>( println!("sql {}\nargs {:?} count {}", sql, args, ids.len()); Ok(ids) } + +fn write_order(req: &mut RequestContext, sql: &mut String) -> Result<()> { + let conf = req.storage.all_config()?; + let tmp_str; + sql.push_str(match conf.browser_sort_kind { + SortKind::NoteCreation => "n.id, c.ord", + SortKind::NoteMod => "n.mod, c.ord", + SortKind::NoteField => "n.sfld collate nocase, c.ord", + SortKind::CardMod => "c.mod", + SortKind::CardReps => "c.reps", + SortKind::CardDue => "c.type, c.due", + SortKind::CardEase => { + tmp_str = format!("c.type = {}, c.factor", CardType::New as i8); + &tmp_str + } + SortKind::CardLapses => "c.lapses", + SortKind::CardInterval => "c.ivl", + }); + if conf.browser_sort_reverse { + sql.push_str(" desc"); + } + Ok(()) +}