CardStates rewritten to TableAccessQuerySet
This commit is contained in:
1 parent
45830a7985
commit
068b9c7197
7 files changed
+88
-40
No files matched your search
@@ -1,22 +1,72 @@
|
||||
use crate::data::entities::{CardStatistics, DeckSettings};
|
||||
use crate::data::entities::{CardState, DeckSettings};
|
||||
use crate::data_provider::TableAccessQuerySet;
|
||||
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
|
||||
use crate::notification::{MaybeError, NotificationData};
|
||||
use chrono::NaiveDate;
|
||||
use fsrs::MemoryState;
|
||||
use log::info;
|
||||
use rusqlite::{Connection, params};
|
||||
use rusqlite::{Connection, Error, Params, Row, params};
|
||||
use std::time::Instant;
|
||||
|
||||
pub struct CardStatesQuerySet;
|
||||
impl TableAccessQuerySet<CardState> for CardStatesQuerySet {
|
||||
fn load() -> &'static str {
|
||||
"INSERT INTO states (word_id, set_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day) VALUES (?1, ?2, ?3, ?4)"
|
||||
}
|
||||
|
||||
fn save() -> &'static str {
|
||||
"UPDATE states SET score = ?1, last_opened = ?2 WHERE id = ?3"
|
||||
}
|
||||
|
||||
fn add() -> &'static str {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn name() -> &'static str {
|
||||
"states"
|
||||
}
|
||||
|
||||
fn from_sql(row: &Row) -> Result<CardState, Error> {
|
||||
Ok(CardState {
|
||||
id: row.get(0)?,
|
||||
word_id: row.get(1)?,
|
||||
set_id: set.id,
|
||||
score: row.get(2)?,
|
||||
fsrs_opened: NaiveDate::from_epoch_days(row.get::<usize, i32>(4)?)
|
||||
.expect("Incorrect date encoding"),
|
||||
fsrs_memory: get_memory(row.get(5)?),
|
||||
last_open: row.get(3)?,
|
||||
fsrs_open_day: NaiveDate::from_epoch_days(row.get::<usize, i32>(6)?)
|
||||
.expect("Incorrect date encoding"),
|
||||
})
|
||||
}
|
||||
|
||||
fn insert_params(entity: &CardState) -> impl Params {
|
||||
(
|
||||
entity.word_id,
|
||||
entity.set_id,
|
||||
entity.score,
|
||||
entity.last_open.timestamp(),
|
||||
entity.fsrs_opened.to_epoch_days(),
|
||||
memory_to_i64(&entity.fsrs_memory),
|
||||
entity.fsrs_open_day.to_epoch_days(),
|
||||
)
|
||||
}
|
||||
|
||||
fn update_params(entity: &CardState) -> impl Params {
|
||||
(entity.score, entity.last_open.timestamp(), entity.id)
|
||||
}
|
||||
}
|
||||
pub fn load_stats_of_deck(
|
||||
set: &DeckSettings,
|
||||
connection: &Connection,
|
||||
) -> Result<Vec<CardStatistics>, NotificationData> {
|
||||
) -> Result<Vec<CardState>, NotificationData> {
|
||||
let mut stmt = connection
|
||||
.prepare("SELECT id, word_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day FROM states WHERE set_id = ?1")
|
||||
.expect("Failed to prepare state query");
|
||||
let iter = stmt
|
||||
.query_map((set.id,), |row| {
|
||||
Ok(CardStatistics {
|
||||
Ok(CardState {
|
||||
id: row.get(0)?,
|
||||
word_id: row.get(1)?,
|
||||
set_id: set.id,
|
||||
@@ -55,7 +105,7 @@ fn memory_to_i64(value: &Option<MemoryState>) -> i64 {
|
||||
value.stability.to_bits() as i64 | ((value.difficulty.to_bits() as i64) << 32)
|
||||
}
|
||||
|
||||
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) -> MaybeError {
|
||||
pub fn add_stat_list(stats: &mut [CardState], connection: &mut Connection) -> MaybeError {
|
||||
let time = Instant::now();
|
||||
let tx = connection
|
||||
.transaction()
|
||||
@@ -96,7 +146,7 @@ pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) -> MaybeError {
|
||||
pub fn update_stat_score(stat: &CardState, connection: &Connection) -> MaybeError {
|
||||
let time = Instant::now();
|
||||
|
||||
connection
|
||||
@@ -109,7 +159,7 @@ pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) -> Mayb
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) -> MaybeError {
|
||||
pub fn delete_stats(stat: &[CardState], connection: &Connection) -> MaybeError {
|
||||
if stat.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user