Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

Named keys for go-sql-driver Scan()

When using the go-sql-driver it feels like I'm re-inventing the wheel a little bit. I'm used to languages where you have a data model (like a User class) which is strictly related to the database model. Then you can have additional models for what you want to expose on an API of course, but still, you have a model for the database object. In Go I'm not sure how to do this the best way. Below is a GetUserByEmail function.

func GetUserByEmail(email string) (*myapp.User, error) {
    smt, err := database.Prepare("SELECT * FROM users WHERE email = ?")

    if err != nil {
        return nil, err
    }

    rows, err := smt.Query(email)
    defer rows.Close()

    if err != nil {
        return nil, err
    }

    var users []*myapp.User

    for rows.Next() {
        var id string
        var confirmed bool
        var password string
        var email string
        var updated string
        var created string
        err = rows.Scan(&id, &confirmed, &password, &email, &updated, &created)

        if err != nil {
            return nil, err
        }

        user := myapp.User{id, confirmed, created, password, email, updated}
        users = append(users, &user)
    }

    if users == nil || len(users) == 0 {
        return nil, errors.New("User not found.")
    }

    if len(users) != 1 {
        return nil, errors.New("Nr of users fetched: " + string(len(users)) + ". Expected: 1.")
    }

    return users[0], nil
}

One problem here is that if I change the order of password and email in the database, my application will silently switch those and I will get no error. That's an awful and dangerous behavior. Can Scan() take keys somehow or can I do this in some other way? I could of course look at Columns() and take the index order from there as an int (although Go doesn't seem to have that function built in) and map it to my User variables, but do I really need to do all that for all my database methods? Is it abstracted somewhere or do I need to do that myself?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...