-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdb.rs
More file actions
136 lines (123 loc) · 3.75 KB
/
Copy pathdb.rs
File metadata and controls
136 lines (123 loc) · 3.75 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use crate::mysql::queries::convert_type::convert_data_type;
#[derive(Debug, PartialEq, Eq)]
pub struct TableColumn {
pub column_name: String,
pub column_comment: Option<String>,
pub udt_name: String,
pub data_type: String,
pub recommended_rust_type: Option<String>,
pub is_nullable: bool,
pub array_depth: i32,
pub is_unique: bool,
pub is_primary_key: bool,
pub foreign_key_table: Option<String>,
pub foreign_key_id: Option<String>,
pub is_auto_populated: bool,
}
#[derive(Debug, PartialEq, Eq, Default)]
pub struct Table {
pub table_name: String,
pub table_comment: Option<String>,
pub table_schema: Option<String>,
pub columns: Vec<TableColumn>,
}
#[derive(Debug, PartialEq, Eq, Default)]
pub struct CustomEnumVariant {
pub name: String,
}
#[derive(Debug, PartialEq, Eq, Default)]
pub struct CustomEnum {
pub name: String,
// Only for Postgres
pub type_name: Option<String>,
/// Only for MySQL
pub child_of_table: Option<String>,
pub schema: Option<String>,
pub variants: Vec<CustomEnumVariant>,
pub comments: Option<String>,
}
pub struct TableColumnBuilder {
column_name: String,
column_comment: Option<String>,
recommended_rust_type: Option<String>,
udt_name: String,
data_type: String,
is_nullable: bool,
array_depth: i32,
is_unique: bool,
is_primary_key: bool,
foreign_key_table: Option<String>,
foreign_key_id: Option<String>,
is_auto_populated: bool,
}
impl TableColumnBuilder {
pub fn new(
column_name: impl ToString,
udt_name: impl ToString,
data_type: impl ToString,
recommended_rust_type: Option<String>,
) -> Self {
Self {
column_name: column_name.to_string(),
column_comment: None,
udt_name: udt_name.to_string(),
data_type: data_type.to_string(),
is_nullable: false,
is_unique: false,
is_primary_key: false,
foreign_key_table: None,
foreign_key_id: None,
is_auto_populated: false,
array_depth: 0,
recommended_rust_type,
}
}
pub fn is_nullable(mut self) -> Self {
self.is_nullable = true;
self
}
pub fn array_depth(mut self, depth: i32) -> Self {
self.array_depth = depth;
self
}
pub fn is_auto_populated(mut self) -> Self {
self.is_auto_populated = true;
self
}
pub fn is_unique(mut self) -> Self {
self.is_unique = true;
self
}
pub fn is_primary_key(mut self) -> Self {
self.is_primary_key = true;
self
}
pub fn add_column_comment(mut self, column_comment: impl Into<String>) -> Self {
self.column_comment = Some(column_comment.into());
self
}
pub fn foreign_key_table(mut self, foreign_key_table: impl ToString) -> Self {
self.foreign_key_table = Some(foreign_key_table.to_string());
self
}
pub fn foreign_key_id(mut self, foreign_key_id: impl ToString) -> Self {
self.foreign_key_id = Some(foreign_key_id.to_string());
self
}
pub fn build(self) -> TableColumn {
TableColumn {
column_name: self.column_name,
recommended_rust_type: self.recommended_rust_type,
udt_name: self.udt_name,
data_type: self.data_type,
is_nullable: self.is_nullable,
array_depth: self.array_depth,
is_unique: self.is_unique,
is_primary_key: self.is_primary_key,
foreign_key_table: self.foreign_key_table,
foreign_key_id: self.foreign_key_id,
column_comment: self.column_comment,
is_auto_populated: self.is_auto_populated,
}
}
}