-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
operator.go
162 lines (153 loc) · 5.55 KB
/
operator.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
package filter
import (
"fmt"
"gorm.io/gorm"
"goyave.dev/goyave/v5/util/sqlutil"
)
// Operator used by filters to build the SQL query.
// The operator function modifies the GORM statement (most of the time by adding
// a WHERE condition) then returns the modified statement.
//
// Operators may need arguments (e.g. "$eq", equals needs a value to compare the field to);
// RequiredArguments define the minimum number of arguments a client must send in order to
// use this operator in a filter. RequiredArguments is checked during Filter parsing.
//
// Operators may return the given tx without change if they don't support the given dataType or
// add a condition that will always be false.
type Operator struct {
Function func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB
RequiredArguments uint8
}
var (
// Operators definitions. The key is the query representation of the operator, (e.g. "$eq").
Operators = map[string]*Operator{
"$eq": {Function: basicComparison("="), RequiredArguments: 1},
"$ne": {Function: basicComparison("<>"), RequiredArguments: 1},
"$gt": {Function: basicComparison(">"), RequiredArguments: 1},
"$lt": {Function: basicComparison("<"), RequiredArguments: 1},
"$gte": {Function: basicComparison(">="), RequiredArguments: 1},
"$lte": {Function: basicComparison("<="), RequiredArguments: 1},
"$starts": {
Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
if dataType != DataTypeText && dataType != DataTypeEnum {
return filter.Where(tx, "FALSE")
}
query := castEnumAsText(column, dataType) + " LIKE ?"
value := sqlutil.EscapeLike(filter.Args[0]) + "%"
return filter.Where(tx, query, value)
},
RequiredArguments: 1,
},
"$ends": {
Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
if dataType != DataTypeText && dataType != DataTypeEnum {
return filter.Where(tx, "FALSE")
}
query := castEnumAsText(column, dataType) + " LIKE ?"
value := "%" + sqlutil.EscapeLike(filter.Args[0])
return filter.Where(tx, query, value)
},
RequiredArguments: 1,
},
"$cont": {
Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
if dataType != DataTypeText && dataType != DataTypeEnum {
return filter.Where(tx, "FALSE")
}
query := castEnumAsText(column, dataType) + " LIKE ?"
value := "%" + sqlutil.EscapeLike(filter.Args[0]) + "%"
return filter.Where(tx, query, value)
},
RequiredArguments: 1,
},
"$excl": {
Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
if dataType != DataTypeText && dataType != DataTypeEnum {
return filter.Where(tx, "FALSE")
}
query := castEnumAsText(column, dataType) + " NOT LIKE ?"
value := "%" + sqlutil.EscapeLike(filter.Args[0]) + "%"
return filter.Where(tx, query, value)
},
RequiredArguments: 1,
},
"$in": {Function: multiComparison("IN"), RequiredArguments: 1},
"$notin": {Function: multiComparison("NOT IN"), RequiredArguments: 1},
"$isnull": {
Function: func(tx *gorm.DB, filter *Filter, column string, _ DataType) *gorm.DB {
return filter.Where(tx, column+" IS NULL")
},
RequiredArguments: 0,
},
"$istrue": {
Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
if dataType != DataTypeBool {
return filter.Where(tx, "FALSE")
}
return filter.Where(tx, column+" IS TRUE")
},
RequiredArguments: 0,
},
"$isfalse": {
Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
if dataType != DataTypeBool {
return filter.Where(tx, "FALSE")
}
return filter.Where(tx, column+" IS FALSE")
},
RequiredArguments: 0,
},
"$notnull": {
Function: func(tx *gorm.DB, filter *Filter, column string, _ DataType) *gorm.DB {
return filter.Where(tx, column+" IS NOT NULL")
},
RequiredArguments: 0,
},
"$between": {
Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
if dataType.IsArray() {
return filter.Where(tx, "FALSE")
}
args, ok := ConvertArgsToSafeType(filter.Args[:2], dataType)
if !ok {
return filter.Where(tx, "FALSE")
}
query := castEnumAsText(column, dataType) + " BETWEEN ? AND ?"
return filter.Where(tx, query, args...)
},
RequiredArguments: 2,
},
}
)
func castEnumAsText(column string, dataType DataType) string {
if dataType == DataTypeEnum || dataType == DataTypeEnumArray {
return fmt.Sprintf("CAST(%s AS TEXT)", column)
}
return column
}
func basicComparison(op string) func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
return func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
if dataType.IsArray() {
return filter.Where(tx, "FALSE")
}
arg, ok := ConvertToSafeType(filter.Args[0], dataType)
if !ok {
return filter.Where(tx, "FALSE")
}
query := fmt.Sprintf("%s %s ?", castEnumAsText(column, dataType), op)
return filter.Where(tx, query, arg)
}
}
func multiComparison(op string) func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
return func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
if dataType.IsArray() {
return filter.Where(tx, "FALSE")
}
args, ok := ConvertArgsToSafeType(filter.Args, dataType)
if !ok {
return filter.Where(tx, "FALSE")
}
query := fmt.Sprintf("%s %s ?", castEnumAsText(column, dataType), op)
return filter.Where(tx, query, args)
}
}