From 28cb67f61c4a7db69c0907c64d8b3363b587ad9f Mon Sep 17 00:00:00 2001 From: Travis Date: Sat, 30 Nov 2019 12:10:26 -0600 Subject: [PATCH] add having clause support to GroupByBuilder --- orm.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/orm.go b/orm.go index 717a2b4..c51e6c6 100644 --- a/orm.go +++ b/orm.go @@ -162,11 +162,15 @@ type PQLBaseQuery struct { // NewPQLBaseQuery creates a new PQLQuery with the given PQL and index. func NewPQLBaseQuery(pql string, index *Index, err error) *PQLBaseQuery { + var hasKeys bool + if index != nil { + hasKeys = index.options.keys + } return &PQLBaseQuery{ index: index, pql: pql, err: err, - hasKeys: index.options.keys, + hasKeys: hasKeys, } } @@ -577,6 +581,7 @@ type groupByBuilder struct { limit int64 filter *PQLRowQuery aggregate *PQLBaseQuery + having *PQLBaseQuery } // GroupByBuilderOption is a functional option type for index.GroupBy @@ -618,6 +623,15 @@ func OptGroupByBuilderAggregate(agg *PQLBaseQuery) GroupByBuilderOption { } } +// OptGroupByBuilderHaving is a functional option on groupByBuilder +// used to set the having clause. +func OptGroupByBuilderHaving(having *PQLBaseQuery) GroupByBuilderOption { + return func(g *groupByBuilder) error { + g.having = having + return nil + } +} + // GroupByBase creates a GroupBy query with the given functional options. func (idx *Index) GroupByBase(opts ...GroupByBuilderOption) *PQLBaseQuery { bldr := &groupByBuilder{} @@ -655,6 +669,12 @@ func (idx *Index) GroupByBase(opts ...GroupByBuilderOption) *PQLBaseQuery { text += fmt.Sprintf(",aggregate=%s", aggregateText) } + // having + if bldr.having != nil { + havingText := bldr.having.Serialize().String() + text += fmt.Sprintf(",having=%s", havingText) + } + text += ")" return NewPQLBaseQuery(text, idx, nil) }