forked from wenj91/gobatis
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmapper.go
More file actions
95 lines (75 loc) · 1.55 KB
/
mapper.go
File metadata and controls
95 lines (75 loc) · 1.55 KB
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
package gobatis
import (
"log"
"sync"
)
type mapper struct {
mappedStmts map[string]*node
mappedSql map[string]*node
cache map[string]*mappedStmt
mu sync.Mutex
}
type mappedStmt struct {
dbType DbType
sqlSource iSqlSource
resultType ResultType
}
func newMapper() *mapper {
return &mapper{
mappedStmts: make(map[string]*node),
mappedSql: make(map[string]*node),
cache: make(map[string]*mappedStmt),
mu: sync.Mutex{},
}
}
func (m *mapper) put(id string, n *node) bool {
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.mappedStmts[id]; ok {
return false
}
m.mappedStmts[id] = n
return true
}
func (m *mapper) putSql(id string, n *node) bool {
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.mappedSql[id]; ok {
return false
}
m.mappedSql[id] = n
return true
}
func (m *mapper) getMappedStmt(id string) *mappedStmt {
if st, ok := m.cache[id]; ok {
return st
}
m.mu.Lock()
defer m.mu.Unlock()
rootNode, ok := m.mappedStmts[id]
if !ok {
log.Fatalln("can not find id:", id, "mapped executor")
}
resultType := ""
if rootNode.Name == "select" {
resultTypeAttr, ok := rootNode.Attrs["resultType"]
if !ok {
log.Fatalln("tag `<select>` must have resultType attr!")
}
resultType = resultTypeAttr.Value
}
sn := createSqlNode(rootNode.Elements...)
ds := &dynamicSqlSource{}
ds.sqlNode = sn[0]
if len(sn) > 1 {
ds.sqlNode = &mixedSqlNode{
sqlNodes: sn,
}
}
stmt := &mappedStmt{
sqlSource: ds,
resultType: ResultType(resultType),
}
m.cache[id] = stmt
return stmt
}