gorm/clause/group_by.go

43 lines
972 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-03-09 12:07:00 +03:00
builder.WriteString(" 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 {
copiedColumns := make([]Column, len(v.Columns))
copy(copiedColumns, v.Columns)
groupBy.Columns = append(copiedColumns, groupBy.Columns...)
copiedHaving := make([]Expression, len(v.Having))
copy(copiedHaving, v.Having)
groupBy.Having = append(copiedHaving, groupBy.Having...)
2020-02-07 18:45:35 +03:00
}
clause.Expression = groupBy
2020-02-02 09:40:44 +03:00
}