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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#[cfg(feature = "with-serde")]
use serde::{Deserialize, Serialize};

use super::{CharSet, Collation};

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
/// All built-in types of MySQL, excluding synonyms
pub enum Type {
    Serial,
    Bit(NumericAttr),
    TinyInt(NumericAttr),
    Bool,
    SmallInt(NumericAttr),
    MediumInt(NumericAttr),
    Int(NumericAttr),
    BigInt(NumericAttr),
    Decimal(NumericAttr),
    Float(NumericAttr),
    Double(NumericAttr),
    Date,
    Time(TimeAttr),
    DateTime(TimeAttr),
    Timestamp(TimeAttr),
    Year,
    Char(StringAttr),
    NChar(StringAttr),
    Varchar(StringAttr),
    NVarchar(StringAttr),
    Binary(StringAttr),
    Varbinary(StringAttr),
    Text(StringAttr),
    TinyText(StringAttr),
    MediumText(StringAttr),
    LongText(StringAttr),
    Blob(BlobAttr),
    TinyBlob,
    MediumBlob,
    LongBlob,
    Enum(EnumDef),
    Set(SetDef),
    Geometry(GeometryAttr),
    Point(GeometryAttr),
    LineString(GeometryAttr),
    Polygon(GeometryAttr),
    MultiPoint(GeometryAttr),
    MultiLineString(GeometryAttr),
    MultiPolygon(GeometryAttr),
    GeometryCollection(GeometryAttr),
    Json,
    Unknown(String),
}

#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
pub struct NumericAttr {
    /// For integer types, M is the maximum display width (deprecated).
    /// For decimal types, M is the total number of digits.
    pub maximum: Option<u32>,
    /// Number of decimal digits.
    pub decimal: Option<u32>,
    /// Whether this number is unsigned
    pub unsigned: Option<bool>,
    /// Deprecated. Prefix 0 up to Z number of digits.
    pub zero_fill: Option<bool>,
}

#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
pub struct TimeAttr {
    pub fractional: Option<u32>,
}

#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
pub struct StringAttr {
    pub length: Option<u32>,
    pub charset: Option<CharSet>,
    pub collation: Option<Collation>,
}

#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
pub struct BlobAttr {
    pub length: Option<u32>,
}

#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
pub struct EnumDef {
    pub values: Vec<String>,
    pub attr: StringAttr,
}

#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
pub struct SetDef {
    pub members: Vec<String>,
    pub attr: StringAttr,
}

#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
pub struct GeometryAttr {
    pub srid: Option<u32>,
}

impl Type {
    pub fn is_numeric(&self) -> bool {
        matches!(
            self,
            Type::Serial
                | Type::Bit(_)
                | Type::TinyInt(_)
                | Type::Bool
                | Type::SmallInt(_)
                | Type::MediumInt(_)
                | Type::Int(_)
                | Type::BigInt(_)
                | Type::Decimal(_)
                | Type::Float(_)
                | Type::Double(_)
        )
    }

    pub fn is_date(&self) -> bool {
        matches!(self, Type::Date | Type::Year)
    }

    pub fn is_time(&self) -> bool {
        matches!(self, Type::Time(_) | Type::DateTime(_) | Type::Timestamp(_))
    }

    pub fn is_string(&self) -> bool {
        matches!(
            self,
            Type::Char(_)
                | Type::NChar(_)
                | Type::Varchar(_)
                | Type::NVarchar(_)
                | Type::Binary(_)
                | Type::Varbinary(_)
                | Type::Text(_)
                | Type::TinyText(_)
                | Type::MediumText(_)
                | Type::LongText(_)
        )
    }

    pub fn is_blob(&self) -> bool {
        matches!(
            self,
            Type::Blob(_) | Type::TinyBlob | Type::MediumBlob | Type::LongBlob
        )
    }

    pub fn is_free_size_blob(&self) -> bool {
        matches!(self, Type::Blob(_))
    }

    pub fn is_geometry(&self) -> bool {
        matches!(
            self,
            Type::Geometry(_)
                | Type::Point(_)
                | Type::LineString(_)
                | Type::Polygon(_)
                | Type::MultiPoint(_)
                | Type::MultiLineString(_)
                | Type::MultiPolygon(_)
                | Type::GeometryCollection(_)
        )
    }

    pub fn is_enum(&self) -> bool {
        matches!(self, Type::Enum(_))
    }

    pub fn is_set(&self) -> bool {
        matches!(self, Type::Set(_))
    }

    pub fn is_other(&self) -> bool {
        matches!(self, Type::Json)
    }

    pub fn is_unknown(&self) -> bool {
        matches!(self, Type::Unknown(_))
    }

    pub fn get_numeric_attr_mut(&mut self) -> &mut NumericAttr {
        match self {
            Type::Serial => panic!("SERIAL has no attr"),
            Type::Bit(attr) => attr,
            Type::TinyInt(attr) => attr,
            Type::Bool => panic!("BOOL has no attr"),
            Type::SmallInt(attr) => attr,
            Type::MediumInt(attr) => attr,
            Type::Int(attr) => attr,
            Type::BigInt(attr) => attr,
            Type::Decimal(attr) => attr,
            Type::Float(attr) => attr,
            Type::Double(attr) => attr,
            _ => panic!("type error"),
        }
    }

    pub fn get_time_attr_mut(&mut self) -> &mut TimeAttr {
        match self {
            Type::Time(attr) => attr,
            Type::DateTime(attr) => attr,
            Type::Timestamp(attr) => attr,
            _ => panic!("type error"),
        }
    }

    pub fn get_string_attr_mut(&mut self) -> &mut StringAttr {
        match self {
            Type::Char(attr) => attr,
            Type::NChar(attr) => attr,
            Type::Varchar(attr) => attr,
            Type::NVarchar(attr) => attr,
            Type::Binary(attr) => attr,
            Type::Varbinary(attr) => attr,
            Type::Text(attr) => attr,
            Type::TinyText(attr) => attr,
            Type::MediumText(attr) => attr,
            Type::LongText(attr) => attr,
            _ => panic!("type error"),
        }
    }

    pub fn get_blob_attr_mut(&mut self) -> &mut BlobAttr {
        match self {
            Type::Blob(attr) => attr,
            _ => panic!("type error"),
        }
    }

    pub fn get_enum_def_mut(&mut self) -> &mut EnumDef {
        match self {
            Type::Enum(def) => def,
            _ => panic!("type error"),
        }
    }

    pub fn get_set_def_mut(&mut self) -> &mut SetDef {
        match self {
            Type::Set(def) => def,
            _ => panic!("type error"),
        }
    }

    pub fn get_geometry_attr_mut(&mut self) -> &mut GeometryAttr {
        match self {
            Type::Geometry(attr) => attr,
            Type::Point(attr) => attr,
            Type::LineString(attr) => attr,
            Type::Polygon(attr) => attr,
            Type::MultiPoint(attr) => attr,
            Type::MultiLineString(attr) => attr,
            Type::MultiPolygon(attr) => attr,
            Type::GeometryCollection(attr) => attr,
            _ => panic!("type error"),
        }
    }
}

impl NumericAttr {
    pub fn m(m: u32) -> Self {
        Self {
            maximum: Some(m),
            decimal: None,
            unsigned: None,
            zero_fill: None,
        }
    }

    pub fn m_d(m: u32, d: u32) -> Self {
        Self {
            maximum: Some(m),
            decimal: Some(d),
            unsigned: None,
            zero_fill: None,
        }
    }

    pub fn unsigned(&mut self) -> &mut Self {
        self.unsigned = Some(true);
        self
    }

    pub fn zero_fill(&mut self) -> &mut Self {
        self.zero_fill = Some(true);
        self
    }

    pub fn take(&self) -> Self {
        Self {
            maximum: self.maximum,
            decimal: self.decimal,
            unsigned: self.unsigned,
            zero_fill: self.zero_fill,
        }
    }
}

impl TimeAttr {
    pub fn fsp(fsp: u32) -> Self {
        Self {
            fractional: Some(fsp),
        }
    }
}

impl StringAttr {
    pub fn length(l: u32) -> Self {
        Self {
            length: Some(l),
            charset: None,
            collation: None,
        }
    }
}

impl BlobAttr {
    pub fn length(l: u32) -> Self {
        Self { length: Some(l) }
    }
}

impl GeometryAttr {
    pub fn srid(id: u32) -> Self {
        Self { srid: Some(id) }
    }
}