-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
column.rs
34 lines (33 loc) · 1.14 KB
/
column.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
use crate::mysql::def::ColumnInfo;
use sea_query::{escape_string, Alias, ColumnDef};
use std::fmt::Write;
impl ColumnInfo {
pub fn write(&self) -> ColumnDef {
let mut col_def =
ColumnDef::new(Alias::new(self.name.as_str())).custom(self.col_type.clone());
if !self.null {
col_def = col_def.not_null();
}
if self.extra.auto_increment {
col_def = col_def.auto_increment();
}
let mut extras = Vec::new();
if let Some(default) = self.default.as_ref() {
let mut string = "".to_owned();
write!(&mut string, "DEFAULT {}", default.expr).unwrap();
extras.push(string);
}
if self.extra.on_update_current_timestamp {
extras.push("ON UPDATE CURRENT_TIMESTAMP".to_owned());
}
if !self.comment.is_empty() {
let mut string = "".to_owned();
write!(&mut string, "COMMENT '{}'", escape_string(&self.comment)).unwrap();
extras.push(string);
}
if !extras.is_empty() {
col_def = col_def.extra(extras.join(" "));
}
col_def
}
}