refactor(sql): add query_map_collect()

This commit is contained in:
link2xt
2025-10-24 01:36:47 +00:00
committed by l
parent 5f3948b462
commit c001a9a983
4 changed files with 30 additions and 21 deletions

View File

@@ -386,6 +386,27 @@ impl Sql {
.await
}
/// Prepares and executes the statement and maps a function over the resulting rows.
///
/// Collects the resulting rows into a generic structure.
pub async fn query_map_collect<T, C, F>(
&self,
sql: &str,
params: impl rusqlite::Params + Send,
f: F,
) -> Result<C>
where
T: Send + 'static,
C: Send + 'static + std::iter::FromIterator<T>,
F: Send + FnMut(&rusqlite::Row) -> rusqlite::Result<T>,
{
self.query_map(sql, params, f, |rows| {
rows.collect::<std::result::Result<C, _>>()
.map_err(Into::into)
})
.await
}
/// Prepares and executes the statement and maps a function over the resulting rows.
///
/// Collects the resulting rows into a `Vec`.
@@ -399,11 +420,7 @@ impl Sql {
T: Send + 'static,
F: Send + FnMut(&rusqlite::Row) -> rusqlite::Result<T>,
{
self.query_map(sql, params, f, |rows| {
rows.collect::<std::result::Result<Vec<_>, _>>()
.map_err(Into::into)
})
.await
self.query_map_collect(sql, params, f).await
}
/// Used for executing `SELECT COUNT` statements only. Returns the resulting count.