forked from wenj91/gobatis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_mapper.go
83 lines (64 loc) · 1.38 KB
/
config_mapper.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
package gobatis
import (
"sync"
)
type mapperConfig struct {
mappedStmts map[string]*node
mappedSql map[string]*node
cache map[string]*mappedStmt
mu sync.Mutex
}
func (this *mapperConfig) put(id string, n *node) bool {
this.mu.Lock()
defer this.mu.Unlock()
if _, ok := this.mappedStmts[id]; ok {
return false
}
this.mappedStmts[id] = n
return true
}
func (this *mapperConfig) putSql(id string, n *node) bool {
this.mu.Lock()
defer this.mu.Unlock()
if _, ok := this.mappedSql[id]; ok {
return false
}
this.mappedSql[id] = n
return true
}
func (this *mapperConfig) getMappedStmt(id string) *mappedStmt {
if nil == this.cache {
this.cache = make(map[string]*mappedStmt)
}
if st, ok := this.cache[id]; ok {
return st
}
this.mu.Lock()
defer this.mu.Unlock()
rootNode, ok := this.mappedStmts[id]
if !ok {
panic("Can not find id:" + id + "mapped stmt")
}
resultType := ""
if rootNode.Name == "select" {
resultTypeAttr, ok := rootNode.Attrs["resultType"]
if !ok {
panic("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),
}
this.cache[id] = stmt
return stmt
}