-
Notifications
You must be signed in to change notification settings - Fork 3
/
mysql_escape.go
61 lines (58 loc) · 958 Bytes
/
mysql_escape.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
package main
import (
"errors"
"fmt"
)
func mysql_escape(source string) (string, error) {
var j int = 0
if len(source) == 0 {
return "", errors.New("source is null")
}
tempStr := source[:]
desc := make([]byte, len(tempStr)*2)
for i := 0; i < len(tempStr); i++ {
flag := false
var escape byte
switch tempStr[i] {
case '\r':
flag = true
escape = '\r'
break
case '\n':
flag = true
escape = '\n'
break
case '\\':
flag = true
escape = '\\'
break
case '\'':
flag = true
escape = '\''
break
case '"':
flag = true
escape = '"'
break
case '\032':
flag = true
escape = 'Z'
break
default:
}
if flag {
desc[j] = '\\'
desc[j+1] = escape
j = j + 2
} else {
desc[j] = tempStr[i]
j = j + 1
}
}
return string(desc[0:j]), nil
}
func main() {
if str, err := mysql_escape(`SELECT * FROM users WHERE user=20 AND password='%s'`); err == nil {
fmt.Println(str)
}
}