From 6c41bd6709735e63c600f015728d51649188a553 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto <yukikishimoto@protonmail.com> Date: Wed, 5 Jun 2024 08:21:38 -0400 Subject: [PATCH] sqlite: fix wrong event order when querying Ref https://github.com/rust-nostr/nostr/issues/454#issuecomment-2149030999 Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com> --- CHANGELOG.md | 1 + crates/nostr-sqlite/src/lib.rs | 16 +++++----------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 069cc5fc0..0b3c5f63f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ * nostr: fix re-serialization of events that contains unknown keys during deserialization ([Yuki Kishimoto]) * pool: fix relay doesn't auto reconnect in certain cases ([Yuki Kishimoto]) * sqlite: fix SQLite database panics when used outside the client context in bindings ([Yuki Kishimoto]) +* sqlite: fix wrong event order when querying ([Yuki Kishimoto]) ### Removed diff --git a/crates/nostr-sqlite/src/lib.rs b/crates/nostr-sqlite/src/lib.rs index b55584f40..5c62797a6 100644 --- a/crates/nostr-sqlite/src/lib.rs +++ b/crates/nostr-sqlite/src/lib.rs @@ -316,22 +316,16 @@ impl NostrDatabase for SQLiteDatabase { Ok(self.indexes.count(filters).await) } - #[tracing::instrument(skip_all, level = "trace")] + #[tracing::instrument(skip_all)] async fn query(&self, filters: Vec<Filter>, order: Order) -> Result<Vec<Event>, Self::Err> { let ids: Vec<EventId> = self.indexes.query(filters, order).await; self.pool .interact(move |conn| { let mut events = Vec::with_capacity(ids.len()); - for chunk in ids.chunks(BATCH_SIZE) { - let mut stmt = conn.prepare_cached(&format!( - "SELECT event FROM events WHERE {};", - chunk - .iter() - .map(|id| format!("event_id = '{id}'")) - .collect::<Vec<_>>() - .join(" OR ") - ))?; - let mut rows = stmt.query([])?; + let mut stmt = + conn.prepare_cached("SELECT event FROM events WHERE event_id = ?;")?; + for id in ids.into_iter() { + let mut rows = stmt.query([id.to_hex()])?; while let Ok(Some(row)) = rows.next() { let buf: Vec<u8> = row.get(0)?; events.push(Event::decode(&buf)?);