This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
where.go
227 lines (211 loc) · 5.04 KB
/
where.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
215
216
217
218
219
220
221
222
223
224
225
226
227
package gormx
import (
"fmt"
"reflect"
"gorm.io/gorm/clause"
)
func buildSQLWhere(where interface{}) (expression clause.Expression, err error) {
defer func() {
if r := recover(); r != nil {
err = packPanicError(r)
}
}()
rv, rt, err := getValueAndType(where)
if err != nil {
return nil, err
}
sqlType, err := parseStructType(rt)
if err != nil {
return nil, err
}
return buildClauseExpression(rv, sqlType, true)
}
func buildClauseExpression(rv reflect.Value, sqlType *structType, joinAnd bool) (result clause.Expression, err error) {
expressions := []clause.Expression{}
for _, name := range sqlType.Names {
column := sqlType.Fields[name] // 前置步骤检查过,一定存在
// 计算字段的值
data := rv.FieldByName(column.Name)
// 字段的值是 nil 直接忽略 不做处理
if isEmptyValue(data) {
continue
}
if data.Kind() == reflect.Ptr {
data = data.Elem()
}
inter := data.Interface()
queryExprBuilder, err := getQueryExpr(column.QueryExpr)
if err != nil {
return nil, err
}
if column.OrType != nil {
orType, err := parseStructType(column.OrType)
if err != nil {
return nil, err
}
if data.Kind() == reflect.Slice {
list := []clause.Expression{}
for i := 0; i < data.Len(); i++ {
or, err := buildClauseExpression(data.Index(i), orType, false)
if err != nil {
return nil, err
} else if or != nil {
list = append(list, or)
}
}
if len(list) > 0 {
expressions = append(expressions, joinExpression(list, false))
}
} else {
or, err := buildClauseExpression(data, orType, false)
if err != nil {
return nil, err
} else if or != nil {
expressions = append(expressions, or)
}
}
} else {
and := queryExprBuilder(column.Column, inter)
if and != nil {
expressions = append(expressions, and)
}
}
}
return joinExpression(expressions, joinAnd), nil
}
func joinExpression(exprs []clause.Expression, joinAnd bool) clause.Expression {
if len(exprs) == 1 {
return exprs[0]
}
if joinAnd {
return clause.And(exprs...)
}
return clause.Or(exprs...)
}
const (
operatorOr = "or" // clause.OrConditions
operatorIn = "in" // clause.IN
operatorNin = "not in" // notIn // 无 clause.NIN
operatorGt = ">" // clause.Gt
operatorGte = ">=" // clause.Gte
operatorLt = "<" // clause.Lt
operatorLte = "<=" // clause.Lte
operatorEq = "=" // clause.Eq
operatorNeq = "!=" // clause.Neq
operatorLike = "like" // clause.Like
operatorNull = "null" // clause.Null
)
type (
buildExpression func(field string, data interface{}) clause.Expression
)
var queryExprMap = map[string]struct {
build buildExpression
}{
operatorLt: {
build: func(field string, data interface{}) clause.Expression {
return clause.Lt{
Column: clause.Column{Name: field},
Value: data,
}
},
},
operatorLte: {
build: func(field string, data interface{}) clause.Expression {
return clause.Lte{
Column: clause.Column{Name: field},
Value: data,
}
},
},
operatorEq: {
build: func(field string, data interface{}) clause.Expression {
return clause.Eq{
Column: clause.Column{Name: field},
Value: data,
}
},
},
"": {
build: func(field string, data interface{}) clause.Expression {
return clause.Eq{
Column: clause.Column{Name: field},
Value: data,
}
},
},
operatorNeq: {
build: func(field string, data interface{}) clause.Expression {
return clause.Neq{
Column: clause.Column{Name: field},
Value: data,
}
},
},
operatorGt: {
build: func(field string, data interface{}) clause.Expression {
return clause.Gt{
Column: clause.Column{Name: field},
Value: data,
}
},
},
operatorGte: {
build: func(field string, data interface{}) clause.Expression {
return clause.Gte{
Column: clause.Column{Name: field},
Value: data,
}
},
},
operatorNull: {
build: func(field string, data interface{}) clause.Expression {
switch v := data.(type) {
case bool:
if v {
return clause.Eq{
Column: clause.Column{Name: field},
Value: nil,
}
} else {
return clause.Neq{
Column: clause.Column{Name: field},
Value: nil,
}
}
}
return nil
},
},
operatorIn: {
build: func(field string, data interface{}) clause.Expression {
return clause.IN{
Column: clause.Column{Name: field},
Values: interfaceToSlice(data),
}
},
},
operatorNin: {
build: func(field string, data interface{}) clause.Expression {
return notIn{clause.IN{
Column: clause.Column{Name: field},
Values: interfaceToSlice(data),
}}
},
},
operatorLike: {
build: func(field string, data interface{}) clause.Expression {
return clause.Like{
Column: clause.Column{Name: field},
Value: data.(string),
}
},
},
operatorOr: {},
}
func getQueryExpr(queryExprString string) (buildExpression, error) {
queryExpr, ok := queryExprMap[queryExprString]
if !ok {
return nil, fmt.Errorf("query_expr '%s' invalid", queryExprString)
}
return queryExpr.build, nil
}