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-08-01 07:10:10 +02:00
|
|
|
// UpdateG a single {{$tableNameSingular}} record.
|
|
|
|
// UpdateG takes a whitelist of column names that should be updated.
|
2016-07-15 12:14:47 +02:00
|
|
|
// The primary key will be used to find the record to update.
|
2016-08-01 07:10:10 +02:00
|
|
|
func (o *{{$tableNameSingular}}) UpdateG(whitelist ...string) error {
|
|
|
|
return o.Update(boil.GetDB(), whitelist...)
|
2016-04-17 11:25:09 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 07:10:10 +02:00
|
|
|
// UpdateGP a single {{$tableNameSingular}} record.
|
|
|
|
// UpdateGP takes a whitelist of column names that should be updated.
|
2016-07-16 13:22:57 +02:00
|
|
|
// The primary key will be used to find the record to update.
|
|
|
|
// Panics on error.
|
2016-08-01 07:10:10 +02:00
|
|
|
func (o *{{$tableNameSingular}}) UpdateGP(whitelist ...string) {
|
|
|
|
if err := o.Update(boil.GetDB(), whitelist...); err != nil {
|
2016-07-16 13:22:57 +02:00
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 07:10:10 +02:00
|
|
|
// Update uses an executor to update the {{$tableNameSingular}}.
|
|
|
|
func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string) error {
|
|
|
|
return o.UpdateAt(exec, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}}, whitelist...)
|
2016-05-06 11:31:51 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 07:10:10 +02:00
|
|
|
// UpdateP uses an executor to update the {{$tableNameSingular}}, and panics on error.
|
|
|
|
func (o *{{$tableNameSingular}}) UpdateP(exec boil.Executor, whitelist ... string) {
|
|
|
|
err := o.UpdateAt(exec, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}}, whitelist...)
|
2016-07-16 13:22:57 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 07:10:10 +02:00
|
|
|
// UpdateAtG updates the {{$tableNameSingular}} using the primary key to find the row to update.
|
|
|
|
func (o *{{$tableNameSingular}}) UpdateAtG({{$pkArgs}}, whitelist ...string) error {
|
|
|
|
return o.UpdateAt(boil.GetDB(), {{$pkNames | join ", "}}, whitelist...)
|
2016-05-06 11:31:51 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 07:10:10 +02:00
|
|
|
// UpdateAtGP updates the {{$tableNameSingular}} using the primary key to find the row to update. Panics on error.
|
|
|
|
func (o *{{$tableNameSingular}}) UpdateAtGP({{$pkArgs}}, whitelist ...string) {
|
|
|
|
if err := o.UpdateAt(boil.GetDB(), {{$pkNames | join ", "}}, whitelist...); err != nil {
|
2016-07-16 13:22:57 +02:00
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 07:10:10 +02:00
|
|
|
// UpdateAt uses an executor to update the {{$tableNameSingular}} using the primary key to find the row to update.
|
|
|
|
func (o *{{$tableNameSingular}}) UpdateAt(exec boil.Executor, {{$pkArgs}}, whitelist ...string) error {
|
2016-04-13 15:51:58 +02:00
|
|
|
if err := o.doBeforeUpdateHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-05 11:01:24 +02:00
|
|
|
var err error
|
2016-06-02 23:07:51 +02:00
|
|
|
var query string
|
2016-07-15 12:14:47 +02:00
|
|
|
var values []interface{}
|
|
|
|
|
|
|
|
wl := o.generateUpdateColumns(whitelist...)
|
|
|
|
|
|
|
|
if len(wl) != 0 {
|
|
|
|
query = fmt.Sprintf(`UPDATE {{.Table.Name}} SET %s WHERE %s`, boil.SetParamNames(wl), boil.WherePrimaryKey(len(wl)+1, {{.Table.PKey.Columns | stringMap .StringFuncs.quoteWrap | join ", "}}))
|
|
|
|
values = boil.GetStructValues(o, wl...)
|
|
|
|
values = append(values, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})
|
|
|
|
_, err = exec.Exec(query, values...)
|
2016-05-05 11:01:24 +02:00
|
|
|
} else {
|
2016-07-15 12:14:47 +02:00
|
|
|
return fmt.Errorf("{{.PkgName}}: unable to update {{.Table.Name}}, could not build whitelist")
|
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-07-15 12:14:47 +02:00
|
|
|
fmt.Fprintln(boil.DebugWriter, values)
|
2016-06-02 23:07:51 +02:00
|
|
|
}
|
|
|
|
|
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-08-01 07:10:10 +02:00
|
|
|
// UpdateAtP uses an executor to update the {{$tableNameSingular}} using the primary key to find the row to update.
|
2016-07-16 13:22:57 +02:00
|
|
|
// Panics on error.
|
2016-08-01 07:10:10 +02:00
|
|
|
func (o *{{$tableNameSingular}}) UpdateAtP(exec boil.Executor, {{$pkArgs}}, whitelist ...string) {
|
|
|
|
if err := o.UpdateAt(exec, {{$pkNames | join ", "}}, whitelist...); err != nil {
|
2016-07-16 13:22:57 +02:00
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAll updates all rows with matching column names.
|
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
|
|
|
}
|
2016-07-15 12:14:47 +02:00
|
|
|
|
2016-07-16 13:22:57 +02:00
|
|
|
// UpdateAllP updates all rows with matching column names, and panics on error.
|
|
|
|
func (q {{$varNameSingular}}Query) UpdateAllP(cols M) {
|
|
|
|
if err := q.UpdateAll(cols); err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 12:14:47 +02:00
|
|
|
// generateUpdateColumns generates the whitelist columns for an update statement
|
|
|
|
func (o *{{$tableNameSingular}}) generateUpdateColumns(whitelist ...string) []string {
|
|
|
|
if len(whitelist) != 0 {
|
|
|
|
return whitelist
|
|
|
|
}
|
|
|
|
|
|
|
|
var wl []string
|
|
|
|
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)
|
|
|
|
|
|
|
|
wl = make([]string, len(cols))
|
|
|
|
copy(wl, cols)
|
|
|
|
|
|
|
|
return wl
|
|
|
|
}
|