-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlutils.go
57 lines (51 loc) · 1.09 KB
/
sqlutils.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 goutils
import "strings"
// Escape the sql to prevent sql injection.
// Simply a copy from https://gist.github.com/siddontang/8875771
func Escape(sql string) string {
dest := make([]byte, 0, 2*len(sql))
var escape byte
for i := 0; i < len(sql); i++ {
c := sql[i]
escape = 0
switch c {
case 0: /* Must be escaped for 'mysql' */
escape = '0'
break
case '\n': /* Must be escaped for logs */
escape = 'n'
break
case '\r':
escape = 'r'
break
case '\\':
escape = '\\'
break
case '\'':
escape = '\''
break
case '"': /* Better safe than sorry */
escape = '"'
break
case '\032': /* This gives problems on Win32 */
escape = 'Z'
}
if escape != 0 {
dest = append(dest, '\\', escape)
} else {
dest = append(dest, c)
}
}
return string(dest)
}
// InParam is a helper method to build in params of sql.
func InParam(ins []string) string {
if len(ins) <= 0 {
panic("input params required")
}
ins2 := make([]string, 0)
for _, v := range ins {
ins2 = append(ins2, "'"+Escape(v)+"'")
}
return "(" + strings.Join(ins2, ",") + ")"
}