-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinterpolate.go
More file actions
109 lines (103 loc) · 2.19 KB
/
interpolate.go
File metadata and controls
109 lines (103 loc) · 2.19 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
package godatabend
import (
"database/sql/driver"
"github.com/pkg/errors"
"reflect"
)
func buildQuery(query string, args []driver.Value, placeholders *[]int) (string, error) {
var q string
var err error
if placeholders != nil {
if len(*placeholders) != len(args) {
return "", errors.Errorf("expect %v args, got %v", len(*placeholders), len(args))
}
q, err = interpolateParams2(query, args, *placeholders)
if err != nil {
return "", err
}
return q, nil
}
if len(args) > 0 && args[0] != nil {
result, err := interpolateParams(query, args)
if err != nil {
return result, errors.Wrap(err, "buildRequest: failed to interpolate params")
}
return result, nil
}
return query, nil
}
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
}