Trying to scan into a struct dynamically from SQL

4,513 views
Skip to first unread message

Monosij Dutta-Roy

unread,
Dec 16, 2014, 1:08:03 PM12/16/14
to golan...@googlegroups.com

Hello All -

A little new to go and reading from a SQL DBMS and trying to scan into a struct.

I am trying to keep it dynamic in sending a query and scanning into the required struct.

The structs I am using for the entities Member, in this case, I would like to use to read from other DBMS as well such as MongoDB thus do not want to be dependent on sql.NllString.

I understand I may have to use a different library such as sqlx by jmoiron - but wanted to ask first if there is a way to just use the sql package?

...
While I have gotten the following code fragment to work:

rows, err := db.Query("SELECT * FROM member")
PanicIf(err)
cols, err := rows.Columns()
PanicIf(err)
defer rows.Close()

vals := make([]interface{}, len(cols))
for i, _ := range cols {
vals[i] = new(sql.RawBytes)
}

I am trying to get the following part in reading into the struct Member to work:

members := []Member{}
for rows.Next() {
mem := Member{}

err := rows.Scan(vals...) // This works but I cannot get it into the Memeber struct as tried below.
// I want the following to work somehow?
// err := rows.Scan(&mem.ID, &mem.Name, &mem.Description&mem.Country&mem.Region)

PanicIf(err)
members = append(members, mem)

Would there be a way to use rows.Scan to scan directly into the Member struct.

...
Member struct is defined as:

type Member struct {
ID int
Name string
Description string
  Country string  // do not want to use sql.NullString as I do not want to make my memeber struct SQL DBMS dependent.
  Region string  // do not want to use sql.NullString as I do not want to make my memeber struct SQL DBMS dependent.
}

...
Thank you for your help!

Jia Mi

unread,
Dec 16, 2014, 8:50:10 PM12/16/14
to Monosij Dutta-Roy, golan...@googlegroups.com
Hi Monosij,

I'm working on a code generator for accessing RDBMS called ModelQ, although only mysql supported for now. You could check it out at https://github.com/mijia/modelq

The basic idea is after you scan the row into RawBytes you have to process/xform from the RayBytes into your own model object. Like the examples in modelq, scan rows into a User object:

func (o _UserObjs) toUser(columns []gmq.Column, rb []sql.RawBytes) User {
    obj := User{}
    if len(columns) == len(rb) {
        for i := range columns {
            switch columns[i].Name {
            case "id":
                obj.Id = gmq.AsInt64(rb[i])
            case "name":
                obj.Name = gmq.AsString(rb[i])
            case "password":
                obj.Password = gmq.AsString(rb[i])
            case "is_married":
                obj.IsMarried = gmq.AsInt(rb[i])
            case "age":
                obj.Age = gmq.AsInt(rb[i])
            case "create_time":
                obj.CreateTime = gmq.AsTime(rb[i])
            case "update_time":
                obj.UpdateTime = gmq.AsTime(rb[i])
            }
        }
    }
    return obj
}

I think the RawBytes is just the string bytes, so you need to parse it yourself.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Monosij Dutta-Roy

unread,
Dec 16, 2014, 9:38:27 PM12/16/14
to golan...@googlegroups.com, monosij...@gmail.com
Hi Jia -

Thanks for your reply. This looks great.

I will take a close look at what you are doing. I would like to use aspects of this library.

I got a chance to do what I needed to do for now using sqlx by jMoiron.

I will get back to you in a day or so and if I can contrbute perhaps I can help as well.

Thank you.

Mono

georgy...@gmail.com

unread,
Jul 30, 2020, 12:07:02 PM7/30/20
to golang-nuts
Hi. I created a library that does exactly that and it's much simpler to use compared to sqlx: https://github.com/georgysavva/scany
Reply all
Reply to author
Forward
0 new messages