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.
|
|
|
|
func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, update bool, conflictColumns []string, updateColumns []string, whitelist ...string) error {
|
|
|
|
if o == nil {
|
|
|
|
return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for upsert")
|
|
|
|
}
|
|
|
|
|
2016-08-04 16:20:17 +02:00
|
|
|
columns := o.generateUpsertColumns(conflictColumns, updateColumns, whitelist)
|
|
|
|
query := o.generateUpsertQuery(update, columns)
|
2016-08-04 05:11:45 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if err := o.doBeforeUpsertHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-04 16:20:17 +02:00
|
|
|
if len(columns.returning) != 0 {
|
|
|
|
err = exec.QueryRow(query, boil.GetStructValues(o, columns.whitelist...)...).Scan(boil.GetStructPointers(o, columns.returning...)...)
|
2016-08-04 05:11:45 +02:00
|
|
|
} else {
|
2016-08-04 16:20:17 +02:00
|
|
|
_, err = exec.Exec(query, {{.Table.Columns | columnNames | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})
|
2016-08-04 05:11:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if boil.DebugMode {
|
2016-08-11 14:26:49 +02:00
|
|
|
fmt.Fprintln(boil.DebugWriter, query)
|
|
|
|
fmt.Fprintln(boil.DebugWriter, boil.GetStructValues(o, columns.whitelist...))
|
2016-08-04 05:11:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("{{.PkgName}}: unable to upsert for {{.Table.Name}}: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := o.doAfterUpsertHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-04 16:20:17 +02:00
|
|
|
// generateUpsertColumns builds an upsertData object, using generated values when necessary.
|
|
|
|
func (o *{{$tableNameSingular}}) generateUpsertColumns(conflict []string, update []string, whitelist []string) upsertData {
|
|
|
|
var upsertCols upsertData
|
|
|
|
|
|
|
|
upsertCols.whitelist, upsertCols.returning = o.generateInsertColumns(whitelist...)
|
|
|
|
|
|
|
|
upsertCols.conflict = make([]string, len(conflict))
|
|
|
|
upsertCols.update = make([]string, len(update))
|
|
|
|
|
|
|
|
// generates the ON CONFLICT() columns if none are provided
|
|
|
|
upsertCols.conflict = o.generateConflictColumns(conflict...)
|
|
|
|
|
|
|
|
// generate the UPDATE SET columns if none are provided
|
|
|
|
upsertCols.update = o.generateUpdateColumns(update...)
|
|
|
|
|
|
|
|
return upsertCols
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateConflictColumns returns the user provided columns.
|
|
|
|
// If no columns are provided, it returns the primary key columns.
|
|
|
|
func (o *{{$tableNameSingular}}) generateConflictColumns(columns ...string) []string {
|
|
|
|
if len(columns) != 0 {
|
|
|
|
return columns
|
2016-08-04 05:11:45 +02:00
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
|
|
|
|
c := make([]string, len({{$varNameSingular}}PrimaryKeyColumns))
|
|
|
|
copy(c, {{$varNameSingular}}PrimaryKeyColumns)
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateUpsertQuery builds a SQL statement string using the upsertData provided.
|
|
|
|
func (o *{{$tableNameSingular}}) generateUpsertQuery(update bool, columns upsertData) string {
|
|
|
|
var set, query string
|
|
|
|
|
2016-08-11 14:26:49 +02:00
|
|
|
conflict := strmangle.IdentQuoteSlice(columns.conflict)
|
|
|
|
whitelist := strmangle.IdentQuoteSlice(columns.whitelist)
|
|
|
|
returning := strmangle.IdentQuoteSlice(columns.returning)
|
2016-08-04 16:20:17 +02:00
|
|
|
|
|
|
|
var sets []string
|
|
|
|
// Generate the UPDATE SET clause
|
|
|
|
for _, v := range columns.update {
|
|
|
|
quoted := strmangle.IdentQuote(v)
|
|
|
|
sets = append(sets, fmt.Sprintf("%s = EXCLUDED.%s", quoted, quoted))
|
|
|
|
}
|
|
|
|
set = strings.Join(sets, ", ")
|
|
|
|
|
|
|
|
query = fmt.Sprintf(
|
2016-08-11 14:26:49 +02:00
|
|
|
"INSERT INTO {{.Table.Name}} (%s) VALUES (%s) ON CONFLICT",
|
|
|
|
strings.Join(whitelist, ", "),
|
|
|
|
strmangle.Placeholders(len(whitelist), 1, 1),
|
2016-08-04 16:20:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if !update {
|
2016-08-05 04:59:40 +02:00
|
|
|
query = query + " DO NOTHING"
|
2016-08-04 16:20:17 +02:00
|
|
|
} else {
|
2016-08-11 14:26:49 +02:00
|
|
|
query = fmt.Sprintf("%s (%s) DO UPDATE SET %s", query, strings.Join(conflict, ", "), set)
|
2016-08-04 16:20:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(columns.returning) != 0 {
|
2016-08-11 14:26:49 +02:00
|
|
|
query = fmt.Sprintf("%s RETURNING %s", query, strings.Join(returning, ", "))
|
2016-08-04 16:20:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return query
|
2016-08-04 05:11:45 +02:00
|
|
|
}
|