Compare commits

..

1 Commits

Author SHA1 Message Date
278d7ed497 Add support for pointer arguments
All checks were successful
Build / build (push) Successful in 1m13s
2025-05-09 10:18:08 +02:00
2 changed files with 13 additions and 8 deletions

View File

@ -44,13 +44,13 @@ func (q *Query) bindInto(into *int, args ...any) *Query {
} }
for i, arg := range args { for i, arg := range args {
*into++ *into++
v := reflect.ValueOf(arg) if arg == nil {
if v.Kind() == reflect.Ptr {
if v.IsNil() {
q.stmt.BindNull(*into) q.stmt.BindNull(*into)
continue continue
} }
arg = v.Elem().Interface() // Dereference the pointer v := reflect.ValueOf(arg)
if v.Kind() == reflect.Ptr {
arg = v.Elem().Interface()
} }
if asString, ok := arg.(string); ok { if asString, ok := arg.(string); ok {
q.stmt.BindText(*into, asString) q.stmt.BindText(*into, asString)

View File

@ -94,9 +94,14 @@ func TestUpdateQuery(t *testing.T) {
} }
func TestUpdateQueryWithWrongArguments(t *testing.T) { func TestUpdateQueryWithWrongArguments(t *testing.T) {
type S struct {
Field string
}
db := openTestDb(t) db := openTestDb(t)
value := "ipsum" abc := S{
err := db.Query("insert into mytable(key, value) values ('lorem', ?)").Bind(&value).Exec() Field: "ipsum",
}
err := db.Query("insert into mytable(key, value) values ('lorem', ?)").Bind(abc).Exec()
require.Error(t, err) require.Error(t, err)
} }
@ -128,7 +133,7 @@ func TestUpdateQueryWithNullValue(t *testing.T) {
tx.MustCommit() tx.MustCommit()
}() }()
var value string var value *string
db.Query("select value from mytable where key = 'lorem'").MustScanSingle(&value) db.Query("select value from mytable where key = 'lorem'").MustScanSingle(&value)
require.Nil(t, value) require.Nil(t, value)
} }