-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels_levels.go
49 lines (41 loc) · 1.03 KB
/
models_levels.go
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
package mysql
type modelsLevels []modelLevel
type modelLevel struct {
name string
level int
}
func (a modelsLevels) Len() int { return len(a) }
func (a modelsLevels) Less(i, j int) bool {
if a[i].level == a[j].level {
return a[i].name < a[j].name
} else {
return a[i].level < a[j].level
}
}
func (a modelsLevels) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (s *MySQL) getModelsLevels() modelsLevels {
s.modelsMtx.RLock()
defer s.modelsMtx.RUnlock()
res := make(modelsLevels, 0, len(s.models))
for name, _ := range s.models {
res = append(res, modelLevel{
name: name,
level: s.getModelLevel(name, 0),
})
}
return res
}
func (s *MySQL) getModelLevel(tableName string, curLevel int) int {
maxLevel := curLevel
for _, extModel := range s.models[tableName].GetRelations() {
relation := s.models[tableName].GetRelation(extModel)
if relation.IsBack {
continue
}
extLevel := s.getModelLevel(relation.ExtModel.GetId(), curLevel+1)
if extLevel > maxLevel {
maxLevel = extLevel
}
}
return maxLevel
}