-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
GoMybatisEngine.go
231 lines (197 loc) · 6.29 KB
/
GoMybatisEngine.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
package GoMybatis
import (
"database/sql"
"github.com/zhuxiujia/GoMybatis/ast"
"github.com/zhuxiujia/GoMybatis/engines"
"github.com/zhuxiujia/GoMybatis/utils"
"sync"
)
type GoMybatisEngine struct {
mutex sync.RWMutex //读写锁
isInit bool //是否初始化
isPrintWarning bool //是否打印警告
dataSourceRouter DataSourceRouter //动态数据源路由器
log Log //日志实现类
logEnable bool //是否允许日志输出(默认开启)
sessionFactory *SessionFactory //session 工厂
sqlArgTypeConvert ast.SqlArgTypeConvert //sql参数转换
expressionEngine ast.ExpressionEngine //表达式解析引擎
sqlBuilder SqlBuilder //sql 构建
sqlResultDecoder SqlResultDecoder //sql查询结果解析引擎
templateDecoder TemplateDecoder //模板解析引擎
goroutineSessionMap *GoroutineSessionMap //map[协程id]Session
goroutineIDEnable bool //是否启用goroutineIDEnable(注意(该方法需要在多协程环境下调用)启用会从栈获取协程id,有一定性能消耗,换取最大的事务定义便捷,单线程处理场景可以关闭此配置)
}
func (it GoMybatisEngine) New() GoMybatisEngine {
it.logEnable = true
it.isInit = true
if it.logEnable == true && it.log == nil {
var s = LogStandard{}
it.log = &s
}
if it.dataSourceRouter == nil {
var newRouter = GoMybatisDataSourceRouter{}.New(nil)
it.SetDataSourceRouter(&newRouter)
}
if it.sqlArgTypeConvert == nil {
it.sqlArgTypeConvert = GoMybatisSqlArgTypeConvert{}
}
if it.expressionEngine == nil {
it.expressionEngine = &engines.ExpressionEngineGoExpress{}
}
if it.sqlResultDecoder == nil {
it.sqlResultDecoder = GoMybatisSqlResultDecoder{}
}
if it.templateDecoder == nil {
it.SetTemplateDecoder(&GoMybatisTemplateDecoder{})
}
if it.sqlBuilder == nil {
var expressionEngineProxy = ExpressionEngineProxy{}.New(it.ExpressionEngine(), true)
var builder = GoMybatisSqlBuilder{}.New(expressionEngineProxy, it.Log(), it.LogEnable())
it.sqlBuilder = &builder
}
if it.sessionFactory == nil {
var factory = SessionFactory{}.New(&it)
it.sessionFactory = &factory
}
if it.goroutineSessionMap == nil {
var gr = GoroutineSessionMap{}.New()
it.goroutineSessionMap = &gr
}
it.goroutineIDEnable = true
return it
}
func (it GoMybatisEngine) initCheck() {
if it.isInit == false {
panic(utils.NewError("GoMybatisEngine", "must call GoMybatisEngine{}.New() to init!"))
}
}
func (it *GoMybatisEngine) WriteMapperPtr(ptr interface{}, xml []byte) {
it.initCheck()
WriteMapperPtrByEngine(ptr, xml, it)
}
func (it *GoMybatisEngine) Name() string {
return "GoMybatisEngine"
}
func (it *GoMybatisEngine) DataSourceRouter() DataSourceRouter {
it.initCheck()
return it.dataSourceRouter
}
func (it *GoMybatisEngine) SetDataSourceRouter(router DataSourceRouter) {
it.initCheck()
it.dataSourceRouter = router
}
func (it *GoMybatisEngine) NewSession(mapperName string) (Session, error) {
it.initCheck()
var session, err = it.DataSourceRouter().Router(mapperName, SessionEngine(it))
return session, err
}
//获取日志实现类,是否启用日志
func (it *GoMybatisEngine) LogEnable() bool {
it.initCheck()
return it.logEnable
}
//设置日志实现类,是否启用日志
func (it *GoMybatisEngine) SetLogEnable(enable bool) {
it.initCheck()
it.logEnable = enable
it.sqlBuilder.SetEnableLog(enable)
}
type EmptyLog struct {
}
func (EmptyLog) Println(messages ...string) {
}
//获取日志实现类
func (it *GoMybatisEngine) Log() Log {
it.initCheck()
if it.log == nil {
return EmptyLog{}
}
return it.log
}
//设置日志实现类
func (it *GoMybatisEngine) SetLog(log Log) {
it.initCheck()
it.log = log
}
//session工厂
func (it *GoMybatisEngine) SessionFactory() *SessionFactory {
it.initCheck()
return it.sessionFactory
}
//设置session工厂
func (it *GoMybatisEngine) SetSessionFactory(factory *SessionFactory) {
it.initCheck()
it.sessionFactory = factory
}
//设置sql类型转换器
func (it *GoMybatisEngine) SetSqlArgTypeConvert(convert ast.SqlArgTypeConvert) {
it.initCheck()
it.sqlArgTypeConvert = convert
}
//表达式执行引擎
func (it *GoMybatisEngine) ExpressionEngine() ast.ExpressionEngine {
it.initCheck()
return it.expressionEngine
}
//设置表达式执行引擎
func (it *GoMybatisEngine) SetExpressionEngine(engine ast.ExpressionEngine) {
it.initCheck()
it.expressionEngine = engine
var proxy = it.sqlBuilder.ExpressionEngineProxy()
proxy.SetExpressionEngine(it.expressionEngine)
}
//sql构建器
func (it *GoMybatisEngine) SqlBuilder() SqlBuilder {
it.initCheck()
return it.sqlBuilder
}
//设置sql构建器
func (it *GoMybatisEngine) SetSqlBuilder(builder SqlBuilder) {
it.initCheck()
it.sqlBuilder = builder
}
//sql查询结果解析器
func (it *GoMybatisEngine) SqlResultDecoder() SqlResultDecoder {
it.initCheck()
return it.sqlResultDecoder
}
//设置sql查询结果解析器
func (it *GoMybatisEngine) SetSqlResultDecoder(decoder SqlResultDecoder) {
it.initCheck()
it.sqlResultDecoder = decoder
}
//打开数据库驱动,初始化连接池放入路由。 sql.DB 就是连接池,可以自定义活跃连接数,具体使用参考官方文档
//driverName: 驱动名称例如"mysql", dataSourceName: string 数据库url
func (it *GoMybatisEngine) Open(driverName, dataSourceLink string) (*sql.DB, error) {
it.initCheck()
db, err := sql.Open(driverName, dataSourceLink)
if err != nil {
return nil, err
}
it.dataSourceRouter.SetDB(driverName, dataSourceLink, db)
return db, nil
}
//模板解析器
func (it *GoMybatisEngine) TemplateDecoder() TemplateDecoder {
return it.templateDecoder
}
//设置模板解析器
func (it *GoMybatisEngine) SetTemplateDecoder(decoder TemplateDecoder) {
it.templateDecoder = decoder
}
func (it *GoMybatisEngine) GoroutineSessionMap() *GoroutineSessionMap {
return it.goroutineSessionMap
}
func (it *GoMybatisEngine) SetGoroutineIDEnable(enable bool) {
it.goroutineIDEnable = enable
}
func (it *GoMybatisEngine) GoroutineIDEnable() bool {
return it.goroutineIDEnable
}
func (it *GoMybatisEngine) SetPrintWarning(print bool) {
it.isPrintWarning = print
}
func (it *GoMybatisEngine) IsPrintWarning() bool {
return it.isPrintWarning
}