From a22f4200b4b57aec0188f47b9f33db9a42e78473 Mon Sep 17 00:00:00 2001 From: "dino.ma" Date: Mon, 4 Dec 2023 16:52:08 +0800 Subject: [PATCH] feat: add order by asc expr (#1034) * feat: add order by asc expr * feat: add order by asc expr --- do_test.go | 12 ++++++++++++ field/expr.go | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/do_test.go b/do_test.go index 75f1e8a0..d1bd6812 100644 --- a/do_test.go +++ b/do_test.go @@ -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 */ *", diff --git a/field/expr.go b/field/expr.go index a0722bdb..a8ff1ce2 100644 --- a/field/expr.go +++ b/field/expr.go @@ -48,6 +48,7 @@ type Expr interface { type OrderExpr interface { Expr Desc() Expr + ASC() Expr } type expression interface{} @@ -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})