sea_query/foreign_key/
common.rs1use crate::types::*;
2
3#[derive(Default, Debug, Clone)]
5pub struct TableForeignKey {
6 pub(crate) name: Option<String>,
7 pub(crate) table: Option<TableRef>,
8 pub(crate) ref_table: Option<TableRef>,
9 pub(crate) columns: Vec<DynIden>,
10 pub(crate) ref_columns: Vec<DynIden>,
11 pub(crate) on_delete: Option<ForeignKeyAction>,
12 pub(crate) on_update: Option<ForeignKeyAction>,
13}
14
15#[derive(Debug, Clone, Copy)]
17pub enum ForeignKeyAction {
18 Restrict,
19 Cascade,
20 SetNull,
21 NoAction,
22 SetDefault,
23}
24
25impl TableForeignKey {
26 pub fn new() -> Self {
28 Self::default()
29 }
30
31 pub fn name<T>(&mut self, name: T) -> &mut Self
33 where
34 T: Into<String>,
35 {
36 self.name = Some(name.into());
37 self
38 }
39
40 pub fn from_tbl<T>(&mut self, table: T) -> &mut Self
42 where
43 T: IntoTableRef,
44 {
45 self.table = Some(table.into_table_ref());
46 self
47 }
48
49 pub fn to_tbl<R>(&mut self, ref_table: R) -> &mut Self
51 where
52 R: IntoTableRef,
53 {
54 self.ref_table = Some(ref_table.into_table_ref());
55 self
56 }
57
58 pub fn from_col<T>(&mut self, column: T) -> &mut Self
60 where
61 T: IntoIden,
62 {
63 self.columns.push(column.into_iden());
64 self
65 }
66
67 pub fn to_col<R>(&mut self, ref_column: R) -> &mut Self
69 where
70 R: IntoIden,
71 {
72 self.ref_columns.push(ref_column.into_iden());
73 self
74 }
75
76 pub fn on_delete(&mut self, action: ForeignKeyAction) -> &mut Self {
78 self.on_delete = Some(action);
79 self
80 }
81
82 pub fn on_update(&mut self, action: ForeignKeyAction) -> &mut Self {
84 self.on_update = Some(action);
85 self
86 }
87
88 pub fn get_ref_table(&self) -> Option<&TableRef> {
89 self.ref_table.as_ref()
90 }
91
92 pub fn get_columns(&self) -> Vec<String> {
93 self.columns.iter().map(|col| col.to_string()).collect()
94 }
95
96 pub fn get_ref_columns(&self) -> Vec<String> {
97 self.ref_columns
98 .iter()
99 .map(|ref_col| ref_col.to_string())
100 .collect()
101 }
102
103 pub fn get_on_delete(&self) -> Option<ForeignKeyAction> {
104 self.on_delete
105 }
106
107 pub fn get_on_update(&self) -> Option<ForeignKeyAction> {
108 self.on_update
109 }
110
111 pub fn take(&mut self) -> Self {
112 Self {
113 name: self.name.take(),
114 table: self.table.take(),
115 ref_table: self.ref_table.take(),
116 columns: std::mem::take(&mut self.columns),
117 ref_columns: std::mem::take(&mut self.ref_columns),
118 on_delete: self.on_delete.take(),
119 on_update: self.on_update.take(),
120 }
121 }
122}