forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.go
79 lines (67 loc) · 1.38 KB
/
filters.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
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
package table
import (
"fmt"
"github.com/yaoapp/gou"
)
// loadFilters 加载查询过滤器
func (table *Table) loadFilters() {
if table.Bind.Model == "" {
return
}
defaults := getDefaultFilters(table.Bind.Model)
for name, filter := range table.Filters {
defaults[name] = filter
}
table.Filters = defaults
}
// getDefaultFilters 读取数据模型索引字段的过滤器
func getDefaultFilters(name string) map[string]Filter {
mod := gou.Select(name)
cmap := mod.Columns
filters := map[string]Filter{}
for _, index := range mod.MetaData.Indexes {
for _, col := range index.Columns {
if _, has := cmap[col]; !has {
continue
}
// primary,unique,index,match
switch index.Type {
case "index", "match":
cmap[col].Index = true
break
case "unique":
if len(index.Columns) == 1 {
cmap[col].Unique = true
}
break
case "primary":
cmap[col].Primary = true
break
}
}
}
for name, col := range cmap {
if col.Type != "ID" && !col.Index && !col.Unique && !col.Primary {
continue
}
vcol, has := elms[col.Type]
if !has {
continue
}
label := col.Label
if label == "" {
label = col.Comment
}
if label == "" {
label = name
}
filter := Filter{
Label: label,
Bind: fmt.Sprintf("where.%s.eq", name),
Input: vcol.Edit,
}
filters[name] = filter
filters[label] = filter
}
return filters
}