gitee.com/eden-framework/sqlx@v0.0.3/builder/addition_group_by.go (about) 1 package builder 2 3 import ( 4 "context" 5 ) 6 7 type GroupByAddition struct { 8 } 9 10 func (GroupByAddition) AdditionType() AdditionType { 11 return AdditionGroupBy 12 } 13 14 func GroupBy(groups ...SqlExpr) *groupBy { 15 return &groupBy{ 16 groups: groups, 17 } 18 } 19 20 var _ Addition = (*groupBy)(nil) 21 22 type groupBy struct { 23 GroupByAddition 24 25 groups []SqlExpr 26 // HAVING 27 havingCond SqlCondition 28 } 29 30 func (g groupBy) Having(cond SqlCondition) *groupBy { 31 g.havingCond = cond 32 return &g 33 } 34 35 func (g *groupBy) IsNil() bool { 36 return g == nil || len(g.groups) == 0 37 } 38 39 func (g *groupBy) Ex(ctx context.Context) *Ex { 40 expr := Expr("GROUP BY ") 41 42 for i, group := range g.groups { 43 if i > 0 { 44 expr.WriteByte(',') 45 } 46 expr.WriteExpr(group) 47 } 48 49 if !(IsNilExpr(g.havingCond)) { 50 expr.WriteString(" HAVING ") 51 expr.WriteExpr(g.havingCond) 52 } 53 54 return expr.Ex(ctx) 55 }