Skip to main content

no_mistakes_postgres_engine/catalog/
model.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub enum RelationKind {
3    Table,
4    PartitionedTable,
5}
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum GeneratedKind {
9    Stored,
10    Virtual,
11}
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct CatalogTable {
15    pub name: String,
16    pub relation_kind: RelationKind,
17    pub partition_of: Option<String>,
18    pub omitted_partition_leaves: Option<u32>,
19    pub inherited_from: Vec<String>,
20    pub comment: Option<String>,
21    pub columns: Vec<CatalogColumn>,
22    pub primary_key: Option<Vec<String>>,
23    pub foreign_keys: Vec<CatalogForeignKey>,
24    pub check_constraints: Vec<CatalogCheck>,
25    pub unique_constraints: Vec<CatalogUnique>,
26    pub indexes: Vec<CatalogIndexInfo>,
27    pub triggers: Vec<CatalogTrigger>,
28    pub partition_key: Option<PartitionKey>,
29}
30
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub struct CatalogColumn {
33    pub name: String,
34    pub data_type: String,
35    pub nullable: bool,
36    pub default_expression: Option<String>,
37    pub generated: Option<GeneratedKind>,
38    pub generated_expression: Option<String>,
39    pub identity: Option<String>,
40    pub comment: Option<String>,
41    pub ordinal_position: u32,
42}
43
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub struct CatalogForeignKey {
46    pub name: String,
47    pub columns: Vec<String>,
48    pub referenced_table: String,
49    pub referenced_columns: Vec<String>,
50    pub on_delete: String,
51    pub on_update: String,
52    pub validated: bool,
53    /// The transaction can postpone the check. Fill-in does not read this.
54    pub deferrable: bool,
55}
56
57#[derive(Debug, Clone, PartialEq, Eq)]
58pub struct CatalogCheck {
59    pub name: String,
60    pub definition: String,
61    pub validated: bool,
62}
63
64#[derive(Debug, Clone, PartialEq, Eq)]
65pub struct CatalogUnique {
66    pub name: String,
67    pub columns: Vec<String>,
68}
69
70#[derive(Debug, Clone, PartialEq, Eq)]
71pub struct CatalogIndexInfo {
72    pub immediate: bool,
73    pub valid: bool,
74    pub ready: bool,
75    pub live: bool,
76    pub name: String,
77    pub unique: bool,
78    pub primary: bool,
79    pub constraint_backed: bool,
80    pub attached_partition: Option<bool>,
81    pub access_method: String,
82    pub keys: Vec<CatalogIndexKey>,
83    pub predicate: Option<String>,
84    pub definition: String,
85}
86
87#[derive(Debug, Clone, PartialEq, Eq)]
88pub struct CatalogIndexKey {
89    pub ordering_supported: bool,
90    pub column: Option<String>,
91    pub expression: String,
92}
93
94#[derive(Debug, Clone, Copy, PartialEq, Eq)]
95pub enum TriggerTiming {
96    Before,
97    After,
98    InsteadOf,
99}
100
101pub use no_mistakes_postgres_sql::TriggerEvent;
102
103#[derive(Debug, Clone, PartialEq, Eq)]
104pub struct CatalogTrigger {
105    pub name: String,
106    pub timing: TriggerTiming,
107    pub events: Vec<TriggerEvent>,
108    pub update_columns: Vec<String>,
109    pub for_each_row: bool,
110    pub function: String,
111    pub arguments: Vec<String>,
112    pub when: Option<String>,
113    pub definition: String,
114}
115
116impl CatalogTrigger {
117    /// `events` matches when it is a subset of this trigger's events.
118    pub fn matches(
119        &self,
120        function: &str,
121        timing: TriggerTiming,
122        events: &[TriggerEvent],
123        for_each_row: bool,
124    ) -> bool {
125        self.function == function
126            && self.timing == timing
127            && self.for_each_row == for_each_row
128            && events.iter().all(|event| self.events.contains(event))
129    }
130}
131
132#[derive(Debug, Clone, Copy, PartialEq, Eq)]
133pub enum PartitionStrategy {
134    Range,
135    List,
136    Hash,
137}
138
139#[derive(Debug, Clone, PartialEq, Eq)]
140pub struct PartitionKey {
141    pub strategy: PartitionStrategy,
142    pub default_only: Option<bool>,
143    pub elements: Vec<PartitionKeyElement>,
144}
145
146#[derive(Debug, Clone, PartialEq, Eq)]
147pub enum PartitionKeyElement {
148    Column(String),
149    Expression(String),
150}
151
152#[derive(Debug, Clone, PartialEq, Eq)]
153pub struct CatalogFunction {
154    pub key: String,
155    pub name: String,
156    pub signature: Option<String>,
157    pub language: Option<String>,
158    pub returns_set: Option<bool>,
159    pub returns_trigger: bool,
160    pub returns_event_trigger: bool,
161    pub definition: String,
162    pub body: Option<String>,
163    pub(super) body_is_expression: bool,
164    pub body_span: Option<(usize, usize)>,
165    pub null_input: String,
166    pub security: String,
167    pub parallel: String,
168    pub leakproof: String,
169    pub volatility: String,
170    pub return_contract: String,
171    pub planner: String,
172}
173
174#[derive(Debug, Clone, PartialEq, Eq)]
175pub struct CatalogEnum {
176    pub name: String,
177    pub values: Vec<String>,
178}
179
180#[derive(Debug, Clone, PartialEq, Eq)]
181pub struct CatalogView {
182    pub name: String,
183    pub indexes: Option<Vec<CatalogIndexInfo>>,
184    pub materialized: bool,
185    pub definition: String,
186    pub comment: Option<String>,
187}
188
189/// Facts guaranteed by a catalog producer. Partial catalogs must not prove full-schema rules.
190#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Deserialize)]
191#[serde(rename_all = "camelCase")]
192pub enum CatalogCoverage {
193    #[default]
194    Complete,
195    Ordering,
196}