-
Notifications
You must be signed in to change notification settings - Fork 3
/
orderby.go
65 lines (53 loc) · 1.44 KB
/
orderby.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
package sqlbuilder
import (
"bytes"
"strings"
)
//
// Author: 陈永佳 chenyongjia@parkingwang.com, yoojiachen@gmail.com
//
type OrderByBuilder struct {
ctx *SQLContext
buffer *bytes.Buffer
}
func newOrderByBuilder(ctx *SQLContext, pre string, columns ...string) *OrderByBuilder {
if len(columns) == 0 {
panic("Columns is required for ORDER BY keyword")
}
ob := &OrderByBuilder{
ctx: ctx,
buffer: new(bytes.Buffer),
}
ob.buffer.WriteString(pre)
ob.buffer.WriteString(" ORDER BY ")
ob.buffer.WriteString(strings.Join(MapStr(columns, ctx.escapeName), ","))
return ob
}
func (slf *OrderByBuilder) Column(column string) *OrderByBuilder {
slf.buffer.WriteString(", ")
slf.buffer.WriteString(slf.ctx.escapeName(column))
return slf
}
func (slf *OrderByBuilder) ASC() *OrderByBuilder {
slf.buffer.WriteString(" ASC")
return slf
}
func (slf *OrderByBuilder) DESC() *OrderByBuilder {
slf.buffer.WriteString(" DESC")
return slf
}
func (slf *OrderByBuilder) Limit(limit int) *LimitBuilder {
return newLimitBuilder(slf.ctx, slf.Compile(), limit)
}
func (slf *OrderByBuilder) GroupBy(columns ...string) *GroupByBuilder {
return newGroupByBuilder(slf.ctx, slf.Compile(), columns...)
}
func (slf *OrderByBuilder) Compile() string {
return slf.buffer.String()
}
func (slf *OrderByBuilder) ToSQL() string {
return sqlEndpoint(slf.buffer)
}
func (slf *OrderByBuilder) Execute() *Executor {
return newExecute(slf.ToSQL(), slf.ctx.prepare)
}