2016-08-04 05:11:45 +02:00
|
|
|
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
|
|
|
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
|
|
|
// UpsertG attempts an insert, and does an update or ignore on conflict.
|
|
|
|
func (o *{{$tableNameSingular}}) UpsertG(update bool, conflictColumns []string, updateColumns []string, whitelist ...string) error {
|
|
|
|
return o.Upsert(boil.GetDB(), update, conflictColumns, updateColumns, whitelist...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpsertGP attempts an insert, and does an update or ignore on conflict. Panics on error.
|
|
|
|
func (o *{{$tableNameSingular}}) UpsertGP(update bool, conflictColumns []string, updateColumns []string, whitelist ...string) {
|
|
|
|
if err := o.Upsert(boil.GetDB(), update, conflictColumns, updateColumns, whitelist...); err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 16:20:17 +02:00
|
|
|
// UpsertP attempts an insert using an executor, and does an update or ignore on conflict.
|
|
|
|
// UpsertP panics on error.
|
|
|
|
func (o *{{$tableNameSingular}}) UpsertP(exec boil.Executor, update bool, conflictColumns []string, updateColumns []string, whitelist ...string) {
|
|
|
|
if err := o.Upsert(exec, update, conflictColumns, updateColumns, whitelist...); err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 05:11:45 +02:00
|
|
|
// Upsert attempts an insert using an executor, and does an update or ignore on conflict.
|
2016-08-14 06:21:26 +02:00
|
|
|
func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, updateOnConflict bool, conflictColumns []string, updateColumns []string, whitelist ...string) error {
|
2016-08-04 05:11:45 +02:00
|
|
|
if o == nil {
|
|
|
|
return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for upsert")
|
|
|
|
}
|
|
|
|
|
2016-08-14 06:21:26 +02:00
|
|
|
var ret []string
|
|
|
|
whitelist, ret = strmangle.InsertColumnSet(
|
|
|
|
{{$varNameSingular}}Columns,
|
|
|
|
{{$varNameSingular}}ColumnsWithDefault,
|
|
|
|
{{$varNameSingular}}ColumnsWithoutDefault,
|
2016-08-24 08:20:41 +02:00
|
|
|
boil.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, {{$varNameSingular}}TitleCases, o),
|
2016-08-14 06:21:26 +02:00
|
|
|
whitelist,
|
|
|
|
)
|
|
|
|
update := strmangle.UpdateColumnSet(
|
|
|
|
{{$varNameSingular}}Columns,
|
|
|
|
{{$varNameSingular}}PrimaryKeyColumns,
|
|
|
|
updateColumns,
|
|
|
|
)
|
|
|
|
conflict := conflictColumns
|
|
|
|
if len(conflict) == 0 {
|
|
|
|
conflict = make([]string, len({{$varNameSingular}}PrimaryKeyColumns))
|
|
|
|
copy(conflict, {{$varNameSingular}}PrimaryKeyColumns)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := generateUpsertQuery("{{.Table.Name}}", updateOnConflict, ret, update, conflict, whitelist)
|
2016-08-04 05:11:45 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if err := o.doBeforeUpsertHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-13 18:58:18 +02:00
|
|
|
if boil.DebugMode {
|
|
|
|
fmt.Fprintln(boil.DebugWriter, query)
|
2016-08-24 08:20:41 +02:00
|
|
|
fmt.Fprintln(boil.DebugWriter, boil.GetStructValues(o, {{$varNameSingular}}TitleCases, whitelist...))
|
2016-08-13 18:58:18 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 06:21:26 +02:00
|
|
|
{{- if .UseLastInsertID}}
|
|
|
|
return errors.New("don't know how to do this yet")
|
|
|
|
{{- else}}
|
|
|
|
if len(ret) != 0 {
|
2016-08-24 08:20:41 +02:00
|
|
|
err = exec.QueryRow(query, boil.GetStructValues(o, {{$varNameSingular}}TitleCases, whitelist...)...).Scan(boil.GetStructPointers(o, {{$varNameSingular}}TitleCases, ret...)...)
|
2016-08-04 05:11:45 +02:00
|
|
|
} else {
|
2016-08-24 08:20:41 +02:00
|
|
|
_, err = exec.Exec(query, boil.GetStructValues(o, {{$varNameSingular}}TitleCases, whitelist...)...)
|
2016-08-04 05:11:45 +02:00
|
|
|
}
|
2016-08-14 06:21:26 +02:00
|
|
|
{{- end}}
|
2016-08-04 05:11:45 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2016-08-13 20:36:03 +02:00
|
|
|
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
|
2016-08-04 05:11:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := o.doAfterUpsertHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|