Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #262 from travisturner/groupby-having
Browse files Browse the repository at this point in the history
add having clause support to GroupByBuilder
  • Loading branch information
travisturner authored Dec 1, 2019
2 parents d55c16e + 28cb67f commit 6bc638d
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -577,6 +581,7 @@ type groupByBuilder struct {
limit int64
filter *PQLRowQuery
aggregate *PQLBaseQuery
having *PQLBaseQuery
}

// GroupByBuilderOption is a functional option type for index.GroupBy
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 6bc638d

Please sign in to comment.