mithril_aggregator/database/query/certificate/
get_master_certificate.rs1use sqlite::Value;
2
3use mithril_common::entities::Epoch;
4use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};
5
6use crate::database::record::CertificateRecord;
7
8pub struct MasterCertificateQuery {
10 condition: WhereCondition,
11}
12
13impl MasterCertificateQuery {
14 pub fn for_epoch(epoch: Epoch) -> Self {
15 let epoch_i64: i64 = epoch.try_into().unwrap();
16 let condition = WhereCondition::new(
17 "certificate.epoch between ?* and ?*",
18 vec![Value::Integer(epoch_i64 - 1), Value::Integer(epoch_i64)],
19 )
20 .and_where(
21 WhereCondition::new("certificate.parent_certificate_id is null", vec![]).or_where(
22 WhereCondition::new("certificate.epoch != parent_certificate.epoch", vec![]),
23 ),
24 );
25
26 Self { condition }
27 }
28}
29
30impl Query for MasterCertificateQuery {
31 type Entity = CertificateRecord;
32
33 fn filters(&self) -> WhereCondition {
34 self.condition.clone()
35 }
36
37 fn get_definition(&self, condition: &str) -> String {
38 let projection = Self::Entity::get_projection().expand(SourceAlias::new(&[
41 ("{:certificate:}", "certificate"),
42 ("{:parent_certificate:}", "parent_certificate"),
43 ]));
44
45 format!(
46 r#"
47select {projection}
48from certificate
49 left join certificate as parent_certificate
50 on parent_certificate.certificate_id = certificate.parent_certificate_id
51where {condition}
52order by certificate.ROWID desc"#
53 )
54 }
55}