improvement: better sql error handling

This commit is contained in:
Greg Heartsfield 2022-01-05 18:33:08 -05:00
parent 4e51e61d16
commit a3124ccea4

View File

@ -417,8 +417,7 @@ pub async fn db_query(
let db_dir = &config.database.data_directory; let db_dir = &config.database.data_directory;
let full_path = Path::new(db_dir).join(DB_FILE); let full_path = Path::new(db_dir).join(DB_FILE);
let conn = let conn = Connection::open_with_flags(&full_path, OpenFlags::SQLITE_OPEN_READ_ONLY)?;
Connection::open_with_flags(&full_path, OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap();
debug!("opened database for reading"); debug!("opened database for reading");
debug!("going to query for: {:?}", sub); debug!("going to query for: {:?}", sub);
let mut row_count: usize = 0; let mut row_count: usize = 0;
@ -426,17 +425,16 @@ pub async fn db_query(
// generate SQL query // generate SQL query
let q = query_from_sub(&sub); let q = query_from_sub(&sub);
// execute the query // execute the query
let mut stmt = conn.prepare(&q).unwrap(); let mut stmt = conn.prepare(&q)?;
let mut event_rows = stmt.query([]).unwrap(); let mut event_rows = stmt.query([])?;
while let Some(row) = event_rows.next().unwrap() { while let Some(row) = event_rows.next()? {
// check if this is still active (we could do this every N rows) // check if this is still active (we could do this every N rows)
if abandon_query_rx.try_recv().is_ok() { if abandon_query_rx.try_recv().is_ok() {
debug!("query aborted"); debug!("query aborted");
return; return Ok(());
} }
row_count += 1; row_count += 1;
// TODO: check before unwrapping let event_json = row.get(0)?;
let event_json = row.get(0).unwrap();
query_tx query_tx
.blocking_send(QueryResult { .blocking_send(QueryResult {
sub_id: sub.get_id(), sub_id: sub.get_id(),
@ -449,5 +447,7 @@ pub async fn db_query(
row_count, row_count,
start.elapsed() start.elapsed()
); );
let ok: Result<()> = Ok(());
return ok;
}); });
} }