2026-08-18 17:45:50 +03:00
|
|
|
use crate::lang::{CardStatistics, DeckSettings};
|
2026-08-19 23:51:41 +03:00
|
|
|
use rusqlite::{Connection, params};
|
2026-07-29 13:30:49 +03:00
|
|
|
use std::time::Instant;
|
2026-04-13 22:25:58 +03:00
|
|
|
|
2026-08-18 15:16:51 +03:00
|
|
|
pub fn load_stats_of_deck(set: &DeckSettings, connection: &Connection) -> Vec<CardStatistics> {
|
2026-08-01 19:43:07 +03:00
|
|
|
let mut stmt = connection
|
|
|
|
|
.prepare("SELECT id, word_id, score, last_opened FROM card_stats WHERE set_id = ?1")
|
|
|
|
|
.unwrap();
|
|
|
|
|
let iter = stmt
|
|
|
|
|
.query_map((set.id,), |row| {
|
|
|
|
|
Ok(CardStatistics {
|
|
|
|
|
id: row.get(0)?,
|
|
|
|
|
word_id: row.get(1)?,
|
|
|
|
|
set_id: set.id,
|
|
|
|
|
score: row.get(2)?,
|
|
|
|
|
last_open: row.get(3)?,
|
|
|
|
|
})
|
2026-04-13 22:25:58 +03:00
|
|
|
})
|
2026-08-01 19:43:07 +03:00
|
|
|
.unwrap();
|
2026-04-13 22:25:58 +03:00
|
|
|
|
|
|
|
|
let mut buffer = vec![];
|
|
|
|
|
for word in iter {
|
|
|
|
|
buffer.push(word.unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-19 17:33:22 +03:00
|
|
|
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) {
|
2026-08-16 14:23:00 +03:00
|
|
|
let time = Instant::now();
|
2026-08-19 17:33:22 +03:00
|
|
|
let tx = connection.transaction().unwrap();
|
|
|
|
|
let count = stats.len();
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut stmt = tx
|
|
|
|
|
.prepare(
|
|
|
|
|
"INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES (?1, ?2, ?3, ?4)",
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
for stat in stats.iter() {
|
|
|
|
|
stmt.execute(params![
|
2026-08-15 13:22:59 +03:00
|
|
|
stat.word_id,
|
|
|
|
|
stat.set_id,
|
|
|
|
|
stat.score,
|
|
|
|
|
stat.last_open.timestamp()
|
2026-08-19 17:33:22 +03:00
|
|
|
])
|
2026-08-19 23:51:41 +03:00
|
|
|
.unwrap();
|
2026-08-19 17:33:22 +03:00
|
|
|
}
|
2026-07-29 13:30:49 +03:00
|
|
|
}
|
|
|
|
|
|
2026-08-19 17:33:22 +03:00
|
|
|
let last_index: u32 = tx.last_insert_rowid() as u32;
|
|
|
|
|
tx.commit().unwrap();
|
2026-07-29 13:30:49 +03:00
|
|
|
|
2026-08-19 17:33:22 +03:00
|
|
|
let start_index = last_index - (count as u32) + 1;
|
2026-07-29 13:30:49 +03:00
|
|
|
|
2026-08-15 13:22:59 +03:00
|
|
|
for (index, id) in (start_index..=last_index).enumerate() {
|
2026-08-19 17:33:22 +03:00
|
|
|
stats[index].id = id.into();
|
2026-07-29 13:30:49 +03:00
|
|
|
}
|
2026-08-18 17:31:19 +03:00
|
|
|
|
2026-08-19 17:33:22 +03:00
|
|
|
println!("Added {} cards for {:?}", stats.len(), time.elapsed());
|
2026-07-29 13:30:49 +03:00
|
|
|
}
|
|
|
|
|
|
2026-08-01 19:43:07 +03:00
|
|
|
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) {
|
2026-07-29 13:30:49 +03:00
|
|
|
let time = Instant::now();
|
|
|
|
|
|
2026-08-01 19:43:07 +03:00
|
|
|
connection
|
|
|
|
|
.execute(
|
|
|
|
|
"UPDATE card_stats SET score = ?1, last_opened = ?2 WHERE id = ?3",
|
|
|
|
|
(&stat.score, &stat.last_open.timestamp(), &stat.id),
|
|
|
|
|
)
|
2026-08-19 23:37:59 +03:00
|
|
|
.unwrap();
|
2026-07-29 13:30:49 +03:00
|
|
|
println!("Updated stat: {}", time.elapsed().as_millis());
|
2026-04-14 15:19:46 +03:00
|
|
|
}
|
|
|
|
|
|
2026-04-15 07:53:32 +03:00
|
|
|
pub fn delete_stat(stat: &CardStatistics, connection: &Connection) {
|
2026-08-18 17:31:19 +03:00
|
|
|
if !stat.id.is_valid() {
|
2026-04-13 22:25:58 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
connection
|
2026-08-19 23:51:41 +03:00
|
|
|
.execute("DELETE FROM card_stats WHERE id = ?1", (&stat.id,))
|
|
|
|
|
.unwrap();
|
2026-04-13 22:25:58 +03:00
|
|
|
}
|
2026-08-20 16:22:38 +03:00
|
|
|
|
|
|
|
|
pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) {
|
|
|
|
|
if stat.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let ids = stat.iter().map(|s| s.id.into()).collect::<Vec<String>>().join(", ");
|
|
|
|
|
connection
|
|
|
|
|
.execute(format!("DELETE FROM card_stats WHERE id in {ids}").as_str(), ())
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|