-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_option.go
214 lines (188 loc) · 5.45 KB
/
session_option.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package database
import (
"fmt"
"gorm.io/gorm"
"reflect"
"strings"
)
const (
sessionOptionOther = 0
sessionOptionLimitation = 1
sessionOptionCountSelect = 2
sessionOptionSelect = 3
sessionOptionGroupBy = 4
sessionOptionOrderBy = 5
sessionOptionUpdateCols = 6
sessionOptionTable = 7
sessionOptionJoin = 8
sessionOptionNoCount = 9
sessionOptionMatch = 10
)
type SessionOption struct {
Type int
Process func(builder *gorm.DB) *gorm.DB
}
type SessionOptionList []SessionOption
func ParseSessionOption(data interface{}) []SessionOption {
result := make([]SessionOption, 0)
elem := GetElem(reflect.ValueOf(data))
sortBy := ""
groupBy := ""
page := 0
pageSize := 10
hasPage := true
hasCount := true
for i := 0; i != elem.NumField(); i++ {
field := elem.Type().Field(i)
fieldValue := GetElem(elem.Field(i))
tag := field.Tag.Get("session")
opt := parseSessionOptionTag(tag)
if opt.name == "-" {
continue
}
if omitEmpty(fieldValue, opt) {
continue
}
if opt.name == "sort_by" {
sortBy = loadString(fieldValue, opt)
continue
}
if opt.name == "group_by" {
groupBy = loadString(fieldValue, opt)
continue
}
if opt.name == "no_count" {
hasCount = false
continue
}
if opt.name == "page" {
hasPage = true
page = loadInt(fieldValue, opt)
continue
}
if opt.name == "page_size" {
hasPage = true
if page == 0 {
page = 1
}
pageSize = loadInt(fieldValue, opt)
continue
}
if opt.name == "select" {
switch fieldValue.Kind() {
case reflect.String:
result = append(result, SessionCondition.WithSelect(fieldValue.String()))
case reflect.Slice:
fields := make([]interface{}, 0)
for _, v := range fieldValue.Interface().([]string) {
fields = append(fields, v)
}
if len(fields) == 1 {
result = append(result, SessionCondition.WithSelect(fields[0]))
} else if len(fields) > 1 {
result = append(result, SessionCondition.WithSelect(fields[0], fields[1:]...))
}
}
continue
}
switch opt.op {
case "equal":
result = append(result, SessionCondition.WithEqual(opt.name, fieldValue.Interface()))
case "not_equal":
result = append(result, SessionCondition.WithNotEqual(opt.name, fieldValue.Interface()))
case "like":
result = append(result, SessionCondition.WithLike(opt.name, loadString(fieldValue, opt)))
case "like_or":
result = append(result, SessionCondition.WithLikeOr(loadString(fieldValue, opt), strings.Split(opt.name, "&")...))
case "in":
in := make([]interface{}, fieldValue.Len())
for i := 0; i != fieldValue.Len(); i++ {
in[i] = fieldValue.Index(i).Interface()
}
result = append(result, SessionCondition.WithIn(opt.name, in...))
case "not_in":
in := make([]interface{}, fieldValue.Len())
for i := 0; i != fieldValue.Len(); i++ {
in[i] = fieldValue.Index(i).Interface()
}
result = append(result, SessionCondition.WithNotIn(opt.name, in...))
case "lt":
result = append(result, SessionCondition.WithLt(opt.name, fieldValue.Interface()))
case "lte":
result = append(result, SessionCondition.WithLte(opt.name, fieldValue.Interface()))
case "gt":
result = append(result, SessionCondition.WithGt(opt.name, fieldValue.Interface()))
case "gte":
result = append(result, SessionCondition.WithGte(opt.name, fieldValue.Interface()))
case "json_contains":
result = append(
result,
SessionCondition.WithWhere(
fmt.Sprintf("JSON_CONTAINS(%s, ?)", opt.name),
fieldValue.Interface(),
),
)
case "match":
result = append(result, SessionCondition.WithMatch(strings.Split(opt.name, "&"), fieldValue.Interface()))
default:
result = append(result, SessionCondition.WithWhere(opt.op, fieldValue.Interface()))
}
}
if len(sortBy) != 0 {
result = append(result, SessionCondition.WithSort(sortBy))
}
if len(groupBy) != 0 {
result = append(result, SessionCondition.WithGroupBy(groupBy))
}
if hasPage && hasCount {
result = append(result, SessionCondition.WithPage(page, pageSize))
}
if !hasCount {
result = append(result, SessionCondition.WithNoCount())
}
return result
}
func processSessionOptionForCount(searchOpt SessionOption, session *gorm.DB) *gorm.DB {
if searchOpt.Type == sessionOptionUpdateCols {
return session
}
if searchOpt.Type != sessionOptionLimitation && searchOpt.Type != sessionOptionSelect &&
searchOpt.Type != sessionOptionOrderBy && searchOpt.Type != sessionOptionGroupBy {
session = searchOpt.Process(session)
}
return session
}
func processSessionOption(searchOpt SessionOption, session *gorm.DB) *gorm.DB {
if searchOpt.Type == sessionOptionUpdateCols {
return session
}
if searchOpt.Type != sessionOptionCountSelect {
return searchOpt.Process(session)
}
return session
}
//func processCondSessionOptionForCount(session *gorm.DB, option SessionOption) builder.Cond {
// switch option.Type {
// case sessionOptionUpdateCols, sessionOptionLimitation, sessionOptionSelect, sessionOptionOrderBy, sessionOptionGroupBy:
// return nil
// case sessionOptionOther:
// return option.Cond()
// default:
// logrus.Debug(token.Serialize.Encode(option))
// option.Process(session)
// return nil
// }
//}
//
//func processCondSessionOption(session *gorm.DB, option SessionOption) builder.Cond {
// switch option.Type {
// case sessionOptionUpdateCols, sessionOptionCountSelect:
// return nil
// case sessionOptionOther:
// return option.Cond()
// default:
// option.Process(session)
// return nil
// }
//}
//