-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsql.go
181 lines (155 loc) · 3.99 KB
/
sql.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
package sqlstring
import (
"encoding/hex"
"encoding/json"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
var (
tmFmtZero = "0000-00-00 00:00:00"
tmFmtWithMS = "2006-01-02 15:04:05.999"
escaper = "'"
nullStr = "NULL"
singleQuoteEscaper = "\\"
escapeRegexp = regexp.MustCompile(`[\0\t\x1a\n\r\"\'\\]`)
//see href='https://dev.mysql.com/doc/refman/8.0/en/string-literals.html#character-escape-sequences'
characterEscapeMap = map[string]string{
"\\0": `\\0`, //ASCII NULL
"\b": `\\b`, //backspace
"\t": `\\t`, //tab
"\x1a": `\\Z`, //ASCII 26 (Control+Z);
"\n": `\\n`, //newline character
"\r": `\\r`, //return character
"\"": `\\"`, //quote (")
"'": `\'`, //quote (')
"\\": `\\\\`, //backslash (\)
// "\\%": `\\%`, //% character
// "\\_": `\\_`, //_ character
}
)
//Escape escape the val for sql
func Escape(val interface{}) string {
return EscapeInLocation(val, time.Local)
}
//toSqlString escape the string val for sql
func toSqlString(val string) string {
return escapeRegexp.ReplaceAllStringFunc(val, func(s string) string {
mVal, ok := characterEscapeMap[s]
if ok {
return mVal
}
return s
})
}
func timeToString(t time.Time, loc *time.Location) string {
if t.IsZero() {
return escaper + tmFmtZero + escaper
}
if loc != nil {
return escaper + t.In(loc).Format(tmFmtWithMS) + escaper
}
return escaper + t.Format(tmFmtWithMS) + escaper
}
func arrayToString(refValue reflect.Value, loc *time.Location) string {
var res []string
for i := 0; i < refValue.Len(); i++ {
res = append(res, EscapeInLocation(refValue.Index(i).Interface(), loc))
}
return strings.Join(res, ",")
}
func bytesToString(b []byte) string {
return "X" + escaper + hex.EncodeToString(b) + escaper
}
//EscapeInLocation escape the val with time.Location
func EscapeInLocation(val interface{}, loc *time.Location) string {
if val == nil {
return nullStr
}
switch v := val.(type) {
case bool:
return strconv.FormatBool(v)
case time.Time:
return timeToString(v, loc)
case *time.Time:
if v == nil {
return nullStr
}
return timeToString(*v, loc)
case []byte:
return bytesToString(v)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return fmt.Sprintf("%d", v)
case float32, float64:
return fmt.Sprintf("%.6f", v)
case string:
return escaper + toSqlString(v) + escaper
default:
refValue := reflect.ValueOf(v)
if v == nil || !refValue.IsValid() {
return nullStr
}
if refValue.Kind() == reflect.Ptr && refValue.IsNil() {
return nullStr
}
if refValue.Kind() == reflect.Ptr && !refValue.IsZero() {
return EscapeInLocation(reflect.Indirect(refValue).Interface(), loc)
}
if refValue.Kind() == reflect.Array || refValue.Kind() == reflect.Slice {
//slice or array
return arrayToString(refValue, loc)
}
stringifyData, err := json.Marshal(v)
if err != nil {
return nullStr
}
return escaper + toSqlString(string(stringifyData)) + escaper
}
}
//Format format the sql with args
func Format(query string, args ...interface{}) string {
if len(args) == 0 {
return query
}
var sql strings.Builder
replaceIndex := 0
for _, v := range query {
if v == '?' {
if len(args) > replaceIndex {
sql.WriteString(Escape(args[replaceIndex]))
replaceIndex++
continue
}
}
sql.WriteRune(v)
}
return sql.String()
}
//FormatInLocation format the sql with args
func FormatInLocation(query string, loc *time.Location, args ...interface{}) string {
if len(args) == 0 {
return query
}
var sql strings.Builder
replaceIndex := 0
for _, v := range query {
if v == '?' {
if len(args) > replaceIndex {
sql.WriteString(EscapeInLocation(args[replaceIndex], loc))
replaceIndex++
continue
}
}
sql.WriteRune(v)
}
return sql.String()
}
//SetSingleQuoteEscaper set the singleQuoteEscaper
//default:\' , e.g. '' 、 \'
func SetSingleQuoteEscaper(escaper string) {
characterEscapeMap["'"] = escaper
// singleQuoteEscaper = escaper
}