Skip to content

Commit

Permalink
fix: nil pointer in when GroupBy has not been called
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Jul 27, 2021
1 parent aec873a commit f690d89
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
3 changes: 3 additions & 0 deletions exp/select_clauses.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ func (c *selectClauses) GroupBy() ColumnListExpression {
}

func (c *selectClauses) GroupByAppend(cl ColumnListExpression) SelectClauses {
if c.groupBy == nil {
return c.SetGroupBy(cl)
}
ret := c.clone()
ret.groupBy = ret.groupBy.Append(cl.Columns()...)
return ret
Expand Down
14 changes: 7 additions & 7 deletions select_dataset_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,18 +415,18 @@ func ExampleSelectDataset_GroupBy() {
}

func ExampleSelectDataset_GroupByAppend() {
sql, _, _ := goqu.From("test").
ds := goqu.From("test").
Select(goqu.SUM("income").As("income_sum")).
GroupBy("age").
GroupBy("age")
sql, _, _ := ds.
GroupByAppend("job").
ToSQL()
fmt.Println(sql)
sql, _, _ = goqu.From("test").
Select(goqu.SUM("income").As("income_sum")).
GroupByAppend("age").
ToSQL()
// the original dataset group by does not change
sql, _, _ = ds.ToSQL()
fmt.Println(sql)
// Output:
// SELECT SUM("income") AS "income_sum" FROM "test" GROUP BY "age"
// SELECT SUM("income") AS "income_sum" FROM "test" GROUP BY "age", "job"
// SELECT SUM("income") AS "income_sum" FROM "test" GROUP BY "age"
}

Expand Down

0 comments on commit f690d89

Please sign in to comment.