mithril_aggregator/database/query/immutable_file_digest/
upsert_immutable_file_digest.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use sqlite::Value;

use mithril_common::entities::ImmutableFileName;
use mithril_common::StdResult;
use mithril_persistence::sqlite::{Query, WhereCondition};

use crate::database::record::ImmutableFileDigestRecord;

/// Query to upsert [ImmutableFileDigestRecord] in the sqlite database
pub struct UpsertImmutableFileDigestQuery {
    condition: WhereCondition,
}

impl UpsertImmutableFileDigestQuery {
    pub fn one(immutable_file_name: &ImmutableFileName, digest: &str) -> StdResult<Self> {
        let expression = "(immutable_file_name, digest) values (?*, ?*)";
        let parameters = vec![
            Value::String(immutable_file_name.to_string()),
            Value::String(digest.to_string()),
        ];

        Ok(Self {
            condition: WhereCondition::new(expression, parameters),
        })
    }
}

impl Query for UpsertImmutableFileDigestQuery {
    type Entity = ImmutableFileDigestRecord;

    fn filters(&self) -> WhereCondition {
        self.condition.clone()
    }

    fn get_definition(&self, condition: &str) -> String {
        let projection = Self::Entity::expand_projection("immutable_file_digest");

        format!(
            r#"
insert into immutable_file_digest {condition} 
  on conflict (immutable_file_name) do update set digest = excluded.digest
returning {projection}
"#
        )
    }
}