Closed edewindt closed 10 months ago
The same issue here but it should be fine as long as you use module jackc/pgx version 4 instead of version 5
func QueryAndScan(ctx context.Context, query string, dest interface{}, args ...interface{}) error {
rows, err := db.Query(ctx, query, args...)
if err != nil {
return err
}
defer rows.Close()
sliceVal := reflect.ValueOf(dest)
if sliceVal.Kind() != reflect.Ptr || sliceVal.Elem().Kind() != reflect.Slice {
return errors.New("dest must be a pointer to a slice")
}
sliceElemType := sliceVal.Elem().Type().Elem()
isPointer := sliceElemType.Kind() == reflect.Ptr
if isPointer {
sliceElemType = sliceElemType.Elem() // Get the type that the pointer points to
}
if sliceElemType.Kind() != reflect.Struct {
return errors.New("slice elements must be structs or pointers to structs")
}
for rows.Next() {
var elem reflect.Value
if isPointer {
elem = reflect.New(sliceElemType)
} else {
elem = reflect.New(sliceElemType).Elem()
}
fields := make([]interface{}, elem.Elem().NumField())
for i := range fields {
fields[i] = elem.Elem().Field(i).Addr().Interface()
}
err := rows.Scan(fields...)
if err != nil {
log.Println("Error scanning row:", err)
continue
}
if isPointer {
sliceVal.Elem().Set(reflect.Append(sliceVal.Elem(), elem))
} else {
sliceVal.Elem().Set(reflect.Append(sliceVal.Elem(), elem.Elem()))
}
}
if err := rows.Err(); err != nil {
return err
}
return nil
}
I've been using this as a replacement until it gets patched
Also assume that db is a *pgxpool.Pool
type
@edewindt @Xamss to use "github.com/jackc/pgx/v5" you need to use "scany/v2".
This is also true:
The same issue here but it should be fine as long as you use module jackc/pgx version 4 instead of version 5
Feel free to reopen if it doesn't solve your problem
db is erroring with:cannot use db (variable of type pgxpool.Pool) as pgxscan.Querier value in argument to pgxscan.Select: pgxpool.Pool does not implement pgxscan.Querier (wrong type for method Query)