cat_gateway/db/event/signed_docs/
doc_ref.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
//! Document Reference filtering object.

use crate::db::event::common::eq_or_ranged_uuid::EqOrRangedUuid;

/// Document Reference filtering struct.
#[derive(Clone, Debug)]
pub(crate) struct DocumentRef {
    /// Document id filtering
    pub(crate) id: Option<EqOrRangedUuid>,
    /// Document ver filtering
    pub(crate) ver: Option<EqOrRangedUuid>,
}

impl DocumentRef {
    /// Return a sql conditional statement by the provided `table_field`
    pub(crate) fn conditional_stmt(&self, table_field: &str) -> String {
        let mut stmt = "TRUE".to_string();
        if let Some(id) = &self.id {
            stmt.push_str(&format!(
                " AND {}",
                id.conditional_stmt(&format!("({table_field}->>'id')::uuid"))
            ));
        }
        if let Some(ver) = &self.ver {
            stmt.push_str(&format!(
                " AND {}",
                ver.conditional_stmt(&format!("({table_field}->>'ver')::uuid"))
            ));
        }
        stmt
    }
}