Skip to content

Commit 5e58803

Browse files
committed
ClickHouse#103 quote slices
1 parent 4dcd9b8 commit 5e58803

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Diff for: helpers.go

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"database/sql/driver"
66
"fmt"
7+
"reflect"
78
"regexp"
89
"strings"
910
"time"
@@ -116,6 +117,14 @@ func isInsert(query string) bool {
116117
}
117118

118119
func quote(v driver.Value) string {
120+
switch v := reflect.ValueOf(v); v.Kind() {
121+
case reflect.Slice:
122+
values := make([]string, 0, v.Len())
123+
for i := 0; i < v.Len(); i++ {
124+
values = append(values, quote(v.Index(i).Interface()))
125+
}
126+
return strings.Join(values, ", ")
127+
}
119128
switch v := v.(type) {
120129
case string:
121130
return "'" + strings.NewReplacer(`\`, `\\`, `'`, `\'`).Replace(v) + "'"

Diff for: helpers_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,14 @@ func Benchmark_NumInput(b *testing.B) {
3838
numInput("SELECT * FROM example WHERE os_id in (@os_id,@browser_id) browser_id = @browser_id")
3939
}
4040
}
41+
42+
func Test_Quote(t *testing.T) {
43+
for expected, value := range map[string]interface{}{
44+
"'a'": "a",
45+
"1": 1,
46+
"'a', 'b', 'c'": []string{"a", "b", "c"},
47+
"1, 2, 3, 4, 5": []int{1, 2, 3, 4, 5},
48+
} {
49+
assert.Equal(t, expected, quote(value))
50+
}
51+
}

0 commit comments

Comments
 (0)