update card in transaction

This commit is contained in:
Damien Elmes 2020-03-27 07:58:07 +10:00
parent fe327e32ca
commit 369d2d89d9
5 changed files with 17 additions and 15 deletions

View File

@ -6,7 +6,7 @@ use crate::backend_proto::backend_input::Value;
use crate::backend_proto::{BuiltinSortKind, Empty, RenderedTemplateReplacement, SyncMediaIn};
use crate::card::{Card, CardID};
use crate::card::{CardQueue, CardType};
use crate::collection::{open_collection, Collection};
use crate::collection::{open_collection, Collection, CollectionOp};
use crate::config::SortKind;
use crate::decks::DeckID;
use crate::err::{AnkiError, NetworkErrorKind, Result, SyncErrorKind};
@ -638,7 +638,11 @@ impl Backend {
fn update_card(&self, pbcard: pb::Card) -> Result<()> {
let mut card = pbcard_to_native(pbcard)?;
self.with_col(|col| col.with_ctx(|ctx| ctx.update_card(&mut card)))
self.with_col(|col| {
col.transact(Some(CollectionOp::UpdateCard), |ctx| {
ctx.update_card(&mut card)
})
})
}
}

View File

@ -3,7 +3,7 @@
use crate::decks::DeckID;
use crate::define_newtype;
use crate::err::Result;
use crate::err::{AnkiError, Result};
use crate::notes::NoteID;
use crate::{collection::RequestContext, timestamp::TimestampSecs, types::Usn};
use num_enum::TryFromPrimitive;
@ -88,8 +88,11 @@ impl Default for Card {
impl RequestContext<'_> {
pub fn update_card(&mut self, card: &mut Card) -> Result<()> {
if card.id.0 == 0 {
return Err(AnkiError::invalid_input("card id not set"));
}
card.mtime = TimestampSecs::now();
card.usn = self.storage.usn()?;
self.storage.update_card(card)
self.storage.flush_card(card)
}
}

View File

@ -51,7 +51,9 @@ pub struct Collection {
state: CollectionState,
}
pub(crate) enum CollectionOp {}
pub(crate) enum CollectionOp {
UpdateCard
}
pub(crate) struct RequestContext<'a> {
pub storage: StorageContext<'a>,

View File

@ -3,7 +3,7 @@
use crate::cached_sql;
use crate::card::{Card, CardID, CardQueue, CardType};
use crate::err::{AnkiError, Result};
use crate::err::Result;
use rusqlite::params;
use rusqlite::{
types::{FromSql, FromSqlError, ValueRef},
@ -70,14 +70,7 @@ flags, data from cards where id=?"
.map_err(Into::into)
}
pub(crate) fn update_card(&mut self, card: &Card) -> Result<()> {
if card.id.0 == 0 {
return Err(AnkiError::invalid_input("card id not set"));
}
self.flush_card(card)
}
fn flush_card(&mut self, card: &Card) -> Result<()> {
pub(crate) fn flush_card(&mut self, card: &Card) -> Result<()> {
let stmt = cached_sql!(
self.update_card_stmt,
self.db,

View File

@ -169,7 +169,7 @@ fn schema_version(db: &Connection) -> Result<(bool, u8)> {
}
fn trace(s: &str) {
println!("sql: {}", s)
println!("sql: {}", s.trim().replace('\n', " "));
}
impl SqliteStorage {