Execute a query which is expected to return zero or one row.
(
&self,
sql: &str,
params: impl rusqlite::Params + Send,
f: F,
)
| 526 | |
| 527 | /// Execute a query which is expected to return zero or one row. |
| 528 | pub async fn query_row_optional<T, F>( |
| 529 | &self, |
| 530 | sql: &str, |
| 531 | params: impl rusqlite::Params + Send, |
| 532 | f: F, |
| 533 | ) -> Result<Option<T>> |
| 534 | where |
| 535 | F: Send + FnOnce(&rusqlite::Row) -> rusqlite::Result<T>, |
| 536 | T: Send + 'static, |
| 537 | { |
| 538 | let query_only = true; |
| 539 | self.call(query_only, move |conn| { |
| 540 | match conn.query_row(sql.as_ref(), params, f) { |
| 541 | Ok(res) => Ok(Some(res)), |
| 542 | Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None), |
| 543 | Err(err) => Err(err.into()), |
| 544 | } |
| 545 | }) |
| 546 | .await |
| 547 | } |
| 548 | |
| 549 | /// Executes a query which is expected to return one row and one |
| 550 | /// column. If the query does not return any rows, returns `Ok(None)`. |