Created
June 3, 2018 06:27
-
-
Save hauxe/476266d32b4b76ae32578852605d07f5 to your computer and use it in GitHub Desktop.
crud create function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create creates from map | |
func (crud *CRUD) Create(data interface{}) error { | |
// build fields | |
result, err := crud.Config.DB.NamedExec(crud.Config.sqlCRUDCreate, data) | |
if err != nil { | |
return errors.Wrap(err, "error crud create") | |
} | |
// set primary key | |
rv := reflect.ValueOf(data) | |
rv = reflect.Indirect(rv) | |
pk := rv.Field(crud.Config.pk.index) | |
if pk.CanSet() && pk.Kind() == reflect.Int64 { | |
id, err := result.LastInsertId() | |
if err != nil { | |
return errors.Wrap(err, "error get last insert id at crud create") | |
} | |
pk.SetInt(id) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment