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
use crate::postgres::def::{ArbitraryPrecisionNumericAttr, ColumnInfo, Type};
use core::num;
use sea_query::{escape_string, Alias, ColumnDef, Iden};
use std::{default, fmt::Write};

impl ColumnInfo {
    pub fn write(&self) -> ColumnDef {
        let mut self_copy = self.clone();
        let mut col_def = ColumnDef::new(Alias::new(self.name.as_str()));
        let mut extras: Vec<String> = Vec::new();
        if let Some(default) = self.default.as_ref() {
            if default.0.starts_with("nextval") {
                match self.col_type {
                    Type::SmallInt => {
                        self_copy.col_type = Type::SmallSerial;
                    }
                    Type::Integer => {
                        self_copy.col_type = Type::Serial;
                    }
                    Type::BigInt => {
                        self_copy.col_type = Type::BigSerial;
                    }
                    _ => {}
                }
            } else {
                let mut string = "".to_owned();
                write!(&mut string, "DEFAULT {}", default.0).unwrap();
                extras.push(string);
            }
        }
        col_def = self_copy.write_col_type(col_def);
        if self.not_null.is_some() {
            col_def = col_def.not_null();
        }
        if !extras.is_empty() {
            col_def = col_def.extra(extras.join(" "));
        }
        col_def
    }

    pub fn write_col_type(&self, mut col_def: ColumnDef) -> ColumnDef {
        match &self.col_type {
            Type::SmallInt => {
                col_def = col_def.small_integer();
            }
            Type::Integer => {
                col_def = col_def.integer();
            }
            Type::BigInt => {
                col_def = col_def.big_integer();
            }
            Type::Decimal(num_attr) | Type::Numeric(num_attr) => {
                if num_attr.precision.is_none() & num_attr.scale.is_none() {
                    col_def = col_def.decimal();
                } else {
                    col_def = col_def.decimal_len(
                        num_attr.precision.unwrap_or(0) as u32,
                        num_attr.scale.unwrap_or(0) as u32,
                    );
                }
            }
            Type::Real => {
                col_def = col_def.float();
            }
            Type::DoublePrecision => {
                col_def = col_def.double();
            }
            Type::SmallSerial => {
                col_def = col_def.small_integer().auto_increment();
            }
            Type::Serial => {
                col_def = col_def.integer().auto_increment();
            }
            Type::BigSerial => {
                col_def = col_def.big_integer().auto_increment();
            }
            Type::Money => {
                col_def = col_def.money();
            }
            Type::Varchar(string_attr) => {
                col_def = match string_attr.length {
                    Some(length) => col_def.string_len(length.into()),
                    None => col_def.string(),
                };
            }
            Type::Char(string_attr) => {
                col_def = match string_attr.length {
                    Some(length) => col_def.char_len(length.into()),
                    None => col_def.char(),
                };
            }
            Type::Text => {
                col_def = col_def.text();
            }
            Type::Bytea => {
                col_def = col_def.binary();
            }
            Type::Timestamp(time_attr) => {
                col_def = match time_attr.precision {
                    Some(precision) => col_def.timestamp_len(precision.into()),
                    None => col_def.timestamp(),
                };
            }
            Type::TimestampWithTimeZone(time_attr) => {
                col_def = match time_attr.precision {
                    Some(precision) => col_def.timestamp_len(precision.into()),
                    None => col_def.timestamp(),
                };
            }
            Type::Date => {
                col_def = col_def.date();
            }
            Type::Time(time_attr) => {
                col_def = match time_attr.precision {
                    Some(precision) => col_def.time_len(precision.into()),
                    None => col_def.time(),
                };
            }
            Type::TimeWithTimeZone(time_attr) => {
                col_def = match time_attr.precision {
                    Some(precision) => col_def.time_len(precision.into()),
                    None => col_def.time(),
                };
            }
            Type::Interval(time_attr) => {}
            Type::Boolean => {
                col_def = col_def.boolean();
            }
            Type::Point => {
                col_def = col_def.custom(Alias::new("point"));
            }
            Type::Line => {
                col_def = col_def.custom(Alias::new("line"));
            }
            Type::Lseg => {
                col_def = col_def.custom(Alias::new("lseg"));
            }
            Type::Box => {
                col_def = col_def.custom(Alias::new("box"));
            }
            Type::Path => {
                col_def = col_def.custom(Alias::new("path"));
            }
            Type::Polygon => {
                col_def = col_def.custom(Alias::new("ploygon"));
            }
            Type::Circle => {
                col_def = col_def.custom(Alias::new("circle"));
            }
            Type::Cidr => {
                col_def = col_def.custom(Alias::new("cidr"));
            }
            Type::Inet => {
                col_def = col_def.custom(Alias::new("inet"));
            }
            Type::MacAddr => {
                col_def = col_def.custom(Alias::new("macaddr"));
            }
            Type::MacAddr8 => {
                col_def = col_def.custom(Alias::new("macaddr8"));
            }
            Type::Bit(bit_attr) => {
                let mut str = String::new();
                write!(str, "bit");
                if bit_attr.length.is_some() {
                    write!(str, "(");
                    if let Some(length) = bit_attr.length {
                        write!(str, "{}", length);
                    }
                    write!(str, ")");
                }
                col_def = col_def.custom(Alias::new(&str));
            }
            Type::TsVector => {
                col_def = col_def.custom(Alias::new("tsvector"));
            }
            Type::TsQuery => {
                col_def = col_def.custom(Alias::new("tsquery"));
            }
            Type::Uuid => {
                col_def = col_def.uuid();
            }
            Type::Xml => {
                col_def = col_def.custom(Alias::new("xml"));
            }
            Type::Json => {
                col_def = col_def.custom(Alias::new("json"));
            }
            Type::Array => {
                col_def = col_def.custom(Alias::new("array"));
            }
            Type::Int4Range => {
                col_def = col_def.custom(Alias::new("int4range"));
            }
            Type::Int8Range => {
                col_def = col_def.custom(Alias::new("int8range"));
            }
            Type::NumRange => {
                col_def = col_def.custom(Alias::new("numrange"));
            }
            Type::TsRange => {
                col_def = col_def.custom(Alias::new("tsrange"));
            }
            Type::TsTzRange => {
                col_def = col_def.custom(Alias::new("tstzrange"));
            }
            Type::DateRange => {
                col_def = col_def.custom(Alias::new("daterange"));
            }
            Type::PgLsn => {
                col_def = col_def.custom(Alias::new("pg_lsn"));
            }
            Type::Unknown(s) => {
                col_def = col_def.custom(Alias::new(s));
            }
        };
        col_def
    }
}