Skip to content

Commit

Permalink
Added OrderPrepend #61
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Jul 11, 2019
1 parent bb2c99b commit 921adc0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## v7.1.1
## v7.2.0

* [FIXED] Sqlite3 does not accept SELECT * UNION (SELECT *) [#79](https://github.com/doug-martin/goqu/issues/79)
* [FIXED] Where(Ex{}) causes panics [mysql] [#49](https://github.com/doug-martin/goqu/issues/49)
* [ADDED] Support for OrderPrepend [#61](https://github.com/doug-martin/goqu/issues/49)

## v7.1.0

Expand Down
6 changes: 6 additions & 0 deletions dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ func (d *Dataset) OrderAppend(order ...exp.OrderedExpression) *Dataset {
return d.copy(d.clauses.OrderAppend(order...))
}

// Adds a more columns to the beginning of the current ORDER BY clause. If no order has be previously specified it is the same as
// calling Order. See examples.
func (d *Dataset) OrderPrepend(order ...exp.OrderedExpression) *Dataset {
return d.copy(d.clauses.OrderPrepend(order...))
}

// Removes the ORDER BY clause. See examples.
func (d *Dataset) ClearOrder() *Dataset {
return d.copy(d.clauses.ClearOrder())
Expand Down
8 changes: 8 additions & 0 deletions dataset_sql_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ func ExampleDataset_OrderAppend() {
// SELECT * FROM "test" ORDER BY "a" ASC, "b" DESC NULLS LAST
}

func ExampleDataset_OrderPrepend() {
ds := goqu.From("test").Order(goqu.C("a").Asc())
sql, _, _ := ds.OrderPrepend(goqu.C("b").Desc().NullsLast()).ToSQL()
fmt.Println(sql)
// Output:
// SELECT * FROM "test" ORDER BY "b" DESC NULLS LAST, "a" ASC
}

func ExampleDataset_ClearOrder() {
ds := goqu.From("test").Order(goqu.C("a").Asc())
sql, _, _ := ds.ClearOrder().ToSQL()
Expand Down
10 changes: 10 additions & 0 deletions exp/clauses.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type (
ClearOrder() Clauses
SetOrder(oes ...OrderedExpression) Clauses
OrderAppend(...OrderedExpression) Clauses
OrderPrepend(...OrderedExpression) Clauses

GroupBy() ColumnListExpression
SetGroupBy(cl ColumnListExpression) Clauses
Expand Down Expand Up @@ -285,6 +286,15 @@ func (c *clauses) OrderAppend(oes ...OrderedExpression) Clauses {
return ret
}

func (c *clauses) OrderPrepend(oes ...OrderedExpression) Clauses {
if c.order == nil {
return c.SetOrder(oes...)
}
ret := c.clone()
ret.order = NewOrderedColumnList(oes...).Append(ret.order.Columns()...)
return ret
}

func (c *clauses) GroupBy() ColumnListExpression {
return c.groupBy
}
Expand Down
13 changes: 13 additions & 0 deletions exp/clauses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@ func (ct *clausesTest) TestOrderAppend() {
assert.Equal(t, NewColumnListExpression(oe, oe2), c2.Order())
}

func (ct *clausesTest) TestOrderPrepend() {
t := ct.T()
oe := NewIdentifierExpression("", "", "a").Desc()
oe2 := NewIdentifierExpression("", "", "b").Desc()

c := NewClauses().SetOrder(oe)
c2 := c.OrderPrepend(oe2)

assert.Equal(t, NewColumnListExpression(oe), c.Order())

assert.Equal(t, NewColumnListExpression(oe2, oe), c2.Order())
}

func (ct *clausesTest) TestGroupBy() {
t := ct.T()
g := NewColumnListExpression(NewIdentifierExpression("", "", "a"))
Expand Down

0 comments on commit 921adc0

Please sign in to comment.