-
Notifications
You must be signed in to change notification settings - Fork 221
/
sqlmap.go
188 lines (155 loc) · 3.4 KB
/
sqlmap.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package xorm
import (
"bufio"
"bytes"
"encoding/json"
"encoding/xml"
"io/ioutil"
"os"
"strings"
)
type SqlMap struct {
SqlMapRootDir string
Sql map[string]string
Extension map[string]string
Capacity uint
Cipher Cipher
}
func (sqlMap *SqlMap) checkNilAndInit() {
if sqlMap.Sql == nil {
if sqlMap.Capacity == 0 {
sqlMap.Sql = make(map[string]string, 100)
} else {
sqlMap.Sql = make(map[string]string, sqlMap.Capacity)
}
}
}
type SqlM interface {
RootDir() string
Extension() string
}
func (sqlMap *SqlMap) walkFunc(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, sqlMap.Extension["xml"]) || strings.HasSuffix(path, sqlMap.Extension["json"]) || strings.HasSuffix(path, sqlMap.Extension["xsql"]) {
err = sqlMap.paresSql(path)
if err != nil {
return err
}
}
return nil
}
func (sqlMap *SqlMap) paresSql(filepath string) error {
content, err := ioutil.ReadFile(filepath)
if err != nil {
return err
}
enc := sqlMap.Cipher
if enc != nil {
content, err = enc.Decrypt(content)
if err != nil {
return err
}
}
sqlMap.checkNilAndInit()
if strings.HasSuffix(filepath, sqlMap.Extension["xml"]) {
var result XmlSql
err = xml.Unmarshal(content, &result)
if err != nil {
return err
}
for _, sql := range result.Sql {
sqlMap.Sql[sql.Id] = sql.Value
}
return nil
}
if strings.HasSuffix(filepath, sqlMap.Extension["json"]) {
var result map[string]string
err = json.Unmarshal(content, &result)
if err != nil {
return err
}
for k := range result {
sqlMap.Sql[k] = result[k]
}
return nil
}
if strings.HasSuffix(filepath, sqlMap.Extension["xsql"]) {
scanner := &Scanner{}
result := scanner.Run(bufio.NewScanner(bytes.NewReader(content)))
for k := range result {
sqlMap.Sql[k] = result[k]
}
return nil
}
return nil
}
func (sqlMap *SqlMap) addSql(key string, sql string) {
sqlMap.checkNilAndInit()
sqlMap.Sql[key] = sql
}
func (sqlMap *SqlMap) updateSql(key string, sql string) {
sqlMap.checkNilAndInit()
sqlMap.Sql[key] = sql
}
func (sqlMap *SqlMap) removeSql(key string) {
sqlMap.checkNilAndInit()
delete(sqlMap.Sql, key)
}
func (sqlMap *SqlMap) batchAddSql(sqlStrMap map[string]string) {
sqlMap.checkNilAndInit()
for k, v := range sqlStrMap {
sqlMap.Sql[k] = v
}
}
func (sqlMap *SqlMap) batchUpdateSql(sqlStrMap map[string]string) {
sqlMap.checkNilAndInit()
for k, v := range sqlStrMap {
sqlMap.Sql[k] = v
}
}
func (sqlMap *SqlMap) batchRemoveSql(key []string) {
sqlMap.checkNilAndInit()
for _, v := range key {
delete(sqlMap.Sql, v)
}
}
func (sqlMap *SqlMap) getSql(key string) string {
return sqlMap.Sql[key]
}
func (sqlMap *SqlMap) getSqlMap(keys ...interface{}) map[string]string {
var resultSqlMap map[string]string
i := len(keys)
if i == 0 {
return sqlMap.Sql
}
if i == 1 {
switch keys[0].(type) {
case string:
resultSqlMap = make(map[string]string, 1)
case []string:
ks := keys[0].([]string)
n := len(ks)
resultSqlMap = make(map[string]string, n)
}
} else {
resultSqlMap = make(map[string]string, i)
}
for k, _ := range keys {
switch keys[k].(type) {
case string:
key := keys[k].(string)
resultSqlMap[key] = sqlMap.Sql[key]
case []string:
ks := keys[k].([]string)
for _, v := range ks {
resultSqlMap[v] = sqlMap.Sql[v]
}
}
}
return resultSqlMap
}