coven_database/
prerequisite_rows.rs1use crate::WriteId;
7use coven_foundation::id_source::DeviceId;
8use coven_merge::Audience;
9use sqlive::{RowScope, SqlValue, TableRead};
10
11#[derive(Clone, Debug, PartialEq, Eq)]
13pub enum PrerequisiteRows {
14 Slot,
16 Claim,
18 Write(WriteId),
20 Reload(Audience),
22 ProviderAccess(String),
24 Operation(crate::OperationId),
26}
27
28fn blob(value: impl Into<Vec<u8>>) -> SqlValue {
29 SqlValue::Blob(value.into())
30}
31
32fn text(value: &str) -> SqlValue {
33 SqlValue::Text(value.as_bytes().to_vec())
34}
35
36fn device(device: DeviceId) -> SqlValue {
37 blob(device.0.to_be_bytes())
38}
39
40fn number(number: u64) -> SqlValue {
41 blob(number.to_be_bytes())
42}
43
44fn rows(table: &str, key: &[(&str, SqlValue)], columns: &[&str]) -> TableRead {
47 let scope = key
48 .iter()
49 .map(|(column, value)| RowScope::equal((*column).into(), vec![value.clone()]))
50 .fold(RowScope::All, RowScope::and);
51 TableRead {
52 table: table.into(),
53 columns: columns.iter().map(|column| (*column).into()).collect(),
54 scope,
55 inserted_only: false,
56 }
57}
58
59const UPLOADS: &[&str] = &["record"];
60const UPLOAD_KEYS: &[&str] = &["sealing_keys"];
61const UPLOAD_CLAIMS: &[&str] = &["claim"];
62const DATA_RELOADS: &[&str] = &["subject", "marker"];
63const PROVIDER_ACCESS: &[&str] = &["intended", "granted", "invitation", "operation", "target"];
64const OPERATIONS: &[&str] = &["kind", "last_step", "data", "started_by"];
65
66impl PrerequisiteRows {
67 pub(crate) fn reads(&self) -> Vec<TableRead> {
68 match self {
69 Self::Slot => vec![rows("_coven_sequence", &[], &["applied"])],
70 Self::Claim => vec![rows(
71 "_coven_claim_journal",
72 &[],
73 &["through", "operation", "settlement", "singleton"],
74 )],
75 Self::Write(write) => {
76 let at = [("device", device(write.device)), ("number", number(write.number))];
77 vec![
78 rows("_coven_sequence", &[], &["applied"]),
79 rows("_coven_uploads", &at, UPLOADS),
80 rows("_coven_upload_keys", &at, UPLOAD_KEYS),
81 rows("_coven_upload_claims", &at, UPLOAD_CLAIMS),
82 ]
83 }
84 Self::Reload(audience) => vec![rows(
85 "_coven_data_reloads",
86 &[("audience", text(&audience.to_string()))],
87 DATA_RELOADS,
88 )],
89 Self::ProviderAccess(account) => {
90 vec![rows("_coven_provider_access", &[("account", text(account))], PROVIDER_ACCESS)]
91 }
92 Self::Operation(id) => {
93 vec![rows("_coven_operations", &[("id", SqlValue::Integer(id.0))], OPERATIONS)]
94 }
95 }
96 }
97}
98
99#[cfg(test)]
100#[path = "prerequisite_rows_tests.rs"]
101mod tests;