-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitlter_expression.go
244 lines (224 loc) · 5.7 KB
/
fitlter_expression.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package db
import (
"database/sql/driver"
"fmt"
"gorm.io/gorm"
"gorm.io/gorm/utils"
"reflect"
"strconv"
"strings"
"time"
"unicode"
)
type Operation int
const (
OperationPlace Operation = iota
Equal
NotEqual
Greater
Less
Between
GreaterOrEqual
LessOrEqual
IsNotNull
IsNull
In
NotIn
LIKE
)
func (m Operation) SQL() string {
switch m {
case Equal:
return "= ?"
case NotEqual:
return "!= ?"
case Greater:
return "> ?"
case Less:
return "< ?"
case Between:
return "BETWEEN ? AND ?"
case GreaterOrEqual:
return ">= ?"
case LessOrEqual:
return "<= ?"
case IsNull:
return "IS NULL"
case IsNotNull:
return "IS NOT NULL"
case In:
return "IN (?)"
case NotIn:
return "NOT IN (?)"
case LIKE:
return "LIKE ?"
}
return ""
}
func (m Operation) String() string {
switch m {
case Equal:
return " = "
case In:
return " IN "
case Between:
return " BETWEEN "
case Greater:
return " > "
case Less:
return " < "
case NotEqual:
return " != "
case GreaterOrEqual:
return " >= "
case LessOrEqual:
return " <= "
case IsNull:
return " IS NULL"
case IsNotNull:
return " IS NOT NULL"
case NotIn:
return " NOT IN "
}
return "="
}
type FilterExpression struct {
Field string `json:"field"`
Operation Operation `json:"method"`
Value []interface{} `json:"value"`
}
type FilterExpressions []FilterExpression
func (f FilterExpressions) Build() string {
var conditions []string
for _, filter := range f {
filter.Field = strings.TrimSpace(filter.Field)
if filter.Field == "" || filter.Operation == 0 || len(filter.Value) == 0 {
continue
}
switch filter.Operation {
case Greater, Less, Equal, NotEqual, GreaterOrEqual, LessOrEqual:
conditions = append(conditions, filter.Field+filter.Operation.String()+ConvertParams(filter.Value[0], "'"))
case In, NotIn:
var vars = make([]string, len(filter.Value))
for idx, v := range filter.Value {
vars[idx] = ConvertParams(v, "'")
}
conditions = append(conditions, filter.Field+filter.Operation.String()+"("+strings.Join(vars, ",")+")")
case Between:
if len(filter.Value) < 2 {
continue
}
var vars = make([]string, len(filter.Value))
for idx, v := range filter.Value {
vars[idx] = ConvertParams(v, "'")
}
conditions = append(conditions, filter.Field+filter.Operation.String()+vars[0]+" AND "+vars[1])
case LIKE:
conditions = append(conditions, filter.Field+filter.Operation.String()+ConvertParams(filter.Value[0], "'"))
case IsNull, IsNotNull:
conditions = append(conditions, filter.Field+filter.Operation.String())
}
}
if len(conditions) == 0 {
return ""
}
return strings.Join(conditions, " AND ")
}
func ConvertParams(v interface{}, escaper string) string {
switch v := v.(type) {
case bool:
return strconv.FormatBool(v)
case time.Time:
return escaper + v.Format(TmFmtWithMS) + escaper
case *time.Time:
if v != nil {
return escaper + v.Format(TmFmtWithMS) + escaper
} else {
return NullStr
}
case driver.Valuer:
reflectValue := reflect.ValueOf(v)
if v != nil && reflectValue.IsValid() && ((reflectValue.Kind() == reflect.Ptr && !reflectValue.IsNil()) || reflectValue.Kind() != reflect.Ptr) {
r, _ := v.Value()
ConvertParams(r, escaper)
} else {
return NullStr
}
case fmt.Stringer:
reflectValue := reflect.ValueOf(v)
if v != nil && reflectValue.IsValid() && ((reflectValue.Kind() == reflect.Ptr && !reflectValue.IsNil()) || reflectValue.Kind() != reflect.Ptr) {
return escaper + strings.Replace(fmt.Sprintf("%v", v), escaper, "\\"+escaper, -1) + escaper
} else {
return NullStr
}
case []byte:
if isPrintable(v) {
return escaper + strings.Replace(string(v), escaper, "\\"+escaper, -1) + escaper
} else {
return escaper + "<binary>" + escaper
}
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return utils.ToString(v)
case float64, float32:
return fmt.Sprintf("%.6f", v)
case string:
return escaper + strings.Replace(v, escaper, "\\"+escaper, -1) + escaper
default:
rv := reflect.ValueOf(v)
if v == nil || !rv.IsValid() || rv.Kind() == reflect.Ptr && rv.IsNil() {
return NullStr
} else if valuer, ok := v.(driver.Valuer); ok {
v, _ = valuer.Value()
ConvertParams(v, escaper)
} else if rv.Kind() == reflect.Ptr && !rv.IsZero() {
ConvertParams(reflect.Indirect(rv).Interface(), escaper)
} else {
for _, t := range convertableTypes {
if rv.Type().ConvertibleTo(t) {
return ConvertParams(rv.Convert(t).Interface(), escaper)
}
}
return escaper + strings.Replace(fmt.Sprint(v), escaper, "\\"+escaper, -1) + escaper
}
}
return ""
}
var convertableTypes = []reflect.Type{reflect.TypeOf(time.Time{}), reflect.TypeOf(false), reflect.TypeOf([]byte{})}
func isPrintable(s []byte) bool {
for _, r := range s {
if !unicode.IsPrint(rune(r)) {
return false
}
}
return true
}
func (f FilterExpressions) BuildORM(odb *gorm.DB) *gorm.DB {
var scopes []func(db *gorm.DB) *gorm.DB
for _, filter := range f {
filter.Field = strings.TrimSpace(filter.Field)
if filter.Field == "" || filter.Operation == 0 || len(filter.Value) == 0 {
continue
}
scopes = append(scopes, func(db *gorm.DB) *gorm.DB {
return db.Where(filter.Field+" "+filter.Operation.SQL(), filter.Value...)
})
}
return odb.Scopes(scopes...)
}
func (f FilterExpressions) BuildSQL() (string, []interface{}) {
var builder strings.Builder
var vars []interface{}
for i, filter := range f {
if filter.Field == "" || filter.Operation == 0 || len(filter.Value) == 0 {
continue
}
builder.WriteString(filter.Field)
builder.WriteByte(' ')
builder.WriteString(filter.Operation.SQL())
if i < len(f) {
builder.WriteString(" AND")
}
vars = append(vars, filter.Value...)
}
return "", nil
}