-
Notifications
You must be signed in to change notification settings - Fork 18
/
Quotes.go
196 lines (175 loc) · 4.88 KB
/
Quotes.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
package S
import (
"fmt"
"runtime"
"strconv"
"github.com/kokizzu/gotro/A"
"github.com/kokizzu/gotro/B"
"github.com/kokizzu/gotro/I"
)
// TODO: find out how backspace \b null \0 character processed on common SQL
// ZT trace function, location of the caller code, replacement for ZC
func ZT(strs ...string) string {
pc := make([]uintptr, 10) // at least 1 entry needed
runtime.Callers(2, pc)
f := runtime.FuncForPC(pc[0])
file, line := f.FileLine(pc[0])
str := A.StrJoin(strs, `|`)
str = Replace(str, "\n", ` `)
str = Replace(str, `\n`, ` `)
return `-- ` + fmt.Sprintf("%s:%d %s|%s", file, line, f.Name(), str)
}
// ZT2 trace function, location of 2nd level caller, parameterless, with newline
func ZT2() string {
pc := make([]uintptr, 10) // at least 1 entry needed
runtime.Callers(3, pc)
f := runtime.FuncForPC(pc[0])
file, line := f.FileLine(pc[0])
return `-- ` + fmt.Sprintf("%s:%d %s", file, line, f.Name()) + "\n"
}
// Q add single quote in the beginning and the end of string, without escaping.
//
// S.Q(`coba`) // `'coba'`
// S.Q(`123`) // `'123'`
func Q(str string) string {
return `'` + str + `'`
}
// QQ add double quote in the beginning and the end of string, without escaping.
//
// S.Q(`coba`) // `"coba"`
// S.Q(`123`) // `"123"`
func QQ(str string) string {
return `"` + str + `"`
}
// BT add backtick quote in the beginning and the end of string, without escaping.
//
// S.Q(`coba`) // "`coba`"
// S.Q(`123`) // "`123`"
func BT(str string) string {
return "`" + str + "`"
}
// ZZ replace ` and give double quote (for table names)
//
// S.ZZ(`coba"`) // `"coba""`
func ZZ(str string) string {
str = Trim(str)
str = Replace(str, `"`, `"`)
return `"` + str + `"`
}
// ZB give ' to boolean value
//
// S.ZB(true) // `'true'`
// S.ZB(false) // `'false'`
func ZB(b bool) string {
return `'` + B.ToS(b) + `'`
}
// ZI give ' to int64 value
//
// S.ZI(23)) // '23'
// S.ZI(03)) // '3'
func ZI(num int64) string {
return `'` + I.ToS(num) + `'`
}
// ZU give ' to uint value
//
// S.ZI(23)) // '23'
// S.ZI(03)) // '3'
func ZU(num uint64) string {
return `'` + strconv.FormatUint(num, 10) + `'`
}
// ZJJ double quote a json string
//
// hai := `{'test':123,"bla":[1,2,3,4]}`
// S.ZJJ(hai) // "{'test':123,\"bla\":[1,2,3,4]}"
func ZJJ(str string) string {
str = Replace(str, `\`, `\\`)
str = Replace(str, "\r", `\r`)
str = Replace(str, "\n", `\n`)
str = Replace(str, `"`, `\"`)
return `"` + str + `"`
}
// ZJ single quote a json string
//
// hai := `{'test':123,"bla":[1,2,3,4]}`
// S.ZJ(hai) // "{'test':123,\"bla\":[1,2,3,4]}"
func ZJ(str string) string {
str = Replace(str, `\`, `\\`)
str = Replace(str, "\r", `\r`)
str = Replace(str, "\n", `\n`)
str = Replace(str, `'`, `\'`)
return `'` + str + `'`
}
// Z trim, replace <, >, ', " and gives single quote
//
// S.Z(`<>'"`) // `<>'"
func Z(str string) string {
str = Trim(str)
str = Replace(str, `<`, `<`)
str = Replace(str, `>`, `>`)
str = Replace(str, `'`, `'`)
str = Replace(str, `"`, `"`)
str = Replace(str, `\`, `\\`)
return `'` + str + `'`
}
// ZS replace <, >, ', " and gives single quote (without trimming)
//
// S.Z(`<>'"`) // `<>'"
func ZS(str string) string {
str = Replace(str, `<`, `<`)
str = Replace(str, `>`, `>`)
str = Replace(str, `'`, `'`)
str = Replace(str, `"`, `"`)
str = Replace(str, `\`, `\\`)
return `'` + str + `'`
}
// ZLIKE replace <, >, ', ", % and gives single quote and %
//
// S.ZLIKE(`coba<`)) // output '%coba<%'
// S.ZLIKE(`"coba"`)) // output '%"coba"%'
func ZLIKE(str string) string {
str = Trim(str)
str = Replace(str, `<`, `<`)
str = Replace(str, `>`, `>`)
str = Replace(str, `'`, `'`)
str = Replace(str, `"`, `"`)
str = Replace(str, `\`, `\\`)
str = Replace(str, `%`, `\%`)
return `'%` + str + `%'`
}
// ZJLIKE ZLIKE but for json (not replacing double quote)
func ZJLIKE(str string) string {
str = Trim(str)
str = Replace(str, `<`, `<`)
str = Replace(str, `>`, `>`)
str = Replace(str, `'`, `'`)
str = Replace(str, `\`, `\\`)
str = Replace(str, `%`, `\%`)
return `'%` + str + `%'`
}
// XSS replace <, >, ', ", % but without giving single quote
func XSS(str string) string {
str = Trim(str)
str = Replace(str, `<`, `<`)
str = Replace(str, `>`, `>`)
str = Replace(str, `'`, `'`)
str = Replace(str, `"`, `"`)
return str
}
// UZ replace <, >, and & back, quot and apos to alternative utf8
func UZ(str string) string {
str = Replace(str, `'`, `‘`)
str = Replace(str, `"`, `ʺ`)
str = Replace(str, `<`, `<`)
str = Replace(str, `>`, `>`)
str = Replace(str, `&`, `&`)
return str
}
// UZRAW replace <, >, and & back, quot and apos to real html
func UZRAW(str string) string {
str = Replace(str, `'`, `'`)
str = Replace(str, `"`, `"`)
str = Replace(str, `<`, `<`)
str = Replace(str, `>`, `>`)
str = Replace(str, `&`, `&`)
return str
}