-
Notifications
You must be signed in to change notification settings - Fork 8
/
sqlrunner.go
397 lines (346 loc) · 10.6 KB
/
sqlrunner.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Licensed to the AcmeStack under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gobatis
import (
"context"
"github.com/acmestack/gobatis/errors"
"github.com/acmestack/gobatis/factory"
"github.com/acmestack/gobatis/logging"
"github.com/acmestack/gobatis/parsing/sqlparser"
"github.com/acmestack/gobatis/reflection"
"github.com/acmestack/gobatis/session"
)
type SessionManager struct {
factory factory.Factory
ParserFactory ParserFactory
}
func NewSessionManager(factory factory.Factory) *SessionManager {
return &SessionManager{
factory: factory,
ParserFactory: DynamicParserFactory,
}
}
type Runner interface {
// Param 参数
// 注意:如果没有参数也必须调用
// 如果参数个数为1并且为struct,将解析struct获得参数
// 如果参数个数大于1并且全部为简单类型,或则个数为1且为简单类型,则使用这些参数
Param(params ...interface{}) Runner
// Result 获得结果
Result(bean interface{}) error
// LastInsertId 最后插入的自增id
LastInsertId() int64
// Context 设置Context
Context(ctx context.Context) Runner
}
type Session struct {
ctx context.Context
log logging.LogFunc
session session.SqlSession
driver string
ParserFactory ParserFactory
}
type BaseRunner struct {
session session.SqlSession
sqlParser sqlparser.SqlParser
action string
metadata *sqlparser.Metadata
log logging.LogFunc
driver string
ctx context.Context
runner Runner
}
type SelectRunner struct {
BaseRunner
}
type InsertRunner struct {
lastId int64
BaseRunner
}
type UpdateRunner struct {
BaseRunner
}
type DeleteRunner struct {
BaseRunner
}
type ExecRunner struct {
BaseRunner
}
// NewSession 使用一个session操作数据库
func (sessionManager *SessionManager) NewSession() *Session {
return &Session{
ctx: context.Background(),
log: sessionManager.factory.LogFunc(),
session: sessionManager.factory.CreateSession(),
driver: sessionManager.factory.GetDataSource().DriverName(),
ParserFactory: sessionManager.ParserFactory,
}
}
// Context 包含session的context
func (sessionManager *SessionManager) Context(ctx context.Context) context.Context {
sess := &Session{
ctx: ctx,
log: sessionManager.factory.LogFunc(),
session: sessionManager.factory.CreateSession(),
driver: sessionManager.factory.GetDataSource().DriverName(),
ParserFactory: sessionManager.ParserFactory,
}
return context.WithValue(ctx, ContextSessionKey, sess)
}
func WithSession(ctx context.Context, sess *Session) context.Context {
return context.WithValue(ctx, ContextSessionKey, sess)
}
func FindSession(ctx context.Context) *Session {
if ctx == nil {
return nil
}
return ctx.Value(ContextSessionKey).(*Session)
}
func (sessionManager *SessionManager) Close() error {
return sessionManager.factory.Close()
}
// SetParserFactory 修改sql解析器创建者
func (sessionManager *SessionManager) SetParserFactory(fac ParserFactory) {
sessionManager.ParserFactory = fac
}
func (session *Session) SetContext(ctx context.Context) *Session {
session.ctx = ctx
return session
}
func (session *Session) GetContext() context.Context {
return session.ctx
}
// SetParserFactory 修改sql解析器创建者
func (session *Session) SetParserFactory(fac ParserFactory) {
session.ParserFactory = fac
}
// Tx 开启事务执行语句
// 返回nil则提交,返回error回滚
// 抛出异常错误触发回滚
func (session *Session) Tx(txFunc func(session *Session) error) (err error) {
e1 := session.session.Begin()
if e1 != nil {
return e1
}
defer func(err *error) {
if r := recover(); r != nil {
*err = session.session.Rollback()
panic(r)
}
}(&err)
if fnErr := txFunc(session); fnErr != nil {
e := session.session.Rollback()
if e != nil {
session.log(logging.WARN, "Rollback error: %v , business error: %v\n", e, fnErr)
}
return fnErr
} else {
return session.session.Commit()
}
}
func (session *Session) Select(sql string) Runner {
return session.createSelect(session.findSqlParser(sql))
}
func (session *Session) Update(sql string) Runner {
return session.createUpdate(session.findSqlParser(sql))
}
func (session *Session) Delete(sql string) Runner {
return session.createDelete(session.findSqlParser(sql))
}
func (session *Session) Insert(sql string) Runner {
return session.createInsert(session.findSqlParser(sql))
}
func (session *Session) Exec(sql string) Runner {
return session.createExec(session.findSqlParser(sql))
}
func (baseRunner *BaseRunner) Param(params ...interface{}) Runner {
//TODO: 使用缓存加速,避免每次都生成动态sql
//测试发现性能提升非常有限,故取消
//key := cache.CalcKey(baseRunner.sqlDynamicData.OriginData, paramMap)
//md := cache.FindMetadata(key)
//var err error
//if md == nil {
// md, err := baseRunner.sqlParser.Parse(params...)
// if err == nil {
// cache.CacheMetadata(key, md)
// }
//}
if baseRunner.sqlParser == nil {
baseRunner.log(logging.WARN, errors.ParseParserNilError.Error())
return baseRunner
}
md, err := baseRunner.sqlParser.ParseMetadata(baseRunner.driver, params...)
if err == nil {
if baseRunner.action == "" || baseRunner.action == md.Action {
baseRunner.metadata = md
} else {
//allow different action
baseRunner.log(logging.WARN, "sql action not match expect %s get %s", baseRunner.action, md.Action)
baseRunner.metadata = md
}
} else {
baseRunner.log(logging.WARN, err.Error())
}
return baseRunner.runner
}
//Context 设置执行的context
func (baseRunner *BaseRunner) Context(ctx context.Context) Runner {
baseRunner.ctx = ctx
return baseRunner.runner
}
func (selectRunner *SelectRunner) Result(bean interface{}) error {
if selectRunner.metadata == nil {
selectRunner.log(logging.WARN, "Sql Metadata is nil")
return errors.RunnerNotReady
}
if reflection.IsNil(bean) {
return errors.ResultPointerIsNil
}
obj, err := ParseObject(bean)
if err != nil {
return err
}
return selectRunner.session.Query(selectRunner.ctx, obj, selectRunner.metadata.PrepareSql, selectRunner.metadata.Params...)
}
func (insertRunner *InsertRunner) Result(bean interface{}) error {
if insertRunner.metadata == nil {
insertRunner.log(logging.WARN, "Sql Metadata is nil")
return errors.RunnerNotReady
}
i, id, err := insertRunner.session.Insert(insertRunner.ctx, insertRunner.metadata.PrepareSql, insertRunner.metadata.Params...)
insertRunner.lastId = id
if reflection.CanSet(bean) {
reflection.SetValue(reflection.ReflectValue(bean), i)
}
return err
}
func (insertRunner *InsertRunner) LastInsertId() int64 {
return insertRunner.lastId
}
func (updateRunner *UpdateRunner) Result(bean interface{}) error {
if updateRunner.metadata == nil {
updateRunner.log(logging.WARN, "Sql Metadata is nil")
return errors.RunnerNotReady
}
i, err := updateRunner.session.Update(updateRunner.ctx, updateRunner.metadata.PrepareSql, updateRunner.metadata.Params...)
if reflection.CanSet(bean) {
reflection.SetValue(reflection.ReflectValue(bean), i)
}
return err
}
func (execRunner *ExecRunner) Result(bean interface{}) error {
if execRunner.metadata == nil {
execRunner.log(logging.WARN, "Sql Metadata is nil")
return errors.RunnerNotReady
}
i, err := execRunner.session.Update(execRunner.ctx, execRunner.metadata.PrepareSql, execRunner.metadata.Params...)
if reflection.CanSet(bean) {
reflection.SetValue(reflection.ReflectValue(bean), i)
}
return err
}
func (deleteRunner *DeleteRunner) Result(bean interface{}) error {
if deleteRunner.metadata == nil {
deleteRunner.log(logging.WARN, "Sql Metadata is nil")
return errors.RunnerNotReady
}
i, err := deleteRunner.session.Delete(deleteRunner.ctx, deleteRunner.metadata.PrepareSql, deleteRunner.metadata.Params...)
if reflection.CanSet(bean) {
reflection.SetValue(reflection.ReflectValue(bean), i)
}
return err
}
func (baseRunner *BaseRunner) Result(bean interface{}) error {
//FAKE RETURN
panic("Cannot be here")
//return nil, nil
}
func (baseRunner *BaseRunner) LastInsertId() int64 {
return -1
}
func (session *Session) createSelect(parser sqlparser.SqlParser) Runner {
ret := &SelectRunner{}
ret.action = sqlparser.SELECT
ret.log = session.log
ret.session = session.session
ret.sqlParser = parser
ret.ctx = session.ctx
ret.driver = session.driver
ret.runner = ret
return ret
}
func (session *Session) createUpdate(parser sqlparser.SqlParser) Runner {
ret := &UpdateRunner{}
ret.action = sqlparser.UPDATE
ret.log = session.log
ret.session = session.session
ret.sqlParser = parser
ret.ctx = session.ctx
ret.driver = session.driver
ret.runner = ret
return ret
}
func (session *Session) createDelete(parser sqlparser.SqlParser) Runner {
ret := &DeleteRunner{}
ret.action = sqlparser.DELETE
ret.log = session.log
ret.session = session.session
ret.sqlParser = parser
ret.ctx = session.ctx
ret.driver = session.driver
ret.runner = ret
return ret
}
func (session *Session) createInsert(parser sqlparser.SqlParser) Runner {
ret := &InsertRunner{}
ret.action = sqlparser.INSERT
ret.log = session.log
ret.session = session.session
ret.sqlParser = parser
ret.ctx = session.ctx
ret.driver = session.driver
ret.runner = ret
return ret
}
func (session *Session) createExec(parser sqlparser.SqlParser) Runner {
ret := &ExecRunner{}
ret.action = ""
ret.log = session.log
ret.session = session.session
ret.sqlParser = parser
ret.ctx = session.ctx
ret.driver = session.driver
ret.runner = ret
return ret
}
func (session *Session) findSqlParser(sqlId string) sqlparser.SqlParser {
ret, ok := FindDynamicSqlParser(sqlId)
if !ok {
ret, ok = FindTemplateSqlParser(sqlId)
}
//FIXME: 当没有查找到sqlId对应的sql语句,则尝试使用sqlId直接操作数据库
//该设计可能需要设计一个更合理的方式
if !ok {
d, err := session.ParserFactory(sqlId)
if err != nil {
session.log(logging.WARN, err.Error())
return nil
}
return d
}
return ret
}