Compare commits
2 Commits
850e4a27d8
...
87f10c73d6
Author | SHA1 | Date | |
---|---|---|---|
87f10c73d6 | |||
58d63b6cf3 |
31
query.go
31
query.go
@ -34,21 +34,37 @@ func (d *Db) query(query string) *Query {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (q *Query) Bind(args ...any) *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 {
|
if q.err != nil || q.stmt == nil {
|
||||||
return q
|
return q
|
||||||
}
|
}
|
||||||
for i, arg := range args {
|
for i, arg := range args {
|
||||||
|
*into++
|
||||||
if asString, ok := arg.(string); ok {
|
if asString, ok := arg.(string); ok {
|
||||||
q.stmt.BindText(i+1, asString)
|
q.stmt.BindText(*into, asString)
|
||||||
} else if asInt, ok := arg.(int); ok {
|
} 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 {
|
} 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 {
|
} else if asBool, ok := arg.(bool); ok {
|
||||||
q.stmt.BindBool(i+1, asBool)
|
q.stmt.BindBool(*into, asBool)
|
||||||
} else {
|
} else {
|
||||||
q.err = fmt.Errorf("unsupported column type %s at index %d", reflect.TypeOf(arg).Name(), i)
|
// Check if the argument is a slice or array of any type
|
||||||
return q
|
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
|
return q
|
||||||
@ -138,6 +154,9 @@ type Rows struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (q *Query) ScanMulti() (*Rows, error) {
|
func (q *Query) ScanMulti() (*Rows, error) {
|
||||||
|
if q.err != nil {
|
||||||
|
return nil, q.err
|
||||||
|
}
|
||||||
return &Rows{
|
return &Rows{
|
||||||
query: q,
|
query: q,
|
||||||
}, nil
|
}, nil
|
||||||
|
@ -133,3 +133,29 @@ func TestTransactionRollback(t *testing.T) {
|
|||||||
db.Query("select value from mytable where key = 'foo'").MustScanSingle(&value)
|
db.Query("select value from mytable where key = 'foo'").MustScanSingle(&value)
|
||||||
require.Equal(t, "bar", value, "expected original value after rollback")
|
require.Equal(t, "bar", value, "expected original value after rollback")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestQueryWithInClause(t *testing.T) {
|
||||||
|
db := openTestDb(t)
|
||||||
|
// Insert additional test rows
|
||||||
|
db.Query("insert into mytable(key, value) values ('key1', 'value1')").MustExec()
|
||||||
|
db.Query("insert into mytable(key, value) values ('key2', 'value2')").MustExec()
|
||||||
|
|
||||||
|
// Execute query with IN clause
|
||||||
|
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()
|
||||||
|
|
||||||
|
// Check results
|
||||||
|
results := make(map[string]string)
|
||||||
|
for rows.MustNext() {
|
||||||
|
var key, value string
|
||||||
|
rows.MustScan(&key, &value)
|
||||||
|
results[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify we got exactly the expected results
|
||||||
|
require.Equal(t, 2, len(results), "expected 2 matching rows")
|
||||||
|
require.Equal(t, "bar", results["foo"], "unexpected value for key 'foo'")
|
||||||
|
require.Equal(t, "value2", results["key2"], "unexpected value for key 'key2'")
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user