refactor(sql): do not expose rusqlite Error type in query_map methods

We use query_and_then() instead of query_map() function now.
The difference is that row processing function
returns anyhow::Result, so simple fallible processing
like JSON parsing can be done inside of it
when calling query_map_vec() and query_map_collect()
without having to resort to query_map()
and iterating over all rows again afterwards.
This commit is contained in:
link2xt
2025-11-03 15:51:17 +00:00
parent 1db6ea70cc
commit 9c2a13b88e
12 changed files with 102 additions and 64 deletions

View File

@@ -2036,7 +2036,10 @@ impl Chat {
ON c.id=cc.contact_id \
WHERE cc.chat_id=? AND cc.add_timestamp >= cc.remove_timestamp",
(self.id,),
|row| row.get::<_, String>(0),
|row| {
let addr: String = row.get(0)?;
Ok(addr)
},
)
.await?;
self.sync(context, SyncAction::SetContacts(addrs)).await?;
@@ -3050,7 +3053,7 @@ pub async fn get_chat_msgs_ex(
))
}
};
let process_rows = |rows: rusqlite::MappedRows<_>| {
let process_rows = |rows: rusqlite::AndThenRows<_>| {
// It is faster to sort here rather than
// let sqlite execute an ORDER BY clause.
let mut sorted_rows = Vec::new();
@@ -3132,7 +3135,10 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
LEFT JOIN chats c ON m.chat_id=c.id
WHERE m.state=10 AND m.hidden=0 AND m.chat_id>9 AND c.archived=1",
(),
|row| row.get::<_, ChatId>(0),
|row| {
let chat_id: ChatId = row.get(0)?;
Ok(chat_id)
},
)
.await?;
if chat_ids_in_archive.is_empty() {
@@ -3310,7 +3316,10 @@ pub async fn get_chat_media(
DC_CHAT_ID_TRASH,
Viewtype::Webxdc,
),
|row| row.get::<_, MsgId>(0),
|row| {
let msg_id: MsgId = row.get(0)?;
Ok(msg_id)
},
)
.await?
} else {
@@ -3340,7 +3349,10 @@ pub async fn get_chat_media(
msg_type
},
),
|row| row.get::<_, MsgId>(0),
|row| {
let msg_id: MsgId = row.get(0)?;
Ok(msg_id)
},
)
.await?
};
@@ -3361,7 +3373,10 @@ pub async fn get_chat_contacts(context: &Context, chat_id: ChatId) -> Result<Vec
WHERE cc.chat_id=? AND cc.add_timestamp >= cc.remove_timestamp
ORDER BY c.id=1, c.last_seen DESC, c.id DESC;",
(chat_id,),
|row| row.get::<_, ContactId>(0),
|row| {
let contact_id: ContactId = row.get(0)?;
Ok(contact_id)
},
)
.await
}
@@ -3383,7 +3398,10 @@ pub async fn get_past_chat_contacts(context: &Context, chat_id: ChatId) -> Resul
AND ? < cc.remove_timestamp
ORDER BY c.id=1, cc.remove_timestamp DESC, c.id DESC",
(chat_id, now.saturating_sub(60 * 24 * 3600)),
|row| row.get::<_, ContactId>(0),
|row| {
let contact_id: ContactId = row.get(0)?;
Ok(contact_id)
},
)
.await
}
@@ -3822,11 +3840,13 @@ pub(crate) async fn shall_attach_selfavatar(context: &Context, chat_id: ChatId)
LEFT JOIN contacts c ON c.id=cc.contact_id
WHERE cc.chat_id=? AND cc.contact_id!=? AND cc.add_timestamp >= cc.remove_timestamp",
(chat_id, ContactId::SELF),
|row| Ok(row.get::<_, i64>(0)),
|row| {
let selfavatar_sent: i64 = row.get(0)?;
Ok(selfavatar_sent)
},
|rows| {
let mut needs_attach = false;
for row in rows {
let row = row?;
let selfavatar_sent = row?;
if selfavatar_sent < timestamp_some_days_ago {
needs_attach = true;