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

add having clause support to GroupByBuilder #262

Merged
merged 1 commit into from
Dec 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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