diff --git a/helpers.go b/helpers.go index 0c4ca91fc5..862d26bfa6 100644 --- a/helpers.go +++ b/helpers.go @@ -4,6 +4,7 @@ import ( "bytes" "database/sql/driver" "fmt" + "reflect" "regexp" "strings" "time" @@ -116,6 +117,14 @@ func isInsert(query string) bool { } func quote(v driver.Value) string { + switch v := reflect.ValueOf(v); v.Kind() { + case reflect.Slice: + values := make([]string, 0, v.Len()) + for i := 0; i < v.Len(); i++ { + values = append(values, quote(v.Index(i).Interface())) + } + return strings.Join(values, ", ") + } switch v := v.(type) { case string: return "'" + strings.NewReplacer(`\`, `\\`, `'`, `\'`).Replace(v) + "'" diff --git a/helpers_test.go b/helpers_test.go index 12d18e82af..5e41bf20e0 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -38,3 +38,14 @@ func Benchmark_NumInput(b *testing.B) { numInput("SELECT * FROM example WHERE os_id in (@os_id,@browser_id) browser_id = @browser_id") } } + +func Test_Quote(t *testing.T) { + for expected, value := range map[string]interface{}{ + "'a'": "a", + "1": 1, + "'a', 'b', 'c'": []string{"a", "b", "c"}, + "1, 2, 3, 4, 5": []int{1, 2, 3, 4, 5}, + } { + assert.Equal(t, expected, quote(value)) + } +}