sqlboiler/templates/13_update.tpl

183 lines
6.3 KiB
Smarty
Raw Normal View History

{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
{{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}}
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", " -}}
{{- $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 {
return o.Update(boil.GetDB(), whitelist...)
}
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 {
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) {
err := o.Update(exec, whitelist...)
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
// 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 {
{{- template "timestamp_update_helper" . -}}
2016-09-04 07:53:42 +02:00
var err error
2016-08-29 14:38:19 +02:00
{{if not .NoHooks -}}
2016-09-04 07:53:42 +02:00
if err = o.doBeforeUpdateHooks(exec); err != nil {
return err
}
2016-09-04 07:53:42 +02:00
{{end -}}
2016-09-04 07:53:42 +02:00
key := makeCacheKey(whitelist, nil)
{{$varNameSingular}}UpdateCacheMut.RLock()
cache, cached := {{$varNameSingular}}UpdateCache[key]
{{$varNameSingular}}UpdateCacheMut.RUnlock()
if !cached {
wl := strmangle.UpdateColumnSet({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns, whitelist)
2016-09-13 08:28:23 +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-04 07:53:42 +02:00
cache.valueMapping, err = boil.BindMapping({{$varNameSingular}}Type, {{$varNameSingular}}Mapping, append(wl, {{$varNameSingular}}PrimaryKeyColumns...))
if err != nil {
return err
}
}
if len(cache.valueMapping) == 0 {
2016-08-13 20:39:03 +02:00
return errors.New("{{.PkgName}}: unable to update {{.Table.Name}}, could not build whitelist")
}
2016-08-13 18:58:18 +02:00
2016-09-04 07:53:42 +02:00
values := boil.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping)
2016-08-13 18:58:18 +02:00
2016-08-13 20:39:03 +02:00
if boil.DebugMode {
2016-09-04 07:53:42 +02:00
fmt.Fprintln(boil.DebugWriter, cache.query)
2016-08-13 20:39:03 +02:00
fmt.Fprintln(boil.DebugWriter, values)
}
2016-09-04 07:53:42 +02:00
result, err := exec.Exec(cache.query, values...)
2016-03-19 07:22:10 +01:00
if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to update {{.Table.Name}} row")
2016-03-19 07:22:10 +01:00
}
if r, err := result.RowsAffected(); err == nil && r != 1 {
return errors.Errorf("failed to update single row, updated %d rows", r)
}
2016-09-04 07:53:42 +02:00
if !cached {
{{$varNameSingular}}UpdateCacheMut.Lock()
{{$varNameSingular}}UpdateCache[key] = cache
{{$varNameSingular}}UpdateCacheMut.Unlock()
}
2016-08-29 14:38:19 +02:00
{{if not .NoHooks -}}
return o.doAfterUpdateHooks(exec)
2016-08-28 12:48:50 +02:00
{{- else -}}
return nil
{{- end}}
2016-03-19 07:22:10 +01: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 {
return errors.Wrap(err, "{{.PkgName}}: unable to update all for {{.Table.Name}}")
2016-05-10 12:20:29 +02:00
}
return nil
}
// 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 {
panic(boil.WrapErr(err))
}
}
// 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 {
ln := int64(len(o))
if ln == 0 {
return nil
}
if len(cols) == 0 {
return errors.New("{{.PkgName}}: update all requires at least one column argument")
}
colNames := make([]string, len(cols))
args := make([]interface{}, len(cols))
i := 0
for name, value := range cols {
2016-09-13 08:49:18 +02:00
colNames[i] = name
args[i] = value
i++
}
// Append all of the primary key values for each column
args = append(args, o.inPrimaryKeyArgs()...)
sql := fmt.Sprintf(
2016-09-13 08:49:18 +02:00
"UPDATE {{$schemaTable}} SET %s WHERE ({{.LQ}}{{.Table.PKey.Columns | join (printf "%s,%s" .LQ .RQ)}}{{.RQ}}) IN (%s)",
strmangle.SetParamNames("{{.LQ}}", "{{.RQ}}", {{if .Dialect.IndexPlaceholders}}1{{else}}0{{end}}, colNames),
strmangle.Placeholders(dialect.IndexPlaceholders, len(o) * len({{$varNameSingular}}PrimaryKeyColumns), len(colNames)+1, len({{$varNameSingular}}PrimaryKeyColumns)),
)
2016-08-13 18:58:18 +02:00
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, sql)
fmt.Fprintln(boil.DebugWriter, args...)
}
result, err := exec.Exec(sql, args...)
if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to update all in {{$varNameSingular}} slice")
}
if r, err := result.RowsAffected(); err == nil && r != ln {
return errors.Errorf("failed to update %d rows, only affected %d", ln, r)
}
return nil
}