teo_runtime/database/mysql/
type.rs1use serde::Serialize;
2use teo_parser::ast::schema::Schema;
3use teo_parser::r#type::reference::Reference;
4
5#[derive(Debug, Serialize, Clone, PartialEq, Eq, Hash)]
6pub struct MySQLEnum {
7 pub variants: Vec<String>,
8}
9
10impl MySQLEnum {
11 pub fn build(schema: &Schema, reference: &Reference) -> Self {
12 let e = schema.find_top_by_path(reference.path()).unwrap().as_enum().unwrap();
13 MySQLEnum {
14 variants: e.members().map(|m| m.identifier().name().to_string()).collect()
15 }
16 }
17}
18
19#[derive(Debug, Serialize, Clone, PartialEq, Eq, Hash)]
20pub enum MySQLType {
21 VarChar(i32),
22 Text,
23 Char(i32),
24 TinyText,
25 MediumText,
26 LongText,
27 Bit(Option<i32>),
28 TinyInt(Option<i32>, bool),
30 Int(Option<i32>, bool),
31 SmallInt(Option<i32>, bool),
32 MediumInt(Option<i32>, bool),
33 BigInt(Option<i32>, bool),
34 Year,
35 Float,
36 Double,
37 Decimal(i32, i32),
38 DateTime(i32),
39 Date,
40 Time(i32),
41 Timestamp(i32),
42 Json,
43 LongBlob,
44 Binary,
45 VarBinary,
46 TinyBlob,
47 Blob,
48 MediumBlob,
49 Enum(MySQLEnum),
50}
51
52impl MySQLType {
53
54
55 pub fn is_var_char(&self) -> bool {
56 self.as_var_char().is_some()
57 }
58
59 pub fn as_var_char(&self) -> Option<i32> {
60 match self {
61 MySQLType::VarChar(len) => Some(*len),
62 _ => None,
63 }
64 }
65
66 pub fn is_text(&self) -> bool {
67 match self {
68 MySQLType::Text => true,
69 _ => false,
70 }
71 }
72
73 pub fn is_char(&self) -> bool {
74 self.as_char().is_some()
75 }
76
77 pub fn as_char(&self) -> Option<i32> {
78 match self {
79 MySQLType::Char(len) => Some(*len),
80 _ => None,
81 }
82 }
83
84 pub fn is_tiny_text(&self) -> bool {
85 match self {
86 MySQLType::TinyText => true,
87 _ => false,
88 }
89 }
90
91 pub fn is_medium_text(&self) -> bool {
92 match self {
93 MySQLType::MediumText => true,
94 _ => false,
95 }
96 }
97
98 pub fn is_long_text(&self) -> bool {
99 match self {
100 MySQLType::LongText => true,
101 _ => false,
102 }
103 }
104
105 pub fn is_bit(&self) -> bool {
106 self.as_bit().is_some()
107 }
108
109 pub fn as_bit(&self) -> Option<Option<i32>> {
110 match self {
111 MySQLType::Bit(len) => Some(*len),
112 _ => None,
113 }
114 }
115
116 pub fn is_tiny_int(&self) -> bool {
117 self.as_tiny_int().is_some()
118 }
119
120 pub fn as_tiny_int(&self) -> Option<(Option<i32>, bool)> {
121 match self {
122 MySQLType::TinyInt(len, signed) => Some((*len, *signed)),
123 _ => None,
124 }
125 }
126
127 pub fn is_int(&self) -> bool {
128 self.as_int().is_some()
129 }
130
131 pub fn as_int(&self) -> Option<(Option<i32>, bool)> {
132 match self {
133 MySQLType::Int(len, signed) => Some((*len, *signed)),
134 _ => None,
135 }
136 }
137
138 pub fn is_small_int(&self) -> bool {
139 self.as_small_int().is_some()
140 }
141
142 pub fn as_small_int(&self) -> Option<(Option<i32>, bool)> {
143 match self {
144 MySQLType::SmallInt(len, signed) => Some((*len, *signed)),
145 _ => None,
146 }
147 }
148
149 pub fn is_medium_int(&self) -> bool {
150 self.as_medium_int().is_some()
151 }
152
153 pub fn as_medium_int(&self) -> Option<(Option<i32>, bool)> {
154 match self {
155 MySQLType::MediumInt(len, signed) => Some((*len, *signed)),
156 _ => None,
157 }
158 }
159
160 pub fn is_big_int(&self) -> bool {
161 self.as_big_int().is_some()
162 }
163
164 pub fn as_big_int(&self) -> Option<(Option<i32>, bool)> {
165 match self {
166 MySQLType::BigInt(len, signed) => Some((*len, *signed)),
167 _ => None,
168 }
169 }
170
171 pub fn is_year(&self) -> bool {
172 match self {
173 MySQLType::Year => true,
174 _ => false,
175 }
176 }
177
178 pub fn is_float(&self) -> bool {
179 match self {
180 MySQLType::Float => true,
181 _ => false,
182 }
183 }
184
185 pub fn is_double(&self) -> bool {
186 match self {
187 MySQLType::Double => true,
188 _ => false,
189 }
190 }
191
192 pub fn is_decimal(&self) -> bool {
193 self.as_decimal().is_some()
194 }
195
196 pub fn as_decimal(&self) -> Option<(i32, i32)> {
197 match self {
198 MySQLType::Decimal(p, s) => Some((*p, *s)),
199 _ => None,
200 }
201 }
202
203 pub fn is_datetime(&self) -> bool {
204 self.as_datetime().is_some()
205 }
206
207 pub fn as_datetime(&self) -> Option<i32> {
208 match self {
209 MySQLType::DateTime(len) => Some(*len),
210 _ => None,
211 }
212 }
213
214 pub fn is_date(&self) -> bool {
215 match self {
216 MySQLType::Date => true,
217 _ => false,
218 }
219 }
220
221 pub fn is_time(&self) -> bool {
222 self.as_time().is_some()
223 }
224
225 pub fn as_time(&self) -> Option<i32> {
226 match self {
227 MySQLType::Time(len) => Some(*len),
228 _ => None,
229 }
230 }
231
232 pub fn is_timestamp(&self) -> bool {
233 self.as_timestamp().is_some()
234 }
235
236 pub fn as_timestamp(&self) -> Option<i32> {
237 match self {
238 MySQLType::Timestamp(len) => Some(*len),
239 _ => None,
240 }
241 }
242
243 pub fn is_json(&self) -> bool {
244 match self {
245 MySQLType::Json => true,
246 _ => false,
247 }
248 }
249
250 pub fn is_binary(&self) -> bool {
251 match self {
252 MySQLType::Binary => true,
253 _ => false,
254 }
255 }
256
257 pub fn is_var_binary(&self) -> bool {
258 match self {
259 MySQLType::VarBinary => true,
260 _ => false,
261 }
262 }
263
264 pub fn is_tiny_blob(&self) -> bool {
265 match self {
266 MySQLType::TinyBlob => true,
267 _ => false,
268 }
269 }
270
271 pub fn is_blob(&self) -> bool {
272 match self {
273 MySQLType::Blob => true,
274 _ => false,
275 }
276 }
277
278 pub fn is_medium_blob(&self) -> bool {
279 match self {
280 MySQLType::MediumBlob => true,
281 _ => false,
282 }
283 }
284
285 pub fn is_long_blob(&self) -> bool {
286 match self {
287 MySQLType::LongBlob => true,
288 _ => false,
289 }
290 }
291
292 pub fn is_enum(&self) -> bool {
293 self.as_enum().is_some()
294 }
295
296 pub fn as_enum(&self) -> Option<&MySQLEnum> {
297 match self {
298 MySQLType::Enum(e) => Some(e),
299 _ => None,
300 }
301 }
302}