github.com/kunlun-qilian/sqlx/v2@v2.24.0/builder/addition_order_by.go (about) 1 package builder 2 3 import ( 4 "context" 5 ) 6 7 type OrderByAddition struct { 8 } 9 10 func (OrderByAddition) AdditionType() AdditionType { 11 return AdditionOrderBy 12 } 13 14 func OrderBy(orders ...*Order) *orderBy { 15 finalOrders := make([]*Order, 0) 16 17 for i := range orders { 18 if IsNilExpr(orders[i]) { 19 continue 20 } 21 finalOrders = append(finalOrders, orders[i]) 22 } 23 24 return &orderBy{ 25 orders: finalOrders, 26 } 27 } 28 29 var _ Addition = (*orderBy)(nil) 30 31 type orderBy struct { 32 OrderByAddition 33 orders []*Order 34 } 35 36 func (o *orderBy) IsNil() bool { 37 return o == nil || len(o.orders) == 0 38 } 39 40 func (o *orderBy) Ex(ctx context.Context) *Ex { 41 e := Expr("ORDER BY ") 42 for i := range o.orders { 43 if i > 0 { 44 e.WriteQueryByte(',') 45 } 46 e.WriteExpr(o.orders[i]) 47 } 48 return e.Ex(ctx) 49 } 50 51 func AscOrder(target SqlExpr) *Order { 52 return &Order{target: target, typ: "ASC"} 53 } 54 55 func DescOrder(target SqlExpr) *Order { 56 return &Order{target: target, typ: "DESC"} 57 } 58 59 var _ SqlExpr = (*Order)(nil) 60 61 type Order struct { 62 target SqlExpr 63 typ string 64 } 65 66 func (o *Order) IsNil() bool { 67 return o == nil || IsNilExpr(o.target) 68 } 69 70 func (o *Order) Ex(ctx context.Context) *Ex { 71 e := Expr("") 72 e.Grow(1) 73 74 e.WriteGroup(func(e *Ex) { 75 e.WriteExpr(o.target) 76 }) 77 78 if o.typ != "" { 79 e.WriteQueryByte(' ') 80 e.WriteQuery(o.typ) 81 } 82 83 return e.Ex(ctx) 84 }