rs_aio_db 0.8.5

All in one (aka Aio) database with async support. Based on sqlite, bevy_reflect and tokio, includes a dead simple API to be used (no SQL needed just pure Rust). Comes with automigration.
Documentation
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use bevy_reflect::{GetField, ReflectMut, ReflectRef, Struct};
use log::debug;
use crate::db::{aio_query::{Next, Operator, QueryRowResult, QueryRowsResult}, models::{GenericValue, Schema}};

pub(crate) fn get_system_char_delimiter() -> &'static str {
     let os = std::env::consts::OS;
     if os == "windows" {
         "\\"
     }
     else if os == "linux" {
         "/"
     }
     else {
         panic!("OS not supported.");
     }
}

pub(crate) fn get_schema_from_generic<T:  Default + Struct>() -> Box<Vec<Schema>> {  
     let default_t = T::default();
     let default_t2 = T::default();
     let my_struct: Box<dyn Struct> = Box::new(default_t);

     let ReflectRef::Struct(reflected) = default_t2.reflect_ref() else { unreachable!() };

     let count = my_struct.iter_fields().count();
     let mut schema_vec: Box<Vec<Schema>> = Box::new(Vec::with_capacity(count));

     for (i, field) in my_struct.iter_fields().enumerate() {
          let field_name = reflected.name_at(i).unwrap();
          let field_type = field.reflect_type_ident().unwrap();
          debug!("Found field named '{}' of type '{}'", field_name, field_type);

          schema_vec.push(Schema {
               field_name: field_name.into(),
               field_type: field_type.into()
          });
     }
     
     return schema_vec;
}

pub(crate) fn get_values_from_generic<'a, T:  Default + Struct + Clone>(value: &'a T) -> Vec<GenericValue<'a>> {  
     let copied_value = value.clone();
     let my_struct: Box<dyn Struct> = Box::new(copied_value);

     let ReflectRef::Struct(reflected) = value.reflect_ref() else { unreachable!() };

     let count = my_struct.iter_fields().count();
     let mut schema_vec: Vec<GenericValue> = Vec::with_capacity(count);

     for (i, field) in my_struct.iter_fields().enumerate() {
          let field_name = reflected.name_at(i).unwrap();
          let field_value = reflected.field_at(i).unwrap();
          let field_type = field.reflect_type_ident().unwrap();
          
          debug!("Found field named '{}' with value '{:?}'", field_name, field_value);
    
          schema_vec.push(GenericValue {
               field_name: field_name.into(),
               field_value: field_value,
               field_type: field_type.into()
          });
     }
     
     return schema_vec;
}

pub(crate) fn set_values_from_row_result<'a, T:  Default + Struct + Clone>(row_result: &mut QueryRowResult<T>) -> Result<T, ()> {  
     let mut struct_mut2: Box<dyn Struct> = Box::new(T::default());
     let ReflectMut::Struct(reflected2) = struct_mut2.reflect_mut() else { unreachable!() };
     let struct_immutable: Box<dyn Struct> = Box::new(T::default());
     
     let mut t_struct = T::default();

     if let Some(row) = row_result.value.as_ref() {
          for (i, field) in struct_immutable.iter_fields().enumerate() {
               let field_type = field.reflect_type_ident().unwrap();
               let field_name = reflected2.name_at(i).clone().unwrap();

               match field_type {
                    "bool" => {
                         *t_struct.get_field_mut::<bool>(field_name).unwrap() = match row.get_field::<bool>(field_name).unwrap_or(&false) {
                              false => false,
                              true => true
                         };
                    },
                    "u8" => {
                         *t_struct.get_field_mut::<u8>(field_name).unwrap() = *row.get_field::<u8>(field_name).unwrap_or(&0) as u8;
                    },
                    "u16" => {
                         *t_struct.get_field_mut::<u16>(field_name).unwrap() = *row.get_field::<u16>(field_name).unwrap_or(&0) as u16;
                    },
                    "u32" => {
                         *t_struct.get_field_mut::<u32>(field_name).unwrap() = *row.get_field::<u32>(field_name).unwrap_or(&0) as u32;
                    },
                    "u64" => {
                         *t_struct.get_field_mut::<u64>(field_name).unwrap() = *row.get_field::<u64>(field_name).unwrap_or(&0) as u64;
                    },
                    "i8" => {
                         *t_struct.get_field_mut::<i8>(field_name).unwrap() = *row.get_field::<i8>(field_name).unwrap_or(&0) as i8;
                    },
                    "i16" => {
                         *t_struct.get_field_mut::<i16>(field_name).unwrap() = *row.get_field::<i16>(field_name).unwrap_or(&0) as i16;
                    },
                    "i32" => {
                         *t_struct.get_field_mut::<i32>(field_name).unwrap() = *row.get_field::<i32>(field_name).unwrap_or(&0) as i32;
                    },
                    "i64" => {
                         *t_struct.get_field_mut::<i64>(field_name).unwrap() = *row.get_field::<i64>(field_name).unwrap_or(&0) as i64;
                    },
                    "f32" => {
                         *t_struct.get_field_mut::<f32>(field_name).unwrap() = *row.get_field::<f32>(field_name).unwrap_or(&0.00f32) as f32;
                    },
                    "f64" => {
                         *t_struct.get_field_mut::<f64>(field_name).unwrap() = *row.get_field::<f64>(field_name).unwrap_or(&0.00f64) as f64;
                    },
                    "char" => {
                         *t_struct.get_field_mut::<char>(field_name).unwrap() = *row.get_field::<char>(field_name).unwrap_or(&' ') as char;
                    },
                    "String" => {
                          let mut buffer: Vec<u8> = Vec::new();
                         buffer.extend_from_slice(row.get_field::<String>(field_name).unwrap_or(&"".into()).as_bytes());
                         *t_struct.get_field_mut::<String>(field_name).unwrap() = String::from_utf8_lossy(&buffer).into_owned();
                    },
                    "Vec" => {
                         let mut buffer: Vec<u8> = Vec::new();
                         buffer.extend_from_slice(row.get_field::<String>(field_name).unwrap_or(&"".into()).as_bytes());
                         //let vec_u8_data = hex::decode(value).unwrap();
                         *t_struct.get_field_mut::<Vec<u8>>(field_name).unwrap() = buffer;
                    },
                    _ => panic!("{} type not supported.", field_type)
               }
          }
     } else {
          return Err(());
     }

     return Ok(t_struct);
}

pub(crate) fn set_values_from_many_rows_result<'a, T:  Default + Struct + Clone>(row_result: &mut QueryRowsResult<T>) -> Result<Vec<T>, ()> {  
     let mut struct_mut2: Box<dyn Struct> = Box::new(T::default());
     let ReflectMut::Struct(reflected2) = struct_mut2.reflect_mut() else { unreachable!() };
     let struct_immutable: Box<dyn Struct> = Box::new(T::default());

     let mut vec_t: Vec<T> = Vec::default();

     if let Some(rows) = row_result.value.as_ref() {
          let count = rows.len();
          let mut i = 0;

          while i < count {
               if let Some(row) = rows.get(i) {
                    let row = row.as_ref().unwrap();

                    let mut t_struct = T::default();

                    for (i, field) in struct_immutable.iter_fields().enumerate() {
                         let field_type = field.reflect_type_ident().unwrap();
                         let field_name = reflected2.name_at(i).clone().unwrap();
          
                         match field_type {
                              "bool" => {
                                   *t_struct.get_field_mut::<bool>(field_name).unwrap() = match row.get_field::<bool>(field_name).unwrap_or(&false) {
                                        false => false,
                                        true => true
                                   };
                              },
                              "u8" => {
                                   *t_struct.get_field_mut::<u8>(field_name).unwrap() = *row.get_field::<u8>(field_name).unwrap_or(&0) as u8;
                              },
                              "u16" => {
                                   *t_struct.get_field_mut::<u16>(field_name).unwrap() = *row.get_field::<u16>(field_name).unwrap_or(&0) as u16;
                              },
                              "u32" => {
                                   *t_struct.get_field_mut::<u32>(field_name).unwrap() = *row.get_field::<u32>(field_name).unwrap_or(&0) as u32;
                              },
                              "u64" => {
                                   *t_struct.get_field_mut::<u64>(field_name).unwrap() = *row.get_field::<u64>(field_name).unwrap_or(&0) as u64;
                              },
                              "i8" => {
                                   *t_struct.get_field_mut::<i8>(field_name).unwrap() = *row.get_field::<i8>(field_name).unwrap_or(&0) as i8;
                              },
                              "i16" => {
                                   *t_struct.get_field_mut::<i16>(field_name).unwrap() = *row.get_field::<i16>(field_name).unwrap_or(&0) as i16;
                              },
                              "i32" => {
                                   *t_struct.get_field_mut::<i32>(field_name).unwrap() = *row.get_field::<i32>(field_name).unwrap_or(&0) as i32;
                              },
                              "i64" => {
                                   *t_struct.get_field_mut::<i64>(field_name).unwrap() = *row.get_field::<i64>(field_name).unwrap_or(&0) as i64;
                              },
                              "f32" => {
                                   *t_struct.get_field_mut::<f32>(field_name).unwrap() = *row.get_field::<f32>(field_name).unwrap_or(&0.00f32) as f32;
                              },
                              "f64" => {
                                   *t_struct.get_field_mut::<f64>(field_name).unwrap() = *row.get_field::<f64>(field_name).unwrap_or(&0.00f64) as f64;
                              },
                              "char" => {
                                   *t_struct.get_field_mut::<char>(field_name).unwrap() = *row.get_field::<char>(field_name).unwrap_or(&' ') as char;
                              },
                              "String" => {
                                   let mut buffer: Vec<u8> = Vec::new();
                                   buffer.extend_from_slice(row.get_field::<String>(field_name).unwrap_or(&"".into()).as_bytes());
                                   *t_struct.get_field_mut::<String>(field_name).unwrap() = String::from_utf8_lossy(&buffer).into_owned();
                              },
                              "Vec" => {
                                   let mut buffer: Vec<u8> = Vec::new();
                                   buffer.extend_from_slice(row.get_field::<String>(field_name).unwrap_or(&"".into()).as_bytes());
                                   //let vec_u8_data = hex::decode(value).unwrap();
                                   *t_struct.get_field_mut::<Vec<u8>>(field_name).unwrap() = buffer;
                              },
                              _ => panic!("{} type not supported.", field_type)
                         }
                    }
          
                    vec_t.push(t_struct);
               } else {
                    break;
               }

               i += 1;
          }

          return Ok(vec_t);
     }

     return Ok(vec![]); // Return an empty vector if no rows are found.
}

pub(crate) fn get_next(next: &Next) -> String {
     if next == &Next::And {
          return "AND".into();
     }
     else {
          return "OR".into();
     }
}

pub(crate) fn get_end_value(value: &str, field_type: &str) -> String {
     let value = value.to_string();

     let end_value: String;

     if field_type == "String" {
          end_value = format!("'{}'", value);
     }
     else {
         end_value = value;
     }

     return end_value;
}

pub(crate) fn push_str_to_query_string(
     query_string: &mut String, 
     field_name: &str, 
     end_value: &str, 
     last_item: bool, 
     sql_operator: &str, 
     next: Option<&Next>) {
     if !last_item {
          let next = next.unwrap();
          let continuation = format!("{} {} {} {} ", field_name, sql_operator, end_value, get_next(next));
          query_string.push_str(continuation.as_str());
     } else {
          let continuation = format!("{} {} {}", field_name, sql_operator, end_value);
          query_string.push_str(continuation.as_str());
     }
}

pub(crate) fn push_contains_to_query_string(
     query_string: &mut String, 
     field_name: &str, 
     end_value: &str, 
     last_item: bool, 
     next: Option<&Next>) {
     let end_value = end_value.to_string();

     let like_end_value = format!("'%{}%'", end_value.replace("'", "''"));

     if !last_item {
          let next = next.unwrap();
          let continuation = format!("{} LIKE {} {} ", field_name, like_end_value, get_next(next));
          query_string.push_str(continuation.as_str());
     } else {
          let continuation = format!("{} LIKE {}", field_name, like_end_value);
          query_string.push_str(continuation.as_str());
     }
}

pub(crate) fn push_starts_with_to_query_string(
     query_string: &mut String, 
     field_name: &str, 
     end_value: &str, 
     last_item: bool, 
     next: Option<&Next>) {
     let end_value = end_value.to_string();

     let like_end_value = format!("'{}%'", end_value.replace("'", "''"));

     if !last_item {
          let next = next.unwrap();
          let continuation = format!("{} LIKE {} {} ", field_name, like_end_value, get_next(next));
          query_string.push_str(continuation.as_str());
     } else {
          let continuation = format!("{} LIKE {}", field_name, like_end_value);
          query_string.push_str(continuation.as_str());
     }
}

pub(crate) fn push_ends_with_to_query_string(
     query_string: &mut String, 
     field_name: &str, 
     end_value: &str, 
     last_item: bool, 
     next: Option<&Next>) {
     let end_value = end_value.to_string();

     let like_end_value = format!("'%{}'", end_value.replace("'", "''"));

     if !last_item {
          let next = next.unwrap();
          let continuation = format!("{} LIKE {} {} ", field_name, like_end_value, get_next(next));
          query_string.push_str(continuation.as_str());
     } else {
          let continuation = format!("{} LIKE {}", field_name, like_end_value);
          query_string.push_str(continuation.as_str());
     }
}

pub(crate) fn query_match_operators(
     operator: &Operator, 
     query_string: &mut String, 
     field_name: &str, 
     field_type: &str, 
     last_item: bool, 
     next: Option<&Next>) {
     match operator {
          Operator::Eq(value) => {
               let end_value = get_end_value(&value, field_type);
               push_str_to_query_string(
                    query_string, 
                    field_name,
                    &end_value,
                    last_item, 
                    "==",
                    next
               );
          },
          Operator::Ne(value) => {
               let end_value = get_end_value(&value, field_type);
               push_str_to_query_string(
                    query_string, 
                    field_name,
                    &end_value,
                    last_item, 
                    "<>",
                    next
               );
          },
          Operator::Gt(value) => {
               let end_value = get_end_value(&value, field_type);
               push_str_to_query_string(
                    query_string, 
                    field_name,
                    &end_value,
                    last_item, 
                    ">",
                    next
               );
          },
          Operator::Lt(value) => {
               let end_value = get_end_value(&value, field_type);
               push_str_to_query_string(
                    query_string, 
                    field_name,
                    &end_value,
                    last_item, 
                    "<",
                    next
               );
          },
          Operator::Ge(value) => {
               let end_value = get_end_value(&value, field_type);
               push_str_to_query_string(
                    query_string, 
                    field_name,
                    &end_value,
                    last_item, 
                    ">=",
                    next
               );
          },
          Operator::Le(value) => {
               let end_value = get_end_value(&value, field_type);
               push_str_to_query_string(
                    query_string, 
                    field_name,
                    &end_value,
                    last_item, 
                    "<=",
                    next
               );
          },
          Operator::Contains(value) => {
               push_contains_to_query_string(
                    query_string, 
                    field_name,
                    &value,
                    last_item,
                    next
               );
          },
          Operator::StartsWith(value) => {
               push_starts_with_to_query_string(
                    query_string, 
                    field_name,
                    &value,
                    last_item,
                    next
               );
          },
          Operator::EndsWith(value) => {
               push_ends_with_to_query_string(
                    query_string, 
                    field_name,
                    &value,
                    last_item,
                    next
               );
          }
     }
}