Rewrite fully second db table into result
This commit is contained in:
1 parent
06bf9138c0
commit
8841a51897
11 files changed
+127
-74
No files matched your search
@@ -23,7 +23,7 @@ pub fn load_sets(connection: &Connection) -> Result<Vec<DeckSettings>, Notificat
|
||||
})
|
||||
.map_err(|_| DbLoadError)?;
|
||||
|
||||
let mut buffer = vec![];
|
||||
let mut buffer = Vec::with_capacity(16);
|
||||
for word in iter.filter_map(|result| result.ok()) {
|
||||
buffer.push(word);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
use crate::data::entities::{CardStatistics, DeckSettings};
|
||||
use crate::notification::NotificationData;
|
||||
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
|
||||
use log::info;
|
||||
use rusqlite::{Connection, params};
|
||||
use std::time::Instant;
|
||||
use log::info;
|
||||
use crate::data::entities::{CardStatistics, DeckSettings};
|
||||
|
||||
pub fn load_stats_of_deck(set: &DeckSettings, connection: &Connection) -> Vec<CardStatistics> {
|
||||
pub fn load_stats_of_deck(
|
||||
set: &DeckSettings,
|
||||
connection: &Connection,
|
||||
) -> Result<Vec<CardStatistics>, NotificationData> {
|
||||
let mut stmt = connection
|
||||
.prepare("SELECT id, word_id, score, last_opened FROM card_stats WHERE set_id = ?1")
|
||||
.unwrap();
|
||||
.expect("Failed to prepare state query");
|
||||
let iter = stmt
|
||||
.query_map((set.id,), |row| {
|
||||
Ok(CardStatistics {
|
||||
@@ -17,19 +22,19 @@ pub fn load_stats_of_deck(set: &DeckSettings, connection: &Connection) -> Vec<Ca
|
||||
last_open: row.get(3)?,
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
.map_err(|_| DbLoadError)?;
|
||||
|
||||
let mut buffer = vec![];
|
||||
for word in iter {
|
||||
buffer.push(word.unwrap());
|
||||
let mut buffer = Vec::with_capacity(512);
|
||||
for word in iter.filter_map(|result| result.ok()) {
|
||||
buffer.push(word);
|
||||
}
|
||||
|
||||
buffer
|
||||
Ok(buffer)
|
||||
}
|
||||
|
||||
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) {
|
||||
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) -> Result<(), NotificationData> {
|
||||
let time = Instant::now();
|
||||
let tx = connection.transaction().unwrap();
|
||||
let tx = connection.transaction().expect("Failed to create transaction");
|
||||
let count = stats.len();
|
||||
|
||||
{
|
||||
@@ -37,7 +42,7 @@ pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection)
|
||||
.prepare(
|
||||
"INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES (?1, ?2, ?3, ?4)",
|
||||
)
|
||||
.unwrap();
|
||||
.expect("Failed to prepare state query");
|
||||
|
||||
for stat in stats.iter() {
|
||||
stmt.execute(params![
|
||||
@@ -46,12 +51,12 @@ pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection)
|
||||
stat.score,
|
||||
stat.last_open.timestamp()
|
||||
])
|
||||
.unwrap();
|
||||
.map_err(|_| DbSaveError)?;
|
||||
}
|
||||
}
|
||||
|
||||
let last_index: u32 = tx.last_insert_rowid() as u32;
|
||||
tx.commit().unwrap();
|
||||
tx.commit().expect("Failed to commit");
|
||||
|
||||
let start_index = last_index - (count as u32) + 1;
|
||||
|
||||
@@ -60,9 +65,10 @@ pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection)
|
||||
}
|
||||
|
||||
info!("Added {} cards for {:?}", stats.len(), time.elapsed());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) {
|
||||
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) -> Result<(), NotificationData> {
|
||||
let time = Instant::now();
|
||||
|
||||
connection
|
||||
@@ -70,13 +76,14 @@ pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) {
|
||||
"UPDATE card_stats SET score = ?1, last_opened = ?2 WHERE id = ?3",
|
||||
(&stat.score, &stat.last_open.timestamp(), &stat.id),
|
||||
)
|
||||
.unwrap();
|
||||
.map_err(|_| DbSaveError)?;
|
||||
info!("Updated stat: {}", time.elapsed().as_millis());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) {
|
||||
pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) -> Result<(), NotificationData> {
|
||||
if stat.is_empty() {
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
let ids = stat
|
||||
.iter()
|
||||
@@ -88,5 +95,6 @@ pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) {
|
||||
format!("DELETE FROM card_stats WHERE id in ({ids})").as_str(),
|
||||
(),
|
||||
)
|
||||
.unwrap();
|
||||
.map_err(|_| DbSaveError)?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user