-
Notifications
You must be signed in to change notification settings - Fork 14
/
util_builder.go
75 lines (62 loc) · 2.02 KB
/
util_builder.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
package gobatis
import (
"io"
"strings"
"gopkg.in/yaml.v2"
)
func buildMapperConfig(r io.Reader) *mapperConfig {
rootNode := parse(r)
conf := &mapperConfig{
mappedStmts: make(map[string]*node),
mappedSql: make(map[string]*node),
cache: make(map[string]*mappedStmt),
}
if rootNode.Name != "mapper" {
LOG.Error("Mapper xml must start with `mapper` tag, please check your xml mapperConfig!")
panic("Mapper xml must start with `mapper` tag, please check your xml mapperConfig!")
}
namespace := ""
if val, ok := rootNode.Attrs["namespace"]; ok {
nStr := strings.TrimSpace(val.Value)
if nStr != "" {
nStr += "."
}
namespace = nStr
}
for _, elem := range rootNode.Elements {
if elem.ElementType == eleTpNode {
childNode := elem.Val.(node)
switch childNode.Name {
case "select", "update", "insert", "delete":
if childNode.Id == "" {
LOG.Error("No id for:" + childNode.Name + "Id must be not null, please check your xml mapperConfig!")
panic("No id for:" + childNode.Name + "Id must be not null, please check your xml mapperConfig!")
}
fid := namespace + childNode.Id
if ok := conf.put(fid, &childNode); !ok {
LOG.Error("Repeat id for:" + fid + "Please check your xml mapperConfig!")
panic("Repeat id for:" + fid + "Please check your xml mapperConfig!")
}
case "sql":
if childNode.Id == "" {
LOG.Error("No id for:" + childNode.Name + "Id must be not null, please check your xml mapperConfig!")
panic("No id for:" + childNode.Name + "Id must be not null, please check your xml mapperConfig!")
}
fid := namespace + childNode.Id
if ok := conf.putSql(fid, &childNode); !ok {
LOG.Error("Repeat id for:" + fid + "Please check your xml mapperConfig!")
panic("Repeat id for:" + fid + "Please check your xml mapperConfig!")
}
}
}
}
return conf
}
func buildDbConfig(ymlStr string) *DBConfig {
dbconf := &DBConfig{}
err := yaml.Unmarshal([]byte(ymlStr), &dbconf)
if err != nil {
panic("error: " + err.Error())
}
return dbconf
}