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
use dao::Value;
use dao::value::Array;

#[derive(Debug, Serialize, PartialEq, Clone)]
pub enum SqlType {
    Bool,
    Tinyint,
    Smallint,
    Int,
    Bigint,

    Real,
    Float,
    Double,
    Numeric,

    Tinyblob,
    Mediumblob,
    Blob,
    Longblob,
    Varbinary,

    Char,
    Varchar,
    Tinytext,
    Mediumtext,
    Text,
    Json,
    TsVector,

    Uuid,
    Date,
    Timestamp,
    TimestampTz,

    Time,
    TimeTz,

    IpAddress,

    // enum list with the choices value
    Enum(String, Vec<String>),
    ArrayType(ArrayType),
}

#[derive(Debug, Serialize, PartialEq, Clone)]
pub enum ArrayType {
    Bool,
    Tinyint,
    Smallint,
    Int,
    Bigint,

    Real,
    Float,
    Double,
    Numeric,

    Char,
    Varchar,
    Tinytext,
    Mediumtext,
    Text,

    Uuid,
    Date,
    Timestamp,
    TimestampTz,

    Enum(String, Vec<String>),
}

impl SqlType {
    pub fn same_type(&self, value: &Value) -> bool {
        macro_rules! match_value{
            ($variant: ident) => {
                match *value {
                    Value::$variant(_) => true,
                    _ => false,
                }
            }
        }
        match *self {
            SqlType::Bool => match_value!(Bool),
            SqlType::Tinyint => match_value!(Tinyint),
            SqlType::Smallint => match_value!(Smallint),
            SqlType::Int => match_value!(Int),
            SqlType::Bigint => match_value!(Bigint),
            SqlType::Float => match_value!(Float),
            SqlType::Double => match_value!(Double),
            SqlType::Numeric => match_value!(BigDecimal),
            SqlType::Blob => match_value!(Blob),
            SqlType::Char => match_value!(Char),
            SqlType::Text | SqlType::Varchar => match_value!(Text),
            SqlType::Json => match_value!(Json),
            SqlType::Uuid => match_value!(Uuid),
            SqlType::Date => match_value!(Date),
            SqlType::Timestamp => match_value!(Timestamp),
            SqlType::TimestampTz => match_value!(Timestamp),
            SqlType::Enum(_, _) => match_value!(Text),
            SqlType::ArrayType(ArrayType::Text) => match *value {
                Value::Array(Array::Text(_)) => true,
                _ => false,
            },
            SqlType::ArrayType(ArrayType::Enum(_, _)) => match *value {
                Value::Array(Array::Text(_)) => true,
                _ => false,
            },
            SqlType::Real => match *value {
                Value::Float(_) => true,
                _ => false,
            },
            SqlType::ArrayType(ArrayType::Float) => match *value {
                Value::Array(Array::Float(_)) => true,
                _ => false,
            },
            SqlType::TsVector => match_value!(Text),
            _ => panic!("not yet implemented for checking {:?} to {:?}", self, value),
        }
    }
}