-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtqla.go
57 lines (45 loc) · 1.29 KB
/
tqla.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
package tqla
import (
"bytes"
"maps"
"text/template"
)
type tqla struct {
placeholder Placeholder
funcs template.FuncMap
}
// New creates a new tqla instance that can be used to generate dynamic queries via sql text/templating
func New(options ...Option) (*tqla, error) {
opts := defaultOptions()
for _, opt := range options {
if err := opt.Apply(opts); err != nil {
return nil, err
}
}
return &tqla{
placeholder: opts.placeholder,
funcs: opts.funcs,
}, nil
}
// Compile accepts a string sql template and a data object that
// is used to build the db args. A finalized sql statement is returned with placeholders where dynamic
// arguments are passed as well as a slice of args. Returns a error if the sql template cannot be parsed. Compile
// is safe to call multiple times.
func (t *tqla) Compile(statement string, data any) (string, []any, error) {
parser := newSqlParser()
funcs := maps.Clone(t.funcs)
funcs["_sql_parser_"] = parser.parsefunc
tmpl := newSqlTemplate("tqla", funcs)
if err := tmpl.parse(statement); err != nil {
return "", nil, err
}
b := &bytes.Buffer{}
if err := tmpl.execute(b, data); err != nil {
return "", nil, err
}
sql, err := t.placeholder.Format(b.String())
if err != nil {
return "", nil, err
}
return sql, parser.args, nil
}