1use crate::model::column_type::ColumnType;
2use crate::model::types::{BooleanMode, DatabaseType};
3use std::collections::HashMap;
4
5#[allow(clippy::result_unit_err)] pub fn parse_boolean_default(value: &str) -> Result<Option<bool>, ()> {
17 let trimmed = value.trim();
18
19 if trimmed.eq_ignore_ascii_case("null") {
20 return Ok(None);
21 }
22
23 match trimmed.to_ascii_lowercase().as_str() {
24 "true" | "1" | "yes" | "on" => Ok(Some(true)),
25 "false" | "0" | "no" | "off" => Ok(Some(false)),
26 _ => Err(()),
27 }
28}
29
30#[derive(Debug, Clone)]
31pub struct Column {
32 schema_name: Option<String>,
33 name: String,
34 column_type: ColumnType,
35 length: i32,
36 scale: i32,
37 required: bool,
38 check_constraint: Option<String>,
39 database_checks: HashMap<DatabaseType, String>,
40 default_constraint: Option<String>,
41 generated: Option<String>,
42 min_value: Option<String>,
43 max_value: Option<String>,
44 enum_type: Option<String>,
45 element_type: Option<String>,
46}
47
48impl Column {
49 pub fn new<S: Into<String>>(
50 schema_name: Option<S>,
51 name: S,
52 column_type: ColumnType,
53 length: i32,
54 scale: i32,
55 required: bool,
56 ) -> Self {
57 Self {
58 schema_name: schema_name.map(|s| s.into()),
59 name: name.into(),
60 column_type,
61 length,
62 scale,
63 required,
64 check_constraint: None,
65 database_checks: HashMap::new(),
66 default_constraint: None,
67 generated: None,
68 min_value: None,
69 max_value: None,
70 enum_type: None,
71 element_type: None,
72 }
73 }
74
75 #[allow(clippy::too_many_arguments)] pub fn new_all<S: Into<String>>(
77 schema_name: Option<S>,
78 name: S,
79 column_type: ColumnType,
80 length: i32,
81 scale: i32,
82 required: bool,
83 check_constraint: Option<String>,
84 default_constraint: Option<String>,
85 generated: Option<String>,
86 min_value: Option<String>,
87 max_value: Option<String>,
88 enum_type: Option<String>,
89 element_type: Option<String>,
90 ) -> Self {
91 Self {
92 schema_name: schema_name.map(|s| s.into()),
93 name: name.into(),
94 column_type,
95 length,
96 scale,
97 required,
98 check_constraint,
99 database_checks: HashMap::new(),
100 default_constraint,
101 generated,
102 min_value,
103 max_value,
104 enum_type,
105 element_type,
106 }
107 }
108
109 pub fn schema_name(&self) -> Option<&str> {
110 self.schema_name.as_deref()
111 }
112
113 pub fn name(&self) -> &str {
114 &self.name
115 }
116
117 pub fn column_type(&self) -> ColumnType {
118 self.column_type
119 }
120
121 pub fn length(&self) -> i32 {
122 self.length
123 }
124
125 pub fn scale(&self) -> i32 {
126 self.scale
127 }
128
129 pub fn required(&self) -> bool {
130 self.required
131 }
132
133 pub fn is_required(&self) -> bool {
135 self.required
136 }
137
138 pub fn with_database_checks(mut self, database_checks: HashMap<DatabaseType, String>) -> Self {
140 self.database_checks = database_checks;
141 self
142 }
143
144 pub fn check_constraint(&self) -> Option<&str> {
147 self.check_constraint.as_deref()
148 }
149
150 pub fn check_constraint_for(&self, database_type: DatabaseType) -> Option<&str> {
153 self.database_checks
154 .get(&database_type)
155 .map(String::as_str)
156 .or(self.check_constraint.as_deref())
157 }
158
159 pub fn database_checks(&self) -> &HashMap<DatabaseType, String> {
161 &self.database_checks
162 }
163
164 pub fn default_constraint(&self) -> Option<&str> {
165 self.default_constraint.as_deref()
166 }
167
168 pub fn generated(&self) -> Option<&str> {
169 self.generated.as_deref()
170 }
171
172 pub fn min_value(&self) -> Option<&str> {
173 self.min_value.as_deref()
174 }
175
176 pub fn max_value(&self) -> Option<&str> {
177 self.max_value.as_deref()
178 }
179
180 pub fn enum_type(&self) -> Option<&str> {
181 self.enum_type.as_deref()
182 }
183
184 pub fn element_type(&self) -> Option<&str> {
185 self.element_type.as_deref()
186 }
187
188 pub fn has_min_or_max_value(&self) -> bool {
189 self.min_value.is_some() || self.max_value.is_some()
190 }
191
192 pub fn needs_check_constraints(&self, boolean_mode: BooleanMode) -> bool {
193 self.check_constraint.is_some()
194 || !self.database_checks.is_empty()
195 || self.min_value.is_some()
196 || self.max_value.is_some()
197 || self.enum_type.is_some()
198 || (self.column_type == ColumnType::Boolean && boolean_mode != BooleanMode::Native)
199 }
200}
201
202#[cfg(test)]
203mod tests {
204 use super::*;
205 use crate::model::types::BooleanMode;
206
207 #[test]
208 fn constructor_and_getters() {
209 let c = Column::new(None, "name", ColumnType::Varchar, 255, 0, true);
210 assert_eq!(c.name(), "name");
211 assert_eq!(c.column_type(), ColumnType::Varchar);
212 assert_eq!(c.length(), 255);
213 assert_eq!(c.scale(), 0);
214 assert!(c.required());
215 assert!(c.is_required());
216 }
217
218 #[test]
219 fn parse_boolean_default_recognizes_truthy_spellings() {
220 for value in ["true", "TRUE", " true ", "1", "yes", "YES", "on"] {
221 assert_eq!(
222 parse_boolean_default(value),
223 Ok(Some(true)),
224 "value: {:?}",
225 value
226 );
227 }
228 }
229
230 #[test]
231 fn parse_boolean_default_recognizes_falsy_spellings() {
232 for value in ["false", "FALSE", " false ", "0", "no", "NO", "off"] {
233 assert_eq!(
234 parse_boolean_default(value),
235 Ok(Some(false)),
236 "value: {:?}",
237 value
238 );
239 }
240 }
241
242 #[test]
243 fn parse_boolean_default_null_sentinel_means_no_default() {
244 assert_eq!(parse_boolean_default("null"), Ok(None));
245 assert_eq!(parse_boolean_default("NULL"), Ok(None));
246 assert_eq!(parse_boolean_default(" null "), Ok(None));
247 }
248
249 #[test]
250 fn parse_boolean_default_rejects_unrecognized_values() {
251 for value in ["maybe", "T", "getdate()", ""] {
252 assert_eq!(parse_boolean_default(value), Err(()), "value: {:?}", value);
253 }
254 }
255
256 #[test]
257 fn database_check_overrides_generic_check_for_that_database_only() {
258 let c = Column::new_all(
259 None::<&str>,
260 "a",
261 ColumnType::Int,
262 0,
263 0,
264 false,
265 Some("a > 0".to_string()),
266 None,
267 None,
268 None,
269 None,
270 None,
271 None,
272 )
273 .with_database_checks(HashMap::from([(
274 DatabaseType::Postgresql,
275 "a > 1".to_string(),
276 )]));
277 assert_eq!(
278 c.check_constraint_for(DatabaseType::Postgresql),
279 Some("a > 1")
280 );
281 assert_eq!(
282 c.check_constraint_for(DatabaseType::SqlServer),
283 Some("a > 0")
284 );
285 assert_eq!(c.check_constraint(), Some("a > 0"));
286 }
287
288 #[test]
289 fn database_only_check_needs_check_constraints() {
290 let c = Column::new(None::<&str>, "a", ColumnType::Int, 0, 0, false)
291 .with_database_checks(HashMap::from([(DatabaseType::Sqlite, "a > 1".to_string())]));
292 assert!(c.needs_check_constraints(BooleanMode::Native));
293 assert_eq!(c.check_constraint_for(DatabaseType::Postgresql), None);
294 }
295
296 #[test]
297 fn needs_check_constraints_logic() {
298 let c = Column::new(None, "b", ColumnType::Boolean, 0, 0, false);
299 assert!(c.needs_check_constraints(BooleanMode::YesNo));
301 assert!(!c.needs_check_constraints(BooleanMode::Native));
303 }
304
305 #[test]
306 fn has_min_or_max_value() {
307 let c = Column::new(None, "n", ColumnType::Int, 0, 0, false);
308 assert!(!c.has_min_or_max_value());
309 }
310}