Faster new set initializing

This commit is contained in:
2026-07-29 13:30:49 +03:00
parent b8be482efd
commit 0b0b9f307d
5 changed files with 97 additions and 50 deletions
+36
View File
@@ -1,6 +1,7 @@
use crate::lang::CardStatistics;
use crate::repetitions::CardSetSettings;
use rusqlite::Connection;
use std::time::Instant;
pub fn load_stats_of_set(set: &CardSetSettings, connection: &Connection) -> Vec<CardStatistics> {
let mut stmt = connection.prepare("SELECT id, word_id, score, last_opened FROM card_stats WHERE set_id = ?1").unwrap();
@@ -22,7 +23,37 @@ pub fn load_stats_of_set(set: &CardSetSettings, connection: &Connection) -> Vec<
buffer
}
pub fn add_stat_list(stat: &mut Vec<CardStatistics>, connection: &Connection) {
let inserting = stat.iter().map(|stat| format!("({}, {}, {}, {})", stat.word_id.to_string(), stat.set_id.to_string(), stat.score.to_string(), stat.last_open.timestamp().to_string())).collect::<Vec<_>>().join(", ");
let query = format!("INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES {}", inserting);
let count = connection
.execute(
query.as_str(),
(
),
);
if count.is_err() {
println!("{}", count.unwrap_err());
return;
}
let last_index : u32 = connection.query_one("SELECT seq from sqlite_sequence WHERE name == ?1", ("card_stats".to_string(),), |row| row.get(0)).unwrap();
let start_index = last_index - (count.unwrap() as u32) + 1;
let mut index = 0;
for id in start_index..=last_index {
stat[index].id = id as u32;
index += 1;
}
}
pub fn add_stat(stat: &mut CardStatistics, connection: &Connection) {
let time = Instant::now();
let index = connection
.query_row(
"INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES (?1, ?2, ?3, ?4) RETURNING id",
@@ -37,9 +68,12 @@ pub fn add_stat(stat: &mut CardStatistics, connection: &Connection) {
.unwrap_or_else(|e| {println!("{}", e); 0});
stat.id = index;
println!("Added stat: {}", time.elapsed().as_millis());
}
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection){
let time = Instant::now();
connection
.execute(
"UPDATE card_stats SET score = ?1, last_opened = ?2 WHERE id = ?3",
@@ -50,6 +84,8 @@ pub fn update_stat_score(stat: &CardStatistics, connection: &Connection){
),
)
.unwrap_or_else(|e| {println!("{}", e); 0});
println!("Updated stat: {}", time.elapsed().as_millis());
}
pub fn delete_stat(stat: &CardStatistics, connection: &Connection) {