2016-08-04 05:11:45 +02:00
|
|
|
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
|
|
|
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
2016-09-14 17:36:36 +02:00
|
|
|
{{- $schemaTable := .Table.Name | .SchemaTable -}}
|
2016-08-04 05:11:45 +02:00
|
|
|
// 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 17:36:36 +02:00
|
|
|
panic(boil.WrapErr(err))
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
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 17:36:36 +02:00
|
|
|
panic(boil.WrapErr(err))
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
}
|
2016-09-15 08:03:05 +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 {
|
2016-09-14 17:36:36 +02:00
|
|
|
return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for upsert")
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{{- template "timestamp_upsert_helper" . }}
|
|
|
|
|
|
|
|
{{if not .NoHooks -}}
|
|
|
|
if err := o.doBeforeUpsertHooks(exec); err != nil {
|
2016-09-14 17:36:36 +02:00
|
|
|
return err
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
{{- end}}
|
2016-08-04 05:11:45 +02:00
|
|
|
|
2016-09-15 08:03:05 +02:00
|
|
|
// Build cache key in-line uglily - mysql vs postgres problems
|
|
|
|
buf := strmangle.GetBuffer()
|
|
|
|
{{if ne .DriverName "mysql" -}}
|
|
|
|
if updateOnConflict {
|
|
|
|
buf.WriteByte('t')
|
|
|
|
} else {
|
|
|
|
buf.WriteByte('f')
|
|
|
|
}
|
|
|
|
buf.WriteByte('.')
|
|
|
|
for _, c := range conflictColumns {
|
|
|
|
buf.WriteString(c)
|
|
|
|
}
|
|
|
|
buf.WriteByte('.')
|
|
|
|
{{end -}}
|
|
|
|
for _, c := range updateColumns {
|
|
|
|
buf.WriteString(c)
|
|
|
|
}
|
|
|
|
buf.WriteByte('.')
|
|
|
|
for _, c := range whitelist {
|
|
|
|
buf.WriteString(c)
|
|
|
|
}
|
|
|
|
key := buf.String()
|
|
|
|
strmangle.PutBuffer(buf)
|
|
|
|
|
|
|
|
{{$varNameSingular}}UpsertCacheMut.RLock()
|
|
|
|
cache, cached := {{$varNameSingular}}UpsertCache[key]
|
|
|
|
{{$varNameSingular}}UpsertCacheMut.RUnlock()
|
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
2016-08-29 15:22:23 +02:00
|
|
|
|
2016-09-15 08:03:05 +02:00
|
|
|
if !cached {
|
|
|
|
var ret []string
|
|
|
|
whitelist, ret = strmangle.InsertColumnSet(
|
|
|
|
{{$varNameSingular}}Columns,
|
|
|
|
{{$varNameSingular}}ColumnsWithDefault,
|
|
|
|
{{$varNameSingular}}ColumnsWithoutDefault,
|
|
|
|
queries.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o),
|
|
|
|
whitelist,
|
|
|
|
)
|
|
|
|
update := strmangle.UpdateColumnSet(
|
|
|
|
{{$varNameSingular}}Columns,
|
|
|
|
{{$varNameSingular}}PrimaryKeyColumns,
|
|
|
|
updateColumns,
|
|
|
|
)
|
|
|
|
|
|
|
|
{{if ne .DriverName "mysql" -}}
|
|
|
|
var conflict []string
|
|
|
|
if len(conflictColumns) == 0 {
|
|
|
|
conflict = make([]string, len({{$varNameSingular}}PrimaryKeyColumns))
|
|
|
|
copy(conflict, {{$varNameSingular}}PrimaryKeyColumns)
|
|
|
|
}
|
|
|
|
cache.query = queries.BuildUpsertQueryPostgres(dialect, "{{$schemaTable}}", updateOnConflict, ret, update, conflict, whitelist)
|
|
|
|
{{- else -}}
|
|
|
|
cache.query = queries.BuildUpsertQueryMySQL(dialect, "{{.Table.Name}}", update, whitelist)
|
|
|
|
cache.retQuery = fmt.Sprintf(
|
|
|
|
"SELECT %s FROM {{.LQ}}{{.Table.Name}}{{.RQ}} WHERE {{whereClause .LQ .RQ 0 .Table.PKey.Columns}}",
|
|
|
|
strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, ret), ","),
|
|
|
|
)
|
|
|
|
{{- end}}
|
|
|
|
|
|
|
|
cache.valueMapping, err = queries.BindMapping({{$varNameSingular}}Type, {{$varNameSingular}}Mapping, whitelist)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(ret) != 0 {
|
|
|
|
cache.retMapping, err = queries.BindMapping({{$varNameSingular}}Type, {{$varNameSingular}}Mapping, ret)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
value := reflect.Indirect(reflect.ValueOf(o))
|
|
|
|
values := queries.ValuesFromMapping(value, cache.valueMapping)
|
|
|
|
var returns []interface{}
|
|
|
|
if len(cache.retMapping) != 0 {
|
|
|
|
returns = queries.PtrsFromMapping(value, cache.retMapping)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-14 06:21:26 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if boil.DebugMode {
|
2016-09-15 08:03:05 +02:00
|
|
|
fmt.Fprintln(boil.DebugWriter, cache.query)
|
|
|
|
fmt.Fprintln(boil.DebugWriter, values)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-04 05:11:45 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
{{- if .UseLastInsertID}}
|
2016-09-15 08:03:05 +02:00
|
|
|
result, err := exec.Exec(cache.query, values...)
|
2016-09-14 17:36:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-13 18:58:18 +02:00
|
|
|
|
2016-09-15 08:03:05 +02:00
|
|
|
if len(cache.retMapping) == 0 {
|
2016-09-14 17:36:36 +02:00
|
|
|
{{if not .NoHooks -}}
|
|
|
|
return o.doAfterUpsertHooks(exec)
|
|
|
|
{{else -}}
|
|
|
|
return nil
|
|
|
|
{{end -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-04 05:11:45 +02:00
|
|
|
|
2016-09-14 17:36:36 +02:00
|
|
|
lastID, err := result.LastInsertId()
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
2016-09-14 17:36:36 +02:00
|
|
|
return ErrSyncFail
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-09-14 17:36:36 +02:00
|
|
|
|
|
|
|
var identifierCols []interface{}
|
|
|
|
if lastID != 0 {
|
|
|
|
{{- $colName := index .Table.PKey.Columns 0 -}}
|
|
|
|
{{- $col := .Table.GetColumn $colName -}}
|
|
|
|
o.{{$colName | singular | titleCase}} = {{$col.Type}}(lastID)
|
|
|
|
identifierCols = []interface{}{lastID}
|
|
|
|
} else {
|
|
|
|
identifierCols = []interface{}{
|
|
|
|
{{range .Table.PKey.Columns -}}
|
|
|
|
o.{{. | singular | titleCase}},
|
|
|
|
{{end -}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 08:03:05 +02:00
|
|
|
if lastID != 0 && len(cache.retMapping) == 1 {
|
2016-09-14 17:36:36 +02:00
|
|
|
if boil.DebugMode {
|
2016-09-15 08:03:05 +02:00
|
|
|
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
|
2016-09-14 17:36:36 +02:00
|
|
|
fmt.Fprintln(boil.DebugWriter, identifierCols...)
|
|
|
|
}
|
|
|
|
|
2016-09-15 08:03:05 +02:00
|
|
|
err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(returns...)
|
2016-09-14 17:36:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}")
|
|
|
|
}
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-09-14 17:36:36 +02:00
|
|
|
{{- else}}
|
2016-09-15 08:03:05 +02:00
|
|
|
if len(cache.retMapping) != 0 {
|
|
|
|
err = exec.QueryRow(cache.query, values...).Scan(returns...)
|
2016-09-14 17:36:36 +02:00
|
|
|
} else {
|
2016-09-15 08:03:05 +02:00
|
|
|
_, err = exec.Exec(cache.query, values...)
|
2016-09-14 17:36:36 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
{{- end}}
|
2016-08-04 05:11:45 +02:00
|
|
|
|
2016-09-15 08:03:05 +02:00
|
|
|
if !cached {
|
|
|
|
{{$varNameSingular}}UpsertCacheMut.Lock()
|
|
|
|
{{$varNameSingular}}UpsertCache[key] = cache
|
|
|
|
{{$varNameSingular}}UpsertCacheMut.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
{{if not .NoHooks -}}
|
2016-09-15 06:59:48 +02:00
|
|
|
return o.doAfterUpsertHooks(exec)
|
|
|
|
{{- else -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
return nil
|
2016-09-15 06:59:48 +02:00
|
|
|
{{- end}}
|
2016-08-04 05:11:45 +02:00
|
|
|
}
|