mithril_aggregator/database/query/open_message/
update_open_message.rs1use anyhow::Context;
2use sqlite::Value;
3
4use mithril_common::StdResult;
5use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};
6
7use crate::database::record::OpenMessageRecord;
8
9pub struct UpdateOpenMessageQuery {
11 condition: WhereCondition,
12}
13
14impl UpdateOpenMessageQuery {
15 pub fn one(open_message: &OpenMessageRecord) -> StdResult<Self> {
16 let expression = "epoch_setting_id = ?*, beacon = ?*, \
17signed_entity_type_id = ?*, protocol_message = ?*, is_certified = ?*, \
18is_expired = ?*, expires_at = ?* where open_message_id = ?*";
19 let beacon_str = open_message.signed_entity_type.get_json_beacon()?;
20 let parameters = vec![
21 Value::Integer(
22 open_message
23 .epoch
24 .try_into()
25 .with_context(|| format!("Can not convert epoch: '{}'", open_message.epoch))?,
26 ),
27 Value::String(beacon_str),
28 Value::Integer(open_message.signed_entity_type.index() as i64),
29 Value::String(serde_json::to_string(&open_message.protocol_message)?),
30 Value::Integer(open_message.is_certified as i64),
31 Value::Integer(open_message.is_expired as i64),
32 open_message
33 .expires_at
34 .map(|d| Value::String(d.to_rfc3339()))
35 .unwrap_or(Value::Null),
36 Value::String(open_message.open_message_id.to_string()),
37 ];
38
39 Ok(Self {
40 condition: WhereCondition::new(expression, parameters),
41 })
42 }
43}
44
45impl Query for UpdateOpenMessageQuery {
46 type Entity = OpenMessageRecord;
47
48 fn filters(&self) -> WhereCondition {
49 self.condition.clone()
50 }
51
52 fn get_definition(&self, condition: &str) -> String {
53 let aliases = SourceAlias::new(&[("{:open_message:}", "open_message")]);
54 let projection = Self::Entity::get_projection().expand(aliases);
55
56 format!("update open_message set {condition} returning {projection}")
57 }
58}