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)]
17#[non_exhaustive]
18pub enum ForeignKeyAction {
19 Restrict,
20 Cascade,
21 SetNull,
22 NoAction,
23 SetDefault,
24}
25
26impl ForeignKeyAction {
27 #[doc(hidden)]
28 pub fn variant_name(&self) -> &'static str {
30 match self {
31 Self::Restrict => "Restrict",
32 Self::Cascade => "Cascade",
33 Self::SetNull => "SetNull",
34 Self::NoAction => "NoAction",
35 Self::SetDefault => "SetDefault",
36 }
37 }
38}
39
40impl TableForeignKey {
41 pub fn new() -> Self {
43 Self::default()
44 }
45
46 pub fn name<T>(&mut self, name: T) -> &mut Self
48 where
49 T: Into<String>,
50 {
51 self.name = Some(name.into());
52 self
53 }
54
55 pub fn from_tbl<T>(&mut self, table: T) -> &mut Self
57 where
58 T: IntoTableRef,
59 {
60 self.table = Some(table.into_table_ref());
61 self
62 }
63
64 pub fn to_tbl<R>(&mut self, ref_table: R) -> &mut Self
66 where
67 R: IntoTableRef,
68 {
69 self.ref_table = Some(ref_table.into_table_ref());
70 self
71 }
72
73 pub fn from_col<T>(&mut self, column: T) -> &mut Self
75 where
76 T: IntoIden,
77 {
78 self.columns.push(column.into_iden());
79 self
80 }
81
82 pub fn to_col<R>(&mut self, ref_column: R) -> &mut Self
84 where
85 R: IntoIden,
86 {
87 self.ref_columns.push(ref_column.into_iden());
88 self
89 }
90
91 pub fn on_delete(&mut self, action: ForeignKeyAction) -> &mut Self {
93 self.on_delete = Some(action);
94 self
95 }
96
97 pub fn on_update(&mut self, action: ForeignKeyAction) -> &mut Self {
99 self.on_update = Some(action);
100 self
101 }
102
103 pub fn get_ref_table(&self) -> Option<&TableRef> {
104 self.ref_table.as_ref()
105 }
106
107 pub fn get_columns(&self) -> Vec<String> {
108 self.columns.iter().map(|col| col.to_string()).collect()
109 }
110
111 pub fn get_ref_columns(&self) -> Vec<String> {
112 self.ref_columns
113 .iter()
114 .map(|ref_col| ref_col.to_string())
115 .collect()
116 }
117
118 pub fn get_on_delete(&self) -> Option<ForeignKeyAction> {
119 self.on_delete
120 }
121
122 pub fn get_on_update(&self) -> Option<ForeignKeyAction> {
123 self.on_update
124 }
125
126 pub fn take(&mut self) -> Self {
127 Self {
128 name: self.name.take(),
129 table: self.table.take(),
130 ref_table: self.ref_table.take(),
131 columns: std::mem::take(&mut self.columns),
132 ref_columns: std::mem::take(&mut self.ref_columns),
133 on_delete: self.on_delete.take(),
134 on_update: self.on_update.take(),
135 }
136 }
137}