backend: Move fn init_conn into handler struct
This commit is contained in:
parent
40b2333767
commit
545b5a3a1b
1 changed files with 43 additions and 43 deletions
|
@ -37,7 +37,7 @@ impl DBHandler {
|
||||||
DBHandlerRequest::GetID(player_tx) => {
|
DBHandlerRequest::GetID(player_tx) => {
|
||||||
// got request to create new player, create new player
|
// got request to create new player, create new player
|
||||||
let mut player_id: u32 = thread_rng().gen();
|
let mut player_id: u32 = thread_rng().gen();
|
||||||
let conn_result = init_conn(&self.sqlite_path, DBFirstRun::NotFirstRun);
|
let conn_result = self.get_conn(DBFirstRun::NotFirstRun);
|
||||||
if let Err(e) = conn_result {
|
if let Err(e) = conn_result {
|
||||||
println!("Failed to get sqlite db connection: {:?}", e);
|
println!("Failed to get sqlite db connection: {:?}", e);
|
||||||
self.shutdown_tx.send(()).ok();
|
self.shutdown_tx.send(()).ok();
|
||||||
|
@ -80,10 +80,9 @@ impl DBHandler {
|
||||||
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn init_conn(sqlite_path: &str, first_run: DBFirstRun) -> Result<Connection, String> {
|
fn get_conn(&self, first_run: DBFirstRun) -> Result<Connection, String> {
|
||||||
if let Ok(conn) = Connection::open(sqlite_path) {
|
if let Ok(conn) = Connection::open(&self.sqlite_path) {
|
||||||
conn.execute("PRAGMA foreign_keys = ON;", [])
|
conn.execute("PRAGMA foreign_keys = ON;", [])
|
||||||
.map_err(|e| format!("Should be able to handle \"foreign_keys\": {:?}", e))?;
|
.map_err(|e| format!("Should be able to handle \"foreign_keys\": {:?}", e))?;
|
||||||
let result = conn.execute(
|
let result = conn.execute(
|
||||||
|
@ -127,6 +126,7 @@ fn init_conn(sqlite_path: &str, first_run: DBFirstRun) -> Result<Connection, Str
|
||||||
} else {
|
} else {
|
||||||
Err(String::from("Failed to open connection"))
|
Err(String::from("Failed to open connection"))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_db_handler_thread(
|
pub fn start_db_handler_thread(
|
||||||
|
@ -141,7 +141,7 @@ pub fn start_db_handler_thread(
|
||||||
};
|
};
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
// temporarily get conn which should initialize on first setup of db
|
// temporarily get conn which should initialize on first setup of db
|
||||||
if let Ok(_conn) = init_conn(&handler.sqlite_path, DBFirstRun::FirstRun) {
|
if let Ok(_conn) = handler.get_conn(DBFirstRun::FirstRun) {
|
||||||
} else {
|
} else {
|
||||||
println!("ERROR: Failed init sqlite db connection");
|
println!("ERROR: Failed init sqlite db connection");
|
||||||
handler.shutdown_tx.send(()).ok();
|
handler.shutdown_tx.send(()).ok();
|
||||||
|
|
Loading…
Reference in a new issue