Skip to content

Commit 5613dcd

Browse files
committed
增加排序支持
1 parent 4d87468 commit 5613dcd

File tree

2 files changed

+149
-17
lines changed

2 files changed

+149
-17
lines changed

pagehelper.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,18 @@ import (
2222
)
2323

2424
const (
25-
pageHelperValue = "_page_helper_value"
25+
pageHelperValue = "_page_helper_value"
26+
orderHelperValue = "_order_helper_value"
27+
28+
ASC = "ASC"
29+
DESC = "DESC"
2630
)
2731

32+
type OrderParam struct {
33+
Field string
34+
Order string
35+
}
36+
2837
type PageParam struct {
2938
Page int
3039
PageSize int
@@ -47,6 +56,10 @@ func StartPage(page, pageSize int, ctx context.Context) context.Context {
4756
return context.WithValue(ctx, pageHelperValue, &PageParam{Page: page, PageSize: pageSize})
4857
}
4958

59+
func OrderBy(field, order string, ctx context.Context) context.Context {
60+
return context.WithValue(ctx, orderHelperValue, &OrderParam{Field: field, Order: order})
61+
}
62+
5063
func (exec *Executor) Close(rollback bool) {
5164
exec.exec.Close(rollback)
5265
}
@@ -68,14 +81,20 @@ func (exec *Executor) Rollback(require bool) error {
6881
}
6982

7083
func (exec *Executor) Query(ctx context.Context, result reflection.Object, sql string, params ...interface{}) error {
84+
o := ctx.Value(orderHelperValue)
85+
if o != nil {
86+
if param, ok := o.(*OrderParam); ok {
87+
sql, params = modifySqlOrder(sql, param, params)
88+
}
89+
}
90+
7191
p := ctx.Value(pageHelperValue)
7292
if p != nil {
7393
if param, ok := p.(*PageParam); ok {
7494
sql = modifySql(sql, param)
75-
exec.log(logging.INFO, "PageHelper Query: [%s]", sql)
7695
}
7796
}
78-
97+
exec.log(logging.DEBUG, "PageHelper Query: [%s], params: %s\n", sql, fmt.Sprint(params))
7998
return exec.exec.Query(ctx, result, sql, params...)
8099
}
81100

@@ -103,6 +122,17 @@ func (f *Factory) CreateExecutor(transaction transaction.Transaction) executor.E
103122
}
104123
}
105124

125+
func modifySqlOrder(sql string, p *OrderParam, params []interface{}) (string, []interface{}) {
126+
if p.Field == "" {
127+
return sql, params
128+
}
129+
b := strings.Builder{}
130+
b.WriteString(strings.TrimSpace(sql))
131+
b.WriteString(fmt.Sprintf(" ORDER BY ? %s ", p.Order))
132+
params = append(params, p.Field)
133+
return b.String(), params
134+
}
135+
106136
func modifySql(sql string, p *PageParam) string {
107137
b := strings.Builder{}
108138
b.WriteString(strings.TrimSpace(sql))

pagehelper_test.go

Lines changed: 116 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/xfali/gobatis"
1414
"github.com/xfali/gobatis/factory"
1515
"github.com/xfali/gobatis/logging"
16+
"strings"
1617
"testing"
1718
"time"
1819
)
@@ -25,17 +26,76 @@ type TestTable struct {
2526
}
2627

2728
func TestPageHelper(t *testing.T) {
28-
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
29-
ctx = StartPage(1, 2, ctx)
30-
31-
p := ctx.Value(pageHelperValue)
32-
printV(t, p)
33-
34-
select {
35-
case <-ctx.Done():
36-
break
37-
}
38-
printV(t, p)
29+
t.Run("StartPage", func(t *testing.T) {
30+
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
31+
ctx = StartPage(1, 2, ctx)
32+
33+
p := ctx.Value(pageHelperValue)
34+
printPage(t, p)
35+
36+
select {
37+
case <-ctx.Done():
38+
break
39+
}
40+
printPage(t, p)
41+
})
42+
43+
t.Run("OrderBy", func(t *testing.T) {
44+
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
45+
ctx = OrderBy("test", ASC, ctx)
46+
47+
p := ctx.Value(orderHelperValue)
48+
printOrder(t, p)
49+
50+
select {
51+
case <-ctx.Done():
52+
break
53+
}
54+
printOrder(t, p)
55+
})
56+
57+
t.Run("PageHelper and OrderBy", func(t *testing.T) {
58+
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
59+
ctx = OrderBy("test", ASC, ctx)
60+
ctx = StartPage(1, 2, ctx)
61+
62+
o := ctx.Value(orderHelperValue)
63+
printOrder(t, o)
64+
65+
p := ctx.Value(pageHelperValue)
66+
printPage(t, p)
67+
68+
select {
69+
case <-ctx.Done():
70+
break
71+
}
72+
printPage(t, p)
73+
printOrder(t, o)
74+
})
75+
76+
t.Run("complex", func(t *testing.T) {
77+
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
78+
ctx = OrderBy("test", ASC, ctx)
79+
ctx = StartPage(1, 2, ctx)
80+
ctx = StartPage(3, 10, ctx)
81+
ctx = OrderBy("tat", DESC, ctx)
82+
ctx, _ = context.WithTimeout(ctx, time.Second)
83+
84+
now := time.Now()
85+
o := ctx.Value(orderHelperValue)
86+
printOrder(t, o)
87+
t.Logf("time :%d ms \n", time.Since(now)/time.Millisecond)
88+
89+
p := ctx.Value(pageHelperValue)
90+
printPage(t, p)
91+
92+
select {
93+
case <-ctx.Done():
94+
break
95+
}
96+
printPage(t, p)
97+
printOrder(t, o)
98+
})
3999
}
40100

41101
func TestPageHelper2(t *testing.T) {
@@ -63,14 +123,56 @@ func TestPageHelper2(t *testing.T) {
63123
session.Select("SELECT * FROM TBL_TEST").Param().Result(&ret)
64124
}
65125

66-
func TestModify(t *testing.T) {
126+
func TestModifyPage(t *testing.T) {
67127
sql := modifySql("select * from x", &PageParam{1, 2})
68128
t.Log(sql)
69129
}
70130

71-
func printV(t *testing.T, p interface{}) {
131+
func order(sql string, params ...interface{}) (string, []interface{}) {
132+
return modifySqlOrder(sql, &OrderParam{"test", ASC}, params)
133+
}
134+
135+
func TestModifyOrder(t *testing.T) {
136+
sql, p := order("select ? from x", "field1")
137+
t.Log(sql)
138+
if len(p) != 2 {
139+
t.Fatal()
140+
}
141+
for _, v := range p {
142+
t.Log(v)
143+
}
144+
}
145+
146+
func TestModifyOrderAndPage(t *testing.T) {
147+
sql, p := order("select ? from x", "field1")
148+
t.Log(sql)
149+
if len(p) != 2 {
150+
t.Fatal()
151+
}
152+
153+
sql = modifySql(sql, &PageParam{1, 2})
154+
155+
t.Log(sql)
156+
for _, v := range p {
157+
t.Log(v)
158+
}
159+
160+
if strings.TrimSpace(sql) != "select ? from x ORDER BY ? ASC LIMIT 2, 2" {
161+
t.Fail()
162+
}
163+
}
164+
165+
func printPage(t *testing.T, p interface{}) {
72166
if p, ok := p.(*PageParam); ok {
73-
t.Logf("param: %d %d", p.Page, p.PageSize)
167+
t.Logf("page param: %d %d", p.Page, p.PageSize)
168+
} else {
169+
t.Fail()
170+
}
171+
}
172+
173+
func printOrder(t *testing.T, p interface{}) {
174+
if p, ok := p.(*OrderParam); ok {
175+
t.Logf("order param: %s %s", p.Field, p.Order)
74176
} else {
75177
t.Fail()
76178
}

0 commit comments

Comments
 (0)