gorm/clause/group_by.go

38 lines
791 B
Go
Raw Normal View History

2020-02-02 09:40:44 +03:00
package clause
// GroupBy group by clause
type GroupBy struct {
2020-02-07 18:45:35 +03:00
Columns []Column
2020-03-08 13:05:22 +03:00
Having []Expression
2020-02-07 18:45:35 +03:00
}
// Name from clause name
func (groupBy GroupBy) Name() string {
return "GROUP BY"
}
// Build build group by clause
func (groupBy GroupBy) Build(builder Builder) {
for idx, column := range groupBy.Columns {
if idx > 0 {
builder.WriteByte(',')
}
builder.WriteQuoted(column)
}
2020-03-08 13:05:22 +03:00
if len(groupBy.Having) > 0 {
2020-02-07 18:45:35 +03:00
builder.Write(" HAVING ")
2020-03-08 13:05:22 +03:00
Where{Exprs: groupBy.Having}.Build(builder)
2020-02-07 18:45:35 +03:00
}
}
// MergeClause merge group by clause
func (groupBy GroupBy) MergeClause(clause *Clause) {
if v, ok := clause.Expression.(GroupBy); ok {
groupBy.Columns = append(v.Columns, groupBy.Columns...)
2020-03-08 13:05:22 +03:00
groupBy.Having = append(v.Having, groupBy.Having...)
2020-02-07 18:45:35 +03:00
}
clause.Expression = groupBy
2020-02-02 09:40:44 +03:00
}