-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
column.rs
126 lines (120 loc) Β· 4.99 KB
/
column.rs
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
use heck::{CamelCase, SnakeCase};
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
use sea_query::{ColumnDef, ColumnType};
#[derive(Clone, Debug)]
pub struct Column {
pub(crate) name: String,
pub(crate) col_type: ColumnType,
}
impl Column {
pub fn get_name_snake_case(&self) -> Ident {
format_ident!("{}", self.name.to_snake_case())
}
pub fn get_name_camel_case(&self) -> Ident {
format_ident!("{}", self.name.to_camel_case())
}
pub fn get_rs_type(&self) -> Ident {
match self.col_type {
ColumnType::Char(_)
| ColumnType::String(_)
| ColumnType::Text
| ColumnType::DateTime(_)
| ColumnType::Timestamp(_)
| ColumnType::Time(_)
| ColumnType::Date
| ColumnType::Json
| ColumnType::JsonBinary
| ColumnType::Custom(_) => format_ident!("String"),
ColumnType::TinyInteger(_)
| ColumnType::SmallInteger(_)
| ColumnType::Integer(_)
| ColumnType::BigInteger(_) => format_ident!("u32"),
ColumnType::Float(_)
| ColumnType::Double(_)
| ColumnType::Decimal(_)
| ColumnType::Money(_) => format_ident!("f32"),
ColumnType::Binary(_) => format_ident!("Vec<u8>"),
ColumnType::Boolean => format_ident!("bool"),
}
}
pub fn get_type(&self) -> TokenStream {
match &self.col_type {
ColumnType::Char(s) => match s {
Some(s) => quote! { ColumnType::Char(Some(#s)) },
None => quote! { ColumnType::Char(None) },
},
ColumnType::String(s) => match s {
Some(s) => quote! { ColumnType::String(Some(#s)) },
None => quote! { ColumnType::String(None) },
},
ColumnType::Text => quote! { ColumnType::Text },
ColumnType::TinyInteger(s) => match s {
Some(s) => quote! { ColumnType::TinyInteger(Some(#s)) },
None => quote! { ColumnType::TinyInteger(None) },
},
ColumnType::SmallInteger(s) => match s {
Some(s) => quote! { ColumnType::SmallInteger(Some(#s)) },
None => quote! { ColumnType::SmallInteger(None) },
},
ColumnType::Integer(s) => match s {
Some(s) => quote! { ColumnType::Integer(Some(#s)) },
None => quote! { ColumnType::Integer(None) },
},
ColumnType::BigInteger(s) => match s {
Some(s) => quote! { ColumnType::BigInteger(Some(#s)) },
None => quote! { ColumnType::BigInteger(None) },
},
ColumnType::Float(s) => match s {
Some(s) => quote! { ColumnType::Float(Some(#s)) },
None => quote! { ColumnType::Float(None) },
},
ColumnType::Double(s) => match s {
Some(s) => quote! { ColumnType::Double(Some(#s)) },
None => quote! { ColumnType::Double(None) },
},
ColumnType::Decimal(s) => match s {
Some((s1, s2)) => quote! { ColumnType::Decimal(Some((#s1, #s2))) },
None => quote! { ColumnType::Decimal(None) },
},
ColumnType::DateTime(s) => match s {
Some(s) => quote! { ColumnType::DateTime(Some(#s)) },
None => quote! { ColumnType::DateTime(None) },
},
ColumnType::Timestamp(s) => match s {
Some(s) => quote! { ColumnType::Timestamp(Some(#s)) },
None => quote! { ColumnType::Timestamp(None) },
},
ColumnType::Time(s) => match s {
Some(s) => quote! { ColumnType::Time(Some(#s)) },
None => quote! { ColumnType::Time(None) },
},
ColumnType::Date => quote! { ColumnType::Date },
ColumnType::Binary(s) => match s {
Some(s) => quote! { ColumnType::Binary(Some(#s)) },
None => quote! { ColumnType::Binary(None) },
},
ColumnType::Boolean => quote! { ColumnType::Boolean },
ColumnType::Money(s) => match s {
Some((s1, s2)) => quote! { ColumnType::Money(Some((#s1, #s2))) },
None => quote! { ColumnType::Money(None) },
},
ColumnType::Json => quote! { ColumnType::Json },
ColumnType::JsonBinary => quote! { ColumnType::JsonBinary },
ColumnType::Custom(s) => {
let s = s.to_string();
quote! { ColumnType::Custom(std::rc::Rc::new(sea_query::Alias::new(#s))) }
}
}
}
}
impl From<&ColumnDef> for Column {
fn from(col_def: &ColumnDef) -> Self {
let name = col_def.get_column_name();
let col_type = match col_def.get_column_type() {
Some(ty) => ty.clone(),
None => panic!("ColumnType should not be empty"),
};
Self { name, col_type }
}
}