-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.go
74 lines (66 loc) · 1.79 KB
/
builder.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
package qry
import (
"fmt"
)
// Query is an SQL query.
type Query interface {
Build() (string, []any)
}
// And returns a ConditionGroup made up of many Conditions separated an AND.
func And(conditions ...Condition) Condition {
return &ConditionGroup{
Conditions: conditions,
Or: false,
}
}
// Or returns a ConditionGroup made up of many Conditions separated an OR.
func Or(conditions ...Condition) Condition {
return &ConditionGroup{
Conditions: conditions,
Or: true,
}
}
// Equal returns a SimpleCondition that will check that the given field has the given value.
func Equal(field Field, value any) Condition {
if value == nil {
return &RawCondition{
SQL: fmt.Sprintf("%s IS NULL", field),
Args: []any{},
}
}
return &SimpleCondition{
Field: field,
Comparison: "=",
Value: value,
}
}
// NotEqual returns a SimpleCondition that will check that the given field does not have the given value.
func NotEqual(field Field, value any) Condition {
if value == nil {
return &RawCondition{
SQL: fmt.Sprintf("%s IS NOT NULL", field),
Args: []any{},
}
}
return &SimpleCondition{
Field: field,
Comparison: "!=",
Value: value,
}
}
// JsonArrayContains returns a Condition that will check if the given value exists in a JSON array stored under
// the given field.
func JsonArrayContains(field Field, value any) Condition {
return &RawCondition{
SQL: fmt.Sprintf("JSON_CONTAINS(%s, ?, '$') = 1", field),
Args: []any{value},
}
}
// NotJsonArrayContains returns a Condition that will check that the given value does not exist in a JSON array stored under
// the given field.
func NotJsonArrayContains(field Field, value any) Condition {
return &RawCondition{
SQL: fmt.Sprintf("JSON_CONTAINS(%s, ?, '$') = 0", field),
Args: []any{value},
}
}