Move sql get_live_data function to clear_rate.rs

This commit is contained in:
Ethan O'Brien
2025-11-30 10:59:07 -06:00
parent fd47262f52
commit 74a00769aa
2 changed files with 27 additions and 20 deletions

View File

@@ -8,6 +8,29 @@ use crate::encryption;
use crate::sql::SQLite;
use crate::router::{global, databases};
trait SqlClearRate {
fn get_live_data(&self, id: i64) -> Result<Live, rusqlite::Error>;
}
impl SqlClearRate for SQLite {
fn get_live_data(&self, id: i64) -> Result<Live, rusqlite::Error> {
let conn = rusqlite::Connection::open(self.get_path()).unwrap();
let mut stmt = conn.prepare("SELECT * FROM lives WHERE live_id=?1")?;
stmt.query_row(params!(id), |row| {
Ok(Live {
live_id: row.get(0)?,
normal_failed: row.get(1)?,
normal_pass: row.get(2)?,
hard_failed: row.get(3)?,
hard_pass: row.get(4)?,
expert_failed: row.get(5)?,
expert_pass: row.get(6)?,
master_failed: row.get(7)?,
master_pass: row.get(8)?,
})
})
}
}
lazy_static! {
static ref DATABASE: SQLite = SQLite::new("live_statistics.db", setup_tables);
static ref CACHED_DATA: Mutex<Option<JsonValue>> = Mutex::new(None);

View File

@@ -1,8 +1,6 @@
use rusqlite::{Connection, params, ToSql};
use rusqlite::{Connection, ToSql};
use json::{JsonValue, array};
use crate::router::clear_rate::Live;
pub struct SQLite {
path: String
}
@@ -18,6 +16,9 @@ impl SQLite {
setup(&conn);
instance
}
pub fn get_path(&self) -> &str {
&self.path
}
pub fn lock_and_exec(&self, command: &str, args: &[&dyn ToSql]) {
let conn = Connection::open(&self.path).unwrap();
conn.execute(command, args).unwrap();
@@ -51,21 +52,4 @@ impl SQLite {
}
Ok(rv)
}
pub fn get_live_data(&self, id: i64) -> Result<Live, rusqlite::Error> {
let conn = Connection::open(&self.path).unwrap();
let mut stmt = conn.prepare("SELECT * FROM lives WHERE live_id=?1")?;
stmt.query_row(params!(id), |row| {
Ok(Live {
live_id: row.get(0)?,
normal_failed: row.get(1)?,
normal_pass: row.get(2)?,
hard_failed: row.get(3)?,
hard_pass: row.get(4)?,
expert_failed: row.get(5)?,
expert_pass: row.get(6)?,
master_failed: row.get(7)?,
master_pass: row.get(8)?,
})
})
}
}