Skip to content

Commit 4ab07f3

Browse files
committed
sentry - db - validator_message - fix query
1 parent 49c2d11 commit 4ab07f3

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

sentry/src/db/validator_message.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,16 @@ pub async fn get_validator_messages(
1515
let mut params: Vec<&(dyn ToSql + Sync)> = vec![&channel_id];
1616

1717
if let Some(validator_id) = validator_id {
18-
where_clauses.push(format!("from = ${}", params.len() + 1));
18+
where_clauses.push(format!(r#""from" = ${}"#, params.len() + 1));
1919
params.push(validator_id);
2020
}
2121

22-
let message_types = message_types.iter().map(|s| format!("'{}'", s)).collect::<Vec<String>>().join(",");
23-
if !message_types.is_empty() {
24-
where_clauses.push(format!("msg->>'type' IN (${})", params.len() + 1));
25-
params.push(dbg!(&message_types));
26-
}
22+
add_message_types_params(&mut where_clauses, &mut params, message_types);
2723

2824
pool
2925
.run(move |connection| {
3026
async move {
31-
let statement = format!("SELECT \"from\", msg, received FROM validator_messages WHERE {} ORDER BY received DESC LIMIT {}", where_clauses.join(" AND "), limit);
27+
let statement = format!(r#"SELECT "from", msg, received FROM validator_messages WHERE {} ORDER BY received DESC LIMIT {}"#, where_clauses.join(" AND "), limit);
3228
match connection.prepare(&statement).await {
3329
Ok(select) => match connection.query(&select, params.as_slice()).await {
3430
Ok(results) => {
@@ -42,3 +38,15 @@ pub async fn get_validator_messages(
4238
})
4339
.await
4440
}
41+
42+
fn add_message_types_params<'a>(where_clauses: &mut Vec<String>, params: &mut Vec<&'a (dyn ToSql + Sync)>, message_types: &'a [String]) {
43+
let mut msg_prep = vec![];
44+
for message_type in message_types.iter() {
45+
msg_prep.push(format!("${}", params.len() + 1));
46+
params.push(message_type);
47+
}
48+
49+
if !msg_prep.is_empty() {
50+
where_clauses.push(format!("msg->>'type' IN ({})", msg_prep.join(",")));
51+
}
52+
}

0 commit comments

Comments
 (0)