Skip to content

Feature request: Iterator support for handling large amount of dataΒ #981

Open
@vroad

Description

@vroad

Could we add a new finisher method that wraps *sql.Rows, so that we could iterate over rows without loading all of them at once?
I found that Query finisher returns *sql.Rows, but couldn't find its usage in the documentation.

Something like iterator can still be done with some boilerplate code, according to #294.

I'd love to be able to load columns without writing boiler code like this for all types. Hooks and eager loading of relationships are not handled with this code, which could be an issue depending on your use case.

rows, err := models.Tweets().Query(db)
columns, err := rows.Columns()
valueMapping, err := queries.BindMapping(tweetType, tweetMapping, columns)
tweetRows := &tweetRows{
	rows:         rows,
	valueMapping: valueMapping,
}
var tweetType = reflect.TypeOf(&models.Tweet{})
var tweetMapping = queries.MakeStructMapping(tweetType)

type TweetRows struct {
	rows         *sql.Rows
	valueMapping []uint64
}

func (r *TweetRows) Next() bool {
	return r.rows.Next()
}

func (r *TweetRows) Scan(tweet *models.Tweet) error {
	pointers := queries.PtrsFromMapping(reflect.Indirect(reflect.ValueOf(tweet)), r.valueMapping)
	return r.rows.Scan(pointers...)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions