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