diff --git a/src/chat.rs b/src/chat.rs index 861c73ca7..0526eed3d 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -951,7 +951,7 @@ impl ChatId { let chat_size: HashMap = 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::, _>>() - .map_err(Into::into) - }, ) .await .context("failed to count chat member sizes")?; diff --git a/src/reaction.rs b/src/reaction.rs index 089735a75..e691b2dde 100644 --- a/src/reaction.rs +++ b/src/reaction.rs @@ -320,9 +320,9 @@ async fn get_self_reaction(context: &Context, msg_id: MsgId) -> Result /// Returns a structure containing all reactions to the message. pub async fn get_msg_reactions(context: &Context, msg_id: MsgId) -> Result { - let reactions = context + let reactions: BTreeMap = 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>>()?), ) .await?; Ok(Reactions { reactions }) diff --git a/src/sql.rs b/src/sql.rs index 80b155a7c..69fefc7d3 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -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( + &self, + sql: &str, + params: impl rusqlite::Params + Send, + f: F, + ) -> Result + where + T: Send + 'static, + C: Send + 'static + std::iter::FromIterator, + F: Send + FnMut(&rusqlite::Row) -> rusqlite::Result, + { + self.query_map(sql, params, f, |rows| { + rows.collect::>() + .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, { - self.query_map(sql, params, f, |rows| { - rows.collect::, _>>() - .map_err(Into::into) - }) - .await + self.query_map_collect(sql, params, f).await } /// Used for executing `SELECT COUNT` statements only. Returns the resulting count. diff --git a/src/stats.rs b/src/stats.rs index e295c7add..00f272dbb 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -517,7 +517,7 @@ async fn get_contact_stats(context: &Context, last_old_contact: u32) -> Result Result> { let mut map: BTreeMap = 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>>()?), ) .await?; @@ -771,9 +770,9 @@ pub(crate) async fn count_securejoin_ux_info( } async fn get_securejoin_source_stats(context: &Context) -> Result { - let map = context + let map: BTreeMap = 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>>()?), ) .await?; @@ -798,9 +796,9 @@ async fn get_securejoin_source_stats(context: &Context) -> Result Result { - let map = context + let map: BTreeMap = 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>>()?), ) .await?;