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
use crate::types::*;
#[derive(Debug, Clone)]
pub struct TableForeignKey {
pub(crate) name: Option<String>,
pub(crate) table: Option<DynIden>,
pub(crate) ref_table: Option<DynIden>,
pub(crate) columns: Vec<DynIden>,
pub(crate) ref_columns: Vec<DynIden>,
pub(crate) on_delete: Option<ForeignKeyAction>,
pub(crate) on_update: Option<ForeignKeyAction>,
}
#[derive(Debug, Clone, Copy)]
pub enum ForeignKeyAction {
Restrict,
Cascade,
SetNull,
NoAction,
SetDefault,
}
impl Default for TableForeignKey {
fn default() -> Self {
Self::new()
}
}
impl TableForeignKey {
pub fn new() -> Self {
Self {
name: None,
table: None,
ref_table: None,
columns: Vec::new(),
ref_columns: Vec::new(),
on_delete: None,
on_update: None,
}
}
pub fn name(&mut self, name: &str) -> &mut Self {
self.name = Some(name.into());
self
}
pub fn from_tbl<T>(&mut self, table: T) -> &mut Self
where
T: IntoIden,
{
self.table = Some(table.into_iden());
self
}
pub fn to_tbl<R>(&mut self, ref_table: R) -> &mut Self
where
R: IntoIden,
{
self.ref_table = Some(ref_table.into_iden());
self
}
pub fn from_col<T>(&mut self, column: T) -> &mut Self
where
T: IntoIden,
{
self.columns.push(column.into_iden());
self
}
pub fn to_col<R>(&mut self, ref_column: R) -> &mut Self
where
R: IntoIden,
{
self.ref_columns.push(ref_column.into_iden());
self
}
pub fn on_delete(&mut self, action: ForeignKeyAction) -> &mut Self {
self.on_delete = Some(action);
self
}
pub fn on_update(&mut self, action: ForeignKeyAction) -> &mut Self {
self.on_update = Some(action);
self
}
pub fn get_ref_table(&self) -> Option<String> {
self.ref_table.as_ref().map(|ref_tbl| ref_tbl.to_string())
}
pub fn get_columns(&self) -> Vec<String> {
self.columns.iter().map(|col| col.to_string()).collect()
}
pub fn get_ref_columns(&self) -> Vec<String> {
self.ref_columns
.iter()
.map(|ref_col| ref_col.to_string())
.collect()
}
pub fn get_on_delete(&self) -> Option<ForeignKeyAction> {
self.on_delete
}
pub fn get_on_update(&self) -> Option<ForeignKeyAction> {
self.on_update
}
pub fn take(&mut self) -> Self {
Self {
name: self.name.take(),
table: self.table.take(),
ref_table: self.ref_table.take(),
columns: std::mem::take(&mut self.columns),
ref_columns: std::mem::take(&mut self.ref_columns),
on_delete: self.on_delete.take(),
on_update: self.on_update.take(),
}
}
}