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.
|
2016-09-14 10:27:20 +02:00
|
|
|
func (o *{{$tableNameSingular}}) UpsertG({{if ne .DriverName "mysql"}}updateOnConflict bool, conflictColumns []string, {{end}}updateColumns []string, whitelist ...string) error {
|
|
|
|
return o.Upsert(boil.GetDB(), {{if ne .DriverName "mysql"}}updateOnConflict, conflictColumns, {{end}}updateColumns, whitelist...)
|
2016-08-04 05:11:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpsertGP attempts an insert, and does an update or ignore on conflict. Panics on error.
|
2016-09-14 10:27:20 +02:00
|
|
|
func (o *{{$tableNameSingular}}) UpsertGP({{if ne .DriverName "mysql"}}updateOnConflict bool, conflictColumns []string, {{end}}updateColumns []string, whitelist ...string) {
|
|
|
|
if err := o.Upsert(boil.GetDB(), {{if ne .DriverName "mysql"}}updateOnConflict, conflictColumns, {{end}}updateColumns, whitelist...); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
2016-08-04 05:11:45 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2016-09-14 10:27:20 +02:00
|
|
|
func (o *{{$tableNameSingular}}) UpsertP(exec boil.Executor, {{if ne .DriverName "mysql"}}updateOnConflict bool, conflictColumns []string, {{end}}updateColumns []string, whitelist ...string) {
|
|
|
|
if err := o.Upsert(exec, {{if ne .DriverName "mysql"}}updateOnConflict, conflictColumns, {{end}}updateColumns, whitelist...); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
2016-08-04 05:11:45 +02:00
|
|
|
// Upsert attempts an insert using an executor, and does an update or ignore on conflict.
|
2016-09-14 10:27:20 +02:00
|
|
|
func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, {{if ne .DriverName "mysql"}}updateOnConflict bool, conflictColumns []string, {{end}}updateColumns []string, whitelist ...string) error {
|
2016-09-14 10:08:30 +02:00
|
|
|
if o == nil {
|
|
|
|
return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for upsert")
|
|
|
|
}
|
|
|
|
|
|
|
|
{{- template "timestamp_upsert_helper" . }}
|
|
|
|
|
|
|
|
{{if not .NoHooks -}}
|
|
|
|
if err := o.doBeforeUpsertHooks(exec); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
{{- end}}
|
2016-08-04 05:11:45 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
|
|
|
var ret []string
|
|
|
|
whitelist, ret = strmangle.InsertColumnSet(
|
|
|
|
{{$varNameSingular}}Columns,
|
|
|
|
{{$varNameSingular}}ColumnsWithDefault,
|
|
|
|
{{$varNameSingular}}ColumnsWithoutDefault,
|
|
|
|
boil.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o),
|
|
|
|
whitelist,
|
|
|
|
)
|
|
|
|
update := strmangle.UpdateColumnSet(
|
|
|
|
{{$varNameSingular}}Columns,
|
|
|
|
{{$varNameSingular}}PrimaryKeyColumns,
|
|
|
|
updateColumns,
|
|
|
|
)
|
2016-08-29 15:22:23 +02:00
|
|
|
|
2016-09-14 10:27:20 +02:00
|
|
|
{{if ne .DriverName "mysql" -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
conflict := conflictColumns
|
|
|
|
if len(conflict) == 0 {
|
|
|
|
conflict = make([]string, len({{$varNameSingular}}PrimaryKeyColumns))
|
|
|
|
copy(conflict, {{$varNameSingular}}PrimaryKeyColumns)
|
|
|
|
}
|
|
|
|
{{- end}}
|
2016-08-28 14:16:08 +02:00
|
|
|
|
2016-09-14 10:27:20 +02:00
|
|
|
{{if eq .DriverName "mysql" -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
query := boil.BuildUpsertQueryMySQL(dialect, "{{.Table.Name}}", update, whitelist)
|
2016-09-14 10:27:20 +02:00
|
|
|
{{- else -}}
|
|
|
|
query := boil.BuildUpsertQueryPostgres(dialect, "{{.Table.Name}}", updateOnConflict, ret, update, conflict, whitelist)
|
2016-09-14 10:08:30 +02:00
|
|
|
{{- end}}
|
2016-08-14 06:21:26 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if boil.DebugMode {
|
|
|
|
fmt.Fprintln(boil.DebugWriter, query)
|
|
|
|
fmt.Fprintln(boil.DebugWriter, boil.GetStructValues(o, whitelist...))
|
|
|
|
}
|
2016-08-04 05:11:45 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
{{- if .UseLastInsertID}}
|
|
|
|
res, err := exec.Exec(query, boil.GetStructValues(o, whitelist...)...)
|
|
|
|
{{- else}}
|
|
|
|
if len(ret) != 0 {
|
|
|
|
err = exec.QueryRow(query, boil.GetStructValues(o, whitelist...)...).Scan(boil.GetStructPointers(o, ret...)...)
|
|
|
|
} else {
|
|
|
|
_, err = exec.Exec(query, boil.GetStructValues(o, whitelist...)...)
|
|
|
|
}
|
|
|
|
{{- end}}
|
2016-08-13 18:58:18 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
|
|
|
|
}
|
2016-08-04 05:11:45 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
{{if .UseLastInsertID -}}
|
|
|
|
if len(ret) != 0 {
|
|
|
|
lid, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "{{.PkgName}}: unable to get last insert id for {{.Table.Name}}")
|
|
|
|
}
|
|
|
|
{{$aipk := autoIncPrimaryKey .Table.Columns .Table.PKey}}
|
|
|
|
aipk := "{{$aipk.Name}}"
|
|
|
|
// if the update did not change anything, lid will be 0
|
|
|
|
if lid == 0 && aipk == "" {
|
|
|
|
// do a select using all pkeys
|
|
|
|
} else if lid != 0 {
|
|
|
|
// do a select using all pkeys + lid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{{- end}}
|
2016-08-04 05:11:45 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
{{if not .NoHooks -}}
|
|
|
|
if err := o.doAfterUpsertHooks(exec); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
{{- end}}
|
2016-08-04 05:11:45 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
return nil
|
2016-08-04 05:11:45 +02:00
|
|
|
}
|