sqlboiler/cmds/templates/update.tpl

33 lines
1.3 KiB
Smarty
Raw Normal View History

2016-03-19 07:22:10 +01:00
{{- $tableNameSingular := titleCaseSingular .Table -}}
// {{$tableNameSingular}}Update updates a single record.
func {{$tableNameSingular}}Update(db boil.DB, id int, columns map[string]interface{}) error {
if id == 0 {
return errors.New("{{.PkgName}}: no id provided for {{.Table}} update")
2016-03-19 07:22:10 +01:00
}
query := fmt.Sprintf(`UPDATE {{.Table}} SET %s WHERE id=$%d`, boil.Update(columns), len(columns))
_, err := db.Exec(query, id, boil.WhereParams(columns))
2016-03-19 07:22:10 +01:00
if err != nil {
return fmt.Errorf("{{.PkgName}}: unable to update row with ID %d in {{.Table}}: %s", id, err)
2016-03-19 07:22:10 +01:00
}
return nil
}
{{if hasPrimaryKey .Columns -}}
// Update updates a single {{$tableNameSingular}} record.
// Update will match against the primary key column to find the record to update.
// WARNING: This Update method will NOT ignore nil members.
// If you pass in nil members, those columnns will be set to null.
func (o *{{$tableNameSingular}}) Update(db boil.DB) error {
2016-03-19 07:22:10 +01:00
{{- $pkeyName := getPrimaryKey .Columns -}}
_, err := db.Exec("UPDATE {{.Table}} SET {{updateParamNames .Columns}} WHERE {{$pkeyName}}=${{len .Columns}}", {{updateParamVariables "o." .Columns}}, o.{{titleCase $pkeyName}})
2016-03-19 07:22:10 +01:00
if err != nil {
return fmt.Errorf("{{.PkgName}}: unable to update {{.Table}} row using primary key {{$pkeyName}}: %s", err)
2016-03-19 07:22:10 +01:00
}
return nil
}
{{- end}}