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
use rustorm_dao::ColumnName;
use rustorm_dao::FromDao;
use rustorm_dao::TableName;
use types::SqlType;
use uuid::Uuid;
#[derive(Debug, PartialEq, Clone)]
pub struct Column {
pub table: TableName,
pub name: ColumnName,
pub comment: Option<String>,
pub specification: ColumnSpecification,
pub stat: Option<ColumnStat>,
}
impl Column {
pub fn is_autoincrement(&self) -> bool {
self.specification
.constraints
.iter()
.any(|c| *c == ColumnConstraint::AutoIncrement)
}
pub fn default_is_generated_uuid(&self) -> bool {
self.specification.constraints.iter().any(|c| match *c {
ColumnConstraint::DefaultValue(ref literal) => match *literal {
Literal::UuidGenerateV4 => true,
_ => false,
},
_ => false,
})
}
pub fn is_not_null(&self) -> bool {
self.specification.constraints.iter().any(|c| match *c {
ColumnConstraint::NotNull => true,
_ => false,
})
}
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),
AutoIncrement,
}
#[derive(Debug, PartialEq, Clone)]
pub enum Literal {
Bool(bool),
Null,
Integer(i64),
Double(f64),
UuidGenerateV4,
Uuid(Uuid),
String(String),
Blob(Vec<u8>),
CurrentTime,
CurrentDate,
CurrentTimestamp,
ArrayInt(Vec<i64>),
ArrayFloat(Vec<f64>),
ArrayString(Vec<String>),
}
#[derive(Debug, PartialEq, FromDao, Clone)]
pub struct ColumnStat {
pub avg_width: i32,
pub n_distinct: f32,
}
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))
}
}