rbatis/plugin/table_sync/
mysql_mapper.rs

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
use crate::table_sync::ColumnMapper;
use rbs::Value;

pub struct MysqlTableMapper {}

impl Default for MysqlTableMapper {
    fn default() -> Self {
        MysqlTableMapper {}
    }
}

impl ColumnMapper for MysqlTableMapper {
    fn driver_type(&self) -> String {
        "mysql".to_string()
    }
    fn get_column_type(&self, _column: &str, v: &Value) -> String {
        match v {
            Value::Null => "NULL".to_string(),
            Value::Bool(_) => "TINYINT".to_string(),
            Value::I32(_) => "INT".to_string(),
            Value::I64(_) => "BIGINT".to_string(),
            Value::U32(_) => "INT".to_string(),
            Value::U64(_) => "BIGINT".to_string(),
            Value::F32(_) => "FLOAT".to_string(),
            Value::F64(_) => "DOUBLE".to_string(),
            Value::String(v) => {
                if v != "" {
                    if v.eq("id") {
                        return "TEXT".to_string();
                    }
                    v.to_string()
                } else {
                    "TEXT".to_string()
                }
            }
            Value::Binary(_) => "BLOB".to_string(),
            Value::Array(_) => "JSON".to_string(),
            Value::Map(_) => "JSON".to_string(),
            Value::Ext(t, _v) => match *t {
                "Date" => "DATE".to_string(),
                "DateTime" => "DATETIME".to_string(),
                "Time" => "TIME".to_string(),
                "Timestamp" => "TIMESTAMP".to_string(),
                "Decimal" => "DECIMAL".to_string(),
                "Json" => "JSON".to_string(),
                "Uuid" => "TEXT".to_string(),
                _ => "NULL".to_string(),
            },
        }
    }
}