2016-03-23 07:18:41 +01:00
|
|
|
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
|
2016-03-19 07:22:10 +01:00
|
|
|
// {{$tableNameSingular}}Update updates a single record.
|
|
|
|
func {{$tableNameSingular}}Update(db boil.DB, id int, columns map[string]interface{}) error {
|
|
|
|
if id == 0 {
|
2016-03-23 07:18:41 +01:00
|
|
|
return errors.New("{{.PkgName}}: no id provided for {{.Table.Name}} update")
|
2016-03-19 07:22:10 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:18:41 +01:00
|
|
|
query := fmt.Sprintf(`UPDATE {{.Table.Name}} SET %s WHERE id=$%d`, boil.Update(columns), len(columns))
|
2016-03-19 07:22:10 +01:00
|
|
|
|
2016-03-23 04:03:35 +01:00
|
|
|
_, err := db.Exec(query, id, boil.WhereParams(columns))
|
2016-03-19 07:22:10 +01:00
|
|
|
if err != nil {
|
2016-03-23 07:18:41 +01:00
|
|
|
return fmt.Errorf("{{.PkgName}}: unable to update row with ID %d in {{.Table.Name}}: %s", id, err)
|
2016-03-19 07:22:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-23 07:18:41 +01:00
|
|
|
{{if hasPrimaryKey .Table.Columns -}}
|
2016-03-19 07:22:10 +01:00
|
|
|
// 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.
|
2016-03-23 04:03:35 +01:00
|
|
|
func (o *{{$tableNameSingular}}) Update(db boil.DB) error {
|
2016-03-23 07:18:41 +01:00
|
|
|
{{- $pkeyName := getPrimaryKey .Table.Columns -}}
|
|
|
|
_, err := db.Exec("UPDATE {{.Table.Name}} SET {{updateParamNames .Table.Columns}} WHERE {{$pkeyName}}=${{len .Table.Columns}}", {{updateParamVariables "o." .Table.Columns}}, o.{{titleCase $pkeyName}})
|
2016-03-19 07:22:10 +01:00
|
|
|
if err != nil {
|
2016-03-23 07:18:41 +01:00
|
|
|
return fmt.Errorf("{{.PkgName}}: unable to update {{.Table.Name}} row using primary key {{$pkeyName}}: %s", err)
|
2016-03-19 07:22:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
{{- end}}
|