-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs.go
198 lines (173 loc) · 4.12 KB
/
funcs.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
package sqlt
import (
"fmt"
"reflect"
"strings"
"text/template"
)
func (c *context) paramWithFunc(name string, fn func(string, interface{}) (string, interface{})) string {
p, err := c.Get(name)
if err != nil {
return c.errorOutput(err)
}
nm := p.name
v := p.value
if fn != nil {
nm, v = fn(nm, v)
}
if c.named || c.dialect.IsOrdinalPlaceholderSupported() {
c.MergeArg(nm, v)
} else {
c.AddArg(nm, v)
}
return c.Placeholder(nm) + c.annotation(name)
}
func (c *context) get(name string) interface{} {
p, err := c.Get(name)
if err != nil {
c.err = err
return nil
}
if s, ok := p.value.(string); ok {
if err = safe(s); err != nil {
c.err = fmt.Errorf("%q contains prohibited character(%s)", name, err.Error())
return nil
}
}
return p.value
}
func (c *context) out(name string) string {
p, err := c.Get(name)
if err != nil {
return c.errorOutput(err)
}
s := fmt.Sprintf("%v", p.value)
if err = safe(s); err != nil {
return c.errorOutput(fmt.Errorf("%q contains prohibited character(%s)", name, err.Error()))
}
return s + c.annotation(name)
}
func (c *context) param(name string) string {
return c.paramWithFunc(name, nil)
}
func (c *context) in(name string) string {
p, err := c.Get(name)
if err != nil {
return c.errorOutput(err)
}
v := reflect.ValueOf(p.value)
if v.Kind() != reflect.Slice {
return "(" + c.param(name) + ")"
}
placeholders := make([]string, v.Len())
for i := 0; i < v.Len(); i++ {
sv := v.Index(i).Interface()
argName := fmt.Sprintf("%s%s%d", name, Connector, i+1)
if c.named || c.dialect.IsOrdinalPlaceholderSupported() {
c.MergeArg(argName, sv)
} else {
c.AddArg(argName, sv)
}
placeholders[i] = c.Placeholder(argName)
}
return "(" + strings.Join(placeholders, ", ") + ")" + c.annotation(name)
}
func (c *context) time() string {
name := "time" + Connector
tm := c.timer.time()
if c.named || c.dialect.IsOrdinalPlaceholderSupported() {
c.MergeArg(name, tm)
} else {
c.AddArg(name, tm)
}
return c.Placeholder(name)
}
func (c *context) now() string {
name := fmt.Sprintf("now%s%d", Connector, c.timer.nowCnt)
c.AddArg(name, c.timer.now())
return c.Placeholder(name)
}
func (c *context) prefix(name string) string {
return c.paramWithEscapeLike(name) + " || '%'" + escapeClause
}
func (c *context) infix(name string) string {
return "'%' || " + c.paramWithEscapeLike(name) + " || '%'" + escapeClause
}
func (c *context) suffix(name string) string {
return "'%' || " + c.paramWithEscapeLike(name) + escapeClause
}
func (c *context) name(args ...interface{}) string {
if len(args) == 0 {
return ""
}
ss := make([]string, len(args), len(args))
for i, a := range args {
if s, ok := a.(string); ok {
ss[i] = s
} else {
ss[i] = fmt.Sprintf("%v", a)
}
}
return strings.Join(ss, ".")
}
func (c *context) paramWithEscapeLike(name string) string {
return c.paramWithFunc(name, func(name string, v interface{}) (string, interface{}) {
nv := c.escapeLike(v)
if nv == v {
return name, v
}
return name + Connector + "esc", nv
})
}
func (c *context) escapeLike(i interface{}) interface{} {
s, ok := i.(string)
if !ok {
return i
}
rs := []rune(s)
v := make([]rune, 0)
for _, r := range rs {
if c.isEscapee(r) {
v = append(v, escapeChar)
}
v = append(v, r)
}
return string(v)
}
func (c *context) isEscapee(r rune) bool {
for _, w := range c.dialect.WildcardRunes() {
if r == w || r == escapeChar {
return true
}
}
return false
}
func (c *context) funcMap(funcs map[string]interface{}) template.FuncMap {
fm := template.FuncMap(funcs)
fm["get"] = c.get
fm["out"] = c.out
fm["o"] = c.out
fm["param"] = c.param
fm["p"] = c.param
fm["in"] = c.in
fm["time"] = c.time
fm["now"] = c.now
fm["prefix"] = c.prefix
fm["infix"] = c.infix
fm["suffix"] = c.suffix
fm["escape"] = c.escapeLike
fm["name"] = c.name
return fm
}
func safe(s string) error {
if strings.Contains(s, "'") {
return fmt.Errorf("single quotation")
}
if strings.Contains(s, ";") {
return fmt.Errorf("semi colon")
}
if strings.Contains(s, "--") || strings.Contains(s, "/*") || strings.Contains(s, "*/") {
return fmt.Errorf("comment")
}
return nil
}