-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqmodel.go
More file actions
46 lines (36 loc) · 850 Bytes
/
Copy pathqmodel.go
File metadata and controls
46 lines (36 loc) · 850 Bytes
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
package main
import (
"fmt"
"strings"
)
// Stupid simple templating system for sql query strings, it just makes
// query creation a little cleaner for the reader.
type QueryTemplate struct {
template string
values map[string]string
}
func newQueryTemplate(query string) *QueryTemplate {
qm := new(QueryTemplate)
qm.template = query
qm.values = make(map[string]string)
return qm
}
func (qm *QueryTemplate) WithValues(m *map[string]string) {
for k, v := range *m {
qm.SetValue(k, v)
}
}
func (qm *QueryTemplate) SetValue(name string, value string) {
name = "%" + name + "%"
qm.values[name] = value
}
func (qm *QueryTemplate) Execute() string {
q := qm.template
for k, v := range qm.values {
q = strings.ReplaceAll(q, k, fmt.Sprint(v))
}
return q
}
func (qm *QueryTemplate) Clear() {
qm.values = make(map[string]string)
}