-
Notifications
You must be signed in to change notification settings - Fork 15
/
sql_util.go
204 lines (176 loc) · 3.56 KB
/
sql_util.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
199
200
201
202
203
204
package pinpoint
import (
"bufio"
"strconv"
"strings"
)
type sqlNormalizer struct {
r *bufio.Reader
output *strings.Builder
param *strings.Builder
paramIndex int
sql string
isChanged bool
}
func newSqlNormalizer(sql string) *sqlNormalizer {
normalizer := sqlNormalizer{}
normalizer.r = bufio.NewReader(strings.NewReader(sql))
normalizer.output = &strings.Builder{}
normalizer.param = &strings.Builder{}
normalizer.paramIndex = 0
normalizer.sql = sql
normalizer.isChanged = false
return &normalizer
}
func (s *sqlNormalizer) run() (string, string) {
numberTokenStartEnable := false
for {
if ch := s.read(); ch == eof {
break
} else if ch == '/' {
s.output.WriteRune(ch)
if s.lookahead('/') {
s.consumeSingleLineComment()
} else if s.lookahead('*') {
s.consumeMultiLineComment()
} else {
numberTokenStartEnable = true
}
} else if ch == '-' {
s.output.WriteRune(ch)
if s.lookahead('-') {
s.consumeSingleLineComment()
} else {
numberTokenStartEnable = true
}
} else if ch == '\'' {
s.output.WriteRune(ch)
if s.lookahead('\'') {
s.output.WriteRune(s.read())
} else {
s.consumeCharLiteral()
}
} else if isDigit(ch) {
if numberTokenStartEnable {
s.unread()
s.consumeNumberLiteral()
} else {
s.output.WriteRune(ch)
}
} else if isLetter(ch) || ch == '.' || ch == '_' || ch == '@' || ch == ':' || ch == '$' {
numberTokenStartEnable = false
s.output.WriteRune(ch)
} else {
numberTokenStartEnable = true
s.output.WriteRune(ch)
}
}
if s.isChanged {
if s.param.Len() > 0 {
return s.output.String(), s.param.String()
} else {
return s.output.String(), ""
}
} else {
return s.sql, ""
}
}
func (s *sqlNormalizer) consumeSingleLineComment() {
var ch rune
for {
if ch = s.read(); ch == eof {
break
}
s.output.WriteRune(ch)
if ch == '\n' {
break
}
}
}
func (s *sqlNormalizer) consumeMultiLineComment() {
var ch rune
prev := eof
s.output.WriteRune(s.read()) /* cousume '*' */
for {
if ch = s.read(); ch == eof {
break
}
s.output.WriteRune(ch)
if prev == '*' && ch == '/' {
break
}
prev = ch
}
}
func (s *sqlNormalizer) consumeCharLiteral() {
var ch rune
s.isChanged = true
if s.param.Len() > 0 {
s.param.WriteRune(',')
}
for {
if ch = s.read(); ch == eof {
break
}
if ch == ',' {
s.param.WriteRune(ch)
} else if ch == '\'' {
if s.lookahead('\'') {
s.param.WriteRune(s.read())
} else {
s.output.WriteString(strconv.Itoa(s.paramIndex))
s.paramIndex++
s.output.WriteRune('$')
s.output.WriteRune('\'')
break
}
}
s.param.WriteRune(ch)
}
}
func (s *sqlNormalizer) consumeNumberLiteral() {
var ch rune
s.isChanged = true
if s.param.Len() > 0 {
s.param.WriteRune(',')
}
s.output.WriteString(strconv.Itoa(s.paramIndex))
s.paramIndex++
s.output.WriteRune('#')
for {
if ch = s.read(); ch == eof {
break
}
if isDigit(ch) || ch == '.' || ch == 'E' || ch == 'e' {
s.param.WriteRune(ch)
} else {
s.unread()
break
}
}
}
func (s *sqlNormalizer) read() rune {
ch, _, err := s.r.ReadRune()
if err != nil {
return eof
}
return ch
}
func (s *sqlNormalizer) unread() {
_ = s.r.UnreadRune()
}
func (s *sqlNormalizer) lookahead(expected rune) bool {
ch, _, err := s.r.ReadRune()
_ = s.r.UnreadRune()
if err != nil {
return false
}
return ch == expected
}
func isLetter(ch rune) bool {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
}
func isDigit(ch rune) bool {
return ch >= '0' && ch <= '9'
}
var eof = rune(0)