sea_schema/mysql/def/
types.rs

1#[cfg(feature = "with-serde")]
2use serde::{Deserialize, Serialize};
3
4use super::{CharSet, Collation};
5
6#[derive(Clone, Debug, PartialEq)]
7#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
8/// All built-in types of MySQL, excluding synonyms
9pub enum Type {
10    Serial,
11    Bit(NumericAttr),
12    TinyInt(NumericAttr),
13    Bool,
14    SmallInt(NumericAttr),
15    MediumInt(NumericAttr),
16    Int(NumericAttr),
17    BigInt(NumericAttr),
18    Decimal(NumericAttr),
19    Float(NumericAttr),
20    Double(NumericAttr),
21    Date,
22    Time(TimeAttr),
23    DateTime(TimeAttr),
24    Timestamp(TimeAttr),
25    Year,
26    Char(StringAttr),
27    NChar(StringAttr),
28    Varchar(StringAttr),
29    NVarchar(StringAttr),
30    Binary(StringAttr),
31    Varbinary(StringAttr),
32    Text(StringAttr),
33    TinyText(StringAttr),
34    MediumText(StringAttr),
35    LongText(StringAttr),
36    Blob(BlobAttr),
37    TinyBlob,
38    MediumBlob,
39    LongBlob,
40    Enum(EnumDef),
41    Set(SetDef),
42    Geometry(GeometryAttr),
43    Point(GeometryAttr),
44    LineString(GeometryAttr),
45    Polygon(GeometryAttr),
46    MultiPoint(GeometryAttr),
47    MultiLineString(GeometryAttr),
48    MultiPolygon(GeometryAttr),
49    GeometryCollection(GeometryAttr),
50    Json,
51    Unknown(String),
52}
53
54#[derive(Clone, Debug, Default, PartialEq)]
55#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
56pub struct NumericAttr {
57    /// For integer types, M is the maximum display width (deprecated).
58    /// For decimal types, M is the total number of digits.
59    pub maximum: Option<u32>,
60    /// Number of decimal digits.
61    pub decimal: Option<u32>,
62    /// Whether this number is unsigned
63    pub unsigned: Option<bool>,
64    /// Deprecated. Prefix 0 up to Z number of digits.
65    pub zero_fill: Option<bool>,
66}
67
68#[derive(Clone, Debug, Default, PartialEq)]
69#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
70pub struct TimeAttr {
71    pub fractional: Option<u32>,
72}
73
74#[derive(Clone, Debug, Default, PartialEq)]
75#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
76pub struct StringAttr {
77    pub length: Option<u32>,
78    pub charset: Option<CharSet>,
79    pub collation: Option<Collation>,
80}
81
82#[derive(Clone, Debug, Default, PartialEq)]
83#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
84pub struct BlobAttr {
85    pub length: Option<u32>,
86}
87
88#[derive(Clone, Debug, Default, PartialEq)]
89#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
90pub struct EnumDef {
91    pub values: Vec<String>,
92    pub attr: StringAttr,
93}
94
95#[derive(Clone, Debug, Default, PartialEq)]
96#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
97pub struct SetDef {
98    pub members: Vec<String>,
99    pub attr: StringAttr,
100}
101
102#[derive(Clone, Debug, Default, PartialEq)]
103#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
104pub struct GeometryAttr {
105    pub srid: Option<u32>,
106}
107
108impl Type {
109    pub fn is_numeric(&self) -> bool {
110        matches!(
111            self,
112            Type::Serial
113                | Type::Bit(_)
114                | Type::TinyInt(_)
115                | Type::Bool
116                | Type::SmallInt(_)
117                | Type::MediumInt(_)
118                | Type::Int(_)
119                | Type::BigInt(_)
120                | Type::Decimal(_)
121                | Type::Float(_)
122                | Type::Double(_)
123        )
124    }
125
126    pub fn is_date(&self) -> bool {
127        matches!(self, Type::Date | Type::Year)
128    }
129
130    pub fn is_time(&self) -> bool {
131        matches!(self, Type::Time(_) | Type::DateTime(_) | Type::Timestamp(_))
132    }
133
134    pub fn is_string(&self) -> bool {
135        matches!(
136            self,
137            Type::Char(_)
138                | Type::NChar(_)
139                | Type::Varchar(_)
140                | Type::NVarchar(_)
141                | Type::Binary(_)
142                | Type::Varbinary(_)
143                | Type::Text(_)
144                | Type::TinyText(_)
145                | Type::MediumText(_)
146                | Type::LongText(_)
147        )
148    }
149
150    pub fn is_blob(&self) -> bool {
151        matches!(
152            self,
153            Type::Blob(_) | Type::TinyBlob | Type::MediumBlob | Type::LongBlob
154        )
155    }
156
157    pub fn is_free_size_blob(&self) -> bool {
158        matches!(self, Type::Blob(_))
159    }
160
161    pub fn is_geometry(&self) -> bool {
162        matches!(
163            self,
164            Type::Geometry(_)
165                | Type::Point(_)
166                | Type::LineString(_)
167                | Type::Polygon(_)
168                | Type::MultiPoint(_)
169                | Type::MultiLineString(_)
170                | Type::MultiPolygon(_)
171                | Type::GeometryCollection(_)
172        )
173    }
174
175    pub fn is_enum(&self) -> bool {
176        matches!(self, Type::Enum(_))
177    }
178
179    pub fn is_set(&self) -> bool {
180        matches!(self, Type::Set(_))
181    }
182
183    pub fn is_other(&self) -> bool {
184        matches!(self, Type::Json)
185    }
186
187    pub fn is_unknown(&self) -> bool {
188        matches!(self, Type::Unknown(_))
189    }
190
191    pub fn get_numeric_attr_mut(&mut self) -> &mut NumericAttr {
192        match self {
193            Type::Serial => panic!("SERIAL has no attr"),
194            Type::Bit(attr) => attr,
195            Type::TinyInt(attr) => attr,
196            Type::Bool => panic!("BOOL has no attr"),
197            Type::SmallInt(attr) => attr,
198            Type::MediumInt(attr) => attr,
199            Type::Int(attr) => attr,
200            Type::BigInt(attr) => attr,
201            Type::Decimal(attr) => attr,
202            Type::Float(attr) => attr,
203            Type::Double(attr) => attr,
204            _ => panic!("type error"),
205        }
206    }
207
208    pub fn get_time_attr_mut(&mut self) -> &mut TimeAttr {
209        match self {
210            Type::Time(attr) => attr,
211            Type::DateTime(attr) => attr,
212            Type::Timestamp(attr) => attr,
213            _ => panic!("type error"),
214        }
215    }
216
217    pub fn get_string_attr_mut(&mut self) -> &mut StringAttr {
218        match self {
219            Type::Char(attr) => attr,
220            Type::NChar(attr) => attr,
221            Type::Varchar(attr) => attr,
222            Type::NVarchar(attr) => attr,
223            Type::Binary(attr) => attr,
224            Type::Varbinary(attr) => attr,
225            Type::Text(attr) => attr,
226            Type::TinyText(attr) => attr,
227            Type::MediumText(attr) => attr,
228            Type::LongText(attr) => attr,
229            _ => panic!("type error"),
230        }
231    }
232
233    pub fn get_blob_attr_mut(&mut self) -> &mut BlobAttr {
234        match self {
235            Type::Blob(attr) => attr,
236            _ => panic!("type error"),
237        }
238    }
239
240    pub fn get_enum_def_mut(&mut self) -> &mut EnumDef {
241        match self {
242            Type::Enum(def) => def,
243            _ => panic!("type error"),
244        }
245    }
246
247    pub fn get_set_def_mut(&mut self) -> &mut SetDef {
248        match self {
249            Type::Set(def) => def,
250            _ => panic!("type error"),
251        }
252    }
253
254    pub fn get_geometry_attr_mut(&mut self) -> &mut GeometryAttr {
255        match self {
256            Type::Geometry(attr) => attr,
257            Type::Point(attr) => attr,
258            Type::LineString(attr) => attr,
259            Type::Polygon(attr) => attr,
260            Type::MultiPoint(attr) => attr,
261            Type::MultiLineString(attr) => attr,
262            Type::MultiPolygon(attr) => attr,
263            Type::GeometryCollection(attr) => attr,
264            _ => panic!("type error"),
265        }
266    }
267}
268
269impl NumericAttr {
270    pub fn m(m: u32) -> Self {
271        Self {
272            maximum: Some(m),
273            decimal: None,
274            unsigned: None,
275            zero_fill: None,
276        }
277    }
278
279    pub fn m_d(m: u32, d: u32) -> Self {
280        Self {
281            maximum: Some(m),
282            decimal: Some(d),
283            unsigned: None,
284            zero_fill: None,
285        }
286    }
287
288    pub fn unsigned(&mut self) -> &mut Self {
289        self.unsigned = Some(true);
290        self
291    }
292
293    pub fn zero_fill(&mut self) -> &mut Self {
294        self.zero_fill = Some(true);
295        self
296    }
297
298    pub fn take(&self) -> Self {
299        Self {
300            maximum: self.maximum,
301            decimal: self.decimal,
302            unsigned: self.unsigned,
303            zero_fill: self.zero_fill,
304        }
305    }
306}
307
308impl TimeAttr {
309    pub fn fsp(fsp: u32) -> Self {
310        Self {
311            fractional: Some(fsp),
312        }
313    }
314}
315
316impl StringAttr {
317    pub fn length(l: u32) -> Self {
318        Self {
319            length: Some(l),
320            charset: None,
321            collation: None,
322        }
323    }
324}
325
326impl BlobAttr {
327    pub fn length(l: u32) -> Self {
328        Self { length: Some(l) }
329    }
330}
331
332impl GeometryAttr {
333    pub fn srid(id: u32) -> Self {
334        Self { srid: Some(id) }
335    }
336}