ormdantic_schema/
relationships.rs1#[derive(Debug, Clone, PartialEq, Eq)]
2pub struct RelationshipDef {
3 field: String,
4 target_table: String,
5 target_field: String,
6 cardinality: RelationshipCardinality,
7 back_reference: Option<String>,
8 local_columns: Vec<String>,
9 remote_columns: Vec<String>,
10 direction: RelationshipDirection,
11 uselist: bool,
12 nullable: bool,
13 secondary_table: Option<String>,
14 cascade: Vec<CascadeAction>,
15 loader_strategy: LoaderStrategy,
16}
17
18impl RelationshipDef {
19 pub fn new(
20 field: impl Into<String>,
21 target_table: impl Into<String>,
22 target_field: impl Into<String>,
23 cardinality: RelationshipCardinality,
24 ) -> Self {
25 Self {
26 field: field.into(),
27 target_table: target_table.into(),
28 target_field: target_field.into(),
29 cardinality,
30 back_reference: None,
31 local_columns: Vec::new(),
32 remote_columns: Vec::new(),
33 direction: match cardinality {
34 RelationshipCardinality::One => RelationshipDirection::ManyToOne,
35 RelationshipCardinality::Many => RelationshipDirection::OneToMany,
36 },
37 uselist: cardinality == RelationshipCardinality::Many,
38 nullable: true,
39 secondary_table: None,
40 cascade: Vec::new(),
41 loader_strategy: LoaderStrategy::Lazy,
42 }
43 }
44
45 pub fn with_back_reference(mut self, back_reference: impl Into<String>) -> Self {
46 self.back_reference = Some(back_reference.into());
47 self
48 }
49
50 pub fn with_columns(mut self, local_columns: Vec<String>, remote_columns: Vec<String>) -> Self {
51 self.local_columns = local_columns;
52 self.remote_columns = remote_columns;
53 self
54 }
55
56 pub fn with_direction(mut self, direction: RelationshipDirection) -> Self {
57 self.direction = direction;
58 self
59 }
60
61 pub fn uselist(mut self, uselist: bool) -> Self {
62 self.uselist = uselist;
63 self
64 }
65
66 pub fn nullable(mut self, nullable: bool) -> Self {
67 self.nullable = nullable;
68 self
69 }
70
71 pub fn secondary_table(mut self, secondary_table: impl Into<String>) -> Self {
72 self.secondary_table = Some(secondary_table.into());
73 self
74 }
75
76 pub fn cascade(mut self, cascade: Vec<CascadeAction>) -> Self {
77 self.cascade = cascade;
78 self
79 }
80
81 pub fn loader_strategy(mut self, loader_strategy: LoaderStrategy) -> Self {
82 self.loader_strategy = loader_strategy;
83 self
84 }
85
86 pub fn field(&self) -> &str {
87 &self.field
88 }
89
90 pub fn target_table(&self) -> &str {
91 &self.target_table
92 }
93
94 pub fn target_field(&self) -> &str {
95 &self.target_field
96 }
97
98 pub fn cardinality(&self) -> &RelationshipCardinality {
99 &self.cardinality
100 }
101
102 pub fn back_reference(&self) -> Option<&str> {
103 self.back_reference.as_deref()
104 }
105
106 pub fn local_columns(&self) -> &[String] {
107 &self.local_columns
108 }
109
110 pub fn remote_columns(&self) -> &[String] {
111 &self.remote_columns
112 }
113
114 pub fn direction(&self) -> &RelationshipDirection {
115 &self.direction
116 }
117
118 pub fn is_uselist(&self) -> bool {
119 self.uselist
120 }
121
122 pub fn is_nullable(&self) -> bool {
123 self.nullable
124 }
125
126 pub fn secondary_table_name(&self) -> Option<&str> {
127 self.secondary_table.as_deref()
128 }
129
130 pub fn cascade_actions(&self) -> &[CascadeAction] {
131 &self.cascade
132 }
133
134 pub fn loader_strategy_ref(&self) -> &LoaderStrategy {
135 &self.loader_strategy
136 }
137}
138
139#[derive(Debug, Clone, Copy, PartialEq, Eq)]
140pub enum RelationshipCardinality {
141 One,
142 Many,
143}
144
145#[derive(Debug, Clone, Copy, PartialEq, Eq)]
146pub enum RelationshipDirection {
147 OneToOne,
148 OneToMany,
149 ManyToOne,
150 ManyToMany,
151}
152
153#[derive(Debug, Clone, Copy, PartialEq, Eq)]
154pub enum CascadeAction {
155 SaveUpdate,
156 Merge,
157 Delete,
158 DeleteOrphan,
159 RefreshExpire,
160 Expunge,
161}
162
163#[derive(Debug, Clone, Copy, PartialEq, Eq)]
164pub enum LoaderStrategy {
165 Lazy,
166 Joined,
167 SelectIn,
168 Raise,
169 NoLoad,
170}