Skip to content

Commit c001a9a

Browse files
committed
refactor(sql): add query_map_collect()
1 parent 5f3948b commit c001a9a

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

src/chat.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ impl ChatId {
951951

952952
let chat_size: HashMap<ChatId, f64> = context
953953
.sql
954-
.query_map(
954+
.query_map_collect(
955955
"SELECT chat_id, count(*) AS n
956956
FROM chats_contacts
957957
WHERE contact_id > ? AND chat_id > ?
@@ -963,10 +963,6 @@ impl ChatId {
963963
let size: f64 = row.get(1)?;
964964
Ok((chat_id, size))
965965
},
966-
|rows| {
967-
rows.collect::<std::result::Result<HashMap<ChatId, f64>, _>>()
968-
.map_err(Into::into)
969-
},
970966
)
971967
.await
972968
.context("failed to count chat member sizes")?;

src/reaction.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,17 +320,16 @@ async fn get_self_reaction(context: &Context, msg_id: MsgId) -> Result<Reaction>
320320

321321
/// Returns a structure containing all reactions to the message.
322322
pub async fn get_msg_reactions(context: &Context, msg_id: MsgId) -> Result<Reactions> {
323-
let reactions = context
323+
let reactions: BTreeMap<ContactId, Reaction> = context
324324
.sql
325-
.query_map(
325+
.query_map_collect(
326326
"SELECT contact_id, reaction FROM reactions WHERE msg_id=?",
327327
(msg_id,),
328328
|row| {
329329
let contact_id: ContactId = row.get(0)?;
330330
let reaction: String = row.get(1)?;
331331
Ok((contact_id, Reaction::from(reaction.as_str())))
332332
},
333-
|rows| Ok(rows.collect::<rusqlite::Result<BTreeMap<_, _>>>()?),
334333
)
335334
.await?;
336335
Ok(Reactions { reactions })

src/sql.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,24 +388,41 @@ impl Sql {
388388

389389
/// Prepares and executes the statement and maps a function over the resulting rows.
390390
///
391-
/// Collects the resulting rows into a `Vec`.
392-
pub async fn query_map_vec<T, F>(
391+
/// Collects the resulting rows into a generic structure.
392+
pub async fn query_map_collect<T, C, F>(
393393
&self,
394394
sql: &str,
395395
params: impl rusqlite::Params + Send,
396396
f: F,
397-
) -> Result<Vec<T>>
397+
) -> Result<C>
398398
where
399399
T: Send + 'static,
400+
C: Send + 'static + std::iter::FromIterator<T>,
400401
F: Send + FnMut(&rusqlite::Row) -> rusqlite::Result<T>,
401402
{
402403
self.query_map(sql, params, f, |rows| {
403-
rows.collect::<std::result::Result<Vec<_>, _>>()
404+
rows.collect::<std::result::Result<C, _>>()
404405
.map_err(Into::into)
405406
})
406407
.await
407408
}
408409

410+
/// Prepares and executes the statement and maps a function over the resulting rows.
411+
///
412+
/// Collects the resulting rows into a `Vec`.
413+
pub async fn query_map_vec<T, F>(
414+
&self,
415+
sql: &str,
416+
params: impl rusqlite::Params + Send,
417+
f: F,
418+
) -> Result<Vec<T>>
419+
where
420+
T: Send + 'static,
421+
F: Send + FnMut(&rusqlite::Row) -> rusqlite::Result<T>,
422+
{
423+
self.query_map_collect(sql, params, f).await
424+
}
425+
409426
/// Used for executing `SELECT COUNT` statements only. Returns the resulting count.
410427
pub async fn count(&self, query: &str, params: impl rusqlite::Params + Send) -> Result<usize> {
411428
let count: isize = self.query_row(query, params, |row| row.get(0)).await?;

src/stats.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ async fn get_contact_stats(context: &Context, last_old_contact: u32) -> Result<V
517517
async fn get_message_stats(context: &Context) -> Result<BTreeMap<Chattype, MessageStats>> {
518518
let mut map: BTreeMap<Chattype, MessageStats> = context
519519
.sql
520-
.query_map(
520+
.query_map_collect(
521521
"SELECT chattype, verified, unverified_encrypted, unencrypted, only_to_self
522522
FROM stats_msgs",
523523
(),
@@ -535,7 +535,6 @@ async fn get_message_stats(context: &Context) -> Result<BTreeMap<Chattype, Messa
535535
};
536536
Ok((chattype, message_stats))
537537
},
538-
|rows| Ok(rows.collect::<rusqlite::Result<BTreeMap<_, _>>>()?),
539538
)
540539
.await?;
541540

@@ -771,17 +770,16 @@ pub(crate) async fn count_securejoin_ux_info(
771770
}
772771

773772
async fn get_securejoin_source_stats(context: &Context) -> Result<SecurejoinSources> {
774-
let map = context
773+
let map: BTreeMap<SecurejoinSource, u32> = context
775774
.sql
776-
.query_map(
775+
.query_map_collect(
777776
"SELECT source, count FROM stats_securejoin_sources",
778777
(),
779778
|row| {
780779
let source: SecurejoinSource = row.get(0)?;
781780
let count: u32 = row.get(1)?;
782781
Ok((source, count))
783782
},
784-
|rows| Ok(rows.collect::<rusqlite::Result<BTreeMap<_, _>>>()?),
785783
)
786784
.await?;
787785

@@ -798,17 +796,16 @@ async fn get_securejoin_source_stats(context: &Context) -> Result<SecurejoinSour
798796
}
799797

800798
async fn get_securejoin_uipath_stats(context: &Context) -> Result<SecurejoinUiPaths> {
801-
let map = context
799+
let map: BTreeMap<SecurejoinUiPath, u32> = context
802800
.sql
803-
.query_map(
801+
.query_map_collect(
804802
"SELECT uipath, count FROM stats_securejoin_uipaths",
805803
(),
806804
|row| {
807805
let uipath: SecurejoinUiPath = row.get(0)?;
808806
let count: u32 = row.get(1)?;
809807
Ok((uipath, count))
810808
},
811-
|rows| Ok(rows.collect::<rusqlite::Result<BTreeMap<_, _>>>()?),
812809
)
813810
.await?;
814811

0 commit comments

Comments
 (0)