cat_gateway/db/event/signed_docs/
doc_ref.rs

1//! Document Reference filtering object.
2
3use std::fmt::Write;
4
5use crate::db::event::common::eq_or_ranged_uuid::EqOrRangedUuid;
6
7/// Document Reference filtering struct.
8#[derive(Clone, Debug)]
9pub(crate) struct DocumentRef {
10    /// Document id filtering
11    pub(crate) id: Option<EqOrRangedUuid>,
12    /// Document ver filtering
13    pub(crate) ver: Option<EqOrRangedUuid>,
14}
15
16impl DocumentRef {
17    /// Return a sql conditional statement by the provided `table_field`
18    pub(crate) fn conditional_stmt(
19        &self,
20        table_field: &str,
21    ) -> String {
22        let mut stmt = "TRUE".to_string();
23        if let Some(id) = &self.id {
24            let _ = write!(
25                stmt,
26                " AND {}",
27                id.conditional_stmt("(doc_ref->>'id')::uuid")
28            );
29        }
30        if let Some(ver) = &self.ver {
31            let _ = write!(
32                stmt,
33                " AND {}",
34                ver.conditional_stmt("(doc_ref->>'ver')::uuid")
35            );
36        }
37
38        format!(
39            "EXISTS (SELECT 1 FROM JSONB_ARRAY_ELEMENTS({table_field}) AS doc_ref WHERE {stmt})"
40        )
41    }
42}