mithril_aggregator/database/query/immutable_file_digest/
upsert_immutable_file_digest.rs

1use sqlite::Value;
2
3use mithril_common::entities::ImmutableFileName;
4use mithril_common::StdResult;
5use mithril_persistence::sqlite::{Query, WhereCondition};
6
7use crate::database::record::ImmutableFileDigestRecord;
8
9/// Query to upsert [ImmutableFileDigestRecord] in the sqlite database
10pub struct UpsertImmutableFileDigestQuery {
11    condition: WhereCondition,
12}
13
14impl UpsertImmutableFileDigestQuery {
15    pub fn one(immutable_file_name: &ImmutableFileName, digest: &str) -> StdResult<Self> {
16        let expression = "(immutable_file_name, digest) values (?*, ?*)";
17        let parameters = vec![
18            Value::String(immutable_file_name.to_string()),
19            Value::String(digest.to_string()),
20        ];
21
22        Ok(Self {
23            condition: WhereCondition::new(expression, parameters),
24        })
25    }
26}
27
28impl Query for UpsertImmutableFileDigestQuery {
29    type Entity = ImmutableFileDigestRecord;
30
31    fn filters(&self) -> WhereCondition {
32        self.condition.clone()
33    }
34
35    fn get_definition(&self, condition: &str) -> String {
36        let projection = Self::Entity::expand_projection("immutable_file_digest");
37
38        format!(
39            r#"
40insert into immutable_file_digest {condition} 
41  on conflict (immutable_file_name) do update set digest = excluded.digest
42returning {projection}
43"#
44        )
45    }
46}