mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 17:26:36 -04:00
search order
This commit is contained in:
parent
d94effcdc7
commit
2c362d6991
2 changed files with 56 additions and 3 deletions
|
@ -16,4 +16,31 @@ pub struct Config {
|
|||
pub(crate) rollover: Option<i8>,
|
||||
pub(crate) creation_offset: Option<i32>,
|
||||
pub(crate) local_offset: Option<i32>,
|
||||
#[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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Vec<ObjID>> {
|
||||
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<i64> = 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(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue