@@ -11,9 +11,12 @@ package pagehelper
11
11
import (
12
12
"context"
13
13
"fmt"
14
+ "github.com/xfali/gobatis/common"
14
15
"github.com/xfali/gobatis/executor"
15
16
"github.com/xfali/gobatis/factory"
17
+ "github.com/xfali/gobatis/logging"
16
18
"github.com/xfali/gobatis/reflection"
19
+ "github.com/xfali/gobatis/session"
17
20
"github.com/xfali/gobatis/transaction"
18
21
"strings"
19
22
)
@@ -27,40 +30,82 @@ type PageParam struct {
27
30
PageSize int
28
31
}
29
32
30
- func New (f * factory.DefaultFactory ) * PageHelperFactory {
31
- return & PageHelperFactory { * f }
33
+ func New (f factory.Factory ) * Factory {
34
+ return & Factory { f }
32
35
}
33
36
34
- type PageHelperFactory struct {
35
- factory.DefaultFactory
37
+ type Factory struct {
38
+ fac factory.Factory
36
39
}
37
40
38
- type PageHelperExecutor struct {
39
- executor.SimpleExecutor
41
+ type Executor struct {
42
+ exec executor.Executor
43
+ log logging.LogFunc
40
44
}
41
45
42
46
func StartPage (page , pageSize int , ctx context.Context ) context.Context {
43
47
return context .WithValue (ctx , pageHelperValue , & PageParam {Page : page , PageSize : pageSize })
44
48
}
45
49
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 {
47
71
p := ctx .Value (pageHelperValue )
48
72
if p != nil {
49
73
if param , ok := p .(* PageParam ); ok {
50
74
sql = modifySql (sql , param )
75
+ exec .log (logging .INFO , "PageHelper Query: [%s]" , sql )
51
76
}
52
77
}
53
78
54
- return exec .SimpleExecutor .Query (ctx , result , sql , params ... )
79
+ return exec .exec .Query (ctx , result , sql , params ... )
55
80
}
56
81
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
+ }
59
104
}
60
105
61
106
func modifySql (sql string , p * PageParam ) string {
62
107
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 ))
65
110
return b .String ()
66
111
}
0 commit comments