diff --git a/exp/case.go b/exp/case.go index 707aea9e..d20800a3 100644 --- a/exp/case.go +++ b/exp/case.go @@ -73,3 +73,6 @@ func (c caseExpression) Else(result interface{}) CaseExpression { c.elseCondition = NewCaseElse(result) return c } + +func (c caseExpression) Asc() OrderedExpression { return asc(c) } +func (c caseExpression) Desc() OrderedExpression { return desc(c) } diff --git a/exp/case_test.go b/exp/case_test.go index abb3b13b..86c6add4 100644 --- a/exp/case_test.go +++ b/exp/case_test.go @@ -57,6 +57,16 @@ func (ces *caseExpressionSuite) TestElse() { ces.Nil(ce.GetElse()) } +func (ces *caseExpressionSuite) TestAsc() { + ce := exp.NewCaseExpression() + ces.Equal(exp.NewOrderedExpression(ce, exp.AscDir, exp.NoNullsSortType), ce.Asc()) +} + +func (ces *caseExpressionSuite) TestDesc() { + ce := exp.NewCaseExpression() + ces.Equal(exp.NewOrderedExpression(ce, exp.DescSortDir, exp.NoNullsSortType), ce.Desc()) +} + type caseWhenSuite struct { suite.Suite } diff --git a/exp/exp.go b/exp/exp.go index 39c0971f..50e5473b 100644 --- a/exp/exp.go +++ b/exp/exp.go @@ -455,6 +455,7 @@ type ( CaseExpression interface { Expression Aliaseable + Orderable GetValue() interface{} GetWhens() []CaseWhen GetElse() CaseElse diff --git a/select_dataset_example_test.go b/select_dataset_example_test.go index 8c026a73..35f62794 100644 --- a/select_dataset_example_test.go +++ b/select_dataset_example_test.go @@ -380,6 +380,14 @@ func ExampleSelectDataset_Order() { // SELECT * FROM "test" ORDER BY "a" ASC } +func ExampleSelectDataset_Order_caseExpression() { + ds := goqu.From("test").Order(goqu.Case().When(goqu.C("num").Gt(10), 0).Else(1).Asc()) + sql, _, _ := ds.ToSQL() + fmt.Println(sql) + // Output: + // SELECT * FROM "test" ORDER BY CASE WHEN ("num" > 10) THEN 0 ELSE 1 END ASC +} + func ExampleSelectDataset_OrderAppend() { ds := goqu.From("test").Order(goqu.C("a").Asc()) sql, _, _ := ds.OrderAppend(goqu.C("b").Desc().NullsLast()).ToSQL()