-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.go
215 lines (190 loc) · 5.98 KB
/
plugin.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package xormmodule
import (
"github.com/go-xorm/xorm"
"github.com/revel/revel"
)
type PostInitProcessorFunc func(*xorm.Engine)
type SessionHandlerFunc func(*xorm.Session) error
var (
Engine *xorm.Engine
Driver string
Spec string
MaxIdleConns int
MaxOpenConns int
ShowSQL bool
ShowDebug bool
postProcessors []PostInitProcessorFunc
)
func Init() {
// Read configuration.
var found bool
if Driver, found = revel.Config.String("db.driver"); !found {
revel.ERROR.Fatal("No db.driver found.")
}
if Spec, found = revel.Config.String("db.spec"); !found {
revel.ERROR.Fatal("No db.spec found.")
}
// create Xorm engine
var err error
Engine, err = xorm.NewEngine(Driver, Spec)
if err != nil {
revel.ERROR.Fatal(err)
return
}
if MaxIdleConns, found := revel.Config.Int("db.maxidleconns"); found {
Engine.SetMaxIdleConns(MaxIdleConns)
}
if MaxOpenConns, found := revel.Config.Int("db.maxopenconns"); found {
Engine.SetMaxOpenConns(MaxOpenConns)
}
if ShowSQL, found := revel.Config.Bool("xorm.showsql"); found {
Engine.ShowSQL = ShowSQL
}
if ShowDebug, found := revel.Config.Bool("xorm.showdebug"); found {
Engine.ShowDebug = ShowDebug
}
for _, processor := range postProcessors {
processor(Engine)
}
}
// Add post xorm.Engine init. handler. Common use cases are sync domain models, custom mapper, etc.
func AddPostInitProcessor(processor PostInitProcessorFunc) {
if postProcessors == nil {
postProcessors = []PostInitProcessorFunc{}
}
if processor != nil {
postProcessors = append(postProcessors, processor)
}
}
// XormController to be added as anonymous member to the revel controller struct, with XormController.Engine attached.
//
// type MyXormController struct {
// *revel.Controller
// xormmodule.XormController
// }
//
type XormController struct {
Engine *xorm.Engine
XormSession *xorm.Session
}
// Create xorm.Session and attached to XormSession member if not already attached.
func (c *XormController) AttachSession() {
if c.XormSession == nil {
c.XormSession = c.Engine.NewSession()
}
}
// Detach XormSession member and call xorm.Session.Close() if has attached XormSession.
func (c *XormController) DetachSession() {
if c.XormSession != nil {
c.XormSession.Close()
c.XormSession = nil
}
}
// Attach XormSession and call handler.
//
// func (c MyXormController) List() revel.Result {
// c.WithSession(func(s *xorm.Session) {
// // fetch list from s
// })
// }
func (c *XormController) WithSession(handler SessionHandlerFunc) error {
c.AttachSession()
return handler(c.XormSession)
}
// Create a new xorm.Session and call handler, the xorm.Session will be closed after handler called. Where
// common use cases being required async DB operation in another thread.
//
// func (c MyXormController) List() revel.Result {
// // async fetch xorm.Session operation
// go c.WithNewSession(func(s *xorm.Session) {
// // fetch list from s
// })
// }
func (c *XormController) WithNewSession(handler SessionHandlerFunc) error {
session := c.Engine.NewSession()
defer session.Close()
return handler(session)
}
func (c *XormController) doTransaction(session *xorm.Session, handler SessionHandlerFunc) error {
err := session.Begin()
if err != nil {
return err
}
if err = handler(session); err == nil {
session.Commit()
} else {
session.Rollback()
}
return err
}
// Begin a SQL transaction and if handler did not return error
// it will commit the transaction, otherwise rollback the transaction.
// This will use attached XormSession if already attached.
func (c *XormController) WithTx(handler SessionHandlerFunc) error {
if c.XormSession != nil {
return c.doTransaction(c.XormSession, handler)
} else {
return c.WithNewTx(handler)
}
}
// Begin a SQL transaction and if handler did not return error
// it will commit the transaction, otherwise rollback the transaction.
// This will create a new xorm.Session for the handler. Where
// common use cases being required async DB operation in another thread.
func (c *XormController) WithNewTx(handler SessionHandlerFunc) error {
session := c.Engine.NewSession()
defer session.Close()
return c.doTransaction(session, handler)
}
// Attach XormController.Engine, this is automatically done at revel.BEFORE revel.InterceptMethod step.
func (c *XormController) Attach() revel.Result {
revel.TRACE.Printf("(*XormController).Attach")
c.Engine = Engine
return nil
}
// Detach XormController.Engine, this is automatically done at revel.FINALLY revel.InterceptMethod step.
func (c *XormController) Detach() revel.Result {
revel.TRACE.Printf("(*XormController).Detach")
if c.XormSession != nil {
c.XormSession.Rollback()
c.XormSession.Close()
c.XormSession = nil
}
c.Engine = nil
return nil
}
// Commit and Close attached XormSession, this is automatically done at revel.AFTER revel.InterceptMethod step.
// Issue panic if commit XormSession is undesired.
func (c *XormController) Commit() revel.Result {
revel.TRACE.Printf("(*XormController).Commit")
if c.XormSession != nil {
c.XormSession.Commit()
c.XormSession.Close()
c.XormSession = nil
}
return nil
}
// XormSessionController to be added as anonymous member to the revel controller struct, with XormController.Engine
// and XormController.XormSession attached.
//
// type MyXormSessionController struct {
// *revel.Controller
// xormmodule.XormSessionController
// }
//
type XormSessionController struct {
XormController
}
// Attach XormSessionController.XormSession, this is automatically done at revel.BEFORE revel.InterceptMethod step.
func (c *XormSessionController) Attach() revel.Result {
revel.TRACE.Printf("(*XormSessionController).Attach")
c.XormSession = c.Engine.NewSession()
return nil
}
func init() {
revel.InterceptMethod((*XormController).Attach, revel.BEFORE)
revel.InterceptMethod((*XormController).Commit, revel.AFTER)
revel.InterceptMethod((*XormController).Detach, revel.FINALLY)
revel.InterceptMethod((*XormSessionController).Attach, revel.BEFORE)
revel.OnAppStart(Init)
}