forked from leonliu315/sqlt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassembler.go
165 lines (135 loc) · 3.35 KB
/
assembler.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
package sqlt
import (
"bytes"
"context"
"io"
"text/template"
"time"
log "github.com/it512/slf4go"
)
type (
SqlRender interface {
Render(w io.Writer, id string, param interface{}) error
}
StdTemplateRender struct {
pattern string
funcMap template.FuncMap
t *template.Template
}
)
func (st *StdTemplateRender) Render(w io.Writer, id string, param interface{}) error {
return st.t.ExecuteTemplate(w, id, param)
}
func NewStdTemplateRender(pattern string, funcMap template.FuncMap) *StdTemplateRender {
tpl := template.New("sqlt-std-template").Funcs(funcMap)
tpl = template.Must(tpl.ParseGlob(pattern))
return &StdTemplateRender{pattern: pattern, funcMap: funcMap, t: tpl}
}
func NewStdTemplateRenderDefault(pattern string) *StdTemplateRender {
return NewStdTemplateRender(pattern, make(template.FuncMap))
}
type (
Config struct {
TimeOut int64
ReadOnly bool
Extra map[string]interface{}
}
Manifest struct {
Default Config
ConfigMap map[string]Config
}
)
var (
DefaultManifest = Manifest{
Default: Config{
TimeOut: 0,
ReadOnly: false,
Extra: make(map[string]interface{}),
},
ConfigMap: make(map[string]Config),
}
)
func (m Manifest) GetConfigCopy(id string) Config {
if c, ok := m.ConfigMap[id]; ok {
config := Config{TimeOut: c.TimeOut, ReadOnly: c.ReadOnly, Extra: c.Extra}
if config.TimeOut == 0 {
config.TimeOut = m.Default.TimeOut
}
return config
}
return m.Default
}
type (
StdSqlDescriber struct {
Id string
Data interface{}
Config Config
bytes.Buffer
cf context.CancelFunc
}
)
func (s *StdSqlDescriber) GetSql(c context.Context) (string, context.Context, error) {
return s.String(), s.WithContext(c), nil
}
func (s *StdSqlDescriber) WithContext(c context.Context) context.Context {
if s.Config.TimeOut > 0 {
ctx, cf := context.WithTimeout(c, time.Duration(s.Config.TimeOut)*time.Millisecond)
s.cf = cf
return ctx
}
return c
}
func (s *StdSqlDescriber) Release() {
if s.cf != nil {
s.cf()
}
}
func (s *StdSqlDescriber) IsReadOnly() bool {
return s.Config.ReadOnly
}
type (
StdSqlAssembler struct {
Render SqlRender
Logger log.Logger
Manifest Manifest
}
)
func (l *StdSqlAssembler) AssembleSql(id string, data interface{}) (SqlDescriber, error) {
desc := new(StdSqlDescriber)
desc.Id = id
desc.Data = data
desc.Config = l.Manifest.GetConfigCopy(id)
e := l.Render.Render(desc, id, data)
if l.Logger.IsDebugEnable() && e == nil {
l.Logger.Debugln(desc, data)
}
return desc, e
}
func NewStdSqlAssemblerDefault(pattern string) *StdSqlAssembler {
return NewStdSqlAssembler(NewStdTemplateRenderDefault(pattern), DefaultManifest)
}
func NewStdSqlAssembler(r SqlRender, m Manifest) *StdSqlAssembler {
logger := log.GetLogger("sqlt-default-loader")
return &StdSqlAssembler{Render: r, Logger: logger, Manifest: m}
}
type (
NestableSqlAssmbler interface {
SqlAssembler
HasId(id string) bool
}
SqlAssemblerSet struct {
def SqlAssembler
assemblers []NestableSqlAssmbler
}
)
func (n *SqlAssemblerSet) AssembleSql(id string, data interface{}) (SqlDescriber, error) {
for _, l := range n.assemblers {
if l.HasId(id) {
return l.AssembleSql(id, data)
}
}
return n.def.AssembleSql(id, data)
}
func NewSqlAssemblerSet(def SqlAssembler, assemblers ...NestableSqlAssmbler) *SqlAssemblerSet {
return &SqlAssemblerSet{def: def, assemblers: assemblers}
}