Skip to main content

schema_model/builder/
table.rs

1use crate::model::aggregation::Aggregation;
2use crate::model::column::Column;
3use crate::model::constraint::Constraint;
4use crate::model::initial_data::InitialData;
5use crate::model::key::Key;
6use crate::model::relation::Relation;
7use crate::model::table::Table;
8use crate::model::trigger::Trigger;
9use crate::model::types::{LockEscalation, TableOption};
10
11/// TableBuilder holds an intermediate, mutable state for a table and produces an immutable Table.
12#[derive(Debug)]
13pub struct TableBuilder {
14    schema_name: Option<String>,
15    name: String,
16    export_date_column: Option<String>,
17    lock_escalation: LockEscalation,
18    no_export: bool,
19    columns: Vec<Column>,
20    keys: Vec<Key>,
21    indexes: Vec<Key>,
22    relations: Vec<Relation>,
23    triggers: Vec<Trigger>,
24    constraints: Vec<Constraint>,
25    initial_data: Vec<InitialData>,
26    options: Vec<TableOption>,
27    aggregations: Vec<Aggregation>,
28}
29
30impl TableBuilder {
31    pub fn new<S: Into<String>>(schema_name: Option<S>, name: S) -> Self {
32        Self {
33            schema_name: schema_name.map(|s| s.into()),
34            name: name.into(),
35            export_date_column: None,
36            lock_escalation: LockEscalation::Auto,
37            no_export: false,
38            columns: Vec::new(),
39            keys: Vec::new(),
40            indexes: Vec::new(),
41            relations: Vec::new(),
42            triggers: Vec::new(),
43            constraints: Vec::new(),
44            initial_data: Vec::new(),
45            options: Vec::new(),
46            aggregations: Vec::new(),
47        }
48    }
49
50    pub fn export_date_column<S: Into<String>>(mut self, col: S) -> Self {
51        self.export_date_column = Some(col.into());
52        self
53    }
54
55    pub fn lock_escalation(mut self, le: LockEscalation) -> Self {
56        self.lock_escalation = le;
57        self
58    }
59
60    pub fn no_export(mut self, v: bool) -> Self {
61        self.no_export = v;
62        self
63    }
64
65    pub fn add_column(mut self, column: Column) -> Self {
66        self.columns.push(column);
67        self
68    }
69
70    pub fn add_key(mut self, key: Key) -> Self {
71        self.keys.push(key);
72        self
73    }
74
75    pub fn add_index(mut self, index: Key) -> Self {
76        self.indexes.push(index);
77        self
78    }
79
80    pub fn add_relation(mut self, relation: Relation) -> Self {
81        self.relations.push(relation);
82        self
83    }
84
85    pub fn add_trigger(mut self, trigger: Trigger) -> Self {
86        self.triggers.push(trigger);
87        self
88    }
89
90    pub fn add_constraint(mut self, constraint: Constraint) -> Self {
91        self.constraints.push(constraint);
92        self
93    }
94
95    pub fn add_initial_data(mut self, initial_data: InitialData) -> Self {
96        self.initial_data.push(initial_data);
97        self
98    }
99
100    pub fn add_option(mut self, o: TableOption) -> Self {
101        self.options.push(o);
102        self
103    }
104
105    pub fn add_aggregation(mut self, aggregation: Aggregation) -> Self {
106        self.aggregations.push(aggregation);
107        self
108    }
109
110    pub fn build(self) -> Table {
111        Table::new(
112            self.schema_name,
113            self.name,
114            self.export_date_column,
115            self.lock_escalation,
116            self.no_export,
117            self.columns,
118            self.keys,
119            self.indexes,
120            self.relations,
121            self.triggers,
122            self.constraints,
123            self.initial_data,
124            self.options,
125            self.aggregations,
126        )
127    }
128}
129
130#[cfg(test)]
131mod tests {
132    use super::*;
133    use crate::model::column::Column;
134    use crate::model::column_type::ColumnType;
135    use crate::model::key::{Key, KeyColumn};
136    use crate::model::types::KeyType;
137
138    #[test]
139    fn build_table_with_columns_and_pk() {
140        let t = TableBuilder::new(Some("s"), "t")
141            .add_column(Column::new(Some("s"), "id", ColumnType::Int, 0, 0, true))
142            .add_column(Column::new(Some("s"), "name", ColumnType::Varchar, 255, 0, false))
143            .add_key(Key::new(KeyType::Primary, vec![KeyColumn::new("id")]))
144            .build();
145        assert_eq!(t.name(), "t");
146        assert_eq!(t.columns().len(), 2);
147        assert!(t.primary_key().is_some());
148    }
149}