Skip to content

Commit 47857ea

Browse files
committed
prepare SQL statements
1 parent 5e800b1 commit 47857ea

File tree

2 files changed

+54
-48
lines changed

2 files changed

+54
-48
lines changed

src/chat.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3605,17 +3605,20 @@ pub(crate) async fn update_chat_contacts_table(
36053605
(timestamp, id),
36063606
)?;
36073607

3608-
for contact_id in contacts {
3609-
// We bumped `add_timestamp` for existing rows above,
3610-
// so on conflict it is enough to set `add_timestamp = remove_timestamp`
3611-
// and this guarantees that `add_timestamp` is no less than `timestamp`.
3612-
transaction.execute(
3608+
if !contacts.is_empty() {
3609+
let mut statement = transaction.prepare(
36133610
"INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
36143611
VALUES (?1, ?2, ?3)
36153612
ON CONFLICT (chat_id, contact_id)
36163613
DO UPDATE SET add_timestamp=remove_timestamp",
3617-
(id, contact_id, timestamp),
36183614
)?;
3615+
3616+
for contact_id in contacts {
3617+
// We bumped `add_timestamp` for existing rows above,
3618+
// so on conflict it is enough to set `add_timestamp = remove_timestamp`
3619+
// and this guarantees that `add_timestamp` is no less than `timestamp`.
3620+
statement.execute((id, contact_id, timestamp))?;
3621+
}
36193622
}
36203623
Ok(())
36213624
})
@@ -3633,13 +3636,14 @@ pub(crate) async fn add_to_chat_contacts_table(
36333636
context
36343637
.sql
36353638
.transaction(move |transaction| {
3639+
let mut add_statement = transaction.prepare(
3640+
"INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp) VALUES(?1, ?2, ?3)
3641+
ON CONFLICT (chat_id, contact_id)
3642+
DO UPDATE SET add_timestamp=MAX(remove_timestamp, ?3)",
3643+
)?;
3644+
36363645
for contact_id in contact_ids {
3637-
transaction.execute(
3638-
"INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp) VALUES(?1, ?2, ?3)
3639-
ON CONFLICT (chat_id, contact_id)
3640-
DO UPDATE SET add_timestamp=MAX(remove_timestamp, ?3)",
3641-
(chat_id, contact_id, timestamp),
3642-
)?;
3646+
add_statement.execute((chat_id, contact_id, timestamp))?;
36433647
}
36443648
Ok(())
36453649
})

src/receive_imf.rs

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,39 +2065,39 @@ async fn create_group(
20652065
context
20662066
.sql
20672067
.transaction(|transaction| {
2068+
let mut add_statement = transaction.prepare(
2069+
"INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
2070+
VALUES (?1, ?2, ?3)
2071+
ON CONFLICT (chat_id, contact_id)
2072+
DO UPDATE SET add_timestamp=MAX(add_timestamp, ?3)",
2073+
)?;
2074+
20682075
for (contact_id, ts) in iter::zip(
20692076
to_ids.iter(),
20702077
chat_group_member_timestamps.iter().take(to_ids.len()),
20712078
) {
2072-
transaction.execute(
2073-
"INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
2074-
VALUES (?1, ?2, ?3)
2075-
ON CONFLICT (chat_id, contact_id)
2076-
DO UPDATE SET add_timestamp=MAX(add_timestamp, ?3)",
2077-
(chat_id, contact_id, ts),
2078-
)?;
2079+
add_statement.execute((chat_id, contact_id, ts))?;
20792080
}
20802081

2082+
let mut remove_statement = transaction.prepare(
2083+
"UPDATE chats_contacts
2084+
SET remove_timestamp=MAX(remove_timestamp, ?)
2085+
WHERE chat_id=? AND contact_id=?",
2086+
)?;
2087+
20812088
for (contact_id, ts) in iter::zip(
20822089
past_ids.iter(),
20832090
chat_group_member_timestamps.iter().skip(to_ids.len()),
20842091
) {
2085-
transaction.execute(
2086-
"UPDATE chats_contacts
2087-
SET remove_timestamp=MAX(remove_timestamp, ?)
2088-
WHERE chat_id=? AND contact_id=?",
2089-
(ts, chat_id, contact_id),
2090-
)?;
2092+
remove_statement.execute((ts, chat_id, contact_id))?;
20912093
}
20922094

20932095
if !to_ids.contains(&from_id) {
2094-
transaction.execute(
2095-
"INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
2096-
VALUES (?1, ?2, ?3)
2097-
ON CONFLICT (chat_id, contact_id)
2098-
DO UPDATE SET add_timestamp=MAX(add_timestamp, ?3)",
2099-
(chat_id, from_id, mime_parser.timestamp_sent),
2100-
)?;
2096+
add_statement.execute((
2097+
chat_id,
2098+
from_id,
2099+
mime_parser.timestamp_sent,
2100+
))?;
21012101
}
21022102

21032103
Ok(())
@@ -2291,20 +2291,21 @@ async fn apply_group_changes(
22912291
context
22922292
.sql
22932293
.transaction(|transaction| {
2294+
let mut add_statement = transaction.prepare(
2295+
"INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
2296+
VALUES (?1, ?2, ?3)
2297+
ON CONFLICT (chat_id, contact_id)
2298+
DO
2299+
UPDATE SET add_timestamp=?3
2300+
WHERE ?3>add_timestamp AND ?3>=remove_timestamp",
2301+
)?;
2302+
22942303
for (contact_id, ts) in iter::zip(
22952304
to_ids.iter(),
22962305
chat_group_member_timestamps.iter().take(to_ids.len()),
22972306
) {
22982307
if *contact_id != from_id {
2299-
let modified = transaction.execute(
2300-
"INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
2301-
VALUES (?1, ?2, ?3)
2302-
ON CONFLICT (chat_id, contact_id)
2303-
DO
2304-
UPDATE SET add_timestamp=?3
2305-
WHERE ?3>add_timestamp AND ?3>=remove_timestamp",
2306-
(chat_id, contact_id, ts),
2307-
)? > 0;
2308+
let modified = add_statement.execute((chat_id, contact_id, ts))? > 0;
23082309

23092310
if modified {
23102311
// It could be that member was already added,
@@ -2315,17 +2316,18 @@ async fn apply_group_changes(
23152316
}
23162317
}
23172318

2319+
let mut remove_statement = transaction.prepare(
2320+
"UPDATE chats_contacts
2321+
SET remove_timestamp=?1
2322+
WHERE chat_id=?2 AND contact_id=?3
2323+
AND ?1>remove_timestamp AND ?1>add_timestamp",
2324+
)?;
2325+
23182326
for (contact_id, ts) in iter::zip(
23192327
past_ids.iter(),
23202328
chat_group_member_timestamps.iter().skip(to_ids.len()),
23212329
) {
2322-
let modified = transaction.execute(
2323-
"UPDATE chats_contacts
2324-
SET remove_timestamp=?1
2325-
WHERE chat_id=?2 AND contact_id=?3
2326-
AND ?1>remove_timestamp AND ?1>add_timestamp",
2327-
(ts, chat_id, contact_id),
2328-
)? > 0;
2330+
let modified = remove_statement.execute((ts, chat_id, contact_id))? > 0;
23292331

23302332
if modified {
23312333
// It could be that member was already removed,

0 commit comments

Comments
 (0)