美文网首页
聊聊gorm的GroupBy

聊聊gorm的GroupBy

作者: go4it | 来源:发表于2021-01-19 23:55 被阅读0次

    本文主要研究一下gorm的GroupBy

    GroupBy

    gorm.io/gorm@v1.20.11/clause/group_by.go

    // 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 {
            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...)
        }
        clause.Expression = groupBy
    }
    

    GroupBy定义了Columns和Having属性,其Build方法遍历Columns,最后针对Having在拼接Having子句

    实例

    func TestGroupBy(t *testing.T) {
        results := []struct {
            Clauses []clause.Interface
            Result  string
            Vars    []interface{}
        }{
            {
                []clause.Interface{clause.Select{}, clause.From{}, clause.GroupBy{
                    Columns: []clause.Column{{Name: "role"}},
                    Having:  []clause.Expression{clause.Eq{"role", "admin"}},
                }},
                "SELECT * FROM `users` GROUP BY `role` HAVING `role` = ?", []interface{}{"admin"},
            },
            {
                []clause.Interface{clause.Select{}, clause.From{}, clause.GroupBy{
                    Columns: []clause.Column{{Name: "role"}},
                    Having:  []clause.Expression{clause.Eq{"role", "admin"}},
                }, clause.GroupBy{
                    Columns: []clause.Column{{Name: "gender"}},
                    Having:  []clause.Expression{clause.Neq{"gender", "U"}},
                }},
                "SELECT * FROM `users` GROUP BY `role`,`gender` HAVING `role` = ? AND `gender` <> ?", []interface{}{"admin", "U"},
            },
        }
    
        for idx, result := range results {
            t.Run(fmt.Sprintf("case #%v", idx), func(t *testing.T) {
                checkBuildClauses(t, result.Clauses, result.Result, result.Vars)
            })
        }
    }
    

    小结

    gorm的GroupBy定义了Columns和Having属性,其Build方法遍历Columns,最后针对Having在拼接Having子句。

    doc

    相关文章

      网友评论

          本文标题:聊聊gorm的GroupBy

          本文链接:https://www.haomeiwen.com/subject/kddhzktx.html