Open
Description
Hi, when a query return a myriad of objects, the slice based solution is not very efficient, it would be great if it was possible to have a way to return some kind of iterator on the rows, something like sql.Rows but type safe.
The first solution that comes to mind is to add a new type of function in the sql queries builder.
Instead of writing -- name: Name :many
we would write -- name: Name :rows
or another relevant keyword
and for the generated code, it would look something like this
CREATE TABLE pilots (
id integer NOT NULL,
name text NOT NULL
);
ALTER TABLE pilots ADD CONSTRAINT pilot_pkey PRIMARY KEY (id);
-- name: ListPilots :rows
SELECT * FROM pilots;
type Pilot struct {
ID int32
Name string
}
type PilotRows struct {
rows *sql.Rows
}
func (p *PilotRows) Close() error {
return p.rows.Close()
}
func (p *PilotRows) Next() bool {
return p.rows.Next()
}
func (p *PilotRows) Scan(dest *Pilot) error {
return p.rows.Scan(dest.ID, dest.Name)
}
func (p *PilotRows) Err() error {
return p.rows.Err()
}
func (q *Queries) ListPilots(ctx context.Context) (*PilotRows, error) {
rows, err := q.query(ctx, q.listPilotsStmtn, listPilots)
if err != nil {
return nil, err
}
return &PilotRows{rows: rows}, nil
}