Compare commits

...

2 Commits

Author SHA1 Message Date
e9b12dc3f6 Add support for int64 scans (#2)
All checks were successful
Build / build (push) Successful in 4m18s
Reviewed-on: #2
2025-05-14 15:21:32 +02:00
7daf1915a5 Fix yet another pointer
All checks were successful
Build / build (push) Successful in 41s
2025-05-09 10:55:16 +02:00
2 changed files with 35 additions and 0 deletions

View File

@ -50,6 +50,10 @@ func (q *Query) bindInto(into *int, args ...any) *Query {
}
v := reflect.ValueOf(arg)
if v.Kind() == reflect.Ptr {
if v.IsNil() {
q.stmt.BindNull(*into)
continue
}
arg = v.Elem().Interface()
}
if asString, ok := arg.(string); ok {
@ -213,6 +217,8 @@ func (r *Rows) scanArgument(i int, arg any) error {
*asString = r.query.stmt.ColumnText(i)
} else if asInt, ok := arg.(*int); ok {
*asInt = r.query.stmt.ColumnInt(i)
} else if asInt, ok := arg.(*int64); ok {
*asInt = r.query.stmt.ColumnInt64(i)
} else if asBool, ok := arg.(*bool); ok {
*asBool = r.query.stmt.ColumnBool(i)
} else if reflect.TypeOf(arg).Kind() == reflect.Ptr && reflect.TypeOf(arg).Elem().Kind() == reflect.Ptr {

View File

@ -122,6 +122,26 @@ func TestUpdateQueryWithPointerValue(t *testing.T) {
require.Equal(t, "ipsum", value)
}
func TestUpdateQueryWithSetPointerValue(t *testing.T) {
type S struct {
value *string
}
db := openTestDb(t)
func() {
tx := db.MustBegin()
defer tx.MustRollback()
tx.Query("insert into mytable(key, value) values ('lorem', 'bar')").MustExec()
s := S{nil}
key := "lorem"
tx.Query("update mytable set value = ? where key = ?").Bind(s.value, key).MustExec()
tx.MustCommit()
}()
var value *string
db.Query("select value from mytable where key = 'lorem'").MustScanSingle(&value)
require.Equal(t, (*string)(nil), value)
}
func TestUpdateQueryWithNullValue(t *testing.T) {
db := openTestDb(t)
func() {
@ -147,6 +167,15 @@ func TestQueryWithPointerStringArguments(t *testing.T) {
require.Equal(t, "bar", *result)
}
func TestQueryWithInt64Scan(t *testing.T) {
db := openTestDb(t)
var result int64
err := db.Query("select 2").ScanSingle(&result)
require.NoError(t, err)
require.NotNil(t, result)
require.Equal(t, int64(2), result)
}
func TestQueryWithPointerStringArgumentsCanSetToNull(t *testing.T) {
db := openTestDb(t)
db.Query("update mytable set value=null where key = 'foo'").MustExec()