gitee.com/eden-framework/sqlx@v0.0.3/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.WriteRune(',') 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 73 e.WriteGroup(func(e *Ex) { 74 e.WriteExpr(o.target) 75 }) 76 77 if o.typ != "" { 78 e.WriteRune(' ') 79 e.WriteString(o.typ) 80 } 81 82 return e.Ex(ctx) 83 }