FAQ
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!

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jia Mi at Dec 17, 2014 at 1:50 am
    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.

    On Wed Dec 17 2014 at 2:09:01 AM Monosij Dutta-Roy wrote:


    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!

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Monosij Dutta-Roy at Dec 17, 2014 at 2:38 am
    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
    On Tuesday, December 16, 2014 8:50:10 PM UTC-5, Jia Mi wrote:

    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.


    On Wed Dec 17 2014 at 2:09:01 AM Monosij Dutta-Roy <monosij...@gmail.com
    <javascript:>> wrote:
    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!

    --
    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 <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 16, '14 at 6:08p
activeDec 17, '14 at 2:38a
posts3
users2
websitegolang.org

2 users in discussion

Monosij Dutta-Roy: 2 posts Jia Mi: 1 post

People

Translate

site design / logo © 2022 Grokbase