forked from YaoApp/gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.filter.go
132 lines (111 loc) · 2.4 KB
/
model.filter.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
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
131
132
package gou
import (
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/maps"
"github.com/yaoapp/xun/dbal"
)
// FliterIn 输入前过滤解码
func (mod *Model) FliterIn(row maps.MapStrAny) {
for name, value := range row {
column, has := mod.Columns[name]
// 删除无效字段
if !has {
row.Del(name)
continue
}
// 过滤输入信息
column.FliterIn(value, row)
}
}
// Filterselect 选择字段
func (mod *Model) Filterselect(alias string, columns []interface{}, cmap map[string]ColumnMap, exportPrefix string) []interface{} {
res := []interface{}{}
if cmap == nil {
cmap = map[string]ColumnMap{}
}
for _, col := range columns {
if _, ok := col.(dbal.Expression); ok {
res = append(res, col)
continue
}
name, ok := col.(string)
if !ok {
continue
}
column, has := mod.Columns[name]
if !has {
continue
}
// alias.field
field := name
varName := name
if alias != "" {
field = alias + "." + name
varName = alias + "_" + name
}
// 字段映射表
export := column.Name
if exportPrefix != "" {
export = exportPrefix + "." + column.Name
}
cmap[varName] = ColumnMap{
Model: mod,
Column: column,
Export: export,
}
// 加密字段
if column.Crypt == "AES" && column.model.Driver == "mysql" {
icrypt := SelectCrypt(column.Crypt)
raw, err := icrypt.Decode(field)
if err != nil {
exception.Err(err, 500).Throw()
}
raw = raw + " as " + varName
res = append(res, dbal.Raw(raw))
} else {
raw := field + " as " + varName
res = append(res, raw)
}
}
return res
}
// FliterWhere 选项
func (mod *Model) FliterWhere(alias string, col interface{}) interface{} {
if _, ok := col.(dbal.Expression); ok {
return col
}
name, ok := col.(string)
if !ok {
return col
}
column, has := mod.Columns[name]
if !has {
return col
}
// alias.field
if alias != "" {
name = alias + "." + name
}
// 加密字段
if column.Crypt == "AES" && column.model.Driver == "mysql" {
icrypt := SelectCrypt(column.Crypt)
raw, err := icrypt.Decode(name)
if err != nil {
exception.Err(err, 500).Throw()
}
return dbal.Raw(raw)
}
return name
}
// FliterOut 输出前过滤解码
func (mod *Model) FliterOut(row maps.MapStrAny) {
for name, value := range row {
column, has := mod.Columns[name]
// 删除无效字段
if !has {
continue
}
// 过滤输入信息
column.FliterOut(value, row)
}
}