mithril_aggregator/database/query/signed_entity/
update_signed_entity.rs

1use sqlite::Value;
2
3use mithril_common::StdResult;
4use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};
5
6use crate::database::record::SignedEntityRecord;
7
8/// Query to update [SignedEntityRecord] in the sqlite database
9pub struct UpdateSignedEntityQuery {
10    condition: WhereCondition,
11}
12
13impl UpdateSignedEntityQuery {
14    pub fn one(signed_entity_record: SignedEntityRecord) -> StdResult<Self> {
15        let expression = "signed_entity_type_id = ?*, certificate_id = ?*, beacon = ?*, artifact = ?*, \
16created_at = ?* \
17where signed_entity_id = ?*";
18        let parameters = vec![
19            Value::Integer(signed_entity_record.signed_entity_type.index() as i64),
20            Value::String(signed_entity_record.certificate_id),
21            Value::String(signed_entity_record.signed_entity_type.get_json_beacon()?),
22            Value::String(signed_entity_record.artifact),
23            Value::String(signed_entity_record.created_at.to_rfc3339()),
24            Value::String(signed_entity_record.signed_entity_id),
25        ];
26
27        Ok(Self {
28            condition: WhereCondition::new(expression, parameters),
29        })
30    }
31}
32
33impl Query for UpdateSignedEntityQuery {
34    type Entity = SignedEntityRecord;
35
36    fn filters(&self) -> WhereCondition {
37        self.condition.clone()
38    }
39
40    fn get_definition(&self, condition: &str) -> String {
41        let projection = Self::Entity::get_projection()
42            .expand(SourceAlias::new(&[("{:signed_entity:}", "signed_entity")]));
43
44        format!("update signed_entity set {condition} returning {projection}")
45    }
46}