mithril_aggregator/database/query/signed_entity/
insert_signed_entity.rs1use sqlite::Value;
2
3use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};
4
5use crate::database::record::SignedEntityRecord;
6
7pub struct InsertSignedEntityRecordQuery {
9 condition: WhereCondition,
10}
11
12impl InsertSignedEntityRecordQuery {
13 pub fn one(signed_entity_record: SignedEntityRecord) -> Self {
14 Self {
15 condition: WhereCondition::new(
16 "(signed_entity_id, signed_entity_type_id, certificate_id, beacon, artifact, created_at) values (?*, ?*, ?*, ?*, ?*, ?*)",
17 vec![
18 Value::String(signed_entity_record.signed_entity_id),
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().unwrap()),
22 Value::String(signed_entity_record.artifact),
23 Value::String(signed_entity_record.created_at.to_rfc3339()),
24 ],
25 )
26 }
27 }
28}
29
30impl Query for InsertSignedEntityRecordQuery {
31 type Entity = SignedEntityRecord;
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()
41 .expand(SourceAlias::new(&[("{:signed_entity:}", "signed_entity")]));
42
43 format!("insert into signed_entity {condition} returning {projection}")
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use mithril_persistence::sqlite::ConnectionExtensions;
50
51 use crate::database::test_helper::main_db_connection;
52
53 use super::*;
54
55 #[test]
56 fn test_insert_signed_entity_record() {
57 let signed_entity_records = SignedEntityRecord::fake_records(5);
58
59 let connection = main_db_connection().unwrap();
60
61 for signed_entity_record in signed_entity_records {
62 let signed_entity_record_saved = connection
63 .fetch_first(InsertSignedEntityRecordQuery::one(
64 signed_entity_record.clone(),
65 ))
66 .unwrap();
67 assert_eq!(Some(signed_entity_record), signed_entity_record_saved);
68 }
69 }
70}