2022-07-11 13:00:36 +02:00
|
|
|
package advsql
|
|
|
|
|
|
|
|
import "context"
|
|
|
|
|
2022-07-12 17:56:06 +02:00
|
|
|
func QueryMany[T any](db *Database, query string, decoder func(v *T, decode ScanFunc) error) QueryManyFunc[T] {
|
2022-07-11 13:00:36 +02:00
|
|
|
ctxfunc := QueryManyContext(db, query, decoder)
|
|
|
|
return func(args ...interface{}) <-chan *T {
|
|
|
|
return ctxfunc(context.Background(), args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 17:56:06 +02:00
|
|
|
func QueryManyContext[T any](db *Database, query string, decoder func(v *T, decode ScanFunc) error) QueryManyContextFunc[T] {
|
2022-07-11 13:00:36 +02:00
|
|
|
s, err := db.db.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
db.closefuncs = append(db.closefuncs, s.Close)
|
|
|
|
|
|
|
|
return func(ctx context.Context, args ...interface{}) <-chan *T {
|
|
|
|
out := make(chan *T, 10)
|
|
|
|
|
|
|
|
rows, err := s.QueryContext(ctx, args...)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer rows.Close()
|
|
|
|
defer close(out)
|
|
|
|
for rows.Next() {
|
|
|
|
v := new(T)
|
|
|
|
if decoder(v, rows.Scan) == nil {
|
|
|
|
out <- v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
}
|