reinhardt_query/query/
foreign_key.rs1use crate::types::{DynIden, ForeignKeyAction, IntoIden, IntoTableRef, TableRef};
8
9#[derive(Debug, Clone)]
23pub struct ForeignKey;
24
25impl ForeignKey {
26 pub fn create() -> ForeignKeyCreateStatement {
28 ForeignKeyCreateStatement::new()
29 }
30}
31
32#[derive(Debug, Clone, Default)]
38pub struct ForeignKeyCreateStatement {
39 pub(crate) name: Option<DynIden>,
40 pub(crate) from_tbl: Option<TableRef>,
41 pub(crate) from_cols: Vec<DynIden>,
42 pub(crate) to_tbl: Option<TableRef>,
43 pub(crate) to_cols: Vec<DynIden>,
44 pub(crate) on_delete: Option<ForeignKeyAction>,
45 pub(crate) on_update: Option<ForeignKeyAction>,
46}
47
48impl ForeignKeyCreateStatement {
49 pub fn new() -> Self {
51 Self::default()
52 }
53
54 pub fn name<T>(&mut self, name: T) -> &mut Self
56 where
57 T: IntoIden,
58 {
59 self.name = Some(name.into_iden());
60 self
61 }
62
63 pub fn from_tbl<T>(&mut self, tbl: T) -> &mut Self
65 where
66 T: IntoTableRef,
67 {
68 self.from_tbl = Some(tbl.into_table_ref());
69 self
70 }
71
72 pub fn from_col<C>(&mut self, col: C) -> &mut Self
74 where
75 C: IntoIden,
76 {
77 self.from_cols.push(col.into_iden());
78 self
79 }
80
81 pub fn to_tbl<T>(&mut self, tbl: T) -> &mut Self
83 where
84 T: IntoTableRef,
85 {
86 self.to_tbl = Some(tbl.into_table_ref());
87 self
88 }
89
90 pub fn to_col<C>(&mut self, col: C) -> &mut Self
92 where
93 C: IntoIden,
94 {
95 self.to_cols.push(col.into_iden());
96 self
97 }
98
99 pub fn on_delete(&mut self, action: ForeignKeyAction) -> &mut Self {
101 self.on_delete = Some(action);
102 self
103 }
104
105 pub fn on_update(&mut self, action: ForeignKeyAction) -> &mut Self {
107 self.on_update = Some(action);
108 self
109 }
110}