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-04 05:23:55 +02:00
|
|
|
// UpdateG a single {{$tableNameSingular}} record. See Update for
|
|
|
|
// whitelist behavior description.
|
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-08-04 05:23:55 +02:00
|
|
|
// Panics on error. See Update for whitelist behavior description.
|
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
|
|
|
// UpdateP uses an executor to update the {{$tableNameSingular}}, and panics on error.
|
2016-08-04 05:23:55 +02:00
|
|
|
// See Update for whitelist behavior description.
|
2016-08-01 07:10:10 +02:00
|
|
|
func (o *{{$tableNameSingular}}) UpdateP(exec boil.Executor, whitelist ... string) {
|
2016-08-04 16:20:17 +02:00
|
|
|
err := o.Update(exec, whitelist...)
|
2016-07-16 13:22:57 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 05:23:55 +02:00
|
|
|
// Update uses an executor to update the {{$tableNameSingular}}.
|
|
|
|
// Whitelist behavior: If a whitelist is provided, only the columns given are updated.
|
|
|
|
// No whitelist behavior: Without a whitelist, columns are inferred by the following rules:
|
2016-08-04 05:38:23 +02:00
|
|
|
// - All columns are inferred to start with
|
2016-08-04 05:23:55 +02:00
|
|
|
// - All primary keys are subtracted from this set
|
2016-08-14 08:38:41 +02:00
|
|
|
// Update does not automatically update the record in case of default values. Use .Reload()
|
|
|
|
// to refresh the records.
|
2016-08-04 05:23:55 +02:00
|
|
|
func (o *{{$tableNameSingular}}) Update(exec boil.Executor, 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{}
|
|
|
|
|
2016-08-14 06:21:26 +02:00
|
|
|
wl := strmangle.UpdateColumnSet({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns, whitelist)
|
2016-08-13 20:39:03 +02:00
|
|
|
if len(wl) == 0 {
|
|
|
|
return errors.New("{{.PkgName}}: unable to update {{.Table.Name}}, could not build whitelist")
|
|
|
|
}
|
2016-08-13 18:58:18 +02:00
|
|
|
|
2016-08-13 20:39:03 +02:00
|
|
|
query = fmt.Sprintf(`UPDATE {{.Table.Name}} SET %s WHERE %s`, strmangle.SetParamNames(wl), strmangle.WhereClause(len(wl)+1, {{$varNameSingular}}PrimaryKeyColumns))
|
|
|
|
values = boil.GetStructValues(o, wl...)
|
|
|
|
values = append(values, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})
|
2016-08-13 18:58:18 +02:00
|
|
|
|
2016-08-13 20:39:03 +02:00
|
|
|
if boil.DebugMode {
|
|
|
|
fmt.Fprintln(boil.DebugWriter, query)
|
|
|
|
fmt.Fprintln(boil.DebugWriter, values)
|
2016-04-13 15:51:58 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
result, err := exec.Exec(query, values...)
|
2016-03-19 07:22:10 +01:00
|
|
|
if err != nil {
|
2016-08-13 20:36:03 +02:00
|
|
|
return errors.Wrap(err, "{{.PkgName}}: unable to update {{.Table.Name}} row")
|
2016-03-19 07:22:10 +01:00
|
|
|
}
|
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
if r, err := result.RowsAffected(); err == nil && r != 1 {
|
|
|
|
return errors.Errorf("failed to update single row, updated %d rows", r)
|
|
|
|
}
|
|
|
|
|
2016-08-13 20:39:03 +02:00
|
|
|
return o.doAfterUpdateHooks()
|
2016-03-19 07:22:10 +01:00
|
|
|
}
|
2016-04-13 15:51:58 +02:00
|
|
|
|
2016-08-11 10:23:47 +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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAll updates all rows with the specified column values.
|
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 {
|
2016-08-13 20:36:03 +02:00
|
|
|
return errors.Wrap(err, "{{.PkgName}}: unable to update all for {{.Table.Name}}")
|
2016-05-10 12:20:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-04-13 15:51:58 +02:00
|
|
|
}
|
2016-07-15 12:14:47 +02:00
|
|
|
|
2016-08-11 10:23:47 +02:00
|
|
|
// UpdateAllG updates all rows with the specified column values.
|
|
|
|
func (o {{$tableNameSingular}}Slice) UpdateAllG(cols M) error {
|
|
|
|
return o.UpdateAll(boil.GetDB(), cols)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAllGP updates all rows with the specified column values, and panics on error.
|
|
|
|
func (o {{$tableNameSingular}}Slice) UpdateAllGP(cols M) {
|
|
|
|
if err := o.UpdateAll(boil.GetDB(), cols); err != nil {
|
2016-07-16 13:22:57 +02:00
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 10:23:47 +02:00
|
|
|
// UpdateAllP updates all rows with the specified column values, and panics on error.
|
|
|
|
func (o {{$tableNameSingular}}Slice) UpdateAllP(exec boil.Executor, cols M) {
|
|
|
|
if err := o.UpdateAll(exec, cols); err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAll updates all rows with the specified column values, using an executor.
|
|
|
|
func (o {{$tableNameSingular}}Slice) UpdateAll(exec boil.Executor, cols M) error {
|
2016-08-14 08:38:41 +02:00
|
|
|
ln := int64(len(o))
|
|
|
|
if ln == 0 {
|
|
|
|
return nil
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
if len(cols) == 0 {
|
|
|
|
return errors.New("{{.PkgName}}: update all requires at least one column argument")
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
colNames := make([]string, len(cols))
|
2016-08-14 08:38:41 +02:00
|
|
|
args := make([]interface{}, len(cols))
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
i := 0
|
2016-08-11 10:23:47 +02:00
|
|
|
for name, value := range cols {
|
2016-08-14 08:38:41 +02:00
|
|
|
colNames[i] = strmangle.IdentQuote(name)
|
|
|
|
args[i] = value
|
|
|
|
i++
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append all of the primary key values for each column
|
2016-08-11 14:26:49 +02:00
|
|
|
args = append(args, o.inPrimaryKeyArgs()...)
|
2016-08-11 10:23:47 +02:00
|
|
|
|
|
|
|
sql := fmt.Sprintf(
|
2016-08-11 14:26:49 +02:00
|
|
|
`UPDATE {{.Table.Name}} SET (%s) = (%s) WHERE (%s) IN (%s)`,
|
2016-08-11 10:23:47 +02:00
|
|
|
strings.Join(colNames, ", "),
|
2016-08-11 14:26:49 +02:00
|
|
|
strmangle.Placeholders(len(colNames), 1, 1),
|
|
|
|
strings.Join(strmangle.IdentQuoteSlice({{$varNameSingular}}PrimaryKeyColumns), ","),
|
|
|
|
strmangle.Placeholders(len(o) * len({{$varNameSingular}}PrimaryKeyColumns), len(colNames)+1, len({{$varNameSingular}}PrimaryKeyColumns)),
|
2016-08-11 10:23:47 +02:00
|
|
|
)
|
|
|
|
|
2016-08-13 18:58:18 +02:00
|
|
|
if boil.DebugMode {
|
|
|
|
fmt.Fprintln(boil.DebugWriter, sql)
|
|
|
|
fmt.Fprintln(boil.DebugWriter, args...)
|
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
result, err := exec.Exec(sql, args...)
|
2016-08-11 10:23:47 +02:00
|
|
|
if err != nil {
|
2016-08-13 20:36:03 +02:00
|
|
|
return errors.Wrap(err, "{{.PkgName}}: unable to update all in {{$varNameSingular}} slice")
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
if r, err := result.RowsAffected(); err == nil && r != ln {
|
|
|
|
return errors.Errorf("failed to update %d rows, only affected %d", ln, r)
|
|
|
|
}
|
|
|
|
|
2016-08-11 10:23:47 +02:00
|
|
|
return nil
|
|
|
|
}
|