forked from databendlabs/databend-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpolate.go
85 lines (80 loc) · 1.55 KB
/
interpolate.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
package godatabend
import (
"database/sql/driver"
"reflect"
)
func placeholders(query string) []int {
n := 0
quote := false
first := -1
for i := 0; i < len(query); i++ {
switch query[i] {
case '\\':
i++
case '\'':
quote = !quote
case '?':
if !quote {
n++
if first == -1 {
first = i
}
}
}
}
if n == 0 {
return nil
}
quote = false
index := make([]int, n)
n = 0
for i, ch := range query[first:] {
switch ch {
case '\'':
quote = !quote
case '?':
if !quote {
index[n] = first + i
n++
}
}
}
return index
}
func interpolateParams(query string, params []driver.Value) (string, error) {
return interpolateParams2(query, params, placeholders(query))
}
func interpolateParams2(query string, params []driver.Value, index []int) (string, error) {
if len(params) == 0 {
return query, nil
}
if reflect.TypeOf(params[0]).Kind() == reflect.Slice {
if reflect.ValueOf(params[0]).Len() == 0 {
return query, nil
}
}
if len(index) != len(params) {
return "", ErrPlaceholderCount
}
var (
queryRaw = []byte(query)
paramsEncoded = make([][]byte, len(params))
n = len(queryRaw) - len(index) // do not count number of placeholders
)
for i, v := range params {
paramsEncoded[i], _ = textEncode.Encode(v)
n += len(paramsEncoded[i])
}
buf := make([]byte, n)
i := 0
k := 0
for j, idx := range index {
copy(buf[k:], queryRaw[i:idx])
k += idx - i
copy(buf[k:], paramsEncoded[j])
i = idx + 1
k += len(paramsEncoded[j])
}
copy(buf[k:], query[i:])
return string(buf), nil
}