-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_column.rs
More file actions
119 lines (105 loc) · 3.42 KB
/
Copy pathtable_column.rs
File metadata and controls
119 lines (105 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use rust_extensions::StrOrString;
use crate::ColumnName;
use super::TableColumnType;
#[derive(Debug, Clone)]
pub struct TableColumn {
pub name: ColumnName,
pub sql_type: TableColumnType,
pub is_nullable: bool,
pub default: Option<StrOrString<'static>>,
}
impl TableColumn {
pub fn update_table_column(&mut self, table_name: &str, column: &Self) {
if !self.sql_type.equals_to(&column.sql_type) {
panic!(
"Two table models for the same table '{}' have different column types",
table_name
);
}
if column.is_nullable {
self.is_nullable = true;
}
}
pub fn is_the_same_to(&self, other: &Self) -> bool {
if !self.sql_type.equals_to(&other.sql_type) {
return false;
}
if self.is_nullable != other.is_nullable {
return false;
}
if !self.is_default_the_same(other) {
return false;
}
true
}
pub fn generate_is_nullable_sql(&self) -> &'static str {
if self.is_nullable {
"null"
} else {
"not null"
}
}
pub fn is_default_the_same(&self, other: &Self) -> bool {
if let Some(self_default) = &self.default {
if let Some(other_default) = &other.default {
return other_default.as_str() == self_default.as_str();
}
} else {
if other.default.is_none() {
return true;
}
}
false
}
pub fn get_default(&self) -> Option<String> {
let default_value = self.default.as_ref()?.as_str();
match &self.sql_type {
TableColumnType::Text => {
if default_value.starts_with("'") {
return Some(default_value.to_string());
} else {
return Some(format!("'{}'", default_value));
}
}
TableColumnType::SmallInt => {
return Some(default_value.to_string());
}
TableColumnType::BigInt => {
return Some(default_value.to_string());
}
TableColumnType::Boolean => {
return Some(default_value.to_string());
}
TableColumnType::Real => {
return Some(default_value.to_string());
}
TableColumnType::Double => {
return Some(default_value.to_string());
}
TableColumnType::Integer => {
return Some(default_value.to_string());
}
TableColumnType::Json => {
if default_value.starts_with("'") {
return Some(default_value.to_string());
} else {
return Some(format!("'{}'", default_value));
}
}
TableColumnType::Timestamp => {
if default_value.starts_with("'") {
return Some(default_value.to_string());
} else {
return Some(format!("'{}'", default_value));
}
}
TableColumnType::Jsonb => {
if default_value.starts_with("'") {
return Some(default_value.to_string());
} else {
return Some(format!("'{}'", default_value));
}
}
}
}
}