mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-10-07 01:32:13 +00:00
refactor(db): use rusqlite query_row/optional instead of hand-rolled reads
pending_backlog, earliest_run_after, ChatStore::get and LinkCache::get each hand-rolled prepare + query + rows.next() for what is a single-row read. query_row + OptionalExtension::optional is the same statement and the same error mapping with less scaffolding; the backlog's NULL-on-empty MIN still goes through the count check, so a pending row with a NULL run_after is not misread. Also fixes the rustfmt drift from the previous commit.
This commit is contained in:
@@ -294,19 +294,16 @@ impl PersistentTaskQueue {
|
||||
let result = self
|
||||
.pool
|
||||
.with_conn(|conn| {
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT COUNT(*), MIN(run_after) FROM tasks WHERE status='pending'")?;
|
||||
let mut rows = stmt.query([])?;
|
||||
match rows.next()? {
|
||||
Some(row) => {
|
||||
let count = row.get::<_, i64>(0)?;
|
||||
match row.get::<_, Option<f64>>(1)? {
|
||||
Some(oldest) if count > 0 => Ok(Some((count, oldest))),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
let (count, oldest) = conn.query_row(
|
||||
"SELECT COUNT(*), MIN(run_after) FROM tasks WHERE status='pending'",
|
||||
[],
|
||||
|row| Ok((row.get::<_, i64>(0)?, row.get::<_, Option<f64>>(1)?)),
|
||||
)?;
|
||||
// `MIN` over zero rows is NULL, so the count is what decides.
|
||||
Ok(match oldest {
|
||||
Some(oldest) if count > 0 => Some((count, oldest)),
|
||||
_ => None,
|
||||
})
|
||||
})
|
||||
.await;
|
||||
match result {
|
||||
@@ -461,13 +458,11 @@ impl QueueWorker {
|
||||
let result = self
|
||||
.pool
|
||||
.with_conn(|conn| {
|
||||
let mut stmt =
|
||||
conn.prepare("SELECT MIN(run_after) FROM tasks WHERE status='pending'")?;
|
||||
let mut rows = stmt.query([])?;
|
||||
match rows.next()? {
|
||||
Some(row) => Ok(row.get::<_, Option<f64>>(0)?),
|
||||
None => Ok(None),
|
||||
}
|
||||
conn.query_row(
|
||||
"SELECT MIN(run_after) FROM tasks WHERE status='pending'",
|
||||
[],
|
||||
|row| row.get::<_, Option<f64>>(0),
|
||||
)
|
||||
})
|
||||
.await;
|
||||
match result {
|
||||
|
||||
Reference in New Issue
Block a user