mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +03:00
refactor(sql): add query_map_collect()
This commit is contained in:
@@ -951,7 +951,7 @@ impl ChatId {
|
|||||||
|
|
||||||
let chat_size: HashMap<ChatId, f64> = context
|
let chat_size: HashMap<ChatId, f64> = context
|
||||||
.sql
|
.sql
|
||||||
.query_map(
|
.query_map_collect(
|
||||||
"SELECT chat_id, count(*) AS n
|
"SELECT chat_id, count(*) AS n
|
||||||
FROM chats_contacts
|
FROM chats_contacts
|
||||||
WHERE contact_id > ? AND chat_id > ?
|
WHERE contact_id > ? AND chat_id > ?
|
||||||
@@ -963,10 +963,6 @@ impl ChatId {
|
|||||||
let size: f64 = row.get(1)?;
|
let size: f64 = row.get(1)?;
|
||||||
Ok((chat_id, size))
|
Ok((chat_id, size))
|
||||||
},
|
},
|
||||||
|rows| {
|
|
||||||
rows.collect::<std::result::Result<HashMap<ChatId, f64>, _>>()
|
|
||||||
.map_err(Into::into)
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.context("failed to count chat member sizes")?;
|
.context("failed to count chat member sizes")?;
|
||||||
|
|||||||
@@ -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.
|
/// Returns a structure containing all reactions to the message.
|
||||||
pub async fn get_msg_reactions(context: &Context, msg_id: MsgId) -> Result<Reactions> {
|
pub async fn get_msg_reactions(context: &Context, msg_id: MsgId) -> Result<Reactions> {
|
||||||
let reactions = context
|
let reactions: BTreeMap<ContactId, Reaction> = context
|
||||||
.sql
|
.sql
|
||||||
.query_map(
|
.query_map_collect(
|
||||||
"SELECT contact_id, reaction FROM reactions WHERE msg_id=?",
|
"SELECT contact_id, reaction FROM reactions WHERE msg_id=?",
|
||||||
(msg_id,),
|
(msg_id,),
|
||||||
|row| {
|
|row| {
|
||||||
@@ -330,7 +330,6 @@ pub async fn get_msg_reactions(context: &Context, msg_id: MsgId) -> Result<React
|
|||||||
let reaction: String = row.get(1)?;
|
let reaction: String = row.get(1)?;
|
||||||
Ok((contact_id, Reaction::from(reaction.as_str())))
|
Ok((contact_id, Reaction::from(reaction.as_str())))
|
||||||
},
|
},
|
||||||
|rows| Ok(rows.collect::<rusqlite::Result<BTreeMap<_, _>>>()?),
|
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(Reactions { reactions })
|
Ok(Reactions { reactions })
|
||||||
|
|||||||
27
src/sql.rs
27
src/sql.rs
@@ -386,6 +386,27 @@ impl Sql {
|
|||||||
.await
|
.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.
|
/// Prepares and executes the statement and maps a function over the resulting rows.
|
||||||
///
|
///
|
||||||
/// Collects the resulting rows into a `Vec`.
|
/// Collects the resulting rows into a `Vec`.
|
||||||
@@ -399,11 +420,7 @@ impl Sql {
|
|||||||
T: Send + 'static,
|
T: Send + 'static,
|
||||||
F: Send + FnMut(&rusqlite::Row) -> rusqlite::Result<T>,
|
F: Send + FnMut(&rusqlite::Row) -> rusqlite::Result<T>,
|
||||||
{
|
{
|
||||||
self.query_map(sql, params, f, |rows| {
|
self.query_map_collect(sql, params, f).await
|
||||||
rows.collect::<std::result::Result<Vec<_>, _>>()
|
|
||||||
.map_err(Into::into)
|
|
||||||
})
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Used for executing `SELECT COUNT` statements only. Returns the resulting count.
|
/// Used for executing `SELECT COUNT` statements only. Returns the resulting count.
|
||||||
|
|||||||
13
src/stats.rs
13
src/stats.rs
@@ -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>> {
|
async fn get_message_stats(context: &Context) -> Result<BTreeMap<Chattype, MessageStats>> {
|
||||||
let mut map: BTreeMap<Chattype, MessageStats> = context
|
let mut map: BTreeMap<Chattype, MessageStats> = context
|
||||||
.sql
|
.sql
|
||||||
.query_map(
|
.query_map_collect(
|
||||||
"SELECT chattype, verified, unverified_encrypted, unencrypted, only_to_self
|
"SELECT chattype, verified, unverified_encrypted, unencrypted, only_to_self
|
||||||
FROM stats_msgs",
|
FROM stats_msgs",
|
||||||
(),
|
(),
|
||||||
@@ -535,7 +535,6 @@ async fn get_message_stats(context: &Context) -> Result<BTreeMap<Chattype, Messa
|
|||||||
};
|
};
|
||||||
Ok((chattype, message_stats))
|
Ok((chattype, message_stats))
|
||||||
},
|
},
|
||||||
|rows| Ok(rows.collect::<rusqlite::Result<BTreeMap<_, _>>>()?),
|
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -771,9 +770,9 @@ pub(crate) async fn count_securejoin_ux_info(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn get_securejoin_source_stats(context: &Context) -> Result<SecurejoinSources> {
|
async fn get_securejoin_source_stats(context: &Context) -> Result<SecurejoinSources> {
|
||||||
let map = context
|
let map: BTreeMap<SecurejoinSource, u32> = context
|
||||||
.sql
|
.sql
|
||||||
.query_map(
|
.query_map_collect(
|
||||||
"SELECT source, count FROM stats_securejoin_sources",
|
"SELECT source, count FROM stats_securejoin_sources",
|
||||||
(),
|
(),
|
||||||
|row| {
|
|row| {
|
||||||
@@ -781,7 +780,6 @@ async fn get_securejoin_source_stats(context: &Context) -> Result<SecurejoinSour
|
|||||||
let count: u32 = row.get(1)?;
|
let count: u32 = row.get(1)?;
|
||||||
Ok((source, count))
|
Ok((source, count))
|
||||||
},
|
},
|
||||||
|rows| Ok(rows.collect::<rusqlite::Result<BTreeMap<_, _>>>()?),
|
|
||||||
)
|
)
|
||||||
.await?;
|
.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> {
|
async fn get_securejoin_uipath_stats(context: &Context) -> Result<SecurejoinUiPaths> {
|
||||||
let map = context
|
let map: BTreeMap<SecurejoinUiPath, u32> = context
|
||||||
.sql
|
.sql
|
||||||
.query_map(
|
.query_map_collect(
|
||||||
"SELECT uipath, count FROM stats_securejoin_uipaths",
|
"SELECT uipath, count FROM stats_securejoin_uipaths",
|
||||||
(),
|
(),
|
||||||
|row| {
|
|row| {
|
||||||
@@ -808,7 +806,6 @@ async fn get_securejoin_uipath_stats(context: &Context) -> Result<SecurejoinUiPa
|
|||||||
let count: u32 = row.get(1)?;
|
let count: u32 = row.get(1)?;
|
||||||
Ok((uipath, count))
|
Ok((uipath, count))
|
||||||
},
|
},
|
||||||
|rows| Ok(rows.collect::<rusqlite::Result<BTreeMap<_, _>>>()?),
|
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user