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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use serde::Serialize;
use teo_parser::ast::schema::Schema;
use teo_parser::r#type::reference::Reference;

#[derive(Debug, Serialize, Clone, PartialEq, Eq, Hash)]
pub struct MySQLEnum {
    pub variants: Vec<String>,
}

impl MySQLEnum {
    pub fn build(schema: &Schema, reference: &Reference) -> Self {
        let e = schema.find_top_by_path(reference.path()).unwrap().as_enum().unwrap();
        MySQLEnum {
            variants: e.members().map(|m| m.identifier().name().to_string()).collect()
        }
    }
}

#[derive(Debug, Serialize, Clone, PartialEq, Eq, Hash)]
pub enum MySQLType {
    VarChar(i32),
    Text,
    Char(i32),
    TinyText,
    MediumText,
    LongText,
    Bit(Option<i32>),
    // second parameter is signed
    TinyInt(Option<i32>, bool),
    Int(Option<i32>, bool),
    SmallInt(Option<i32>, bool),
    MediumInt(Option<i32>, bool),
    BigInt(Option<i32>, bool),
    Year,
    Float,
    Double,
    Decimal(i32, i32),
    DateTime(i32),
    Date,
    Time(i32),
    Timestamp(i32),
    Json,
    LongBlob,
    Binary,
    VarBinary,
    TinyBlob,
    Blob,
    MediumBlob,
    Enum(MySQLEnum),
}

impl MySQLType {


    pub fn is_var_char(&self) -> bool {
        self.as_var_char().is_some()
    }

    pub fn as_var_char(&self) -> Option<i32> {
        match self {
            MySQLType::VarChar(len) => Some(*len),
            _ => None,
        }
    }

    pub fn is_text(&self) -> bool {
        match self {
            MySQLType::Text => true,
            _ => false,
        }
    }

    pub fn is_char(&self) -> bool {
        self.as_char().is_some()
    }

    pub fn as_char(&self) -> Option<i32> {
        match self {
            MySQLType::Char(len) => Some(*len),
            _ => None,
        }
    }

    pub fn is_tiny_text(&self) -> bool {
        match self {
            MySQLType::TinyText => true,
            _ => false,
        }
    }

    pub fn is_medium_text(&self) -> bool {
        match self {
            MySQLType::MediumText => true,
            _ => false,
        }
    }

    pub fn is_long_text(&self) -> bool {
        match self {
            MySQLType::LongText => true,
            _ => false,
        }
    }

    pub fn is_bit(&self) -> bool {
        self.as_bit().is_some()
    }

    pub fn as_bit(&self) -> Option<Option<i32>> {
        match self {
            MySQLType::Bit(len) => Some(*len),
            _ => None,
        }
    }

    pub fn is_tiny_int(&self) -> bool {
        self.as_tiny_int().is_some()
    }

    pub fn as_tiny_int(&self) -> Option<(Option<i32>, bool)> {
        match self {
            MySQLType::TinyInt(len, signed) => Some((*len, *signed)),
            _ => None,
        }
    }

    pub fn is_int(&self) -> bool {
        self.as_int().is_some()
    }

    pub fn as_int(&self) -> Option<(Option<i32>, bool)> {
        match self {
            MySQLType::Int(len, signed) => Some((*len, *signed)),
            _ => None,
        }
    }

    pub fn is_small_int(&self) -> bool {
        self.as_small_int().is_some()
    }

    pub fn as_small_int(&self) -> Option<(Option<i32>, bool)> {
        match self {
            MySQLType::SmallInt(len, signed) => Some((*len, *signed)),
            _ => None,
        }
    }

    pub fn is_medium_int(&self) -> bool {
        self.as_medium_int().is_some()
    }

    pub fn as_medium_int(&self) -> Option<(Option<i32>, bool)> {
        match self {
            MySQLType::MediumInt(len, signed) => Some((*len, *signed)),
            _ => None,
        }
    }

    pub fn is_big_int(&self) -> bool {
        self.as_big_int().is_some()
    }

    pub fn as_big_int(&self) -> Option<(Option<i32>, bool)> {
        match self {
            MySQLType::BigInt(len, signed) => Some((*len, *signed)),
            _ => None,
        }
    }

    pub fn is_year(&self) -> bool {
        match self {
            MySQLType::Year => true,
            _ => false,
        }
    }

    pub fn is_float(&self) -> bool {
        match self {
            MySQLType::Float => true,
            _ => false,
        }
    }

    pub fn is_double(&self) -> bool {
        match self {
            MySQLType::Double => true,
            _ => false,
        }
    }

    pub fn is_decimal(&self) -> bool {
        self.as_decimal().is_some()
    }

    pub fn as_decimal(&self) -> Option<(i32, i32)> {
        match self {
            MySQLType::Decimal(p, s) => Some((*p, *s)),
            _ => None,
        }
    }

    pub fn is_datetime(&self) -> bool {
        self.as_datetime().is_some()
    }

    pub fn as_datetime(&self) -> Option<i32> {
        match self {
            MySQLType::DateTime(len) => Some(*len),
            _ => None,
        }
    }

    pub fn is_date(&self) -> bool {
        match self {
            MySQLType::Date => true,
            _ => false,
        }
    }

    pub fn is_time(&self) -> bool {
        self.as_time().is_some()
    }

    pub fn as_time(&self) -> Option<i32> {
        match self {
            MySQLType::Time(len) => Some(*len),
            _ => None,
        }
    }

    pub fn is_timestamp(&self) -> bool {
        self.as_timestamp().is_some()
    }

    pub fn as_timestamp(&self) -> Option<i32> {
        match self {
            MySQLType::Timestamp(len) => Some(*len),
            _ => None,
        }
    }

    pub fn is_json(&self) -> bool {
        match self {
            MySQLType::Json => true,
            _ => false,
        }
    }

    pub fn is_binary(&self) -> bool {
        match self {
            MySQLType::Binary => true,
            _ => false,
        }
    }

    pub fn is_var_binary(&self) -> bool {
        match self {
            MySQLType::VarBinary => true,
            _ => false,
        }
    }

    pub fn is_tiny_blob(&self) -> bool {
        match self {
            MySQLType::TinyBlob => true,
            _ => false,
        }
    }

    pub fn is_blob(&self) -> bool {
        match self {
            MySQLType::Blob => true,
            _ => false,
        }
    }

    pub fn is_medium_blob(&self) -> bool {
        match self {
            MySQLType::MediumBlob => true,
            _ => false,
        }
    }

    pub fn is_long_blob(&self) -> bool {
        match self {
            MySQLType::LongBlob => true,
            _ => false,
        }
    }

    pub fn is_enum(&self) -> bool {
        self.as_enum().is_some()
    }

    pub fn as_enum(&self) -> Option<&MySQLEnum> {
        match self {
            MySQLType::Enum(e) => Some(e),
            _ => None,
        }
    }
}