Skip to main content

schema_model/builder/
column.rs

1use crate::model::column::Column;
2use crate::model::column_type::ColumnType;
3use crate::model::types::DatabaseType;
4use std::collections::HashMap;
5
6/// ColumnBuilder holds intermediate settings for a column and produces a model::Column on build.
7#[derive(Debug)]
8pub struct ColumnBuilder {
9    schema_name: Option<String>,
10    name: String,
11    column_type: ColumnType,
12    length: i32,
13    scale: i32,
14    required: bool,
15    check_constraint: Option<String>,
16    database_checks: HashMap<DatabaseType, String>,
17    default_constraint: Option<String>,
18    generated: Option<String>,
19    min_value: Option<String>,
20    max_value: Option<String>,
21    enum_type: Option<String>,
22    element_type: Option<String>,
23}
24
25impl ColumnBuilder {
26    pub fn new<S: Into<String>>(schema_name: Option<S>, name: S, column_type: ColumnType) -> Self {
27        Self {
28            schema_name: schema_name.map(|s| s.into()),
29            name: name.into(),
30            column_type,
31            length: 0,
32            scale: 0,
33            required: false,
34            check_constraint: None,
35            database_checks: HashMap::new(),
36            default_constraint: None,
37            generated: None,
38            min_value: None,
39            max_value: None,
40            enum_type: None,
41            element_type: None,
42        }
43    }
44    pub fn length(mut self, length: i32) -> Self {
45        self.length = length;
46        self
47    }
48
49    pub fn scale(mut self, scale: i32) -> Self {
50        self.scale = scale;
51        self
52    }
53
54    pub fn required(mut self, required: bool) -> Self {
55        self.required = required;
56        self
57    }
58
59    pub fn check_constraint(mut self, check_constraint: Option<String>) -> Self {
60        self.check_constraint = check_constraint;
61        self
62    }
63
64    /// Sets the check for one database; it overrides the generic `check_constraint` there.
65    pub fn database_check(mut self, database_type: DatabaseType, check: String) -> Self {
66        self.database_checks.insert(database_type, check);
67        self
68    }
69
70    pub fn default_constraint(mut self, default_constraint: Option<String>) -> Self {
71        self.default_constraint = default_constraint;
72        self
73    }
74
75    pub fn generated(mut self, generated: Option<String>) -> Self {
76        self.generated = generated;
77        self
78    }
79
80    pub fn min_value(mut self, min_value: Option<String>) -> Self {
81        self.min_value = min_value;
82        self
83    }
84
85    pub fn max_value(mut self, max_value: Option<String>) -> Self {
86        self.max_value = max_value;
87        self
88    }
89
90    pub fn enum_type(mut self, enum_type: Option<String>) -> Self {
91        self.enum_type = enum_type;
92        self
93    }
94
95    pub fn element_type(mut self, element_type: Option<String>) -> Self {
96        self.element_type = element_type;
97        self
98    }
99
100    pub fn build(self) -> Column {
101        Column::new_all(
102            self.schema_name,
103            self.name,
104            self.column_type,
105            self.length,
106            self.scale,
107            self.required,
108            self.check_constraint,
109            self.default_constraint,
110            self.generated,
111            self.min_value,
112            self.max_value,
113            self.enum_type,
114            self.element_type,
115        )
116        .with_database_checks(self.database_checks)
117    }
118}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123
124    #[test]
125    fn build_basic_column() {
126        let c = ColumnBuilder::new(None, "name", ColumnType::Varchar)
127            .length(100)
128            .required(true)
129            .build();
130        assert_eq!(c.name(), "name");
131        assert_eq!(c.length(), 100);
132        assert!(c.required());
133    }
134}