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

@@ -951,7 +951,7 @@ impl ChatId {
let chat_size: HashMap<ChatId, f64> = context
.sql
.query_map(
.query_map_collect(
"SELECT chat_id, count(*) AS n
FROM chats_contacts
WHERE contact_id > ? AND chat_id > ?
@@ -963,10 +963,6 @@ impl ChatId {
let size: f64 = row.get(1)?;
Ok((chat_id, size))
},
|rows| {
rows.collect::<std::result::Result<HashMap<ChatId, f64>, _>>()
.map_err(Into::into)
},
)
.await
.context("failed to count chat member sizes")?;

View File

@@ -320,9 +320,9 @@ async fn get_self_reaction(context: &Context, msg_id: MsgId) -> Result<Reaction>
/// Returns a structure containing all reactions to the message.
pub async fn get_msg_reactions(context: &Context, msg_id: MsgId) -> Result<Reactions> {
let reactions = context
let reactions: BTreeMap<ContactId, Reaction> = context
.sql
.query_map(
.query_map_collect(
"SELECT contact_id, reaction FROM reactions WHERE msg_id=?",
(msg_id,),
|row| {
@@ -330,7 +330,6 @@ pub async fn get_msg_reactions(context: &Context, msg_id: MsgId) -> Result<React
let reaction: String = row.get(1)?;
Ok((contact_id, Reaction::from(reaction.as_str())))
},
|rows| Ok(rows.collect::<rusqlite::Result<BTreeMap<_, _>>>()?),
)
.await?;
Ok(Reactions { reactions })

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.

View File

@@ -517,7 +517,7 @@ async fn get_contact_stats(context: &Context, last_old_contact: u32) -> Result<V
async fn get_message_stats(context: &Context) -> Result<BTreeMap<Chattype, MessageStats>> {
let mut map: BTreeMap<Chattype, MessageStats> = context
.sql
.query_map(
.query_map_collect(
"SELECT chattype, verified, unverified_encrypted, unencrypted, only_to_self
FROM stats_msgs",
(),
@@ -535,7 +535,6 @@ async fn get_message_stats(context: &Context) -> Result<BTreeMap<Chattype, Messa
};
Ok((chattype, message_stats))
},
|rows| Ok(rows.collect::<rusqlite::Result<BTreeMap<_, _>>>()?),
)
.await?;
@@ -771,9 +770,9 @@ pub(crate) async fn count_securejoin_ux_info(
}
async fn get_securejoin_source_stats(context: &Context) -> Result<SecurejoinSources> {
let map = context
let map: BTreeMap<SecurejoinSource, u32> = context
.sql
.query_map(
.query_map_collect(
"SELECT source, count FROM stats_securejoin_sources",
(),
|row| {
@@ -781,7 +780,6 @@ async fn get_securejoin_source_stats(context: &Context) -> Result<SecurejoinSour
let count: u32 = row.get(1)?;
Ok((source, count))
},
|rows| Ok(rows.collect::<rusqlite::Result<BTreeMap<_, _>>>()?),
)
.await?;
@@ -798,9 +796,9 @@ async fn get_securejoin_source_stats(context: &Context) -> Result<SecurejoinSour
}
async fn get_securejoin_uipath_stats(context: &Context) -> Result<SecurejoinUiPaths> {
let map = context
let map: BTreeMap<SecurejoinUiPath, u32> = context
.sql
.query_map(
.query_map_collect(
"SELECT uipath, count FROM stats_securejoin_uipaths",
(),
|row| {
@@ -808,7 +806,6 @@ async fn get_securejoin_uipath_stats(context: &Context) -> Result<SecurejoinUiPa
let count: u32 = row.get(1)?;
Ok((uipath, count))
},
|rows| Ok(rows.collect::<rusqlite::Result<BTreeMap<_, _>>>()?),
)
.await?;