-
Notifications
You must be signed in to change notification settings - Fork 222
/
sqltemplate_pongo2.go
314 lines (250 loc) · 6.51 KB
/
sqltemplate_pongo2.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package xorm
import (
"io/ioutil"
"os"
"strings"
"gopkg.in/flosch/pongo2.v3"
)
type Pongo2Template struct {
SqlTemplateRootDir string
Template map[string]*pongo2.Template
extension string
Capacity uint
Cipher Cipher
Type int
Funcs map[string]FuncMap
}
func (sqlTemplate *Pongo2Template) RootDir() string {
return sqlTemplate.SqlTemplateRootDir
}
func (sqlTemplate *Pongo2Template) SetFuncs(key string, funcMap FuncMap) {
sqlTemplate.Funcs[key] = funcMap
}
func (sqlTemplate *Pongo2Template) Extension() string {
return sqlTemplate.extension
}
func (sqlTemplate *Pongo2Template) SetSqlTemplateCipher(cipher Cipher) {
sqlTemplate.Cipher = cipher
}
func (sqlTemplate *Pongo2Template) WalkFunc(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, sqlTemplate.extension) {
err = sqlTemplate.paresSqlTemplate(info.Name(), path)
if err != nil {
return err
}
}
return nil
}
func (sqlTemplate *Pongo2Template) paresSqlTemplate(filename string, filepath string) error {
var sqlt *pongo2.Template
var err error
var content []byte
if sqlTemplate.Cipher == nil {
sqlt, err = pongo2.FromFile(filepath)
if err != nil {
return err
}
} else {
content, err = sqlTemplate.ReadTemplate(filepath)
if err != nil {
return err
}
sqlt, err = pongo2.FromString(string(content))
if err != nil {
return err
}
}
sqlTemplate.checkNilAndInit()
sqlTemplate.Template[filename] = sqlt
return nil
}
func (sqlTemplate *Pongo2Template) ReadTemplate(filepath string) ([]byte, error) {
content, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
content, err = sqlTemplate.Cipher.Decrypt(content)
if err != nil {
return nil, err
}
return content, nil
}
//-------------------------------------------------------------------------------------------------------------
func (sqlTemplate *Pongo2Template) LoadSqlTemplate(filepath string) error {
if strings.HasSuffix(filepath, sqlTemplate.extension) {
err := sqlTemplate.loadSqlTemplate(filepath)
if err != nil {
return err
}
}
return nil
}
func (sqlTemplate *Pongo2Template) BatchLoadSqlTemplate(filepathSlice []string) error {
for _, filepath := range filepathSlice {
if strings.HasSuffix(filepath, sqlTemplate.extension) {
err := sqlTemplate.loadSqlTemplate(filepath)
if err != nil {
return err
}
}
}
return nil
}
func (sqlTemplate *Pongo2Template) ReloadSqlTemplate(filepath string) error {
if strings.HasSuffix(filepath, sqlTemplate.extension) {
err := sqlTemplate.reloadSqlTemplate(filepath)
if err != nil {
return err
}
}
return nil
}
func (sqlTemplate *Pongo2Template) BatchReloadSqlTemplate(filepathSlice []string) error {
for _, filepath := range filepathSlice {
if strings.HasSuffix(filepath, sqlTemplate.extension) {
err := sqlTemplate.loadSqlTemplate(filepath)
if err != nil {
return err
}
}
}
return nil
}
func (sqlTemplate *Pongo2Template) loadSqlTemplate(filepath string) error {
info, err := os.Lstat(filepath)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
err = sqlTemplate.paresSqlTemplate(info.Name(), filepath)
if err != nil {
return err
}
return nil
}
func (sqlTemplate *Pongo2Template) reloadSqlTemplate(filepath string) error {
info, err := os.Lstat(filepath)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
err = sqlTemplate.paresSqlTemplate(info.Name(), filepath)
if err != nil {
return err
}
return nil
}
func (sqlTemplate *Pongo2Template) checkNilAndInit() {
if sqlTemplate.Template == nil {
sqlTemplate.Template = make(map[string]*pongo2.Template, 100)
}
}
func (sqlTemplate *Pongo2Template) AddSqlTemplate(key string, sqlTemplateStr string) error {
template, err := pongo2.FromString(sqlTemplateStr)
if err != nil {
return err
}
sqlTemplate.checkNilAndInit()
sqlTemplate.Template[key] = template
return nil
}
func (sqlTemplate *Pongo2Template) UpdateSqlTemplate(key string, sqlTemplateStr string) error {
template, err := pongo2.FromString(sqlTemplateStr)
if err != nil {
return err
}
sqlTemplate.checkNilAndInit()
sqlTemplate.Template[key] = template
return nil
}
func (sqlTemplate *Pongo2Template) RemoveSqlTemplate(key string) {
sqlTemplate.checkNilAndInit()
delete(sqlTemplate.Template, key)
}
func (sqlTemplate *Pongo2Template) BatchAddSqlTemplate(key string, sqlTemplateStrMap map[string]string) error {
sqlTemplate.checkNilAndInit()
for k, v := range sqlTemplateStrMap {
template, err := pongo2.FromString(v)
if err != nil {
return err
}
sqlTemplate.Template[k] = template
}
return nil
}
func (sqlTemplate *Pongo2Template) BatchUpdateSqlTemplate(key string, sqlTemplateStrMap map[string]string) error {
sqlTemplate.checkNilAndInit()
for k, v := range sqlTemplateStrMap {
template, err := pongo2.FromString(v)
if err != nil {
return err
}
sqlTemplate.Template[k] = template
}
return nil
}
func (sqlTemplate *Pongo2Template) BatchRemoveSqlTemplate(key []string) {
sqlTemplate.checkNilAndInit()
for _, v := range key {
delete(sqlTemplate.Template, v)
}
}
func (sqlTemplate *Pongo2Template) GetSqlTemplate(key string) *pongo2.Template {
return sqlTemplate.Template[key]
}
func (sqlTemplate *Pongo2Template) GetSqlTemplates(keys ...interface{}) map[string]*pongo2.Template {
var resultSqlTemplates map[string]*pongo2.Template
i := len(keys)
if i == 0 {
return sqlTemplate.Template
}
if i == 1 {
switch keys[0].(type) {
case string:
resultSqlTemplates = make(map[string]*pongo2.Template, 1)
case []string:
ks := keys[0].([]string)
n := len(ks)
resultSqlTemplates = make(map[string]*pongo2.Template, n)
}
} else {
resultSqlTemplates = make(map[string]*pongo2.Template, i)
}
for k, _ := range keys {
switch keys[k].(type) {
case string:
key := keys[k].(string)
resultSqlTemplates[key] = sqlTemplate.Template[key]
case []string:
ks := keys[k].([]string)
for _, v := range ks {
resultSqlTemplates[v] = sqlTemplate.Template[v]
}
}
}
return resultSqlTemplates
}
func (sqlTemplate *Pongo2Template) Execute(key string, args ...interface{}) (string, error) {
if sqlTemplate.Template[key] == nil {
return "", nil
}
if len(args) == 0 {
parmap := &pongo2.Context{"1": 1}
sql, err := sqlTemplate.Template[key].Execute(*parmap)
return sql, err
} else {
map1 := args[0].(*map[string]interface{})
sql, err := sqlTemplate.Template[key].Execute(*map1)
return sql, err
}
}