-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.go
More file actions
248 lines (216 loc) · 5.07 KB
/
expression.go
File metadata and controls
248 lines (216 loc) · 5.07 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
package dbx
import (
"fmt"
"reflect"
"sort"
"strings"
)
// Expression can build SQL and bind parameters.
type Expression interface {
Build(db *DB, params Params) string
}
type rawExp struct {
sql string
params Params
}
// Raw builds SQL expression from raw SQL with optional params.
func Raw(sql string, params ...Params) Expression {
out := Params{}
if len(params) > 0 {
for k, v := range params[0] {
out[k] = v
}
}
return rawExp{sql: sql, params: out}
}
func (e rawExp) Build(_ *DB, params Params) string {
for k, v := range e.params {
params[k] = v
}
return e.sql
}
// HashExp builds conjunction: col1=val1 AND col2=val2...
type HashExp map[string]any
func (e HashExp) Build(db *DB, params Params) string {
if len(e) == 0 {
return ""
}
keys := make([]string, 0, len(e))
for k := range e {
keys = append(keys, k)
}
sort.Strings(keys)
parts := make([]string, 0, len(keys))
for _, k := range keys {
v := e[k]
col := db.QuoteColumnName(k)
switch {
case v == nil:
parts = append(parts, col+" IS NULL")
case isSlice(v):
parts = append(parts, buildIn(db, params, k, v, false))
case isExpression(v):
parts = append(parts, col+"=("+v.(Expression).Build(db, params)+")")
default:
p := addParam(params, v)
parts = append(parts, col+"="+p)
}
}
return strings.Join(parts, " AND ")
}
type opExp struct {
op string
args []Expression
}
// And joins expressions with AND.
func And(exprs ...Expression) Expression {
return opExp{op: "AND", args: exprs}
}
// Or joins expressions with OR.
func Or(exprs ...Expression) Expression {
return opExp{op: "OR", args: exprs}
}
func (e opExp) Build(db *DB, params Params) string {
parts := make([]string, 0, len(e.args))
for _, a := range e.args {
if a == nil {
continue
}
s := strings.TrimSpace(a.Build(db, params))
if s == "" {
continue
}
parts = append(parts, "("+s+")")
}
return strings.Join(parts, " "+e.op+" ")
}
type notExp struct {
e Expression
}
// Not negates expression.
func Not(e Expression) Expression {
return notExp{e: e}
}
func (e notExp) Build(db *DB, params Params) string {
if e.e == nil {
return ""
}
s := e.e.Build(db, params)
if strings.TrimSpace(s) == "" {
return ""
}
return "NOT (" + s + ")"
}
type likeExp struct {
col string
value any
}
// Like creates column LIKE '%value%' expression.
func Like(col string, value any) Expression {
return likeExp{col: col, value: value}
}
func (e likeExp) Build(db *DB, params Params) string {
p := addParam(params, "%"+fmt.Sprint(e.value)+"%")
return db.QuoteColumnName(e.col) + " LIKE " + p
}
type inExp struct {
col string
values any
not bool
}
// In creates column IN (...) expression.
func In(col string, values any) Expression {
return inExp{col: col, values: values}
}
// NotIn creates column NOT IN (...) expression.
func NotIn(col string, values any) Expression {
return inExp{col: col, values: values, not: true}
}
func (e inExp) Build(db *DB, params Params) string {
return buildIn(db, params, e.col, e.values, e.not)
}
type betweenExp struct {
col string
start any
end any
negated bool
}
// Between creates BETWEEN expression.
func Between(col string, start, end any) Expression {
return betweenExp{col: col, start: start, end: end}
}
func (e betweenExp) Build(db *DB, params Params) string {
p1 := addParam(params, e.start)
p2 := addParam(params, e.end)
if e.negated {
return db.QuoteColumnName(e.col) + " NOT BETWEEN " + p1 + " AND " + p2
}
return db.QuoteColumnName(e.col) + " BETWEEN " + p1 + " AND " + p2
}
type existsExp struct {
query *SelectQuery
not bool
}
// Exists creates EXISTS (subquery).
func Exists(q *SelectQuery) Expression {
return existsExp{query: q}
}
func (e existsExp) Build(_ *DB, params Params) string {
if e.query == nil {
return ""
}
sql, p := e.query.build()
for k, v := range p {
params[k] = v
}
if e.not {
return "NOT EXISTS (" + sql + ")"
}
return "EXISTS (" + sql + ")"
}
func buildIn(db *DB, params Params, col string, values any, negated bool) string {
v := reflect.ValueOf(values)
if !v.IsValid() || (v.Kind() == reflect.Slice && v.Len() == 0) {
if negated {
return "1=1"
}
return "1=0"
}
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
p := addParam(params, values)
if negated {
return db.QuoteColumnName(col) + "!=" + p
}
return db.QuoteColumnName(col) + "=" + p
}
holders := make([]string, 0, v.Len())
for i := 0; i < v.Len(); i++ {
holders = append(holders, addParam(params, v.Index(i).Interface()))
}
op := " IN "
if negated {
op = " NOT IN "
}
return db.QuoteColumnName(col) + op + "(" + strings.Join(holders, ", ") + ")"
}
func addParam(params Params, v any) string {
if params == nil {
panic("params cannot be nil")
}
for i := len(params) + 1; ; i++ {
name := fmt.Sprintf("p%d", i)
if _, ok := params[name]; ok {
continue
}
params[name] = v
return "{:" + name + "}"
}
}
func isSlice(v any) bool {
rv := reflect.ValueOf(v)
return rv.IsValid() && (rv.Kind() == reflect.Slice || rv.Kind() == reflect.Array)
}
func isExpression(v any) bool {
_, ok := v.(Expression)
return ok
}