-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwherer.go
60 lines (51 loc) · 1.39 KB
/
wherer.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
package patcher
import "strings"
// Wherer is an interface that can be used to specify the WHERE clause to use. By using this interface,
// the package will default to using an "AND" WHERE clause. If you want to use an "OR" WHERE clause, you can
// use the WhereTyper interface instead.
type Wherer interface {
Where() (string, []any)
}
// WhereTyper is an interface that can be used to specify the type of WHERE clause to use. By using this
// interface, you can specify whether to use an "AND" or "OR" WHERE clause.
type WhereTyper interface {
Wherer
WhereType() WhereType
}
type WhereType string
const (
WhereTypeAnd WhereType = "AND"
WhereTypeOr WhereType = "OR"
)
func (w WhereType) IsValid() bool {
switch w {
case WhereTypeAnd, WhereTypeOr:
return true
}
return false
}
func appendWhere(where Wherer, builder *strings.Builder, args *[]any) {
if where == nil {
return
}
wSQL, fwArgs := where.Where()
if fwArgs == nil {
fwArgs = make([]any, 0)
}
wtStr := WhereTypeAnd // default to AND
wt, ok := where.(WhereTyper)
if ok && wt.WhereType().IsValid() {
wtStr = wt.WhereType()
}
builder.WriteString(string(wtStr) + " ")
builder.WriteString(strings.TrimSpace(wSQL))
builder.WriteString("\n")
*args = append(*args, fwArgs...)
}
type whereStringOption struct {
where string
args []any
}
func (w *whereStringOption) Where() (sqlStr string, args []any) {
return w.where, w.args
}