-
Notifications
You must be signed in to change notification settings - Fork 6
/
groupby.go
80 lines (67 loc) · 1.61 KB
/
groupby.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
package querybuilder
import (
"github.com/goal-web/contracts"
"strings"
)
type GroupBy []string
func (this GroupBy) IsEmpty() bool {
return len(this) == 0
}
func (this GroupBy) String() string {
if this.IsEmpty() {
return ""
}
return strings.Join(this, ",")
}
func (builder *Builder) GroupBy(columns ...string) contracts.QueryBuilder {
builder.groupBy = append(builder.groupBy, columns...)
return builder
}
func (builder *Builder) Having(field string, args ...interface{}) contracts.QueryBuilder {
var (
arg interface{}
condition = "="
whereType = contracts.And
)
switch len(args) {
case 1:
arg = args[0]
case 2:
condition = args[0].(string)
arg = args[1]
case 3:
condition = args[0].(string)
arg = args[1]
whereType = args[2].(contracts.WhereJoinType)
}
raw, bindings := builder.prepareArgs(condition, arg)
builder.having.wheres[whereType] = append(builder.having.wheres[whereType], &Where{
field: field,
condition: condition,
arg: raw,
})
return builder.addBinding(havingBinding, bindings...)
}
func (builder *Builder) OrHaving(field string, args ...interface{}) contracts.QueryBuilder {
var (
arg interface{}
condition = "="
)
switch len(args) {
case 1:
arg = args[0]
case 2:
condition = args[0].(string)
arg = args[1]
default:
condition = args[0].(string)
arg = args[1]
}
raw, bindings := builder.prepareArgs(condition, arg)
builder.having.wheres[contracts.Or] = append(builder.having.wheres[contracts.Or], &Where{
field: field,
condition: condition,
arg: raw,
})
return builder.addBinding(havingBinding, bindings...)
}