From 25bb595da5048352309cc645a171e6440b806a85 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sun, 22 Dec 2024 18:50:40 +0000 Subject: [PATCH] fix: reduce number of repeat_vars() calls --- src/chat.rs | 17 ++++++++++------- src/ephemeral.rs | 32 +++++++++++++++----------------- src/imap.rs | 19 +++++++++++-------- src/smtp.rs | 23 +++++++++++------------ 4 files changed, 47 insertions(+), 44 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 437dbff4ef..0a299fdc5f 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -3267,14 +3267,17 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()> context .sql - .execute( - &format!( - "UPDATE msgs SET state=13 WHERE state=10 AND hidden=0 AND chat_id IN ({});", - sql::repeat_vars(chat_ids_in_archive.len()) - ), - rusqlite::params_from_iter(&chat_ids_in_archive), - ) + .transaction(|transaction| { + for chat_id_in_archive in &chat_ids_in_archive { + transaction.execute( + "UPDATE msgs SET state=13 WHERE state=10 AND hidden=0 AND chat_id = ?", + (chat_id_in_archive,), + )?; + } + Ok(()) + }) .await?; + for chat_id_in_archive in chat_ids_in_archive { context.emit_event(EventType::MsgsNoticed(chat_id_in_archive)); chatlist_events::emit_chatlist_item_changed(context, chat_id_in_archive); diff --git a/src/ephemeral.rs b/src/ephemeral.rs index f8ff056dab..9cd00fd8a9 100644 --- a/src/ephemeral.rs +++ b/src/ephemeral.rs @@ -84,7 +84,6 @@ use crate::location; use crate::log::LogExt; use crate::message::{Message, MessageState, MsgId, Viewtype}; use crate::mimeparser::SystemMessage; -use crate::sql::{self, params_iter}; use crate::stock_str; use crate::tools::{duration_to_str, time, SystemTime}; @@ -329,23 +328,22 @@ pub(crate) async fn start_ephemeral_timers_msgids( msg_ids: &[MsgId], ) -> Result<()> { let now = time(); - let count = context + let should_interrupt = + context .sql - .execute( - &format!( - "UPDATE msgs SET ephemeral_timestamp = ? + ephemeral_timer - WHERE (ephemeral_timestamp == 0 OR ephemeral_timestamp > ? + ephemeral_timer) AND ephemeral_timer > 0 - AND id IN ({})", - sql::repeat_vars(msg_ids.len()) - ), - rusqlite::params_from_iter( - std::iter::once(&now as &dyn crate::sql::ToSql) - .chain(std::iter::once(&now as &dyn crate::sql::ToSql)) - .chain(params_iter(msg_ids)), - ), - ) - .await?; - if count > 0 { + .transaction(move |transaction| { + let mut should_interrupt = false; + for msg_id in msg_ids { + should_interrupt |= transaction.execute( + "UPDATE msgs SET ephemeral_timestamp = ?1 + ephemeral_timer + WHERE (ephemeral_timestamp == 0 OR ephemeral_timestamp > ?1 + ephemeral_timer) AND ephemeral_timer > 0 + AND id=?2", + (now, msg_id) + )? > 0; + } + Ok(should_interrupt) + }).await?; + if should_interrupt { context.scheduler.interrupt_ephemeral_task().await; } Ok(()) diff --git a/src/imap.rs b/src/imap.rs index 50a159b387..b78e24986f 100644 --- a/src/imap.rs +++ b/src/imap.rs @@ -906,20 +906,23 @@ impl Session { uid_set: &str, row_ids: Vec, ) -> Result<()> { + if row_ids.is_empty() { + return Ok(()); + } + // mark the message for deletion self.add_flag_finalized_with_set(uid_set, "\\Deleted") .await?; context .sql - .execute( - &format!( - "DELETE FROM imap WHERE id IN ({})", - sql::repeat_vars(row_ids.len()) - ), - rusqlite::params_from_iter(row_ids), - ) + .transaction(|transaction| { + for row_id in row_ids { + transaction.execute("DELETE FROM imap WHERE id = ?", (row_id,))?; + } + Ok(()) + }) .await - .context("cannot remove deleted messages from imap table")?; + .context("Cannot remove deleted messages from imap table")?; context.emit_event(EventType::ImapMessageDeleted(format!( "IMAP messages {uid_set} marked as deleted" diff --git a/src/smtp.rs b/src/smtp.rs index 7bc51a2780..f32e265604 100644 --- a/src/smtp.rs +++ b/src/smtp.rs @@ -21,7 +21,6 @@ use crate::mimefactory::MimeFactory; use crate::net::proxy::ProxyConfig; use crate::net::session::SessionBufStream; use crate::scheduler::connectivity::ConnectivityStore; -use crate::sql; use crate::stock_str::unencrypted_email; use crate::tools::{self, time_elapsed}; @@ -585,18 +584,18 @@ async fn send_mdn_rfc724_mid( info!(context, "Successfully sent MDN for {rfc724_mid}."); context .sql - .execute("DELETE FROM smtp_mdns WHERE rfc724_mid = ?", (rfc724_mid,)) + .transaction(|transaction| { + transaction + .execute("DELETE FROM smtp_mdns WHERE rfc724_mid = ?", (rfc724_mid,))?; + for additional_rfc724_mid in additional_rfc724_mids { + transaction.execute( + "DELETE FROM smtp_mdns WHERE rfc724_mid = ?", + (additional_rfc724_mid,), + )?; + } + Ok(()) + }) .await?; - if !additional_rfc724_mids.is_empty() { - let q = format!( - "DELETE FROM smtp_mdns WHERE rfc724_mid IN({})", - sql::repeat_vars(additional_rfc724_mids.len()) - ); - context - .sql - .execute(&q, rusqlite::params_from_iter(additional_rfc724_mids)) - .await?; - } Ok(true) } SendResult::Retry => {