Upsert fixed
This commit is contained in:
parent
7d377f42ae
commit
1c28f761f1
3 changed files with 95 additions and 52 deletions
|
@ -98,18 +98,34 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
|
|||
}
|
||||
|
||||
lastID, err := result.LastInsertId()
|
||||
if err != nil || lastID == 0 || len({{$varNameSingular}}PrimaryKeyColumns) != 1 {
|
||||
if err != nil {
|
||||
return ErrSyncFail
|
||||
}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
|
||||
fmt.Fprintln(boil.DebugWriter, lastID)
|
||||
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 -}}
|
||||
}
|
||||
}
|
||||
|
||||
err = exec.QueryRow(cache.retQuery, lastID).Scan(boil.PtrsFromMapping(value, cache.retMapping)...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}")
|
||||
if lastID != 0 && len(cache.retMapping) == 1 {
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
|
||||
fmt.Fprintln(boil.DebugWriter, identifierCols...)
|
||||
}
|
||||
|
||||
err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(boil.PtrsFromMapping(value, cache.retMapping)...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}")
|
||||
}
|
||||
}
|
||||
{{else}}
|
||||
if len(cache.retMapping) != 0 {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
||||
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
||||
{{- $schemaTable := .Table.Name | .SchemaTable -}}
|
||||
// UpsertG attempts an insert, and does an update or ignore on conflict.
|
||||
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...)
|
||||
|
@ -8,7 +9,7 @@ func (o *{{$tableNameSingular}}) UpsertG({{if ne .DriverName "mysql"}}updateOnCo
|
|||
// UpsertGP attempts an insert, and does an update or ignore on conflict. Panics on error.
|
||||
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 {
|
||||
panic(boil.WrapErr(err))
|
||||
panic(boil.WrapErr(err))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,92 +17,118 @@ func (o *{{$tableNameSingular}}) UpsertGP({{if ne .DriverName "mysql"}}updateOnC
|
|||
// UpsertP panics on error.
|
||||
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 {
|
||||
panic(boil.WrapErr(err))
|
||||
panic(boil.WrapErr(err))
|
||||
}
|
||||
}
|
||||
|
||||
// Upsert attempts an insert using an executor, and does an update or ignore on conflict.
|
||||
func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, {{if ne .DriverName "mysql"}}updateOnConflict bool, conflictColumns []string, {{end}}updateColumns []string, whitelist ...string) error {
|
||||
if o == nil {
|
||||
return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for upsert")
|
||||
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
|
||||
return err
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
var err error
|
||||
var ret []string
|
||||
whitelist, ret = strmangle.InsertColumnSet(
|
||||
{{$varNameSingular}}Columns,
|
||||
{{$varNameSingular}}ColumnsWithDefault,
|
||||
{{$varNameSingular}}ColumnsWithoutDefault,
|
||||
boil.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o),
|
||||
whitelist,
|
||||
{{$varNameSingular}}ColumnsWithDefault,
|
||||
{{$varNameSingular}}Columns,
|
||||
{{$varNameSingular}}ColumnsWithoutDefault,
|
||||
boil.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o),
|
||||
whitelist,
|
||||
)
|
||||
update := strmangle.UpdateColumnSet(
|
||||
{{$varNameSingular}}Columns,
|
||||
{{$varNameSingular}}PrimaryKeyColumns,
|
||||
updateColumns,
|
||||
{{$varNameSingular}}Columns,
|
||||
{{$varNameSingular}}PrimaryKeyColumns,
|
||||
updateColumns,
|
||||
)
|
||||
|
||||
{{if ne .DriverName "mysql" -}}
|
||||
conflict := conflictColumns
|
||||
if len(conflict) == 0 {
|
||||
conflict = make([]string, len({{$varNameSingular}}PrimaryKeyColumns))
|
||||
copy(conflict, {{$varNameSingular}}PrimaryKeyColumns)
|
||||
conflict = make([]string, len({{$varNameSingular}}PrimaryKeyColumns))
|
||||
copy(conflict, {{$varNameSingular}}PrimaryKeyColumns)
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
{{if eq .DriverName "mysql" -}}
|
||||
query := boil.BuildUpsertQueryMySQL(dialect, "{{.Table.Name}}", update, whitelist)
|
||||
query := boil.BuildUpsertQueryPostgres(dialect, "{{$schemaTable}}", updateOnConflict, ret, update, conflict, whitelist)
|
||||
{{- else -}}
|
||||
query := boil.BuildUpsertQueryPostgres(dialect, "{{.Table.Name}}", updateOnConflict, ret, update, conflict, whitelist)
|
||||
query := boil.BuildUpsertQueryMySQL(dialect, "{{.Table.Name}}", update, whitelist)
|
||||
{{- end}}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, query)
|
||||
fmt.Fprintln(boil.DebugWriter, boil.GetStructValues(o, whitelist...))
|
||||
fmt.Fprintln(boil.DebugWriter, query)
|
||||
fmt.Fprintln(boil.DebugWriter, boil.GetStructValues(o, whitelist...))
|
||||
}
|
||||
|
||||
{{- if .UseLastInsertID}}
|
||||
res, err := exec.Exec(query, boil.GetStructValues(o, whitelist...)...)
|
||||
result, err := exec.Exec(query, boil.GetStructValues(o, whitelist...)...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
|
||||
}
|
||||
|
||||
if len(ret) == 0 {
|
||||
{{if not .NoHooks -}}
|
||||
return o.doAfterUpsertHooks(exec)
|
||||
{{else -}}
|
||||
return nil
|
||||
{{end -}}
|
||||
}
|
||||
|
||||
lastID, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return ErrSyncFail
|
||||
}
|
||||
|
||||
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 -}}
|
||||
}
|
||||
}
|
||||
|
||||
if lastID != 0 && len(ret) == 1 {
|
||||
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), ","),
|
||||
)
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, ret)
|
||||
fmt.Fprintln(boil.DebugWriter, identifierCols...)
|
||||
}
|
||||
|
||||
err = exec.QueryRow(retQuery, identifierCols...).Scan(boil.GetStructPointers(o, ret...)...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}")
|
||||
}
|
||||
}
|
||||
{{- else}}
|
||||
if len(ret) != 0 {
|
||||
err = exec.QueryRow(query, boil.GetStructValues(o, whitelist...)...).Scan(boil.GetStructPointers(o, ret...)...)
|
||||
err = exec.QueryRow(query, boil.GetStructValues(o, whitelist...)...).Scan(boil.GetStructPointers(o, ret...)...)
|
||||
} else {
|
||||
_, err = exec.Exec(query, boil.GetStructValues(o, whitelist...)...)
|
||||
_, err = exec.Exec(query, boil.GetStructValues(o, whitelist...)...)
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
|
||||
}
|
||||
|
||||
{{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
|
||||
}
|
||||
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
{{if not .NoHooks -}}
|
||||
if err := o.doAfterUpsertHooks(exec); err != nil {
|
||||
return err
|
||||
return err
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ func (m *mysqlTester) setup() error {
|
|||
return err
|
||||
}
|
||||
|
||||
dumpCmd := exec.Command("mysqldump", m.defaultsFile(), m.dbName)
|
||||
dumpCmd := exec.Command("mysqldump", m.defaultsFile(), "--no-data", m.dbName)
|
||||
createCmd := exec.Command("mysql", m.defaultsFile(), "--database", m.testDBName)
|
||||
|
||||
r, w := io.Pipe()
|
||||
|
|
Loading…
Add table
Reference in a new issue