-
Notifications
You must be signed in to change notification settings - Fork 40
/
group_by.go
59 lines (46 loc) · 1.06 KB
/
group_by.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
package clause
import (
"io"
"github.com/stephenafamo/bob"
)
type GroupBy struct {
Groups []any
Distinct bool
With string // ROLLUP | CUBE
}
func (g *GroupBy) AppendGroup(e any) {
g.Groups = append(g.Groups, e)
}
func (g *GroupBy) SetGroupWith(with string) {
g.With = with
}
func (g *GroupBy) SetGroupByDistinct(distinct bool) {
g.Distinct = distinct
}
func (g GroupBy) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
w.Write([]byte("GROUP BY "))
if g.Distinct {
w.Write([]byte("DISTINCT "))
}
args, err := bob.ExpressSlice(w, d, start, g.Groups, "", ", ", "")
if err != nil {
return nil, err
}
if g.With != "" {
w.Write([]byte(" WITH "))
w.Write([]byte(g.With))
}
return args, nil
}
type GroupingSet struct {
Groups []bob.Expression
Type string // GROUPING SET | CUBE | ROLLUP
}
func (g GroupingSet) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
w.Write([]byte(g.Type))
args, err := bob.ExpressSlice(w, d, start, g.Groups, " (", ", ", ")")
if err != nil {
return nil, err
}
return args, nil
}