1
#[cfg(feature = "postgres")]
2
use crate::constants::postgresql_type;
3
#[cfg(feature = "mssql")]
4
use crate::constants::sqlserver_type;
5
use crate::constants::{regex_patterns, rust_type};
6

            
7
use canyon_entities::register_types::CanyonRegisterEntityField;
8
use regex::Regex;
9

            
10
/// Return the postgres datatype and parameters to create a column for a given rust type
11
#[cfg(feature = "postgres")]
12
pub fn to_postgres_syntax(field: &CanyonRegisterEntityField) -> String {
13
    let rust_type_clean = field.field_type.replace(' ', "");
14

            
15
    match rust_type_clean.as_str() {
16
        rust_type::I8 | rust_type::U8 => {
17
            String::from(&format!("{} NOT NULL", postgresql_type::INTEGER))
18
        }
19
        rust_type::OPT_I8 | rust_type::OPT_U8 => String::from(postgresql_type::INTEGER),
20

            
21
        rust_type::I16 | rust_type::U16 => {
22
            String::from(&format!("{} NOT NULL", postgresql_type::INTEGER))
23
        }
24
        rust_type::OPT_I16 | rust_type::OPT_U16 => String::from(postgresql_type::INTEGER),
25

            
26
        rust_type::I32 | rust_type::U32 => {
27
            String::from(&format!("{} NOT NULL", postgresql_type::INTEGER))
28
        }
29
        rust_type::OPT_I32 | rust_type::OPT_U32 => String::from(postgresql_type::INTEGER),
30

            
31
        rust_type::I64 | rust_type::U64 => {
32
            String::from(&format!("{} NOT NULL", postgresql_type::BIGINT))
33
        }
34
        rust_type::OPT_I64 | rust_type::OPT_U64 => String::from(postgresql_type::BIGINT),
35

            
36
        rust_type::STRING => String::from(&format!("{} NOT NULL", postgresql_type::TEXT)),
37
        rust_type::OPT_STRING => String::from(postgresql_type::TEXT),
38

            
39
        rust_type::BOOL => String::from(&format!("{} NOT NULL", postgresql_type::BOOLEAN)),
40
        rust_type::OPT_BOOL => String::from(postgresql_type::BOOLEAN),
41

            
42
        rust_type::NAIVE_DATE => String::from(&format!("{} NOT NULL", postgresql_type::DATE)),
43
        rust_type::OPT_NAIVE_DATE => String::from(postgresql_type::DATE),
44

            
45
        rust_type::NAIVE_TIME => String::from(&format!("{} NOT NULL", postgresql_type::TIME)),
46
        rust_type::OPT_NAIVE_TIME => String::from(postgresql_type::TIME),
47

            
48
        rust_type::NAIVE_DATE_TIME => {
49
            String::from(&format!("{} NOT NULL", postgresql_type::DATETIME))
50
        }
51
        rust_type::OPT_NAIVE_DATE_TIME => String::from(postgresql_type::DATETIME),
52
        &_ => todo!("Not supported datatype for this migrations version"),
53
    }
54
}
55

            
56
/// Return the postgres datatype and parameters to create a column for a given rust type
57
/// for Microsoft SQL Server
58
#[cfg(feature = "mssql")]
59
pub fn to_sqlserver_syntax(field: &CanyonRegisterEntityField) -> String {
60
    let rust_type_clean = field.field_type.replace(' ', "");
61

            
62
    match rust_type_clean.as_str() {
63
        rust_type::I8 | rust_type::U8 => String::from(&format!("{} NOT NULL", sqlserver_type::INT)),
64
        rust_type::OPT_I8 | rust_type::OPT_U8 => String::from(sqlserver_type::INT),
65

            
66
        rust_type::I16 | rust_type::U16 => {
67
            String::from(&format!("{} NOT NULL", sqlserver_type::INT))
68
        }
69
        rust_type::OPT_I16 | rust_type::OPT_U16 => String::from(sqlserver_type::INT),
70

            
71
        rust_type::I32 | rust_type::U32 => {
72
            String::from(&format!("{} NOT NULL", sqlserver_type::INT))
73
        }
74
        rust_type::OPT_I32 | rust_type::OPT_U32 => String::from(sqlserver_type::INT),
75

            
76
        rust_type::I64 | rust_type::U64 => {
77
            String::from(&format!("{} NOT NULL", sqlserver_type::BIGINT))
78
        }
79
        rust_type::OPT_I64 | rust_type::OPT_U64 => String::from(sqlserver_type::BIGINT),
80

            
81
        rust_type::STRING => {
82
            String::from(&format!("{} NOT NULL DEFAULT ''", sqlserver_type::NVARCHAR))
83
        }
84
        rust_type::OPT_STRING => String::from(sqlserver_type::NVARCHAR),
85

            
86
        rust_type::BOOL => String::from(&format!("{} NOT NULL", sqlserver_type::BIT)),
87
        rust_type::OPT_BOOL => String::from(sqlserver_type::BIT),
88

            
89
        rust_type::NAIVE_DATE => String::from(&format!("{} NOT NULL", sqlserver_type::DATE)),
90
        rust_type::OPT_NAIVE_DATE => String::from(sqlserver_type::DATE),
91

            
92
        rust_type::NAIVE_TIME => String::from(&format!("{} NOT NULL", sqlserver_type::TIME)),
93
        rust_type::OPT_NAIVE_TIME => String::from(sqlserver_type::TIME),
94

            
95
        rust_type::NAIVE_DATE_TIME => {
96
            String::from(&format!("{} NOT NULL", sqlserver_type::DATETIME))
97
        }
98
        rust_type::OPT_NAIVE_DATE_TIME => String::from(sqlserver_type::DATETIME),
99
        &_ => todo!("Not supported datatype for this migrations version"),
100
    }
101
}
102

            
103
#[cfg(feature = "postgres")]
104
pub fn to_postgres_alter_syntax(field: &CanyonRegisterEntityField) -> String {
105
    let mut rust_type_clean = field.field_type.replace(' ', "");
106
    let rs_type_is_optional = field.field_type.to_uppercase().starts_with("OPTION");
107

            
108
    if rs_type_is_optional {
109
        let type_regex = Regex::new(regex_patterns::EXTRACT_RUST_OPT_REGEX).unwrap();
110
        let capture_rust_type = type_regex.captures(rust_type_clean.as_str()).unwrap();
111
        rust_type_clean = capture_rust_type
112
            .name("rust_type")
113
            .unwrap()
114
            .as_str()
115
            .to_string();
116
    }
117

            
118
    match rust_type_clean.as_str() {
119
        rust_type::I8 | rust_type::U8 | rust_type::OPT_I8 | rust_type::OPT_U8 => {
120
            String::from(postgresql_type::INT_8)
121
        }
122
        rust_type::I16 | rust_type::U16 | rust_type::OPT_I16 | rust_type::OPT_U16 => {
123
            String::from(postgresql_type::SMALL_INT)
124
        }
125
        rust_type::I32 | rust_type::U32 | rust_type::OPT_I32 | rust_type::OPT_U32 => {
126
            String::from(postgresql_type::INTEGER)
127
        }
128
        rust_type::I64 | rust_type::U64 | rust_type::OPT_I64 | rust_type::OPT_U64 => {
129
            String::from(postgresql_type::BIGINT)
130
        }
131
        rust_type::STRING | rust_type::OPT_STRING => String::from(postgresql_type::TEXT),
132
        rust_type::BOOL | rust_type::OPT_BOOL => String::from(postgresql_type::BOOLEAN),
133
        rust_type::NAIVE_DATE | rust_type::OPT_NAIVE_DATE => String::from(postgresql_type::DATE),
134
        rust_type::NAIVE_TIME | rust_type::OPT_NAIVE_TIME => String::from(postgresql_type::TIME),
135
        rust_type::NAIVE_DATE_TIME | rust_type::OPT_NAIVE_DATE_TIME => {
136
            String::from(postgresql_type::DATETIME)
137
        }
138
        &_ => todo!("Not supported datatype for this migrations version"),
139
    }
140
}
141

            
142
#[cfg(feature = "mssql")]
143
pub fn to_sqlserver_alter_syntax(field: &CanyonRegisterEntityField) -> String {
144
    let mut rust_type_clean = field.field_type.replace(' ', "");
145
    let rs_type_is_optional = field.field_type.to_uppercase().starts_with("OPTION");
146

            
147
    if rs_type_is_optional {
148
        let type_regex = Regex::new(regex_patterns::EXTRACT_RUST_OPT_REGEX).unwrap();
149
        let capture_rust_type = type_regex.captures(rust_type_clean.as_str()).unwrap();
150
        rust_type_clean = capture_rust_type
151
            .name("rust_type")
152
            .unwrap()
153
            .as_str()
154
            .to_string();
155
    }
156

            
157
    match rust_type_clean.as_str() {
158
        rust_type::I8 | rust_type::U8 | rust_type::OPT_I8 | rust_type::OPT_U8 => {
159
            String::from(sqlserver_type::TINY_INT)
160
        }
161
        rust_type::I16 | rust_type::U16 | rust_type::OPT_I16 | rust_type::OPT_U16 => {
162
            String::from(sqlserver_type::SMALL_INT)
163
        }
164
        rust_type::I32 | rust_type::U32 | rust_type::OPT_I32 | rust_type::OPT_U32 => {
165
            String::from(sqlserver_type::INT)
166
        }
167
        rust_type::I64 | rust_type::U64 | rust_type::OPT_I64 | rust_type::OPT_U64 => {
168
            String::from(sqlserver_type::BIGINT)
169
        }
170
        rust_type::STRING | rust_type::OPT_STRING => String::from(sqlserver_type::NVARCHAR),
171
        rust_type::BOOL | rust_type::OPT_BOOL => String::from(sqlserver_type::BIT),
172
        rust_type::NAIVE_DATE | rust_type::OPT_NAIVE_DATE => String::from(sqlserver_type::DATE),
173
        rust_type::NAIVE_TIME | rust_type::OPT_NAIVE_TIME => String::from(sqlserver_type::TIME),
174
        rust_type::NAIVE_DATE_TIME | rust_type::OPT_NAIVE_DATE_TIME => {
175
            String::from(sqlserver_type::DATETIME)
176
        }
177
        &_ => todo!("Not supported datatype for this migrations version"),
178
    }
179
}