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-16 07:08:45 +02:00
|
|
|
nzDefaults := queries.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o)
|
|
|
|
|
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)
|
|
|
|
}
|
2016-09-16 07:08:45 +02:00
|
|
|
buf.WriteByte('.')
|
|
|
|
for _, c := range nzDefaults {
|
|
|
|
buf.WriteString(c)
|
|
|
|
}
|
2016-09-15 08:03:05 +02:00
|
|
|
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,
|
2016-09-16 07:08:45 +02:00
|
|
|
nzDefaults,
|
2016-09-15 08:03:05 +02:00
|
|
|
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-16 07:17:12 +02:00
|
|
|
var lastID int64
|
|
|
|
var identifierCols []interface{}
|
2016-09-15 08:03:05 +02:00
|
|
|
if len(cache.retMapping) == 0 {
|
2016-09-16 07:17:12 +02:00
|
|
|
goto CacheNoHooks
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-04 05:11:45 +02:00
|
|
|
|
2016-09-16 07:17:12 +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
|
|
|
|
2016-09-17 05:13:44 +02:00
|
|
|
{{- $colName := index .Table.PKey.Columns 0 -}}
|
|
|
|
{{- $col := .Table.GetColumn $colName -}}
|
|
|
|
{{- $colTitled := $colName | singular | titleCase}}
|
|
|
|
{{if eq 1 (len .Table.PKey.Columns)}}
|
|
|
|
{{$cnames := .Table.Columns | filterColumnsByDefault true | columnNames}}
|
|
|
|
{{if setInclude $colName $cnames}}
|
|
|
|
o.{{$colTitled}} = {{$col.Type}}(lastID)
|
|
|
|
identifierCols = []interface{}{lastID}
|
|
|
|
{{end}}
|
|
|
|
{{else}}
|
|
|
|
identifierCols = []interface{}{
|
|
|
|
{{range .Table.PKey.Columns -}}
|
|
|
|
o.{{. | singular | titleCase}},
|
|
|
|
{{end -}}
|
|
|
|
}
|
|
|
|
{{end}}
|
2016-09-14 17:36:36 +02:00
|
|
|
|
2016-09-16 07:28:23 +02:00
|
|
|
if lastID == 0 || len(cache.retMapping) != 1 || cache.retMapping[0] == {{$varNameSingular}}Mapping["{{$colTitled}}"] {
|
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-16 07:17:12 +02:00
|
|
|
{{if .UseLastInsertID -}}
|
|
|
|
CacheNoHooks:
|
|
|
|
{{end -}}
|
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
|
|
|
}
|