Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iteration doesn't support parsing raw data into struct? #59

Closed
Mattieuga opened this issue Feb 4, 2014 · 4 comments
Closed

Iteration doesn't support parsing raw data into struct? #59

Mattieuga opened this issue Feb 4, 2014 · 4 comments

Comments

@Mattieuga
Copy link

I may be missing something, but the docs aren't really clear on this. They mention the Rows() function can be used to iterate over table but since this returns *sql.Row, I'm not sure how to scan this into the struct for this row. The great thing about gorm is that it handles parsing the sql data into a struct, but this seems to be missing for iteration.

The example shows this, which really isn't great given how easy all of the other functions are to use.

rows.Scan(&name, &age, &email)
@jinzhu
Copy link
Member

jinzhu commented Feb 18, 2014

Hi @Mattieuga

Yes, we don't have this function now. ;(

The Rows method is build for those users who don't like gorm's syntax, but only would like to use gorm as a query builder.

But if gorm will support this feature, do you have any suggestions for the API naming?

@Mattieuga
Copy link
Author

Something with a similar API to Rows but that takes care of scanning the data would be great. Something like this would match the style of the rest of Gorm pretty well:

user := User{}

iter, err := db.Where("role = ?", "admin").Or("role = ?", "super_admin").Iter()

for iter.Next(&user) {
  ...
}

Error handling can be tricky with the Close() that has to be deferred. I like the way labix handles this in the mgo package. You can keep the nice for loop and you basically do if err := iter.Close(); err != nil {...} after the for loop to check if an error happened during the iteration.

@colegleason
Copy link

@jinzhu I actually would like to see this support not because I dislike the syntax of Gorm but because I don't want to load every object into memory at once. If I have a table with a large number of rows I use the iterator to process them.

Iterating with the Rows is less that ideals since I can't Scan with a struct. Would it be possible to either add Iter support as @Mattieuga suggested or exposing the function that unmarshals Rows into Structs?

@jinzhu jinzhu added this to the v1.0 milestone Jan 10, 2016
jinzhu added a commit that referenced this issue Feb 14, 2016
@jinzhu
Copy link
Member

jinzhu commented Feb 14, 2016

Usage

rows, err := db.Model(&User{}).Where("name = ?", "jinzhu").Select("name, age, email").Rows() // (*sql.Rows, error)
defer rows.Close()

for rows.Next() {
  var user User
  db.ScanRows(rows, &user)
  // do something
}

@jinzhu jinzhu closed this as completed Feb 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants