mithril_signer/database/query/protocol_initializer/
get_protocol_initializer.rs1use sqlite::Value;
2
3use mithril_common::entities::Epoch;
4use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};
5
6use crate::database::record::ProtocolInitializerRecord;
7
8pub struct GetProtocolInitializerQuery {
10 condition: WhereCondition,
11 limit: Option<usize>,
12}
13
14impl GetProtocolInitializerQuery {
15 pub fn for_epoch(epoch: Epoch) -> Self {
17 let epoch_i64: i64 = epoch.try_into().unwrap();
18 let condition = WhereCondition::new(
19 "protocol_initializer.epoch = ?",
20 vec![Value::Integer(epoch_i64)],
21 );
22
23 Self {
24 condition,
25 limit: None,
26 }
27 }
28
29 pub fn last_n(limit: usize) -> Self {
30 let condition = WhereCondition::default();
31 Self {
32 condition,
33 limit: Some(limit),
34 }
35 }
36}
37
38impl Query for GetProtocolInitializerQuery {
39 type Entity = ProtocolInitializerRecord;
40
41 fn filters(&self) -> WhereCondition {
42 self.condition.clone()
43 }
44
45 fn get_definition(&self, condition: &str) -> String {
46 let aliases = SourceAlias::new(&[("{:protocol_initializer:}", "protocol_initializer")]);
47 let projection = Self::Entity::get_projection().expand(aliases);
48 let limit = self
49 .limit
50 .map_or("".to_string(), |limit| format!(" limit {}", limit));
51 format!("select {projection} from protocol_initializer where {condition} order by rowid desc{limit}")
52 }
53}