-
Notifications
You must be signed in to change notification settings - Fork 1
/
condition.go
71 lines (63 loc) · 1.79 KB
/
condition.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
package gqb
import (
"fmt"
"strings"
)
// Condition is common condition struct
type condition struct {
comparison Comparison
field string
value interface{}
combine CombineType
}
// conditionBuilder::getCombine() interface implementation
func (c condition) Combine() string {
return string(c.combine)
}
// conditionBuilder::Build() interface implementation
func (c condition) Build(binds []interface{}) (string, []interface{}) {
var clause string
switch c.comparison {
case In:
q := ""
values, ok := c.value.([]interface{})
if ok {
for _, v := range values {
q += driverCompat.PlaceHolder(len(binds)+1) + ", "
binds = bind(binds, v)
}
clause = fmt.Sprintf("%s IN (%s)", quote(c.field), strings.Trim(q, ", "))
}
case Equal:
if c.value == nil {
clause = fmt.Sprintf("%s IS NULL", quote(c.field))
} else {
clause = fmt.Sprintf("%s %s %s", quote(c.field), string(c.comparison), driverCompat.PlaceHolder(len(binds)+1))
binds = bind(binds, c.value)
}
case NotEqual:
if c.value == nil {
clause = fmt.Sprintf("%s IS NOT NULL", quote(c.field))
} else {
clause = fmt.Sprintf("%s %s %s", quote(c.field), string(c.comparison), driverCompat.PlaceHolder(len(binds)+1))
binds = bind(binds, c.value)
}
default:
clause = fmt.Sprintf("%s %s %s", quote(c.field), string(c.comparison), driverCompat.PlaceHolder(len(binds)+1))
binds = bind(binds, c.value)
}
return clause, binds
}
// Raw clause condition
type rawCondition struct {
rawClause string
combine CombineType
}
// conditionBuilder::getCombine() interface implementation
func (r rawCondition) Combine() string {
return string(r.combine)
}
// conditionBuilder::Build() interface implementation
func (r rawCondition) Build(binds []interface{}) (string, []interface{}) {
return r.rawClause, binds
}