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
use super::{BaseBuilder, Builder, BuilderExt, ChildBuilder, ReferenceBuilder};
#[derive(Default)]
pub struct ForeignKeyBuilder {
pub(crate) base: BaseBuilder,
pub(crate) symbol: String,
pub(crate) columns: Vec<String>,
pub(crate) actions: Vec<String>,
pub(crate) reference: ReferenceBuilder,
}
impl ChildBuilder for ForeignKeyBuilder {
fn parent(&self) -> &BaseBuilder {
&self.base
}
fn parent_mut(&mut self) -> &mut BaseBuilder {
&mut self.base
}
}
impl ForeignKeyBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn symbol(&mut self, symbol: impl Into<String>) -> &mut Self {
self.symbol = symbol.into();
self
}
pub fn column(&mut self, column: impl Into<String>) -> &mut Self {
self.columns.push(column.into());
self
}
pub fn reference(&mut self, reference: ReferenceBuilder) -> &mut Self {
self.reference = reference;
self
}
pub fn on_delete(&mut self, action: impl Into<String>) -> &mut Self {
let mut action = action.into();
action.insert_str(0, "ON DELETE ");
self.actions.push(action);
self
}
pub fn on_update(&mut self, action: impl Into<String>) -> &mut Self {
let mut action = action.into();
action.insert_str(0, "ON UPDATE ");
self.actions.push(action);
self
}
}
impl Builder for ForeignKeyBuilder {
fn build(self) -> (String, Vec<String>) {
let mut base = self.base;
if !self.symbol.is_empty() {
base.ident(self.symbol).pad();
}
base.push_str("FOREIGN KEY");
let columns = self.columns;
base.nested(|b| {
b.ident_comma(columns);
});
base.pad().join(self.reference);
for action in self.actions {
base.pad().push_str(action);
}
base.build()
}
}