-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.go
More file actions
328 lines (300 loc) · 6.64 KB
/
Copy pathsql.go
File metadata and controls
328 lines (300 loc) · 6.64 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
package filters
import (
"fmt"
"reflect"
"runtime"
"strings"
vocab "github.com/go-ap/activitypub"
"github.com/leporo/sqlf"
)
func SQLLimit(st *Stmt, f ...Check) {
if st == nil || len(f) == 0 {
return
}
lim := MaxItems
for _, check := range f {
switch c := check.(type) {
case *counter:
lim = c.max
break
}
}
st.Limit(lim)
}
type Stmt = sqlf.Stmt
func SQLWhere(s *Stmt, ff ...Check) error {
getWhereClauses(s, ff...)
return nil
}
func getWhereClauses(s *Stmt, f ...Check) {
//addNotClauses(s, f...)
addTypeWheres(s, f...)
addIRIWheres(s, f...)
addNLVWheres(s, f...)
addInReplyToWheres(s, f...)
addAttributedToWheres(s, f...)
addURLWheres(s, f...)
addContextWheres(s, f...)
}
func addNotClauses(s *Stmt, f ...Check) {
if s == nil || len(f) == 0 {
return
}
nots := make([]any, 0)
var os *Stmt
hasNots := false
/*
for _, check := range f {
switch c := check.(type) {
case notCrit:
nots = append()
}
}
*/
if len(nots) > 0 {
if len(nots) == 1 {
s.Where("iri = ?", nots[0])
} else {
s.Where("iri").In(nots...)
}
}
if hasNots {
tsql := strings.TrimPrefix(s.String(), " WHERE ")
os.Where("("+tsql+" OR iri IS NULL)", s.Args()...)
}
}
func addIRIWheres(s *Stmt, f ...Check) {
if s == nil || len(f) == 0 {
return
}
inVal := make([]any, 0)
likeVal := make([]any, 0)
var os *Stmt
andNil := false
for _, check := range f {
switch i := check.(type) {
case idEquals:
if u, err := vocab.IRI(i).URL(); err == nil {
inVal = append(inVal, vocab.IRI(u.String()))
if u.Scheme == "https" {
u.Scheme = "http"
} else {
u.Scheme = "https"
}
inVal = append(inVal, vocab.IRI(u.String()))
}
case iriEquals:
if u, err := vocab.IRI(i).URL(); err == nil {
inVal = append(inVal, vocab.IRI(u.String()))
if u.Scheme == "https" {
u.Scheme = "http"
} else {
u.Scheme = "https"
}
inVal = append(inVal, vocab.IRI(u.String()))
}
case iriLike:
likeVal = append(likeVal, "%"+string(i)+"%")
case idLike:
likeVal = append(likeVal, "%"+string(i)+"%")
case iriNil:
andNil = true
os = s
s = sqlf.New("")
case idNil:
andNil = true
os = s
s = sqlf.New("")
}
}
if len(inVal) > 0 {
if len(inVal) == 1 {
s.Where("iri = ?", inVal[0])
} else {
s.Where("iri").In(inVal...)
}
}
if len(likeVal) > 0 {
if len(likeVal) == 1 {
s.Where("iri LIKE ?", likeVal[0])
} else {
lors := sqlf.New("")
for _, like := range likeVal {
lors.Where("iri LIKE ?", like)
}
orsq := strings.TrimPrefix(lors.String(), " WHERE ")
s.Where(strings.ReplaceAll(orsq, " AND ", " OR "), lors.Args()...)
}
}
if andNil {
tsql := strings.TrimPrefix(s.String(), " WHERE ")
os.Where("("+tsql+" OR iri IS NULL)", s.Args()...)
}
}
func addTypeWheres(s *Stmt, f ...Check) {
if s == nil || len(f) == 0 {
return
}
inVal := make([]any, 0)
var os *Stmt
andNil := false
for _, check := range f {
if ands, ok := check.(checkAll); ok {
addTypeWheres(s, ands...)
continue
}
if ors, ok := check.(checkAny); ok {
addTypeWheres(s, ors...)
continue
}
c, ok := check.(withTypes)
if !ok {
continue
}
for _, typ := range c {
if typ == vocab.NilType {
andNil = true
os = s
s = sqlf.New("")
continue
}
inVal = append(inVal, typ)
}
}
if len(inVal) > 0 {
if len(inVal) == 1 {
s.Where("type = ?", inVal[0])
} else {
s.Where("type").In(inVal...)
}
}
if andNil {
tsql := strings.TrimPrefix(s.String(), " WHERE ")
os.Where("("+tsql+" OR type IS NULL)", s.Args()...)
}
}
func addContextWheres(s *Stmt, f ...Check) {
for _, check := range f {
switch c := check.(type) {
case contextNil:
jsonIsNull(s, "context")
case contextEquals:
jsonEquals(s, "context", vocab.IRI(c))
case contextLike:
jsonLike(s, "context", vocab.IRI(c))
}
}
}
func addURLWheres(s *Stmt, f ...Check) {
for _, check := range f {
switch c := check.(type) {
case urlNil:
s.Where("url IS NULL")
case urlEquals:
s.Where("url = ?", vocab.IRI(c))
case urlLike:
s.Where("url LIKE ?", "%"+vocab.IRI(c)+"%")
}
}
}
func sameFns(f1, f2 any) bool {
p1 := reflect.ValueOf(f1).Pointer()
p2 := reflect.ValueOf(f2).Pointer()
if p1 == p2 {
return true
}
if p1 == 0 || p2 == 0 {
return false
}
s1, l1 := runtime.FuncForPC(p1).FileLine(p1)
s2, l2 := runtime.FuncForPC(p2).FileLine(p2)
return s1 == s2 && l1 == l2
}
func addNLVWheres(s *Stmt, f ...Check) {
for _, check := range f {
switch c := check.(type) {
case naturalLanguageValCheck:
var field string
switch c.typ {
case byName:
field = keyName
case byPreferredUsername:
field = "preferred_username"
case bySummary:
field = keySummary
case byContent:
field = keyContent
}
switch {
case sameFns(c.checkFn, naturalLanguageEmpty):
s.Where(field + " IS NULL")
case sameFns(c.checkFn, naturalLanguageValuesLike):
s.Where(field+" LIKE ?", "%"+c.checkValue+"%")
case sameFns(c.checkFn, naturalLanguageValuesEquals):
s.Where(field+" = ?", c.checkValue)
}
}
}
}
func addInReplyToWheres(s *Stmt, f ...Check) {
for _, check := range f {
switch c := check.(type) {
case inReplyToNil:
jsonIsNull(s, "inReplyTo")
case inReplyToEquals:
jsonEquals(s, "inReplyTo", vocab.IRI(c))
case inReplyToLike:
jsonLike(s, "inReplyTo", vocab.IRI(c))
}
}
}
func addAttributedToWheres(s *Stmt, f ...Check) {
for _, check := range f {
switch c := check.(type) {
case attributedToNil:
jsonIsNull(s, "attributedTo")
case attributedToEquals:
jsonEquals(s, "attributedTo", vocab.IRI(c))
case attributedToLike:
jsonLike(s, "attributedTo", vocab.IRI(c))
}
}
}
func jsonLike(s *Stmt, prop string, val fmt.Stringer) {
isPg := stmtIsPostgres(s)
if isPg {
s.Where(fmt.Sprintf(`raw->>'%s' LIKE ?`, prop), "%"+val.String()+"%")
} else {
s.Where(fmt.Sprintf(`json_extract(raw, '$.%s') LIKE ?`, prop), "%"+val.String()+"%")
}
}
func jsonIsNull(s *Stmt, prop string) {
isPg := stmtIsPostgres(s)
if isPg {
s.Where(fmt.Sprintf(`raw->>'%s' IS NULL`, prop))
} else {
s.Where(fmt.Sprintf(`json_extract(raw, '$.%s') IS NULL`, prop))
}
}
func jsonIsNotNull(s *Stmt, prop string) {
isPg := stmtIsPostgres(s)
if isPg {
s.Where(fmt.Sprintf(`raw->>'%s' IS NOT NULL`, prop))
} else {
s.Where(fmt.Sprintf(`json_extract(raw, '$.%s') IS NOT NULL`, prop))
}
}
func jsonEquals(s *Stmt, prop string, val any) {
isPg := stmtIsPostgres(s)
if isPg {
s.Where(fmt.Sprintf(`raw->>'%s' = ?`, prop), val)
} else {
s.Where(fmt.Sprintf(`json_extract(raw, '$.%s') = ?`, prop), val)
}
}
func stmtIsPostgres(s *Stmt) bool {
sc := s.Clone()
sc.Where("t = ?", 1)
isPg := strings.Contains(sc.String(), "$1")
return isPg
}