2016-06-19 22:22:50 -07:00
|
|
|
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
|
|
|
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
2016-06-11 16:36:01 -07:00
|
|
|
// Update a single {{$tableNameSingular}} record. It takes a whitelist of
|
|
|
|
// column_name's that should be updated. The primary key will be used to find
|
|
|
|
// the record to update.
|
|
|
|
// WARNING: Update does NOT ignore nil members - only the whitelist can be used
|
|
|
|
// to control the set of columns that will be saved.
|
2016-04-17 19:25:09 +10:00
|
|
|
func (o *{{$tableNameSingular}}) Update(whitelist ... string) error {
|
|
|
|
return o.UpdateX(boil.GetDB(), whitelist...)
|
|
|
|
}
|
|
|
|
|
2016-06-19 22:57:46 -07:00
|
|
|
// UpdateX uses an executor to update the {{$tableNameSingular}}.
|
2016-04-17 19:25:09 +10:00
|
|
|
func (o *{{$tableNameSingular}}) UpdateX(exec boil.Executor, whitelist ... string) error {
|
2016-06-19 22:57:46 -07:00
|
|
|
return o.UpdateAtX(exec, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}}, whitelist...)
|
2016-05-06 19:31:51 +10:00
|
|
|
}
|
|
|
|
|
2016-06-19 22:57:46 -07:00
|
|
|
// UpdateAt updates the {{$tableNameSingular}} using the primary key to find the row to update.
|
2016-06-22 23:48:49 -07:00
|
|
|
func (o *{{$tableNameSingular}}) UpdateAt({{sqlColDefinitions .Table.Columns .Table.PKey.Columns | sqlColDefStrings | join " "}}, whitelist ...string) error {
|
2016-06-19 22:22:50 -07:00
|
|
|
return o.UpdateAtX(boil.GetDB(), {{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | join ", "}}, whitelist...)
|
2016-05-06 19:31:51 +10:00
|
|
|
}
|
|
|
|
|
2016-06-19 22:57:46 -07:00
|
|
|
// UpdateAtX uses an executor to update the {{$tableNameSingular}} using the primary key to find the row to update.
|
2016-06-22 23:48:49 -07:00
|
|
|
func (o *{{$tableNameSingular}}) UpdateAtX(exec boil.Executor, {{sqlColDefinitions .Table.Columns .Table.PKey.Columns | sqlColDefStrings | join " "}}, whitelist ...string) error {
|
2016-04-13 23:51:58 +10:00
|
|
|
if err := o.doBeforeUpdateHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(whitelist) == 0 {
|
2016-05-06 19:31:51 +10:00
|
|
|
cols := {{$varNameSingular}}ColumnsWithoutDefault
|
|
|
|
cols = append(boil.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o), cols...)
|
|
|
|
// Subtract primary keys and autoincrement columns
|
|
|
|
cols = boil.SetComplement(cols, {{$varNameSingular}}PrimaryKeyColumns)
|
|
|
|
cols = boil.SetComplement(cols, {{$varNameSingular}}AutoIncrementColumns)
|
|
|
|
|
|
|
|
whitelist = make([]string, len(cols))
|
|
|
|
copy(whitelist, cols)
|
2016-05-05 19:01:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2016-06-03 07:07:51 +10:00
|
|
|
var query string
|
2016-05-05 19:01:24 +10:00
|
|
|
if len(whitelist) != 0 {
|
2016-06-19 22:57:46 -07:00
|
|
|
query = fmt.Sprintf(`UPDATE {{.Table.Name}} SET %s WHERE %s`, boil.SetParamNames(whitelist), boil.WherePrimaryKey(len(whitelist)+1, {{.Table.PKey.Columns | stringMap .StringFuncs.quoteWrap | join ", "}}))
|
|
|
|
_, err = exec.Exec(query, boil.GetStructValues(o, whitelist...), {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})
|
2016-05-05 19:01:24 +10:00
|
|
|
} else {
|
2016-05-06 19:31:51 +10:00
|
|
|
return fmt.Errorf("{{.PkgName}}: unable to update {{.Table.Name}}, could not build a whitelist for row: %s", err)
|
2016-04-13 23:51:58 +10:00
|
|
|
}
|
|
|
|
|
2016-06-03 07:07:51 +10:00
|
|
|
if boil.DebugMode {
|
|
|
|
fmt.Fprintln(boil.DebugWriter, query)
|
|
|
|
}
|
|
|
|
|
2016-03-19 16:22:10 +10:00
|
|
|
if err != nil {
|
2016-04-04 20:28:58 +10:00
|
|
|
return fmt.Errorf("{{.PkgName}}: unable to update {{.Table.Name}} row: %s", err)
|
2016-03-19 16:22:10 +10:00
|
|
|
}
|
|
|
|
|
2016-04-13 23:51:58 +10:00
|
|
|
if err := o.doAfterUpdateHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-19 16:22:10 +10:00
|
|
|
return nil
|
|
|
|
}
|
2016-04-13 23:51:58 +10:00
|
|
|
|
2016-05-10 20:20:29 +10:00
|
|
|
func (q {{$varNameSingular}}Query) UpdateAll(cols M) error {
|
|
|
|
boil.SetUpdate(q.Query, cols)
|
|
|
|
|
|
|
|
_, err := boil.ExecQuery(q.Query)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("{{.PkgName}}: unable to update all for {{.Table.Name}}: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-04-13 23:51:58 +10:00
|
|
|
}
|