forked from go-dev-frame/sponge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_condition.go
More file actions
380 lines (340 loc) · 9.09 KB
/
Copy pathquery_condition.go
File metadata and controls
380 lines (340 loc) · 9.09 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Package query is a library of custom condition queries, support for complex conditional paging queries.
package query
import (
"fmt"
"strconv"
"strings"
"time"
)
const (
// Eq equal
Eq = "eq"
// Neq not equal
Neq = "neq"
// Gt greater than
Gt = "gt"
// Gte greater than or equal
Gte = "gte"
// Lt less than
Lt = "lt"
// Lte less than or equal
Lte = "lte"
// Like fuzzy lookup
Like = "like"
// In include
In = "in"
// NotIN not include
NotIN = "notin"
// IsNull is null
IsNull = "isnull"
// IsNotNull is not null
IsNotNull = "isnotnull"
// AND logic and
AND string = "and"
// OR logic or
OR string = "or"
)
var expMap = map[string]string{
Eq: " = ",
Neq: " <> ",
Gt: " > ",
Gte: " >= ",
Lt: " < ",
Lte: " <= ",
Like: " LIKE ",
In: " IN ",
NotIN: " NOT IN ",
IsNull: " IS NULL ",
IsNotNull: " IS NOT NULL ",
"=": " = ",
"!=": " <> ",
">": " > ",
">=": " >= ",
"<": " < ",
"<=": " <= ",
"not in": " NOT IN ",
"is null": " IS NULL ",
"is not null": " IS NOT NULL ",
}
var logicMap = map[string]string{
AND: " AND ",
OR: " OR ",
"&": " AND ",
"&&": " AND ",
"|": " OR ",
"||": " OR ",
"AND": " AND ",
"OR": " OR ",
"and:(": " AND ",
"and:)": " AND ",
"or:(": " OR ",
"or:)": " OR ",
}
// ---------------------------------------------------------------------------
type rulerOptions struct {
whitelistNames map[string]bool
validateFn func(columns []Column) error
}
// RulerOption set the parameters of ruler options
type RulerOption func(*rulerOptions)
func (o *rulerOptions) apply(opts ...RulerOption) {
for _, opt := range opts {
opt(o)
}
}
// WithWhitelistNames set white list names of columns
func WithWhitelistNames(whitelistNames map[string]bool) RulerOption {
return func(o *rulerOptions) {
o.whitelistNames = whitelistNames
}
}
// WithValidateFn set validate function of columns
func WithValidateFn(fn func(columns []Column) error) RulerOption {
return func(o *rulerOptions) {
o.validateFn = fn
}
}
// -----------------------------------------------------------------------------
// Params query parameters
type Params struct {
Page int `json:"page" form:"page" binding:"gte=0"`
Limit int `json:"limit" form:"limit" binding:"gte=1"`
Sort string `json:"sort,omitempty" form:"sort" binding:""`
Columns []Column `json:"columns,omitempty" form:"columns"` // not required
// Deprecated: use Limit instead in sponge version v1.8.6, will remove in the future
Size int `json:"size" form:"size"`
}
// Column query info
type Column struct {
Name string `json:"name" form:"name"` // column name
Exp string `json:"exp" form:"exp"` // expressions, default value is "=", support =, !=, >, >=, <, <=, like, in, notin, isnull, isnotnull
Value interface{} `json:"value" form:"value"` // column value
Logic string `json:"logic" form:"logic"` // logical type, defaults to and when the value is null, with &(and), ||(or)
}
// converting ExpType to sql expressions and LogicType to sql using characters
func (c *Column) checkExp() (string, error) {
symbol := "?"
if c.Exp == "" {
c.Exp = Eq
}
if v, ok := expMap[strings.ToLower(c.Exp)]; ok { //nolint
c.Exp = v
switch c.Exp {
case " LIKE ":
val, ok1 := c.Value.(string)
if !ok1 {
return symbol, fmt.Errorf("invalid value type '%s'", c.Value)
}
// Use rune-safe slicing to preserve multi-byte characters
r := []rune(val)
if len(r) > 2 {
middle := string(r[1 : len(r)-1])
middle = strings.ReplaceAll(middle, "%", "\\%")
middle = strings.ReplaceAll(middle, "_", "\\_")
val = string(r[0]) + middle + string(r[len(r)-1])
}
if strings.HasPrefix(val, "%") ||
strings.HasPrefix(val, "_") ||
strings.HasSuffix(val, "%") ||
strings.HasSuffix(val, "_") {
c.Value = val
} else {
c.Value = "%" + val + "%"
}
case " IN ", " NOT IN ":
val, ok1 := c.Value.(string)
if ok1 {
values := []interface{}{}
ss := strings.Split(val, ",")
for _, s := range ss {
s = strings.TrimSpace(s)
if strings.HasPrefix(s, "\"") {
values = append(values, strings.Trim(s, "\""))
continue
} else if strings.HasPrefix(s, "'") {
values = append(values, strings.Trim(s, "'"))
continue
}
value, err := strconv.Atoi(s)
if err == nil {
values = append(values, value)
} else {
values = append(values, s)
}
}
c.Value = values
}
symbol = "(?)"
case " IS NULL ", " IS NOT NULL ":
c.Value = nil
symbol = ""
}
} else {
return symbol, fmt.Errorf("unsported exp type '%s'", c.Exp)
}
if c.Logic == "" {
c.Logic = AND
} else {
logic := strings.ToLower(c.Logic)
if _, ok := logicMap[logic]; ok { //nolint
c.Logic = logic
} else {
return symbol, fmt.Errorf("unsported logic type '%s'", c.Logic)
}
}
return symbol, nil
}
// ConvertToPage converted to page
func (p *Params) ConvertToPage() (order string, limit int, offset int) { //nolint
page := NewPage(p.Page, p.Limit, p.Sort)
order = page.sort
limit = page.limit
offset = page.page * page.limit
return //nolint
}
// ConvertToGormConditions conversion to gorm-compliant parameters based on the Columns parameter
// ignore the logical type of the last column, whether it is a one-column or multi-column query
func (p *Params) ConvertToGormConditions(opts ...RulerOption) (string, []interface{}, error) { //nolint
str := ""
args := []interface{}{}
l := len(p.Columns)
if l == 0 {
return "", nil, nil
}
isUseIN := true
if l == 1 {
isUseIN = false
}
field := p.Columns[0].Name
o := rulerOptions{}
o.apply(opts...)
if o.validateFn != nil {
err := o.validateFn(p.Columns)
if err != nil {
return "", nil, err
}
}
for i, column := range p.Columns {
// check name
if column.Name == "" || (o.whitelistNames != nil && !o.whitelistNames[column.Name]) {
return "", nil, fmt.Errorf("field name '%s' is not allowed", column.Name)
}
// check value
if column.Value == nil {
v := expMap[strings.ToLower(column.Exp)]
if v != " IS NULL " && v != " IS NOT NULL " {
return "", nil, fmt.Errorf("field 'value' cannot be nil")
}
} else {
column.Value = convertValue(column.Value)
}
// check exp
symbol, err := column.checkExp()
if err != nil {
return "", nil, err
}
if i == l-1 { // ignore the logical type of the last column
switch column.Logic {
case "or:)", "and:)":
str += column.Name + column.Exp + symbol + " ) "
default:
str += column.Name + column.Exp + symbol
}
} else {
switch column.Logic {
case "or:(", "and:(":
str += " ( " + column.Name + column.Exp + symbol + logicMap[column.Logic]
case "or:)", "and:)":
str += column.Name + column.Exp + symbol + " ) " + logicMap[column.Logic]
default:
str += column.Name + column.Exp + symbol + logicMap[column.Logic]
}
}
if column.Value != nil {
args = append(args, column.Value)
}
// when multiple columns are the same, determine whether the use of IN
if isUseIN {
if field != column.Name {
isUseIN = false
continue
}
if column.Exp != expMap[Eq] {
isUseIN = false
}
}
}
if isUseIN {
str = field + " IN (?)"
args = []interface{}{args}
}
return str, args, nil
}
// if the value is a string or an integer, if true means it is a string, otherwise it is an integer
func convertValue(v interface{}) interface{} {
s, ok := v.(string)
if !ok {
return v
}
s = strings.TrimSpace(s)
if strings.HasPrefix(s, "\"") {
s2 := strings.Trim(s, "\"")
if _, err := strconv.Atoi(s2); err == nil {
return s2
}
return s
}
intVal, err := strconv.Atoi(s)
if err == nil {
return intVal
}
boolVal, err := strconv.ParseBool(s)
if err == nil {
return boolVal
}
floatVal, err := strconv.ParseFloat(s, 64)
if err == nil {
return floatVal
}
// try to parse as RFC3339
if t, err := time.Parse(time.RFC3339, s); err == nil {
return t
}
// support other formats
layouts := []string{
"2006-01-02T15:04:05Z07:00",
"2006-01-02T15:04:05Z0700",
"2006-01-02T15:04:05.999999999Z0700",
"2006-01-02T15:04:05.999999999Z07:00",
"2006-01-02",
"2006-01-02 15:04:05",
"2006-01-02 15:04:05.999999999",
"2006-01-02 15:04:05 -07:00",
"2006-01-02 15:04:05.999999999 -07:00",
}
for _, layout := range layouts {
t, err := time.Parse(layout, s)
if err == nil {
return t
}
}
return v
}
// -------------------------------------------------------------------------------------------
// Conditions query conditions
type Conditions struct {
Columns []Column `json:"columns" form:"columns" binding:"min=1"` // columns info
}
// ConvertToGorm conversion to gorm-compliant parameters based on the Columns parameter
// ignore the logical type of the last column, whether it is a one-column or multi-column query
func (c *Conditions) ConvertToGorm(opts ...RulerOption) (string, []interface{}, error) {
p := &Params{Columns: c.Columns}
return p.ConvertToGormConditions(opts...)
}
// CheckValid check valid
func (c *Conditions) CheckValid() error {
if len(c.Columns) == 0 {
return fmt.Errorf("field 'columns' cannot be empty")
}
return nil
}