diff --git a/query.go b/query.go index 1472a54..73ef203 100644 --- a/query.go +++ b/query.go @@ -34,21 +34,37 @@ func (d *Db) query(query string) *Query { } func (q *Query) Bind(args ...any) *Query { + into := 0 + return q.bindInto(&into, args...) +} + +func (q *Query) bindInto(into *int, args ...any) *Query { if q.err != nil || q.stmt == nil { return q } for i, arg := range args { + *into++ if asString, ok := arg.(string); ok { - q.stmt.BindText(i+1, asString) + q.stmt.BindText(*into, asString) } else if asInt, ok := arg.(int); ok { - q.stmt.BindInt64(i+1, int64(asInt)) + q.stmt.BindInt64(*into, int64(asInt)) } else if asInt, ok := arg.(int64); ok { - q.stmt.BindInt64(i+1, asInt) + q.stmt.BindInt64(*into, asInt) } else if asBool, ok := arg.(bool); ok { - q.stmt.BindBool(i+1, asBool) + q.stmt.BindBool(*into, asBool) } else { - q.err = fmt.Errorf("unsupported column type %s at index %d", reflect.TypeOf(arg).Name(), i) - return q + // Check if the argument is a slice or array of any type + v := reflect.ValueOf(arg) + if v.Kind() == reflect.Slice || v.Kind() == reflect.Array { + *into-- + for i := 0; i < v.Len(); i++ { + q.bindInto(into, v.Index(i).Interface()) + } + } else { + *into-- + q.err = fmt.Errorf("unsupported column type %s at index %d", reflect.TypeOf(arg).Name(), i) + return q + } } } return q diff --git a/query_test.go b/query_test.go index 16269ec..81bf575 100644 --- a/query_test.go +++ b/query_test.go @@ -141,7 +141,8 @@ func TestQueryWithInClause(t *testing.T) { db.Query("insert into mytable(key, value) values ('key2', 'value2')").MustExec() // Execute query with IN clause - rows, err := db.Query("select key, value from mytable where key in (?, ?)").Bind("foo", "key2").ScanMulti() + args := []string{"foo", "key2"} + rows, err := db.Query("select key, value from mytable where key in (?, ?)").Bind(args).ScanMulti() require.NoError(t, err) defer rows.MustFinish()