Skip to content

Commit

Permalink
feat: add order by asc expr (go-gorm#1034)
Browse files Browse the repository at this point in the history
* feat: add order by asc expr

* feat: add order by asc expr
  • Loading branch information
dino-ma authored Dec 4, 2023
1 parent f534623 commit a22f420
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions do_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ func TestDO_methods(t *testing.T) {
Expr: u.Order(u.ID.Desc()).Order(u.Age),
Result: "ORDER BY `id` DESC,`age`",
},
{
Expr: u.Order(u.ID.Asc()),
Result: "ORDER BY `id` ASC",
},
{
Expr: u.Order(u.ID.Asc(), u.Age),
Result: "ORDER BY `id` ASC,`age`",
},
{
Expr: u.Order(u.ID.Asc()).Order(u.Age),
Result: "ORDER BY `id` ASC,`age`",
},
{
Expr: u.Clauses(hints.New("hint")).Select(),
Result: "SELECT /*+ hint */ *",
Expand Down
7 changes: 7 additions & 0 deletions field/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Expr interface {
type OrderExpr interface {
Expr
Desc() Expr
ASC() Expr
}

type expression interface{}
Expand Down Expand Up @@ -274,10 +275,16 @@ func (e expr) As(alias string) Expr {
return e
}

// Desc sort by desc
func (e expr) Desc() Expr {
return e.setE(clause.Expr{SQL: "? DESC", Vars: []interface{}{e.RawExpr()}})
}

// Asc sort by asc
func (e expr) Asc() Expr {
return e.setE(clause.Expr{SQL: "? ASC", Vars: []interface{}{e.RawExpr()}})
}

// ======================== general experssion ========================
func (e expr) value(value interface{}) AssignExpr {
return e.setE(clause.Eq{Column: e.col.Name, Value: value})
Expand Down

0 comments on commit a22f420

Please sign in to comment.