2016-06-20 07:22:50 +02:00
|
|
|
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
|
|
|
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
2016-09-24 09:52:18 +02:00
|
|
|
{{- $schemaTable := .Table.Name | .SchemaTable}}
|
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 {
|
2016-09-14 10:08:30 +02:00
|
|
|
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) {
|
2016-09-14 10:08:30 +02:00
|
|
|
if err := o.Update(boil.GetDB(), whitelist...); err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
panic(errors.Err(err))
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-07-16 13:22:57 +02:00
|
|
|
}
|
|
|
|
|
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-09-14 10:08:30 +02:00
|
|
|
err := o.Update(exec, whitelist...)
|
|
|
|
if err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
panic(errors.Err(err))
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-07-16 13:22:57 +02:00
|
|
|
}
|
|
|
|
|
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-09-14 10:08:30 +02:00
|
|
|
{{- template "timestamp_update_helper" . -}}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
{{if not .NoHooks -}}
|
|
|
|
if err = o.doBeforeUpdateHooks(exec); err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Err(err)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
{{end -}}
|
|
|
|
|
|
|
|
key := makeCacheKey(whitelist, nil)
|
|
|
|
{{$varNameSingular}}UpdateCacheMut.RLock()
|
|
|
|
cache, cached := {{$varNameSingular}}UpdateCache[key]
|
|
|
|
{{$varNameSingular}}UpdateCacheMut.RUnlock()
|
|
|
|
|
|
|
|
if !cached {
|
2017-03-16 08:54:55 +01:00
|
|
|
wl := strmangle.UpdateColumnSet(
|
|
|
|
{{$varNameSingular}}Columns,
|
|
|
|
{{$varNameSingular}}PrimaryKeyColumns,
|
|
|
|
whitelist,
|
|
|
|
)
|
2017-03-19 10:44:15 +01:00
|
|
|
{{if eq .DriverName "mssql"}}
|
|
|
|
wl = strmangle.SetComplement(wl, {{$varNameSingular}}ColumnsWithAuto)
|
|
|
|
{{end}}
|
2017-02-25 07:52:09 +01:00
|
|
|
if len(whitelist) == 0 {
|
2018-04-16 18:41:55 +02:00
|
|
|
wl = strmangle.SetComplement(wl, []string{"created_at","updated_at"})
|
2017-02-25 07:52:09 +01:00
|
|
|
}
|
2016-09-20 10:05:33 +02:00
|
|
|
if len(wl) == 0 {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Err("{{.PkgName}}: unable to update {{.Table.Name}}, could not build whitelist")
|
2016-09-20 10:05:33 +02:00
|
|
|
}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
cache.query = fmt.Sprintf("UPDATE {{$schemaTable}} SET %s WHERE %s",
|
|
|
|
strmangle.SetParamNames("{{.LQ}}", "{{.RQ}}", {{if .Dialect.IndexPlaceholders}}1{{else}}0{{end}}, wl),
|
|
|
|
strmangle.WhereClause("{{.LQ}}", "{{.RQ}}", {{if .Dialect.IndexPlaceholders}}len(wl)+1{{else}}0{{end}}, {{$varNameSingular}}PrimaryKeyColumns),
|
|
|
|
)
|
2016-09-15 05:59:55 +02:00
|
|
|
cache.valueMapping, err = queries.BindMapping({{$varNameSingular}}Type, {{$varNameSingular}}Mapping, append(wl, {{$varNameSingular}}PrimaryKeyColumns...))
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Err(err)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 05:59:55 +02:00
|
|
|
values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping)
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
if boil.DebugMode {
|
2017-09-02 17:27:27 +02:00
|
|
|
fmt.Fprintln(boil.DebugWriter, cache.query)
|
|
|
|
fmt.Fprintln(boil.DebugWriter, values)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 05:20:00 +02:00
|
|
|
_, err = exec.Exec(cache.query, values...)
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Prefix("{{.PkgName}}: unable to update {{.Table.Name}} row", err)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !cached {
|
|
|
|
{{$varNameSingular}}UpdateCacheMut.Lock()
|
|
|
|
{{$varNameSingular}}UpdateCache[key] = cache
|
|
|
|
{{$varNameSingular}}UpdateCacheMut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
{{if not .NoHooks -}}
|
|
|
|
return o.doAfterUpdateHooks(exec)
|
|
|
|
{{- else -}}
|
|
|
|
return nil
|
|
|
|
{{- end}}
|
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.
|
2017-05-09 21:30:04 +02:00
|
|
|
func (q {{$tableNameSingular}}Query) UpdateAllP(cols M) {
|
2016-09-14 10:08:30 +02:00
|
|
|
if err := q.UpdateAll(cols); err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
panic(errors.Err(err))
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAll updates all rows with the specified column values.
|
2017-05-09 21:30:04 +02:00
|
|
|
func (q {{$tableNameSingular}}Query) UpdateAll(cols M) error {
|
2016-09-15 05:59:55 +02:00
|
|
|
queries.SetUpdate(q.Query, cols)
|
2016-05-10 12:20:29 +02:00
|
|
|
|
2016-09-14 18:14:30 +02:00
|
|
|
_, err := q.Query.Exec()
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Prefix("{{.PkgName}}: unable to update all for {{.Table.Name}}", err)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-05-10 12:20:29 +02:00
|
|
|
|
2016-09-14 10:08:30 +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 {
|
2016-09-14 10:08:30 +02:00
|
|
|
return o.UpdateAll(boil.GetDB(), cols)
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAllGP updates all rows with the specified column values, and panics on error.
|
|
|
|
func (o {{$tableNameSingular}}Slice) UpdateAllGP(cols M) {
|
2016-09-14 10:08:30 +02:00
|
|
|
if err := o.UpdateAll(boil.GetDB(), cols); err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
panic(errors.Err(err))
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-07-16 13:22:57 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-09-14 10:08:30 +02:00
|
|
|
if err := o.UpdateAll(exec, cols); err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
panic(errors.Err(err))
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAll updates all rows with the specified column values, using an executor.
|
|
|
|
func (o {{$tableNameSingular}}Slice) UpdateAll(exec boil.Executor, cols M) error {
|
2016-09-14 10:08:30 +02:00
|
|
|
ln := int64(len(o))
|
|
|
|
if ln == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cols) == 0 {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Err("{{.PkgName}}: update all requires at least one column argument")
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
colNames := make([]string, len(cols))
|
|
|
|
args := make([]interface{}, len(cols))
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for name, value := range cols {
|
|
|
|
colNames[i] = name
|
|
|
|
args[i] = value
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append all of the primary key values for each column
|
2016-09-16 08:44:44 +02:00
|
|
|
for _, obj := range o {
|
|
|
|
pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), {{$varNameSingular}}PrimaryKeyMapping)
|
|
|
|
args = append(args, pkeyArgs...)
|
|
|
|
}
|
2017-04-01 20:00:13 +02:00
|
|
|
|
2017-03-15 17:35:44 +01:00
|
|
|
sql := fmt.Sprintf("UPDATE {{$schemaTable}} SET %s WHERE %s",
|
|
|
|
strmangle.SetParamNames("{{.LQ}}", "{{.RQ}}", {{if .Dialect.IndexPlaceholders}}1{{else}}0{{end}}, colNames),
|
2017-04-01 19:30:40 +02:00
|
|
|
strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), {{if .Dialect.IndexPlaceholders}}len(colNames)+1{{else}}0{{end}}, {{$varNameSingular}}PrimaryKeyColumns, len(o)))
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
if boil.DebugMode {
|
2017-09-02 17:27:27 +02:00
|
|
|
fmt.Fprintln(boil.DebugWriter, sql)
|
|
|
|
fmt.Fprintln(boil.DebugWriter, args...)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 05:20:00 +02:00
|
|
|
_, err := exec.Exec(sql, args...)
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Prefix("{{.PkgName}}: unable to update all in {{$varNameSingular}} slice", err)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|