Skip to content

Commit a82c5d6

Browse files
committed
增加注释和说明,修正错误
1 parent 5613dcd commit a82c5d6

File tree

5 files changed

+154
-7
lines changed

5 files changed

+154
-7
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,34 @@ select查询返回的result还未能携带page、pageSize、total信息,需要
3939
4040
var ret []TestTable
4141
session.SetContext(ctx).Select("SELECT * FROM TBL_TEST").Param().Result(&ret)
42+
```
43+
44+
### 3、配置排序参数
45+
```cassandraql
46+
session := sessMgr.NewSession()
47+
ctx := pagehelper.OrderBy("myfield", pagehelper.DESC, context.Background())
48+
49+
var ret []TestTable
50+
session.SetContext(ctx).Select("SELECT * FROM TBL_TEST").Param().Result(&ret)
51+
```
52+
*注意:*
53+
54+
由于golang对order by不能使用placeholder的方式,所以存在注入风险,请谨慎使用排序功能,如果使用,则需要自己做防注入的工作。
55+
56+
举例:
57+
58+
在获得order by参数时做参数校验
59+
```$xslt
60+
valid := regexp.MustCompile("^[A-Za-z0-9_]+$")
61+
if !valid.MatchString(ordCol) {
62+
// invalid column name, do not proceed in order to prevent SQL injection
63+
}
64+
```
65+
66+
### 4、使用builder
67+
```$xslt
68+
session := sessMgr.NewSession()
69+
ctx := pagehelper.C(context.Background()).Page(1, 3).Order("test", pagehelper.ASC).Build()
70+
var ret []TestTable
71+
session.SetContext(ctx).Select("SELECT * FROM TBL_TEST").Param().Result(&ret)
4272
```

builder.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (C) 2019, Xiongfa Li.
2+
// All right reserved.
3+
// @author xiongfa.li
4+
// @version V1.0
5+
// Description:
6+
7+
package pagehelper
8+
9+
import "context"
10+
11+
type builder struct {
12+
ctx context.Context
13+
}
14+
15+
//创建builder
16+
//ctx 初始context
17+
func C(ctx context.Context) *builder {
18+
return &builder{ctx: ctx}
19+
}
20+
21+
//分页
22+
//page 页码
23+
//pageSize 分页大小
24+
func (b *builder) Page(page, pageSize int) *builder {
25+
b.ctx = StartPage(page, pageSize, b.ctx)
26+
return b
27+
}
28+
29+
//手动指定字段和排序
30+
//field 字段
31+
//order 排序 [ASC | DESC]
32+
func (b *builder) Order(field, order string) *builder {
33+
b.ctx = OrderBy(field, order, b.ctx)
34+
return b
35+
}
36+
37+
//升序(默认)
38+
//field 字段
39+
func (b *builder) ASC(field string) *builder {
40+
b.ctx = OrderBy(field, ASC, b.ctx)
41+
return b
42+
}
43+
44+
//降序
45+
//field 字段
46+
func (b *builder) DESC(field string) *builder {
47+
b.ctx = OrderBy(field, DESC, b.ctx)
48+
return b
49+
}
50+
51+
//获得含分页/排序信息的context
52+
func (b *builder) Build() context.Context {
53+
return b.ctx
54+
}

pagehelper.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,18 @@ type Executor struct {
5252
log logging.LogFunc
5353
}
5454

55+
//分页
56+
//page 页码
57+
//pageSize 分页大小
58+
//ctx 初始context
5559
func StartPage(page, pageSize int, ctx context.Context) context.Context {
5660
return context.WithValue(ctx, pageHelperValue, &PageParam{Page: page, PageSize: pageSize})
5761
}
5862

63+
//排序
64+
//field 字段
65+
//order 排序 [ASC | DESC]
66+
//ctx 初始context
5967
func OrderBy(field, order string, ctx context.Context) context.Context {
6068
return context.WithValue(ctx, orderHelperValue, &OrderParam{Field: field, Order: order})
6169
}
@@ -84,7 +92,7 @@ func (exec *Executor) Query(ctx context.Context, result reflection.Object, sql s
8492
o := ctx.Value(orderHelperValue)
8593
if o != nil {
8694
if param, ok := o.(*OrderParam); ok {
87-
sql, params = modifySqlOrder(sql, param, params)
95+
sql = modifySqlOrder(sql, param)
8896
}
8997
}
9098

@@ -122,15 +130,14 @@ func (f *Factory) CreateExecutor(transaction transaction.Transaction) executor.E
122130
}
123131
}
124132

125-
func modifySqlOrder(sql string, p *OrderParam, params []interface{}) (string, []interface{}) {
133+
func modifySqlOrder(sql string, p *OrderParam) string {
126134
if p.Field == "" {
127-
return sql, params
135+
return sql
128136
}
129137
b := strings.Builder{}
130138
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
139+
b.WriteString(fmt.Sprintf(" ORDER BY `%s` %s ", p.Field, p.Order))
140+
return b.String()
134141
}
135142

136143
func modifySql(sql string, p *PageParam) string {

pagehelper_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestModifyPage(t *testing.T) {
129129
}
130130

131131
func order(sql string, params ...interface{}) (string, []interface{}) {
132-
return modifySqlOrder(sql, &OrderParam{"test", ASC}, params)
132+
return modifySqlOrder(sql, &OrderParam{"test", ASC}), params
133133
}
134134

135135
func TestModifyOrder(t *testing.T) {

test/builder_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (C) 2019, Xiongfa Li.
2+
// All right reserved.
3+
// @author xiongfa.li
4+
// @version V1.0
5+
// Description:
6+
7+
package test
8+
9+
import (
10+
"context"
11+
"github.com/xfali/pagehelper"
12+
"testing"
13+
"time"
14+
)
15+
16+
const (
17+
pageHelperValue = "_page_helper_value"
18+
orderHelperValue = "_order_helper_value"
19+
)
20+
21+
func TestBuilder(t *testing.T) {
22+
ctx := pagehelper.C(context.Background()).Page(1, 3).Order("test", pagehelper.ASC).Build()
23+
ctx, _ = context.WithTimeout(ctx, time.Second)
24+
p := ctx.Value(pageHelperValue)
25+
o := ctx.Value(orderHelperValue)
26+
27+
printOrder(t, o)
28+
printPage(t, p)
29+
}
30+
31+
func TestBuilder2(t *testing.T) {
32+
ctx := pagehelper.C(context.Background()).Page(1, 3).Order("test", pagehelper.ASC).Build()
33+
ctx = pagehelper.C(ctx).DESC("new_field").Build()
34+
ctx, _ = context.WithTimeout(ctx, time.Second)
35+
p := ctx.Value(pageHelperValue)
36+
o := ctx.Value(orderHelperValue)
37+
38+
printOrder(t, o)
39+
printPage(t, p)
40+
}
41+
42+
func printPage(t *testing.T, p interface{}) {
43+
if p, ok := p.(*pagehelper.PageParam); ok {
44+
t.Logf("page param: %d %d", p.Page, p.PageSize)
45+
} else {
46+
t.Fail()
47+
}
48+
}
49+
50+
func printOrder(t *testing.T, p interface{}) {
51+
if p, ok := p.(*pagehelper.OrderParam); ok {
52+
t.Logf("order param: %s %s", p.Field, p.Order)
53+
} else {
54+
t.Fail()
55+
}
56+
}

0 commit comments

Comments
 (0)