Skip to content

Commit 4d87468

Browse files
committed
修改实现为静态代理
1 parent 2e88a9c commit 4d87468

File tree

4 files changed

+117
-29
lines changed

4 files changed

+117
-29
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ select查询返回的result还未能携带page、pageSize、total信息,需要
1515
### 1、使用pagehelper的Factory生成SessionManager
1616

1717
```cassandraql
18-
fac := factory.DefaultFactory{
19-
Host: "localhost",
20-
Port: 3306,
21-
DBName: "test",
22-
Username: "root",
23-
Password: "123",
24-
Charset: "utf8",
25-
26-
MaxConn: 1000,
27-
MaxIdleConn: 500,
28-
29-
Log: logging.DefaultLogf,
30-
}
31-
sessMgr := gobatis.NewSessionManager(pagehelper.New(&fac))
18+
pFac := pagehelper.New(&factory.DefaultFactory{
19+
Host: "localhost",
20+
Port: 3306,
21+
DBName: "test",
22+
Username: "root",
23+
Password: "123",
24+
Charset: "utf8",
25+
26+
MaxConn: 1000,
27+
MaxIdleConn: 500,
28+
29+
Log: logging.DefaultLogf,
30+
})
31+
pFac.InitDB()
32+
sessMgr := gobatis.NewSessionManager(pFac)
3233
```
3334

3435
### 2、配置分页参数
3536
```cassandraql
3637
session := sessMgr.NewSession()
37-
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
38-
ctx = pagehelper.StartPage(1, 2, ctx)
38+
ctx := pagehelper.StartPage(1, 10, context.Background())
3939
4040
var ret []TestTable
4141
session.SetContext(ctx).Select("SELECT * FROM TBL_TEST").Param().Result(&ret)

factory.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 (
10+
"github.com/xfali/gobatis/executor"
11+
"github.com/xfali/gobatis/logging"
12+
"github.com/xfali/gobatis/session"
13+
"github.com/xfali/gobatis/transaction"
14+
)
15+
16+
type IFactory struct {
17+
InitDBFunc func() error
18+
CreateTransactionFunc func() transaction.Transaction
19+
CreateExecutorFunc func(transaction.Transaction) executor.Executor
20+
CreateSessionFunc func() session.SqlSession
21+
LogFuncFunc func() logging.LogFunc
22+
}
23+
24+
func (f *IFactory) InitDB() error {
25+
return f.InitDBFunc()
26+
}
27+
28+
func (f *IFactory) CreateTransaction() transaction.Transaction {
29+
return f.CreateTransactionFunc()
30+
}
31+
32+
func (f *IFactory) CreateSession() session.SqlSession {
33+
tx := f.CreateTransactionFunc()
34+
return session.NewDefaultSqlSession(f.LogFuncFunc(), tx, f.CreateExecutorFunc(tx), false)
35+
}
36+
37+
func (f *IFactory) LogFunc() logging.LogFunc {
38+
return f.LogFuncFunc()
39+
}
40+
41+
func (f *IFactory) CreateExecutor(transaction transaction.Transaction) executor.Executor {
42+
return f.CreateExecutorFunc(transaction)
43+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github/xfali/pagehelper
1+
module github.com/xfali/pagehelper
22

33
go 1.12
44

pagehelper.go

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ package pagehelper
1111
import (
1212
"context"
1313
"fmt"
14+
"github.com/xfali/gobatis/common"
1415
"github.com/xfali/gobatis/executor"
1516
"github.com/xfali/gobatis/factory"
17+
"github.com/xfali/gobatis/logging"
1618
"github.com/xfali/gobatis/reflection"
19+
"github.com/xfali/gobatis/session"
1720
"github.com/xfali/gobatis/transaction"
1821
"strings"
1922
)
@@ -27,40 +30,82 @@ type PageParam struct {
2730
PageSize int
2831
}
2932

30-
func New(f *factory.DefaultFactory) *PageHelperFactory {
31-
return &PageHelperFactory{*f}
33+
func New(f factory.Factory) *Factory {
34+
return &Factory{f}
3235
}
3336

34-
type PageHelperFactory struct {
35-
factory.DefaultFactory
37+
type Factory struct {
38+
fac factory.Factory
3639
}
3740

38-
type PageHelperExecutor struct {
39-
executor.SimpleExecutor
41+
type Executor struct {
42+
exec executor.Executor
43+
log logging.LogFunc
4044
}
4145

4246
func StartPage(page, pageSize int, ctx context.Context) context.Context {
4347
return context.WithValue(ctx, pageHelperValue, &PageParam{Page: page, PageSize: pageSize})
4448
}
4549

46-
func (exec *PageHelperExecutor) Query(ctx context.Context, result reflection.Object, sql string, params ...interface{}) error {
50+
func (exec *Executor) Close(rollback bool) {
51+
exec.exec.Close(rollback)
52+
}
53+
54+
func (exec *Executor) Exec(ctx context.Context, sql string, params ...interface{}) (common.Result, error) {
55+
return exec.exec.Exec(ctx, sql, params...)
56+
}
57+
58+
func (exec *Executor) Begin() error {
59+
return exec.exec.Begin()
60+
}
61+
62+
func (exec *Executor) Commit(require bool) error {
63+
return exec.exec.Commit(require)
64+
}
65+
66+
func (exec *Executor) Rollback(require bool) error {
67+
return exec.exec.Rollback(require)
68+
}
69+
70+
func (exec *Executor) Query(ctx context.Context, result reflection.Object, sql string, params ...interface{}) error {
4771
p := ctx.Value(pageHelperValue)
4872
if p != nil {
4973
if param, ok := p.(*PageParam); ok {
5074
sql = modifySql(sql, param)
75+
exec.log(logging.INFO, "PageHelper Query: [%s]", sql)
5176
}
5277
}
5378

54-
return exec.SimpleExecutor.Query(ctx, result, sql, params...)
79+
return exec.exec.Query(ctx, result, sql, params...)
5580
}
5681

57-
func (f *PageHelperFactory) CreateExecutor(transaction transaction.Transaction) executor.Executor {
58-
return &PageHelperExecutor{*executor.NewSimpleExecutor(transaction)}
82+
func (f *Factory) InitDB() error {
83+
return f.fac.InitDB()
84+
}
85+
86+
func (f *Factory) CreateTransaction() transaction.Transaction {
87+
return f.fac.CreateTransaction()
88+
}
89+
90+
func (f *Factory) CreateSession() session.SqlSession {
91+
tx := f.CreateTransaction()
92+
return session.NewDefaultSqlSession(f.LogFunc(), tx, f.CreateExecutor(tx), false)
93+
}
94+
95+
func (f *Factory) LogFunc() logging.LogFunc {
96+
return f.fac.LogFunc()
97+
}
98+
99+
func (f *Factory) CreateExecutor(transaction transaction.Transaction) executor.Executor {
100+
return &Executor{
101+
exec: executor.NewSimpleExecutor(transaction),
102+
log: f.LogFunc(),
103+
}
59104
}
60105

61106
func modifySql(sql string, p *PageParam) string {
62107
b := strings.Builder{}
63-
b.WriteString(sql)
64-
b.WriteString(fmt.Sprintf(" OFFSET %d LIMIT %d ", p.Page*p.PageSize, p.PageSize))
108+
b.WriteString(strings.TrimSpace(sql))
109+
b.WriteString(fmt.Sprintf(" LIMIT %d, %d ", p.Page*p.PageSize, p.PageSize))
65110
return b.String()
66111
}

0 commit comments

Comments
 (0)