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
use crate::{
    types::SqlType,
    ColumnName,
    FromDao,
    TableName,
};
use uuid::Uuid;

#[derive(Debug, PartialEq, Clone)]
pub struct ColumnDef {
    pub table: TableName,
    pub name: ColumnName,
    pub comment: Option<String>,
    pub specification: ColumnSpecification,
    pub stat: Option<ColumnStat>,
}

impl ColumnDef {
    /// check all the column constraint if any has AutoIncrement
    pub fn is_autoincrement(&self) -> bool { self.autoincrement_sequence_name().is_some() }

    /// get the sequnce name of this autoincrement column
    pub fn autoincrement_sequence_name(&self) -> Option<&String> {
        self.specification.constraints.iter().find_map(|c| {
            match &c {
                ColumnConstraint::AutoIncrement(sequence_name) => sequence_name.as_ref(),
                _ => None,
            }
        })
    }

    /// check if any of the column constraint default is generated from uuid
    pub fn default_is_generated_uuid(&self) -> bool {
        self.specification
            .constraints
            .iter()
            .any(|c| matches!(*c, ColumnConstraint::DefaultValue(Literal::UuidGenerateV4)))
    }

    pub fn is_not_null(&self) -> bool {
        self.specification
            .constraints
            .iter()
            .any(|c| matches!(*c, ColumnConstraint::NotNull))
    }

    pub fn get_sql_type(&self) -> SqlType { self.specification.sql_type.clone() }

    pub fn cast_as(&self) -> Option<SqlType> { self.get_sql_type().cast_as() }

    pub fn has_generated_default(&self) -> bool {
        self.specification.constraints.iter().any(|c| {
            match *c {
                ColumnConstraint::DefaultValue(ref literal) => {
                    match *literal {
                        Literal::Bool(_) => true,
                        Literal::Null => false,
                        Literal::Integer(_) => true,
                        Literal::Double(_) => true,
                        Literal::UuidGenerateV4 => true,
                        Literal::Uuid(_) => true,
                        Literal::String(_) => false,
                        Literal::Blob(_) => false,
                        Literal::CurrentTime => true,
                        Literal::CurrentDate => true,
                        Literal::CurrentTimestamp => true,
                        Literal::ArrayInt(_) => false,
                        Literal::ArrayFloat(_) => false,
                        Literal::ArrayString(_) => false,
                    }
                }
                _ => false,
            }
        })
    }
}

#[derive(Debug, PartialEq, Clone)]
pub struct ColumnSpecification {
    pub sql_type: SqlType,
    pub capacity: Option<Capacity>,
    pub constraints: Vec<ColumnConstraint>,
}

impl ColumnSpecification {
    pub fn get_limit(&self) -> Option<i32> {
        match self.capacity {
            Some(ref capacity) => capacity.get_limit(),
            None => None,
        }
    }
}

#[derive(Debug, PartialEq, Clone)]
pub enum Capacity {
    Limit(i32),
    Range(i32, i32),
}

impl Capacity {
    fn get_limit(&self) -> Option<i32> {
        match *self {
            Capacity::Limit(limit) => Some(limit),
            Capacity::Range(_whole, _decimal) => None,
        }
    }
}

#[derive(Debug, PartialEq, Clone)]
pub enum ColumnConstraint {
    NotNull,
    DefaultValue(Literal),
    /// the string contains the sequence name of this serial column
    AutoIncrement(Option<String>),
}

#[derive(Debug, PartialEq, Clone)]
pub enum Literal {
    Bool(bool),
    Null,
    Integer(i64),
    Double(f64),
    UuidGenerateV4, // pg: uuid_generate_v4();
    Uuid(Uuid),
    String(String),
    Blob(Vec<u8>),
    CurrentTime,      // pg: now()
    CurrentDate,      //pg: today()
    CurrentTimestamp, // pg: now()
    ArrayInt(Vec<i64>),
    ArrayFloat(Vec<f64>),
    ArrayString(Vec<String>),
}

/// column stat, derive from pg_stats
#[derive(Debug, PartialEq, FromDao, Clone)]
pub struct ColumnStat {
    pub avg_width: i32, /* average width of the column, (the number of characters) */
    //most_common_values: Value,//top 5 most common values
    pub n_distinct: f32, // the number of distinct values of these column
}

impl From<i64> for Literal {
    fn from(i: i64) -> Self { Literal::Integer(i) }
}

impl From<String> for Literal {
    fn from(s: String) -> Self { Literal::String(s) }
}

impl<'a> From<&'a str> for Literal {
    fn from(s: &'a str) -> Self { Literal::String(String::from(s)) }
}