-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
GoMybatisDataSourceRouter.go
66 lines (58 loc) · 1.8 KB
/
GoMybatisDataSourceRouter.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
package GoMybatis
import (
"database/sql"
"github.com/zhuxiujia/GoMybatis/utils"
)
//动态数据源路由
type GoMybatisDataSourceRouter struct {
driverLinkDBMap map[string]*sql.DB // map[driverLink]*DB
driverLinkUrlMap map[string]string // map[driverLink]Url
routerFunc func(mapperName string) *string
}
//初始化路由,routerFunc为nil或者routerFunc返回nil,则框架自行选择第一个数据库作为数据源
func (it GoMybatisDataSourceRouter) New(routerFunc func(mapperName string) *string) GoMybatisDataSourceRouter {
if routerFunc == nil {
routerFunc = func(mapperName string) *string {
return nil
}
}
it.driverLinkDBMap = make(map[string]*sql.DB)
it.driverLinkUrlMap = make(map[string]string)
it.routerFunc = routerFunc
return it
}
func (it *GoMybatisDataSourceRouter) SetDB(driverType string, driverLink string, db *sql.DB) {
it.driverLinkDBMap[driverLink] = db
it.driverLinkUrlMap[driverLink] = driverType
}
func (it *GoMybatisDataSourceRouter) Router(mapperName string, engine SessionEngine) (Session, error) {
var key *string
var db *sql.DB
if it.routerFunc != nil {
key = it.routerFunc(mapperName)
}
if key != nil && *key != "" {
db = it.driverLinkDBMap[*key]
} else {
for k, v := range it.driverLinkDBMap {
if v != nil {
db = v
key = &k
break
}
}
}
if db == nil {
return nil, utils.NewError("GoMybatisDataSourceRouter", "router not find datasource opened ! do you forget invoke GoMybatis.GoMybatisEngine{}.New().Open(\"driverName\", Uri)?")
}
var driverLink = ""
if key != nil {
driverLink = *key
}
var local = LocalSession{}.New(it.driverLinkUrlMap[driverLink], driverLink, db, engine.Log())
var session = Session(&local)
return session, nil
}
func (it *GoMybatisDataSourceRouter) Name() string {
return "GoMybatisDataSourceRouter"
}