Skip to content

Commit

Permalink
implement orderBy for SqlFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
GlebBeloded committed Jan 28, 2021
1 parent 865b53c commit 771c22c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
21 changes: 21 additions & 0 deletions dialect/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,27 @@ func (pt *postgresTest) TestWindowFunction() {
}, entries)
}

func (pt *postgresTest) TestOrderByFunction() {
ds := pt.db.From("entry").
Select(goqu.ROW_NUMBER().Over(goqu.W()).As("id")).Window().Order(goqu.ROW_NUMBER().Over(goqu.W()).Desc())

var entries []entry
pt.NoError(ds.ScanStructs(&entries))

pt.Equal([]entry{
{ID: 10},
{ID: 9},
{ID: 8},
{ID: 7},
{ID: 6},
{ID: 5},
{ID: 4},
{ID: 3},
{ID: 2},
{ID: 1},
}, entries)
}

func TestPostgresSuite(t *testing.T) {
suite.Run(t, new(postgresTest))
}
2 changes: 2 additions & 0 deletions exp/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ type (
Aliaseable
Rangeable
Comparable
Orderable
Isable
Inable
Likeable
Expand All @@ -414,6 +415,7 @@ type (
Aliaseable
Rangeable
Comparable
Orderable
Isable
Inable
Likeable
Expand Down
3 changes: 3 additions & 0 deletions exp/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ func (sfe sqlFunctionExpression) Over(we WindowExpression) SQLWindowFunctionExpr
func (sfe sqlFunctionExpression) OverName(windowName IdentifierExpression) SQLWindowFunctionExpression {
return NewSQLWindowFunctionExpression(sfe, windowName, nil)
}

func (sfe sqlFunctionExpression) Asc() OrderedExpression { return asc(sfe) }
func (sfe sqlFunctionExpression) Desc() OrderedExpression { return desc(sfe) }
2 changes: 2 additions & 0 deletions exp/func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func (sfes *sqlFunctionExpressionSuite) TestAllOthers() {
{Ex: fn.IsNotTrue(), Expected: NewBooleanExpression(IsNotOp, fn, true)},
{Ex: fn.IsFalse(), Expected: NewBooleanExpression(IsOp, fn, false)},
{Ex: fn.IsNotFalse(), Expected: NewBooleanExpression(IsNotOp, fn, false)},
{Ex: fn.Desc(), Expected: desc(fn)},
{Ex: fn.Asc(), Expected: asc(fn)},
}

for _, tc := range testCases {
Expand Down
3 changes: 3 additions & 0 deletions exp/window_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ func (swfe sqlWindowFunctionExpression) IsNotTrue() BooleanExpression { return
func (swfe sqlWindowFunctionExpression) IsFalse() BooleanExpression { return is(swfe, false) }
func (swfe sqlWindowFunctionExpression) IsNotFalse() BooleanExpression { return isNot(swfe, false) }

func (swfe sqlWindowFunctionExpression) Asc() OrderedExpression { return asc(swfe) }
func (swfe sqlWindowFunctionExpression) Desc() OrderedExpression { return desc(swfe) }

func (swfe sqlWindowFunctionExpression) Func() SQLFunctionExpression {
return swfe.fn
}
Expand Down
2 changes: 2 additions & 0 deletions exp/window_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func (swfet *sqlWindowFunctionExpressionTest) TestAllOthers() {
{Ex: wf.IsNotTrue(), Expected: NewBooleanExpression(IsNotOp, wf, true)},
{Ex: wf.IsFalse(), Expected: NewBooleanExpression(IsOp, wf, false)},
{Ex: wf.IsNotFalse(), Expected: NewBooleanExpression(IsNotOp, wf, false)},
{Ex: wf.Desc(), Expected: desc(wf)},
{Ex: wf.Asc(), Expected: asc(wf)},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 771c22c

Please sign in to comment.