2016-06-20 07:22:50 +02:00
|
|
|
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
|
|
|
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
2016-06-27 08:56:05 +02:00
|
|
|
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
|
|
|
|
{{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}}
|
|
|
|
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", "}}
|
2016-06-12 01:36:01 +02: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 11:25:09 +02:00
|
|
|
func (o *{{$tableNameSingular}}) Update(whitelist ... string) error {
|
|
|
|
return o.UpdateX(boil.GetDB(), whitelist...)
|
|
|
|
}
|
|
|
|
|
2016-06-20 07:57:46 +02:00
|
|
|
// UpdateX uses an executor to update the {{$tableNameSingular}}.
|
2016-04-17 11:25:09 +02:00
|
|
|
func (o *{{$tableNameSingular}}) UpdateX(exec boil.Executor, whitelist ... string) error {
|
2016-06-20 07:57:46 +02:00
|
|
|
return o.UpdateAtX(exec, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}}, whitelist...)
|
2016-05-06 11:31:51 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 07:57:46 +02:00
|
|
|
// UpdateAt updates the {{$tableNameSingular}} using the primary key to find the row to update.
|
2016-06-27 08:56:05 +02:00
|
|
|
func (o *{{$tableNameSingular}}) UpdateAt({{$pkArgs}}, whitelist ...string) error {
|
|
|
|
return o.UpdateAtX(boil.GetDB(), {{$pkNames | join ", "}}, whitelist...)
|
2016-05-06 11:31:51 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 07:57:46 +02:00
|
|
|
// UpdateAtX uses an executor to update the {{$tableNameSingular}} using the primary key to find the row to update.
|
2016-06-27 08:56:05 +02:00
|
|
|
func (o *{{$tableNameSingular}}) UpdateAtX(exec boil.Executor, {{$pkArgs}}, whitelist ...string) error {
|
2016-04-13 15:51:58 +02:00
|
|
|
if err := o.doBeforeUpdateHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(whitelist) == 0 {
|
2016-05-06 11:31:51 +02: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 11:01:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2016-06-02 23:07:51 +02:00
|
|
|
var query string
|
2016-05-05 11:01:24 +02:00
|
|
|
if len(whitelist) != 0 {
|
2016-06-20 07:57:46 +02: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 11:01:24 +02:00
|
|
|
} else {
|
2016-05-06 11:31:51 +02:00
|
|
|
return fmt.Errorf("{{.PkgName}}: unable to update {{.Table.Name}}, could not build a whitelist for row: %s", err)
|
2016-04-13 15:51:58 +02:00
|
|
|
}
|
|
|
|
|
2016-06-02 23:07:51 +02:00
|
|
|
if boil.DebugMode {
|
|
|
|
fmt.Fprintln(boil.DebugWriter, query)
|
|
|
|
}
|
|
|
|
|
2016-03-19 07:22:10 +01:00
|
|
|
if err != nil {
|
2016-04-04 12:28:58 +02:00
|
|
|
return fmt.Errorf("{{.PkgName}}: unable to update {{.Table.Name}} row: %s", err)
|
2016-03-19 07:22:10 +01:00
|
|
|
}
|
|
|
|
|
2016-04-13 15:51:58 +02:00
|
|
|
if err := o.doAfterUpdateHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-19 07:22:10 +01:00
|
|
|
return nil
|
|
|
|
}
|
2016-04-13 15:51:58 +02:00
|
|
|
|
2016-05-10 12:20:29 +02: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 15:51:58 +02:00
|
|
|
}
|