-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin.go
145 lines (134 loc) · 3.5 KB
/
builtin.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
package structquery
import (
"fmt"
"gorm.io/gorm/clause"
)
const (
Empty QueryType = ""
Eq QueryType = "eq"
Neq QueryType = "neq"
Like QueryType = "like"
LLike QueryType = "llike"
RLike QueryType = "rlike"
In QueryType = "in"
NotIn QueryType = "not_in"
Gt QueryType = "gt"
Gte QueryType = "gte"
Lt QueryType = "lt"
Lte QueryType = "lte"
JSONExtractEq QueryType = "json_extract_eq"
JSONExtractLike QueryType = "json_extract_like"
MySQLJSONContains QueryType = "my_json_contains"
UnsafeRawSQL QueryType = "unsaferaw" // dangerous
)
func registerBuiltin(q *Queryer) {
q.Register(Empty, queryEq)
q.Register(Eq, queryEq)
q.Register(Neq, queryNeq)
q.Register(Like, func(f Field) clause.Expression {
return clause.Like{
Column: f.ColumnName,
Value: `%` + fmt.Sprintf("%v", f.Value) + `%`,
}
})
q.Register(LLike, func(f Field) clause.Expression {
return clause.Like{
Column: f.ColumnName,
Value: `%` + fmt.Sprintf("%v", f.Value),
}
})
q.Register(RLike, func(f Field) clause.Expression {
return clause.Like{
Column: f.ColumnName,
Value: fmt.Sprintf("%v", f.Value) + `%`,
}
})
q.Register(In, queryEq)
q.Register(NotIn, queryNeq)
q.Register(Gt, func(f Field) clause.Expression {
return clause.Gt{
Column: f.ColumnName,
Value: f.Value,
}
})
q.Register(Gte, func(f Field) clause.Expression {
return clause.Gte{
Column: f.ColumnName,
Value: f.Value,
}
})
q.Register(Lt, func(f Field) clause.Expression {
return clause.Lt{
Column: f.ColumnName,
Value: f.Value,
}
})
q.Register(Lte, func(f Field) clause.Expression {
return clause.Lte{
Column: f.ColumnName,
Value: f.Value,
}
})
q.Register(JSONExtractEq, queryJSONExtractEq)
q.Register(JSONExtractLike, queryJSONExtractLike)
// support for mysql
q.Register(MySQLJSONContains, queryMySQLJSONContains)
// unsafe raw sql
q.Register(UnsafeRawSQL, queryUnsafeRawSQL)
}
func queryEq(field Field) clause.Expression {
return clause.Eq{
Column: field.ColumnName,
Value: field.Value,
}
}
func queryNeq(field Field) clause.Expression {
return clause.Neq{
Column: field.ColumnName,
Value: field.Value,
}
}
func queryJSONExtractEq(field Field) clause.Expression {
jsonPath := field.FieldMeta.Options["path"]
if jsonPath == "" {
return nil
}
sql := fmt.Sprintf("JSON_EXTRACT(%s, '%s') = ?", field.ColumnName, jsonPath)
var values = []interface{}{field.Value}
return clause.NamedExpr{
SQL: sql,
Vars: values,
}
}
func queryJSONExtractLike(field Field) clause.Expression {
jsonPath := field.FieldMeta.Options["path"]
if jsonPath == "" {
return nil
}
sql := fmt.Sprintf("JSON_EXTRACT(%s, '%s') LIKE ?", field.ColumnName, jsonPath)
var values = []interface{}{`%` + fmt.Sprintf("%v", field.Value) + `%`}
return clause.NamedExpr{
SQL: sql,
Vars: values,
}
}
func queryMySQLJSONContains(field Field) clause.Expression {
jsonPath := field.FieldMeta.Options["path"]
var sql string
if jsonPath == "" {
sql = fmt.Sprintf("JSON_CONTAINS(%s, ?)", field.ColumnName)
} else {
sql = fmt.Sprintf("JSON_CONTAINS(%s, ?, '%s')", field.ColumnName, jsonPath)
}
var values = []interface{}{field.Value}
return clause.NamedExpr{
SQL: sql,
Vars: values,
}
}
func queryUnsafeRawSQL(field Field) clause.Expression {
return clause.NamedExpr{
SQL: field.FieldMeta.Options["sql"],
Vars: []interface{}{field.Value},
}
}