-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
SessionFactory.go
73 lines (65 loc) · 1.57 KB
/
SessionFactory.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
package GoMybatis
import (
"sync"
)
type SessionFactory struct {
Engine SessionEngine
SessionMap sync.Map //map[string]Session
}
func (it SessionFactory) New(Engine SessionEngine) SessionFactory {
it.Engine = Engine
return it
}
func (it *SessionFactory) NewSession(mapperName string, sessionType SessionType) Session {
if it.Engine == nil {
panic("[GoMybatis] SessionFactory not init! you must call method SessionFactory.New(*)")
}
var newSession Session
var err error
switch sessionType {
case SessionType_Default:
var session, err = it.Engine.NewSession(mapperName)
if err != nil {
panic(err)
}
var factorySession = SessionFactorySession{
Session: session,
Factory: it,
}
newSession = Session(&factorySession)
break
case SessionType_Local:
newSession, err = it.Engine.NewSession(mapperName)
if err != nil {
panic(err)
}
break
default:
panic("[GoMybatis] newSession() must have a SessionType!")
}
it.SessionMap.Store(newSession.Id(), newSession)
return newSession
}
func (it *SessionFactory) GetSession(id string) Session {
var v, _ = it.SessionMap.Load(id)
return v.(Session)
}
func (it *SessionFactory) SetSession(id string, session Session) {
it.SessionMap.Store(id, session)
}
func (it *SessionFactory) Close(id string) {
var s, _ = it.SessionMap.Load(id)
if s != nil {
s.(Session).Close()
it.SessionMap.Delete(id)
}
}
func (it *SessionFactory) CloseAll(id string) {
it.SessionMap.Range(func(key, value interface{}) bool {
if value != nil {
value.(Session).Close()
it.SessionMap.Delete(key)
}
return true
})
}