code cleanup
This commit is contained in:
@@ -4,16 +4,20 @@ 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();
|
||||
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)?,
|
||||
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)?,
|
||||
})
|
||||
})
|
||||
}).unwrap();
|
||||
.unwrap();
|
||||
|
||||
let mut buffer = vec![];
|
||||
for word in iter {
|
||||
@@ -23,23 +27,38 @@ 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(),
|
||||
(
|
||||
),
|
||||
);
|
||||
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 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;
|
||||
|
||||
@@ -48,10 +67,8 @@ pub fn add_stat_list(stat: &mut Vec<CardStatistics>, connection: &Connection) {
|
||||
stat[index].id = id as u32;
|
||||
index += 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
pub fn add_stat(stat: &mut CardStatistics, connection: &Connection) {
|
||||
let time = Instant::now();
|
||||
let index = connection
|
||||
@@ -71,21 +88,19 @@ pub fn add_stat(stat: &mut CardStatistics, connection: &Connection) {
|
||||
println!("Added stat: {}", time.elapsed().as_millis());
|
||||
}
|
||||
|
||||
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection){
|
||||
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",
|
||||
(
|
||||
&stat.score,
|
||||
&stat.last_open.timestamp(),
|
||||
&stat.id
|
||||
),
|
||||
)
|
||||
.unwrap_or_else(|e| {println!("{}", e); 0});
|
||||
connection
|
||||
.execute(
|
||||
"UPDATE card_stats SET score = ?1, last_opened = ?2 WHERE id = ?3",
|
||||
(&stat.score, &stat.last_open.timestamp(), &stat.id),
|
||||
)
|
||||
.unwrap_or_else(|e| {
|
||||
println!("{}", e);
|
||||
0
|
||||
});
|
||||
println!("Updated stat: {}", time.elapsed().as_millis());
|
||||
|
||||
}
|
||||
|
||||
pub fn delete_stat(stat: &CardStatistics, connection: &Connection) {
|
||||
|
||||
Reference in New Issue
Block a user