Skip to content

Commit b44be09

Browse files
committed
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.
1 parent 1db6ea7 commit b44be09

File tree

11 files changed

+98
-63
lines changed

11 files changed

+98
-63
lines changed

src/chat.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,7 +2036,10 @@ impl Chat {
20362036
ON c.id=cc.contact_id \
20372037
WHERE cc.chat_id=? AND cc.add_timestamp >= cc.remove_timestamp",
20382038
(self.id,),
2039-
|row| row.get::<_, String>(0),
2039+
|row| {
2040+
let addr: String = row.get(0)?;
2041+
Ok(addr)
2042+
},
20402043
)
20412044
.await?;
20422045
self.sync(context, SyncAction::SetContacts(addrs)).await?;
@@ -3050,7 +3053,7 @@ pub async fn get_chat_msgs_ex(
30503053
))
30513054
}
30523055
};
3053-
let process_rows = |rows: rusqlite::MappedRows<_>| {
3056+
let process_rows = |rows: rusqlite::AndThenRows<_>| {
30543057
// It is faster to sort here rather than
30553058
// let sqlite execute an ORDER BY clause.
30563059
let mut sorted_rows = Vec::new();
@@ -3132,7 +3135,10 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
31323135
LEFT JOIN chats c ON m.chat_id=c.id
31333136
WHERE m.state=10 AND m.hidden=0 AND m.chat_id>9 AND c.archived=1",
31343137
(),
3135-
|row| row.get::<_, ChatId>(0),
3138+
|row| {
3139+
let chat_id: ChatId = row.get(0)?;
3140+
Ok(chat_id)
3141+
},
31363142
)
31373143
.await?;
31383144
if chat_ids_in_archive.is_empty() {
@@ -3310,7 +3316,10 @@ pub async fn get_chat_media(
33103316
DC_CHAT_ID_TRASH,
33113317
Viewtype::Webxdc,
33123318
),
3313-
|row| row.get::<_, MsgId>(0),
3319+
|row| {
3320+
let msg_id: MsgId = row.get(0)?;
3321+
Ok(msg_id)
3322+
},
33143323
)
33153324
.await?
33163325
} else {
@@ -3340,7 +3349,10 @@ pub async fn get_chat_media(
33403349
msg_type
33413350
},
33423351
),
3343-
|row| row.get::<_, MsgId>(0),
3352+
|row| {
3353+
let msg_id: MsgId = row.get(0)?;
3354+
Ok(msg_id)
3355+
},
33443356
)
33453357
.await?
33463358
};
@@ -3361,7 +3373,10 @@ pub async fn get_chat_contacts(context: &Context, chat_id: ChatId) -> Result<Vec
33613373
WHERE cc.chat_id=? AND cc.add_timestamp >= cc.remove_timestamp
33623374
ORDER BY c.id=1, c.last_seen DESC, c.id DESC;",
33633375
(chat_id,),
3364-
|row| row.get::<_, ContactId>(0),
3376+
|row| {
3377+
let contact_id: ContactId = row.get(0)?;
3378+
Ok(contact_id)
3379+
},
33653380
)
33663381
.await
33673382
}
@@ -3383,7 +3398,10 @@ pub async fn get_past_chat_contacts(context: &Context, chat_id: ChatId) -> Resul
33833398
AND ? < cc.remove_timestamp
33843399
ORDER BY c.id=1, cc.remove_timestamp DESC, c.id DESC",
33853400
(chat_id, now.saturating_sub(60 * 24 * 3600)),
3386-
|row| row.get::<_, ContactId>(0),
3401+
|row| {
3402+
let contact_id: ContactId = row.get(0)?;
3403+
Ok(contact_id)
3404+
},
33873405
)
33883406
.await
33893407
}
@@ -3822,11 +3840,13 @@ pub(crate) async fn shall_attach_selfavatar(context: &Context, chat_id: ChatId)
38223840
LEFT JOIN contacts c ON c.id=cc.contact_id
38233841
WHERE cc.chat_id=? AND cc.contact_id!=? AND cc.add_timestamp >= cc.remove_timestamp",
38243842
(chat_id, ContactId::SELF),
3825-
|row| Ok(row.get::<_, i64>(0)),
3843+
|row| {
3844+
let selfavatar_sent: i64 = row.get(0)?;
3845+
Ok(selfavatar_sent)
3846+
},
38263847
|rows| {
38273848
let mut needs_attach = false;
38283849
for row in rows {
3829-
let row = row?;
38303850
let selfavatar_sent = row?;
38313851
if selfavatar_sent < timestamp_some_days_ago {
38323852
needs_attach = true;

src/chatlist.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,6 @@ impl Chatlist {
107107
Ok((chat_id, msg_id))
108108
};
109109

110-
let process_rows = |rows: rusqlite::MappedRows<_>| {
111-
rows.collect::<std::result::Result<Vec<_>, _>>()
112-
.map_err(Into::into)
113-
};
114-
115110
let skip_id = if flag_for_forwarding {
116111
ChatId::lookup_by_contact(context, ContactId::DEVICE)
117112
.await?
@@ -132,7 +127,7 @@ impl Chatlist {
132127
// groups. Otherwise it would be hard to follow conversations.
133128
let ids = if let Some(query_contact_id) = query_contact_id {
134129
// show chats shared with a given contact
135-
context.sql.query_map(
130+
context.sql.query_map_vec(
136131
"SELECT c.id, m.id
137132
FROM chats c
138133
LEFT JOIN msgs m
@@ -150,7 +145,6 @@ impl Chatlist {
150145
ORDER BY c.archived=?3 DESC, IFNULL(m.timestamp,c.created_timestamp) DESC, m.id DESC;",
151146
(MessageState::OutDraft, query_contact_id, ChatVisibility::Pinned),
152147
process_row,
153-
process_rows,
154148
).await?
155149
} else if flag_archived_only {
156150
// show archived chats
@@ -159,7 +153,7 @@ impl Chatlist {
159153
// and adapting the number requires larger refactorings and seems not to be worth the effort)
160154
context
161155
.sql
162-
.query_map(
156+
.query_map_vec(
163157
"SELECT c.id, m.id
164158
FROM chats c
165159
LEFT JOIN msgs m
@@ -177,7 +171,6 @@ impl Chatlist {
177171
ORDER BY IFNULL(m.timestamp,c.created_timestamp) DESC, m.id DESC;",
178172
(MessageState::OutDraft,),
179173
process_row,
180-
process_rows,
181174
)
182175
.await?
183176
} else if let Some(query) = query {
@@ -195,7 +188,7 @@ impl Chatlist {
195188
let str_like_cmd = format!("%{query}%");
196189
context
197190
.sql
198-
.query_map(
191+
.query_map_vec(
199192
"SELECT c.id, m.id
200193
FROM chats c
201194
LEFT JOIN msgs m
@@ -214,7 +207,6 @@ impl Chatlist {
214207
ORDER BY IFNULL(m.timestamp,c.created_timestamp) DESC, m.id DESC;",
215208
(MessageState::OutDraft, skip_id, str_like_cmd, only_unread, MessageState::InFresh),
216209
process_row,
217-
process_rows,
218210
)
219211
.await?
220212
} else {
@@ -229,7 +221,7 @@ impl Chatlist {
229221
let msg_id: Option<MsgId> = row.get(3)?;
230222
Ok((chat_id, typ, param, msg_id))
231223
};
232-
let process_rows = |rows: rusqlite::MappedRows<_>| {
224+
let process_rows = |rows: rusqlite::AndThenRows<_>| {
233225
rows.filter_map(|row: std::result::Result<(_, _, Params, _), _>| match row {
234226
Ok((chat_id, typ, param, msg_id)) => {
235227
if typ == Chattype::Mailinglist
@@ -243,7 +235,6 @@ impl Chatlist {
243235
Err(e) => Some(Err(e)),
244236
})
245237
.collect::<std::result::Result<Vec<_>, _>>()
246-
.map_err(Into::into)
247238
};
248239
context.sql.query_map(
249240
"SELECT c.id, c.type, c.param, m.id
@@ -272,7 +263,7 @@ impl Chatlist {
272263
).await?
273264
} else {
274265
// show normal chatlist
275-
context.sql.query_map(
266+
context.sql.query_map_vec(
276267
"SELECT c.id, m.id
277268
FROM chats c
278269
LEFT JOIN msgs m
@@ -290,7 +281,6 @@ impl Chatlist {
290281
ORDER BY c.id=0 DESC, c.archived=? DESC, IFNULL(m.timestamp,c.created_timestamp) DESC, m.id DESC;",
291282
(MessageState::OutDraft, skip_id, ChatVisibility::Archived, ChatVisibility::Pinned),
292283
process_row,
293-
process_rows,
294284
).await?
295285
};
296286
if !flag_no_specials && get_archived_cnt(context).await? > 0 {

src/configure.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,11 @@ impl Context {
194194
pub async fn list_transports(&self) -> Result<Vec<EnteredLoginParam>> {
195195
let transports = self
196196
.sql
197-
.query_map(
198-
"SELECT entered_param FROM transports",
199-
(),
200-
|row| row.get::<_, String>(0),
201-
|rows| {
202-
rows.flatten()
203-
.map(|s| Ok(serde_json::from_str(&s)?))
204-
.collect::<Result<Vec<EnteredLoginParam>>>()
205-
},
206-
)
197+
.query_map_vec("SELECT entered_param FROM transports", (), |row| {
198+
let entered_param: String = row.get(0)?;
199+
let transport: EnteredLoginParam = serde_json::from_str(&entered_param)?;
200+
Ok(transport)
201+
})
207202
.await?;
208203

209204
Ok(transports)

src/contact.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,10 @@ impl Contact {
12851285
.query_map_vec(
12861286
"SELECT id FROM contacts WHERE id>? AND blocked!=0 ORDER BY last_seen DESC, id DESC;",
12871287
(ContactId::LAST_SPECIAL,),
1288-
|row| row.get::<_, ContactId>(0),
1288+
|row| {
1289+
let contact_id: ContactId = row.get(0)?;
1290+
Ok(contact_id)
1291+
}
12891292
)
12901293
.await?;
12911294
Ok(list)
@@ -2044,7 +2047,7 @@ impl RecentlySeenLoop {
20442047
// become unseen in the future.
20452048
let mut unseen_queue: BinaryHeap<MyHeapElem> = context
20462049
.sql
2047-
.query_map(
2050+
.query_map_collect(
20482051
"SELECT id, last_seen FROM contacts
20492052
WHERE last_seen > ?",
20502053
(now_ts - SEEN_RECENTLY_SECONDS,),
@@ -2053,10 +2056,6 @@ impl RecentlySeenLoop {
20532056
let last_seen: i64 = row.get("last_seen")?;
20542057
Ok((Reverse(last_seen + SEEN_RECENTLY_SECONDS), contact_id))
20552058
},
2056-
|rows| {
2057-
rows.collect::<std::result::Result<BinaryHeap<MyHeapElem>, _>>()
2058-
.map_err(Into::into)
2059-
},
20602059
)
20612060
.await
20622061
.unwrap_or_default();

src/context.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,10 @@ impl Context {
11011101
" ORDER BY m.timestamp DESC,m.id DESC;"
11021102
),
11031103
(MessageState::InFresh, time()),
1104-
|row| row.get::<_, MsgId>(0),
1104+
|row| {
1105+
let msg_id: MsgId = row.get(0)?;
1106+
Ok(msg_id)
1107+
},
11051108
)
11061109
.await?;
11071110
Ok(list)
@@ -1205,7 +1208,10 @@ impl Context {
12051208
AND IFNULL(txt_normalized, txt) LIKE ?
12061209
ORDER BY m.timestamp,m.id;",
12071210
(chat_id, str_like_in_text),
1208-
|row| row.get::<_, MsgId>("id"),
1211+
|row| {
1212+
let msg_id: MsgId = row.get("id")?;
1213+
Ok(msg_id)
1214+
},
12091215
)
12101216
.await?
12111217
} else {
@@ -1234,7 +1240,10 @@ impl Context {
12341240
AND IFNULL(txt_normalized, txt) LIKE ?
12351241
ORDER BY m.id DESC LIMIT 1000",
12361242
(str_like_in_text,),
1237-
|row| row.get::<_, MsgId>("id"),
1243+
|row| {
1244+
let msg_id: MsgId = row.get("id")?;
1245+
Ok(msg_id)
1246+
},
12381247
)
12391248
.await?
12401249
};

src/key.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,15 @@ pub(crate) async fn load_self_public_key(context: &Context) -> Result<SignedPubl
159159
pub(crate) async fn load_self_public_keyring(context: &Context) -> Result<Vec<SignedPublicKey>> {
160160
let keys = context
161161
.sql
162-
.query_map(
162+
.query_map_vec(
163163
r#"SELECT public_key
164164
FROM keypairs
165165
ORDER BY id=(SELECT value FROM config WHERE keyname='key_id') DESC"#,
166166
(),
167-
|row| row.get::<_, Vec<u8>>(0),
168-
|keys| keys.collect::<Result<Vec<_>, _>>().map_err(Into::into),
167+
|row| {
168+
let public_key_bytes: Vec<u8> = row.get(0)?;
169+
Ok(public_key_bytes)
170+
},
169171
)
170172
.await?
171173
.into_iter()
@@ -232,13 +234,15 @@ pub(crate) async fn load_self_secret_key(context: &Context) -> Result<SignedSecr
232234
pub(crate) async fn load_self_secret_keyring(context: &Context) -> Result<Vec<SignedSecretKey>> {
233235
let keys = context
234236
.sql
235-
.query_map(
237+
.query_map_vec(
236238
r#"SELECT private_key
237239
FROM keypairs
238240
ORDER BY id=(SELECT value FROM config WHERE keyname='key_id') DESC"#,
239241
(),
240-
|row| row.get::<_, Vec<u8>>(0),
241-
|keys| keys.collect::<Result<Vec<_>, _>>().map_err(Into::into),
242+
|row| {
243+
let bytes: Vec<u8> = row.get(0)?;
244+
Ok(bytes)
245+
},
242246
)
243247
.await?
244248
.into_iter()

src/location.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,10 @@ pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64
348348
.query_map_vec(
349349
"SELECT id FROM chats WHERE locations_send_until>?;",
350350
(now,),
351-
|row| row.get::<_, i32>(0),
351+
|row| {
352+
let id: i32 = row.get(0)?;
353+
Ok(id)
354+
},
352355
)
353356
.await?;
354357

0 commit comments

Comments
 (0)