fix filtered decks not honoring sort order

https://forums.ankiweb.net/t/2-1-36-filtered-decks-bug/5649/
This commit is contained in:
Damien Elmes 2020-12-09 22:50:49 +10:00
parent dffbe2bfdf
commit a9ea8e11a2
4 changed files with 26 additions and 2 deletions

View file

@ -230,7 +230,7 @@ impl Collection {
let order = order_and_limit_for_search(term, ctx.today);
self.search_cards_into_table(&search, SortMode::Custom(order))?;
for mut card in self.storage.all_searched_cards()? {
for mut card in self.storage.all_searched_cards_in_search_order()? {
let original = card.clone();
card.move_into_filtered_deck(ctx, position);
self.update_card(&mut card, &original, ctx.usn)?;

View file

@ -94,11 +94,17 @@ impl Collection {
pub(crate) fn search_cards_into_table(&mut self, search: &str, mode: SortMode) -> Result<()> {
let top_node = Node::Group(parse(search)?);
let writer = SqlWriter::new(self);
let want_order = mode != SortMode::NoOrder;
let (mut sql, args) = writer.build_cards_query(&top_node, mode.required_table())?;
self.add_order(&mut sql, mode)?;
self.storage.setup_searched_cards_table()?;
if want_order {
self.storage
.setup_searched_cards_table_to_preserve_order()?;
} else {
self.storage.setup_searched_cards_table()?;
}
let sql = format!("insert into search_cids {}", sql);
self.storage.db.prepare(&sql)?.execute(&args)?;

View file

@ -273,6 +273,16 @@ impl super::SqliteStorage {
.collect()
}
pub(crate) fn all_searched_cards_in_search_order(&self) -> Result<Vec<Card>> {
self.db
.prepare_cached(concat!(
include_str!("get_card.sql"),
", search_cids where cards.id = search_cids.cid order by search_cids.rowid"
))?
.query_and_then(NO_PARAMS, |r| row_to_card(r).map_err(Into::into))?
.collect()
}
pub(crate) fn for_each_card_in_search<F>(&self, mut func: F) -> Result<()>
where
F: FnMut(Card) -> Result<()>,
@ -333,6 +343,12 @@ impl super::SqliteStorage {
Ok(())
}
pub(crate) fn setup_searched_cards_table_to_preserve_order(&self) -> Result<()> {
self.db
.execute_batch(include_str!("search_cids_setup_ordered.sql"))?;
Ok(())
}
pub(crate) fn clear_searched_cards_table(&self) -> Result<()> {
self.db
.execute("drop table if exists search_cids", NO_PARAMS)?;

View file

@ -0,0 +1,2 @@
drop table if exists search_cids;
create temporary table search_cids (cid integer not null);