Fix mysql LastInsertId()

This commit is contained in:
Aaron L 2016-09-24 16:10:26 -07:00
parent 5eb14f262a
commit 32290d9236
2 changed files with 58 additions and 52 deletions

View file

@ -78,56 +78,61 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
value := reflect.Indirect(reflect.ValueOf(o))
vals := queries.ValuesFromMapping(value, cache.valueMapping)
{{if .UseLastInsertID}}
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, cache.query)
fmt.Fprintln(boil.DebugWriter, vals)
}
{{if .UseLastInsertID -}}
{{- $canLastInsertID := .Table.CanLastInsertID -}}
{{if $canLastInsertID -}}
result, err := exec.Exec(cache.query, vals...)
{{else -}}
_, err = exec.Exec(cache.query, vals...)
{{- end}}
if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
}
var lastID int64
{{if $canLastInsertID -}}
var lastID int64
{{- end}}
var identifierCols []interface{}
if len(cache.retMapping) == 0 {
goto CacheNoHooks
}
{{if $canLastInsertID -}}
lastID, err = result.LastInsertId()
if err != nil {
return ErrSyncFail
}
{{- $colName := index .Table.PKey.Columns 0 -}}
{{$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}}
{{- $colTitled := $colName | titleCase}}
o.{{$colTitled}} = {{$col.Type}}(lastID)
identifierCols = []interface{}{lastID}
{{end}}
{{else}}
if lastID != 0 && len(cache.retMapping) == 1 && cache.retMapping[0] == {{$varNameSingular}}Mapping["{{$colTitled}}"] {
goto CacheNoHooks
}
{{- end}}
identifierCols = []interface{}{
{{range .Table.PKey.Columns -}}
o.{{. | singular | titleCase}},
o.{{. | titleCase}},
{{end -}}
}
{{end}}
if lastID == 0 || len(cache.retMapping) != 1 || cache.retMapping[0] == {{$varNameSingular}}Mapping["{{$colTitled}}"] {
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
fmt.Fprintln(boil.DebugWriter, identifierCols...)
}
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
fmt.Fprintln(boil.DebugWriter, identifierCols...)
}
err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...)
if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}")
}
err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(queries.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 {
@ -136,15 +141,11 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
_, err = exec.Exec(cache.query, vals...)
}
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, cache.query)
fmt.Fprintln(boil.DebugWriter, vals)
}
if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
}
{{end}}
{{if .UseLastInsertID -}}
CacheNoHooks:
{{- end}}

View file

@ -117,7 +117,7 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, {{if ne .DriverName
}
value := reflect.Indirect(reflect.ValueOf(o))
values := queries.ValuesFromMapping(value, cache.valueMapping)
vals := queries.ValuesFromMapping(value, cache.valueMapping)
var returns []interface{}
if len(cache.retMapping) != 0 {
returns = queries.PtrsFromMapping(value, cache.retMapping)
@ -125,59 +125,64 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, {{if ne .DriverName
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, cache.query)
fmt.Fprintln(boil.DebugWriter, values)
fmt.Fprintln(boil.DebugWriter, vals)
}
{{- if .UseLastInsertID}}
result, err := exec.Exec(cache.query, values...)
{{if .UseLastInsertID -}}
{{- $canLastInsertID := .Table.CanLastInsertID -}}
{{if $canLastInsertID -}}
result, err := exec.Exec(cache.query, vals...)
{{else -}}
_, err = exec.Exec(cache.query, vals...)
{{- end}}
if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
}
{{if $canLastInsertID -}}
var lastID int64
{{- end}}
var identifierCols []interface{}
if len(cache.retMapping) == 0 {
goto CacheNoHooks
}
{{if $canLastInsertID -}}
lastID, err = result.LastInsertId()
if err != nil {
return ErrSyncFail
}
{{- $colName := index .Table.PKey.Columns 0 -}}
{{$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}}
{{- $colTitled := $colName | titleCase}}
o.{{$colTitled}} = {{$col.Type}}(lastID)
identifierCols = []interface{}{lastID}
{{end}}
{{else}}
if lastID != 0 && len(cache.retMapping) == 1 && cache.retMapping[0] == {{$varNameSingular}}Mapping["{{$colTitled}}"] {
goto CacheNoHooks
}
{{- end}}
identifierCols = []interface{}{
{{range .Table.PKey.Columns -}}
o.{{. | singular | titleCase}},
o.{{. | titleCase}},
{{end -}}
}
{{end}}
if lastID == 0 || len(cache.retMapping) != 1 || cache.retMapping[0] == {{$varNameSingular}}Mapping["{{$colTitled}}"] {
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
fmt.Fprintln(boil.DebugWriter, identifierCols...)
}
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
fmt.Fprintln(boil.DebugWriter, identifierCols...)
}
err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(returns...)
if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}")
}
err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(returns...)
if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}")
}
{{- else}}
if len(cache.retMapping) != 0 {
err = exec.QueryRow(cache.query, values...).Scan(returns...)
err = exec.QueryRow(cache.query, vals...).Scan(returns...)
} else {
_, err = exec.Exec(cache.query, values...)
_, err = exec.Exec(cache.query, vals...)
}
if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")