wtf it works

This commit is contained in:
Ben Visness 2022-04-16 21:45:39 -05:00
parent 2229ac85d5
commit 8ff5f02916
1 changed files with 11 additions and 7 deletions

View File

@ -414,7 +414,7 @@ func (it *Iterator[T]) Next() (*T, bool) {
if len(vals) != 1 {
panic(fmt.Errorf("tried to query a scalar value, but got %v values in the row", len(vals)))
}
result.Set(reflect.ValueOf(vals[0]))
setValueFromDB(result.Elem(), reflect.ValueOf(vals[0]))
return result.Interface().(*T), true
} else {
var currentField reflect.StructField
@ -463,12 +463,7 @@ func (it *Iterator[T]) Next() (*T, bool) {
}
currentValue = valReflected
switch field.Kind() {
case reflect.Int:
field.SetInt(valReflected.Int())
default:
field.Set(valReflected)
}
setValueFromDB(field, valReflected)
currentField = reflect.StructField{}
currentValue = reflect.Value{}
@ -478,6 +473,15 @@ func (it *Iterator[T]) Next() (*T, bool) {
}
}
func setValueFromDB(dest reflect.Value, value reflect.Value) {
switch dest.Kind() {
case reflect.Int:
dest.SetInt(value.Int())
default:
dest.Set(value)
}
}
func (it *Iterator[any]) Close() {
it.rows.Close()
select {