cratestack_sql/descriptor/
mod.rs1use std::fmt::Write;
2use std::marker::PhantomData;
3
4use cratestack_core::ModelEventKind;
5use cratestack_policy::ReadPolicy;
6
7mod defaults;
8mod model_impls;
9mod read_source;
10mod view;
11
12#[cfg(test)]
13mod tests_view;
14
15pub use defaults::{CreateDefault, CreateDefaultType};
16pub use read_source::{ReadSource, WriteSource};
17pub use view::ViewDescriptor;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct ModelColumn {
21 pub rust_name: &'static str,
22 pub sql_name: &'static str,
23}
24
25#[derive(Debug, Clone, Copy)]
26pub struct ModelDescriptor<M, PK> {
27 pub schema_name: &'static str,
28 pub table_name: &'static str,
29 pub columns: &'static [ModelColumn],
30 pub primary_key: &'static str,
31 pub allowed_fields: &'static [&'static str],
32 pub allowed_includes: &'static [&'static str],
33 pub allowed_sorts: &'static [&'static str],
34 pub read_allow_policies: &'static [ReadPolicy],
35 pub read_deny_policies: &'static [ReadPolicy],
36 pub detail_allow_policies: &'static [ReadPolicy],
37 pub detail_deny_policies: &'static [ReadPolicy],
38 pub create_allow_policies: &'static [ReadPolicy],
39 pub create_deny_policies: &'static [ReadPolicy],
40 pub update_allow_policies: &'static [ReadPolicy],
41 pub update_deny_policies: &'static [ReadPolicy],
42 pub delete_allow_policies: &'static [ReadPolicy],
43 pub delete_deny_policies: &'static [ReadPolicy],
44 pub create_defaults: &'static [CreateDefault],
45 pub emitted_events: &'static [ModelEventKind],
46 pub version_column: Option<&'static str>,
50 pub audit_enabled: bool,
54 pub pii_columns: &'static [&'static str],
59 pub sensitive_columns: &'static [&'static str],
62 pub soft_delete_column: Option<&'static str>,
68 pub retention_days: Option<u32>,
73 pub upsert_update_columns: &'static [&'static str],
80 _marker: PhantomData<fn() -> (M, PK)>,
81}
82
83impl<M, PK> ModelDescriptor<M, PK> {
84 #[allow(clippy::too_many_arguments)]
87 pub const fn new(
88 schema_name: &'static str,
89 table_name: &'static str,
90 columns: &'static [ModelColumn],
91 primary_key: &'static str,
92 allowed_fields: &'static [&'static str],
93 allowed_includes: &'static [&'static str],
94 allowed_sorts: &'static [&'static str],
95 read_allow_policies: &'static [ReadPolicy],
96 read_deny_policies: &'static [ReadPolicy],
97 detail_allow_policies: &'static [ReadPolicy],
98 detail_deny_policies: &'static [ReadPolicy],
99 create_allow_policies: &'static [ReadPolicy],
100 create_deny_policies: &'static [ReadPolicy],
101 update_allow_policies: &'static [ReadPolicy],
102 update_deny_policies: &'static [ReadPolicy],
103 delete_allow_policies: &'static [ReadPolicy],
104 delete_deny_policies: &'static [ReadPolicy],
105 create_defaults: &'static [CreateDefault],
106 emitted_events: &'static [ModelEventKind],
107 version_column: Option<&'static str>,
108 audit_enabled: bool,
109 pii_columns: &'static [&'static str],
110 sensitive_columns: &'static [&'static str],
111 soft_delete_column: Option<&'static str>,
112 retention_days: Option<u32>,
113 upsert_update_columns: &'static [&'static str],
114 ) -> Self {
115 Self {
116 schema_name,
117 table_name,
118 columns,
119 primary_key,
120 allowed_fields,
121 allowed_includes,
122 allowed_sorts,
123 read_allow_policies,
124 read_deny_policies,
125 detail_allow_policies,
126 detail_deny_policies,
127 create_allow_policies,
128 create_deny_policies,
129 update_allow_policies,
130 update_deny_policies,
131 delete_allow_policies,
132 delete_deny_policies,
133 create_defaults,
134 emitted_events,
135 version_column,
136 audit_enabled,
137 pii_columns,
138 sensitive_columns,
139 soft_delete_column,
140 retention_days,
141 upsert_update_columns,
142 _marker: PhantomData,
143 }
144 }
145
146 pub fn emits(&self, operation: ModelEventKind) -> bool {
147 self.emitted_events.contains(&operation)
148 }
149
150 pub fn select_projection(&self) -> String {
151 let mut sql = String::new();
152 for (index, column) in self.columns.iter().enumerate() {
153 if index > 0 {
154 sql.push_str(", ");
155 }
156 let _ = write!(sql, "{} AS \"{}\"", column.sql_name, column.rust_name);
157 }
158 sql
159 }
160
161 pub fn select_projection_subset(&self, columns: &[&str]) -> String {
170 let mut sql = String::new();
171 let mut emitted = false;
172 for column in self.columns.iter() {
173 if columns.contains(&column.sql_name) && {
174 if emitted {
175 sql.push_str(", ");
176 }
177 let _ = write!(sql, "{} AS \"{}\"", column.sql_name, column.rust_name);
178 emitted = true;
179 true
180 } {}
181 }
182 if !emitted {
183 if let Some(pk_column) = self
189 .columns
190 .iter()
191 .find(|column| column.sql_name == self.primary_key)
192 {
193 let _ = write!(sql, "{} AS \"{}\"", pk_column.sql_name, pk_column.rust_name,);
194 }
195 }
196 sql
197 }
198}
199
200