-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.go
229 lines (200 loc) · 5.05 KB
/
executor.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
package sqlp
import (
"context"
"database/sql"
"errors"
"strconv"
"strings"
"text/template"
)
type sqlmode uint8
// SqlMode is an identifier that
// determines in what way the template is replaced
// before the SQL is executed.
const (
SqlModeNormal sqlmode = iota
SqlModeBatch
)
type sqlpoint interface {
ExecContext(context.Context, string, ...any) (sql.Result, error)
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
}
type executor struct {
t *template.Template
sqlm string
p sqlpoint
ctx context.Context
}
// At sets the implementation of sqlpoint,
// which may be *sql.DB, *sql.Conn or *sql.TX.
func (e *executor) At(p sqlpoint) *executor {
e.p = p
return e
}
// Ctx sets context.Context for sqlpoint.
func (e *executor) Ctx(ctx context.Context) *executor {
e.ctx = ctx
return e
}
// Parse parses into SQL statements (strings), and provides placeholder arguments.
func (e *executor) Parse(mode sqlmode, args ...map[string]any) (sqls string, placeholder []any, err error) {
var data any
data = args
if mode == SqlModeNormal {
if len(args) > 0 {
data = args[0]
}
}
b := &strings.Builder{}
if err = e.t.ExecuteTemplate(b, e.sqlm, data); err != nil {
return
}
sqls = b.String()
mapping := func(key string) (value any, exist bool, err error) {
if len(args) < 1 {
return
}
i := 0
if mode == SqlModeBatch {
sp := strings.Split(key, ".")
if len(sp) != 2 {
err = errors.New("invalid '@index.key': " + key)
return
}
isp := strings.TrimLeft(sp[0], "@")
key = sp[1]
var i64 int64
if i64, err = strconv.ParseInt(isp, 10, 32); err != nil {
return
}
i = int(i64)
}
value, exist = args[i][key]
return
}
sqls, _, err = expand("${", "}", sqls, func(key string) (string, any, bool, error) {
replaced := ""
value, exist, e := mapping(key)
if e == nil {
switch a := value.(type) {
case string:
replaced = a
case []byte:
replaced = string(a)
default:
e = errors.New("the '" + key + "' must be string or []byte")
}
}
return replaced, value, exist, e
})
if err != nil {
return
}
sqls, placeholder, err = expand("#{", "}", sqls, func(key string) (string, any, bool, error) {
value, exist, e := mapping(key)
return "?", value, exist, e
})
if err != nil {
return
}
sqls = strings.ReplaceAll(sqls, "\r", " ")
sqls = strings.ReplaceAll(sqls, "\n", " ")
return
}
func (e *executor) check() (err error) {
if e.p == nil {
err = errors.New("undefined sql point")
}
return
}
// Exec executes a query with args that doesn't return rows.
func (e *executor) Exec(args map[string]any) (sql.Result, error) {
return e.exec(SqlModeNormal, args)
}
// ExecBatch executes a query with the array of args that doesn't return rows.
func (e *executor) ExecBatch(args ...map[string]any) (sql.Result, error) {
return e.exec(SqlModeBatch, args...)
}
func (e *executor) exec(mode sqlmode, args ...map[string]any) (sql.Result, error) {
if err := e.check(); err != nil {
return nil, err
}
sqls, placeholder, err := e.Parse(mode, args...)
if err != nil {
return nil, err
}
return e.p.ExecContext(e.ctx, sqls, placeholder...)
}
// Query executes a query with args that returns rows, typically a SELECT.
func (e *executor) Query(args map[string]any) (*sql.Rows, error) {
return e.query(SqlModeNormal, args)
}
// QueryBatch executes a query with the array of args that returns rows, typically a SELECT.
func (e *executor) QueryBatch(args ...map[string]any) (*sql.Rows, error) {
return e.query(SqlModeBatch, args...)
}
func (e *executor) query(mode sqlmode, args ...map[string]any) (*sql.Rows, error) {
if err := e.check(); err != nil {
return nil, err
}
sqls, placeholder, err := e.Parse(mode, args...)
if err != nil {
return nil, err
}
return e.p.QueryContext(e.ctx, sqls, placeholder...)
}
// Scan executes a query with args that returns rows, and maps to dest.
func (e *executor) Scan(dest any, args map[string]any) error {
rs, err := e.Query(args)
if err != nil {
return err
}
return e.scan(dest, rs)
}
// Scan executes a query with the array of args that returns rows, and maps to dest.
func (e *executor) ScanBatch(dest any, args ...map[string]any) error {
rs, err := e.QueryBatch(args...)
if err != nil {
return err
}
return e.scan(dest, rs)
}
func (e *executor) scan(dest any, rs *sql.Rows) error {
// if something happens here, we want to make sure the rows are Closed
defer func() {
if rs != nil {
_ = rs.Close()
}
}()
return scanAny(dest, rs)
}
type mapfunc func(key string) (replaced string, value any, exist bool, err error)
func expand(open, close string, s string, f mapfunc) (string, []any, error) {
var (
args []any
err error
)
for {
pos0 := strings.Index(s, open)
if pos0 < 0 {
break
}
pos1 := pos0 + len(open)
pos2 := strings.Index(s[pos1:], close)
if pos2 < 0 {
break
}
key := s[pos1 : pos1+pos2]
pos3 := pos1 + pos2 + len(close)
replaced, value, exist, e := f(key)
if e != nil {
err = e
break
}
if exist {
args = append(args, value)
}
s = s[:pos0] + replaced + s[pos3:]
}
return s, args, err
}