mirror of https://github.com/go-gorm/gorm.git
38 lines
797 B
Go
38 lines
797 B
Go
package clause
|
|
|
|
// GroupBy group by clause
|
|
type GroupBy struct {
|
|
Columns []Column
|
|
Having []Expression
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
if len(groupBy.Having) > 0 {
|
|
builder.WriteString(" HAVING ")
|
|
Where{Exprs: groupBy.Having}.Build(builder)
|
|
}
|
|
}
|
|
|
|
// 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...)
|
|
groupBy.Having = append(v.Having, groupBy.Having...)
|
|
}
|
|
clause.Expression = groupBy
|
|
}
|