gocraft / dbr

Additions to Go's database/sql for super fast performance and convenience.
MIT License
1.84k stars 209 forks source link

dbr.InsertInto struct fields auto mapping #16

Closed badoet closed 8 years ago

badoet commented 9 years ago

some db library have auto mapping of the struct field's db table name so something like

type A struct {
  Id int
  Name string
  LongField string
}

will simply map to Columns("id", "name", "long_field") so this way its much less verbose compared to manually adding the field name into the Columns() and changing the struct requires the function to be updated as well.

tyler-smith commented 9 years ago

Hey, sorry to not respond to this for so long.

This seems like it could be nice, but I'd like to think about it some more. We're also focused pretty hard right now on something things (like Postgres!), but I'll keep this open for now and try to solicit some feedback from the rest of the team.

(Also other community members' opinions here are welcome and appreciated)

badoet commented 9 years ago

no prob. i will come up with some suggestion soon. im currently rushing to complete the main features for my project once done, i will revisit this again

taylorchu commented 9 years ago

@badoet Will it be a problem if one just wants to update some but not all fields in a struct?

badoet commented 9 years ago

For insert, normally we will insert all field? unless some field you want to leave it to the db's default. perhaps we can add dbr: "ignore_insert" for fields struct we dont want to be automatically inserted at auto mapping.

iktakahiro commented 8 years ago

I agree with this enhancement idea.

type User struct {
   Name  string `db:"name"`
   Email string `db:"email"`
}

user := &User{"Michael", "michael@example.com"}

//  as they are
sess.InsertInto("project_report").Columns("name", "email").Record(user).Exec()

// idea
sess.InsertInto("project_report").Columns(user).Record(user).Exec()

// or
sess.InsertInto("project_report").Record(user).Exec()
jostyee commented 8 years ago
sess.InsertInto("project_report").Record(user).Exec()

looks good to me.

ghost commented 8 years ago

I use gocraft/dbr everyday! And I wrote some tool for creating table struct for quick changing. https://github.com/finalist736/dbrmodels It generates this file: /go/src/project/dbmodels/tutorial/model.go

package tutorial

import _ "github.com/gocraft/dbr"

var fieldsNames = []string{"Name", "Current", "Content"}

type Tutorial struct {
    Name    string `db:"Name"`
    Current bool   `db:"Current"`
    Content string `db:"Content"`
}

func New() *Tutorial {
    return new(Tutorial)
}

func NewSlice() []*Tutorial {
    return make([]*Tutorial, 0)
}

func FieldsNames() []string {
    return fieldsNames
}

func FieldsNamesWithOutID() []string {
    slice := make([]string, 0)
    for _, iterator := range fieldsNames {
        if iterator == "ID" {
            continue
        }
        slice = append(slice, iterator)
    }
    return slice
}

I can use this structure in insert query:

sess.InsertInto("project_report").Columns(toturial.FieldsNames()...).Record(user).Exec()

If table has ID field, I use:

sess.InsertInto("project_report").Columns(toturial.FieldsNamesWithOutID()...).Record(user).Exec()
tyler-smith commented 8 years ago

@finalist736 Glad you like the project, and thanks for the script! Looks really cool.

As for the main issue here, it seems that there is some decent support for the feature but I don't think any of us on the gocraft side will have time to dedicate to making it happen any time soon.

If somebody wanted to work on it and submit a Pull Request we'd gladly work with you though!

tyler-smith commented 8 years ago

I'm going to close this issue for now, but if anybody wants to put together a Pull Request for it I'll gladly test it and work with you on getting it merged in.

ghost commented 8 years ago

Thank you! I forked dbr and working on integrating dbrmodels into dbr repository. What you think about this name dbrmodels?