-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathparams_replacer.go
More file actions
201 lines (180 loc) · 4.33 KB
/
params_replacer.go
File metadata and controls
201 lines (180 loc) · 4.33 KB
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
package gohive
import (
"database/sql/driver"
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
)
const (
TimeStampLayout = "2006-01-02 15:04:05.999999999"
DateLayout = "2006-01-02"
)
type ParamsInterpolator struct {
Local *time.Location
}
func NewParamsInterpolator() *ParamsInterpolator {
return &ParamsInterpolator{
Local: time.Local,
}
}
func (p *ParamsInterpolator) InterpolateNamedValue(query string, namedArgs []driver.NamedValue) (string, error) {
args, err := namedValueToValue(namedArgs)
if err != nil {
return "", err
}
return p.Interpolate(query, args)
}
func (p *ParamsInterpolator) Interpolate(query string, args []driver.Value) (string, error) {
if strings.Count(query, "?") != len(args) {
return "", fmt.Errorf("gohive driver: number of ? [%d] must be equal to len(args): [%d]",
strings.Count(query, "?"), len(args))
}
var err error
argIdx := 0
var buf = make([]byte, 0, len(query)+len(args)*15)
for i := 0; i < len(query); i++ {
q := strings.IndexByte(query[i:], '?')
if q == -1 {
buf = append(buf, query[i:]...)
break
}
buf = append(buf, query[i:i+q]...)
i += q
arg := args[argIdx]
argIdx++
buf, err = p.interpolateOne(buf, arg)
if err != nil {
return "", fmt.Errorf("gohive driver: failed to interpolate failed: %w, args[%d]: [%v]",
err, argIdx, arg)
}
}
if argIdx != len(args) {
return "", fmt.Errorf("gohive driver: args are not all filled into SQL, argIdx: %d, total: %d",
argIdx, len(args))
}
return string(buf), nil
}
func (p *ParamsInterpolator) interpolateOne(buf []byte, arg driver.Value) ([]byte, error) {
if arg == nil {
buf = append(buf, "NULL"...)
return buf, nil
}
switch v := arg.(type) {
case int64:
buf = strconv.AppendInt(buf, v, 10)
case uint64:
// Handle uint64 explicitly because our custom ConvertValue emits unsigned values
buf = strconv.AppendUint(buf, v, 10)
case float64:
buf = strconv.AppendFloat(buf, v, 'g', -1, 64)
case bool:
if v {
buf = append(buf, "'true'"...)
} else {
buf = append(buf, "'false'"...)
}
case time.Time:
if v.IsZero() {
buf = append(buf, "'0000-00-00'"...)
} else {
buf = append(buf, '\'')
buf = appendDateTime(buf, v.In(p.Local))
buf = append(buf, '\'')
}
case json.RawMessage:
buf = append(buf, '\'')
buf = appendBytes(buf, v)
buf = append(buf, '\'')
case []byte:
if v == nil {
buf = append(buf, "NULL"...)
} else {
buf = append(buf, "X'"...)
buf = appendBytes(buf, v)
buf = append(buf, '\'')
}
case string:
buf = append(buf, '\'')
buf = escapeStringBackslash(buf, v)
buf = append(buf, '\'')
default:
return nil, fmt.Errorf("gohive driver: unexpected args type: %T", arg)
}
return buf, nil
}
func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) {
args := make([]driver.Value, len(named))
for n, param := range named {
if len(param.Name) > 0 {
return nil, fmt.Errorf("gohive driver: driver does not support the use of Named Parameters")
}
args[n] = param.Value
}
return args, nil
}
func appendBytes(buf, v []byte) []byte {
pos := len(buf)
buf = reserveBuffer(buf, len(v)+hex.EncodedLen(len(v)))
pos += hex.Encode(buf[pos:], v)
return buf[:pos]
}
func appendDateTime(buf []byte, t time.Time) []byte {
buf = t.AppendFormat(buf, TimeStampLayout)
return buf
}
func escapeStringBackslash(buf []byte, v string) []byte {
pos := len(buf)
buf = reserveBuffer(buf, len(v)*2)
for i := 0; i < len(v); i++ {
c := v[i]
switch c {
case '\x00':
buf[pos+1] = '0'
buf[pos] = '\\'
pos += 2
case '\n':
buf[pos+1] = 'n'
buf[pos] = '\\'
pos += 2
case '\r':
buf[pos+1] = 'r'
buf[pos] = '\\'
pos += 2
case '\x1a':
buf[pos+1] = 'Z'
buf[pos] = '\\'
pos += 2
case '\'':
buf[pos+1] = '\''
buf[pos] = '\\'
pos += 2
case '"':
buf[pos+1] = '"'
buf[pos] = '\\'
pos += 2
case '\\':
buf[pos+1] = '\\'
buf[pos] = '\\'
pos += 2
default:
buf[pos] = c
pos++
}
}
return buf[:pos]
}
// reserveBuffer checks cap(buf) and expand buffer to len(buf) + appendSize.
// If cap(buf) is not enough, reallocate new buffer.
func reserveBuffer(buf []byte, appendSize int) []byte {
newSize := len(buf) + appendSize
if cap(buf) < newSize {
// Grow buffer exponentially
newBuf := make([]byte, len(buf)*2+appendSize)
copy(newBuf, buf)
buf = newBuf
}
return buf[:newSize]
}