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
use super::schema::{table_name_to_model_name, RelationDef, RelationType, TableSchema};
use std::collections::HashMap;
pub struct RelationAnalyzer;
impl RelationAnalyzer {
/// Menganalisis seluruh tabel dalam skema dan mengisi relasi deklaratif
pub fn analyze(tables: &mut [TableSchema]) {
// Peta nama tabel -> nama model
let table_to_model: HashMap<String, String> = tables
.iter()
.map(|t| (t.name.clone(), t.model_name.clone()))
.collect();
// 1. Deteksi Pivot Tables (Many-to-Many)
let mut pivot_relations: Vec<(String, RelationDef)> = Vec::new();
for table in tables.iter_mut() {
// Syarat tabel pivot: memiliki tepat 2 Foreign Keys dan kolomnya relatif sedikit (<= 5)
if table.foreign_keys.len() == 2 && table.columns.len() <= 5 {
table.is_pivot = true;
let fk1 = &table.foreign_keys[0];
let fk2 = &table.foreign_keys[1];
if let (Some(m1), Some(m2)) = (
table_to_model.get(&fk1.foreign_table),
table_to_model.get(&fk2.foreign_table),
) {
// Relasi untuk Model 1 -> Model 2
pivot_relations.push((
fk1.foreign_table.clone(),
RelationDef {
relation_type: RelationType::BelongsToMany,
target_model: m2.clone(),
foreign_key: fk1.column_name.clone(),
pivot_table: Some(table.name.clone()),
local_key: Some(fk2.column_name.clone()),
},
));
// Relasi untuk Model 2 -> Model 1
pivot_relations.push((
fk2.foreign_table.clone(),
RelationDef {
relation_type: RelationType::BelongsToMany,
target_model: m1.clone(),
foreign_key: fk2.column_name.clone(),
pivot_table: Some(table.name.clone()),
local_key: Some(fk1.column_name.clone()),
},
));
}
}
}
// Terapkan pivot relations
for (tbl_name, rel) in pivot_relations {
if let Some(target_table) = tables.iter_mut().find(|t| t.name == tbl_name) {
if !target_table.relations.iter().any(|r| {
r.relation_type == RelationType::BelongsToMany
&& r.target_model == rel.target_model
&& r.pivot_table == rel.pivot_table
}) {
target_table.relations.push(rel);
}
}
}
// 2. Deteksi 1-to-N (BelongsTo dan HasMany) untuk tabel non-pivot
let mut one_to_many_pairs: Vec<(String, String, String)> = Vec::new(); // (parent_table, child_table, fk_column)
for table in tables.iter_mut() {
if table.is_pivot {
continue;
}
for fk in &table.foreign_keys {
let target_model = table_to_model
.get(&fk.foreign_table)
.cloned()
.unwrap_or_else(|| table_name_to_model_name(&fk.foreign_table));
// Tambahkan belongs_to pada model anak (child)
if !table.relations.iter().any(|r| {
r.relation_type == RelationType::BelongsTo
&& r.target_model == target_model
&& r.foreign_key == fk.column_name
}) {
table.relations.push(RelationDef {
relation_type: RelationType::BelongsTo,
target_model,
foreign_key: fk.column_name.clone(),
pivot_table: None,
local_key: None,
});
}
one_to_many_pairs.push((
fk.foreign_table.clone(),
table.name.clone(),
fk.column_name.clone(),
));
}
}
// Terapkan has_many pada model induk (parent)
for (parent_tbl, child_tbl, fk_col) in one_to_many_pairs {
let child_model = table_to_model
.get(&child_tbl)
.cloned()
.unwrap_or_else(|| table_name_to_model_name(&child_tbl));
if let Some(parent) = tables.iter_mut().find(|t| t.name == parent_tbl) {
if !parent.relations.iter().any(|r| {
r.relation_type == RelationType::HasMany
&& r.target_model == child_model
&& r.foreign_key == fk_col
}) {
parent.relations.push(RelationDef {
relation_type: RelationType::HasMany,
target_model: child_model,
foreign_key: fk_col,
pivot_table: None,
local_key: None,
});
}
}
}
}
}