-
Notifications
You must be signed in to change notification settings - Fork 542
/
qmhelper.go
84 lines (70 loc) · 1.75 KB
/
qmhelper.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
package qmhelper
import (
"fmt"
"reflect"
"github.com/volatiletech/sqlboiler/queries"
)
// Nullable object
type Nullable interface {
IsZero() bool
}
// WhereQueryMod allows construction of where clauses
type WhereQueryMod struct {
Clause string
Args []interface{}
}
// Apply implements QueryMod.Apply.
func (qm WhereQueryMod) Apply(q *queries.Query) {
queries.AppendWhere(q, qm.Clause, qm.Args...)
}
// WhereNullEQ is a helper for doing equality with null types
func WhereNullEQ(name string, negated bool, value interface{}) WhereQueryMod {
isNull := false
if nullable, ok := value.(Nullable); ok {
isNull = nullable.IsZero()
} else {
isNull = reflect.ValueOf(value).IsNil()
}
if isNull {
var not string
if negated {
not = "not "
}
return WhereQueryMod{
Clause: fmt.Sprintf("%s is %snull", name, not),
}
}
op := "="
if negated {
op = "!="
}
return WhereQueryMod{
Clause: fmt.Sprintf("%s %s ?", name, op),
Args: []interface{}{value},
}
}
// WhereIsNull is a helper that just returns "name is null"
func WhereIsNull(name string) WhereQueryMod {
return WhereQueryMod{Clause: fmt.Sprintf("%s is null", name)}
}
// WhereIsNotNull is a helper that just returns "name is not null"
func WhereIsNotNull(name string) WhereQueryMod {
return WhereQueryMod{Clause: fmt.Sprintf("%s is not null", name)}
}
type operator string
// Supported operations
const (
EQ operator = "="
NEQ operator = "!="
LT operator = "<"
LTE operator = "<="
GT operator = ">"
GTE operator = ">="
)
// Where is a helper for doing operations on primitive types
func Where(name string, operator operator, value interface{}) WhereQueryMod {
return WhereQueryMod{
Clause: fmt.Sprintf("%s %s ?", name, string(operator)),
Args: []interface{}{value},
}
}