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