mirror of
https://git.ethanthesleepy.one/ethanaobrien/ew.git
synced 2025-05-13 11:37:33 -05:00
Move back to mutex in sql.rs
This commit is contained in:
parent
7493724cd7
commit
b21deaee5f
1 changed files with 57 additions and 80 deletions
67
src/sql.rs
67
src/sql.rs
|
@ -4,9 +4,24 @@ use json::{JsonValue, array};
|
|||
|
||||
use crate::router::clear_rate::Live;
|
||||
|
||||
macro_rules! lock_onto_mutex {
|
||||
($mutex:expr) => {{
|
||||
loop {
|
||||
match $mutex.lock() {
|
||||
Ok(value) => {
|
||||
break value;
|
||||
}
|
||||
Err(_) => {
|
||||
$mutex.clear_poison();
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
pub struct SQLite {
|
||||
engine: Mutex<Connection>,
|
||||
sleep_duration: u64
|
||||
engine: Mutex<Connection>
|
||||
}
|
||||
|
||||
impl SQLite {
|
||||
|
@ -14,30 +29,17 @@ impl SQLite {
|
|||
let conn = Connection::open(crate::get_data_path(path)).unwrap();
|
||||
conn.execute("PRAGMA foreign_keys = ON;", ()).unwrap();
|
||||
let instance = SQLite {
|
||||
engine: Mutex::new(conn),
|
||||
sleep_duration: 10
|
||||
engine: Mutex::new(conn)
|
||||
};
|
||||
setup(&instance);
|
||||
instance
|
||||
}
|
||||
pub fn lock_and_exec(&self, command: &str, args: &[&dyn ToSql]) {
|
||||
loop {
|
||||
match self.engine.lock() {
|
||||
Ok(conn) => {
|
||||
let conn = lock_onto_mutex!(self.engine);
|
||||
conn.execute(command, args).unwrap();
|
||||
return;
|
||||
}
|
||||
Err(_) => {
|
||||
self.engine.clear_poison();
|
||||
std::thread::sleep(std::time::Duration::from_millis(self.sleep_duration));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn lock_and_select(&self, command: &str, args: &[&dyn ToSql]) -> Result<String, rusqlite::Error> {
|
||||
loop {
|
||||
match self.engine.lock() {
|
||||
Ok(conn) => {
|
||||
let conn = lock_onto_mutex!(self.engine);
|
||||
let mut stmt = conn.prepare(command)?;
|
||||
return stmt.query_row(args, |row| {
|
||||
match row.get::<usize, i64>(0) {
|
||||
|
@ -46,17 +48,8 @@ impl SQLite {
|
|||
}
|
||||
});
|
||||
}
|
||||
Err(_) => {
|
||||
self.engine.clear_poison();
|
||||
std::thread::sleep(std::time::Duration::from_millis(self.sleep_duration));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn lock_and_select_all(&self, command: &str, args: &[&dyn ToSql]) -> Result<JsonValue, rusqlite::Error> {
|
||||
loop {
|
||||
match self.engine.lock() {
|
||||
Ok(conn) => {
|
||||
let conn = lock_onto_mutex!(self.engine);
|
||||
let mut stmt = conn.prepare(command)?;
|
||||
let map = stmt.query_map(args, |row| {
|
||||
match row.get::<usize, i64>(0) {
|
||||
|
@ -74,17 +67,8 @@ impl SQLite {
|
|||
}
|
||||
return Ok(rv);
|
||||
}
|
||||
Err(_) => {
|
||||
self.engine.clear_poison();
|
||||
std::thread::sleep(std::time::Duration::from_millis(self.sleep_duration));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn get_live_data(&self, id: i64) -> Result<Live, rusqlite::Error> {
|
||||
loop {
|
||||
match self.engine.lock() {
|
||||
Ok(conn) => {
|
||||
let conn = lock_onto_mutex!(self.engine);
|
||||
let mut stmt = conn.prepare("SELECT * FROM lives WHERE live_id=?1")?;
|
||||
return stmt.query_row(params!(id), |row| {
|
||||
Ok(Live {
|
||||
|
@ -100,13 +84,6 @@ impl SQLite {
|
|||
})
|
||||
});
|
||||
}
|
||||
Err(_) => {
|
||||
self.engine.clear_poison();
|
||||
std::thread::sleep(std::time::Duration::from_millis(self.sleep_duration));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn create_store_v2(&self, table: &str) {
|
||||
self.lock_and_exec(table, params!());
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue