Fixing some code problems

This commit is contained in:
2026-08-19 17:33:22 +03:00
parent a4d77196d0
commit 98c8093961
9 changed files with 32 additions and 109 deletions
+22 -48
View File
@@ -1,5 +1,5 @@
use crate::lang::{CardStatistics, DeckSettings};
use rusqlite::Connection;
use rusqlite::{params, Connection};
use std::time::Instant;
pub fn load_stats_of_deck(set: &DeckSettings, connection: &Connection) -> Vec<CardStatistics> {
@@ -26,67 +26,41 @@ pub fn load_stats_of_deck(set: &DeckSettings, connection: &Connection) -> Vec<Ca
buffer
}
pub fn add_stat_list(stat: &mut [CardStatistics], connection: &Connection) {
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) {
let time = Instant::now();
let inserting = stat
.iter()
.map(|stat| {
format!(
"({}, {}, {}, {})",
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![
stat.word_id,
stat.set_id,
stat.score,
stat.last_open.timestamp()
)
})
.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() {
return;
])
.unwrap();
}
}
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 last_index: u32 = tx.last_insert_rowid() as u32;
tx.commit().unwrap();
let start_index = last_index - (stat.len() as u32) + 1;
let start_index = last_index - (count as u32) + 1;
for (index, id) in (start_index..=last_index).enumerate() {
stat[index].id = id.into();
stats[index].id = id.into();
}
println!("Added {} cards for {:?}", stat.len(), time.elapsed());
println!("Added {} cards for {:?}", stats.len(), time.elapsed());
}
// 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",
// (
// &stat.word_id,
// &stat.set_id,
// &stat.score,
// &stat.last_open.timestamp(),
// ),
// |row| row.get(0)
// )
// .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();
+1 -7
View File
@@ -46,15 +46,9 @@ pub fn add_words(words: &mut [WordData], connection: &mut Connection) {
}
}
let last_index: u32 = tx.last_insert_rowid() as u32;
tx.commit().unwrap();
let last_index: u32 = connection
.query_one(
"SELECT seq from sqlite_sequence WHERE name == ?1",
("words".to_string(),),
|row| row.get(0),
)
.unwrap();
let start_index = last_index - (count as u32) + 1;