Fix mysql LastInsertId()
This commit is contained in:
parent
5eb14f262a
commit
32290d9236
2 changed files with 58 additions and 52 deletions
|
@ -78,56 +78,61 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
|
||||||
|
|
||||||
value := reflect.Indirect(reflect.ValueOf(o))
|
value := reflect.Indirect(reflect.ValueOf(o))
|
||||||
vals := queries.ValuesFromMapping(value, cache.valueMapping)
|
vals := queries.ValuesFromMapping(value, cache.valueMapping)
|
||||||
{{if .UseLastInsertID}}
|
|
||||||
if boil.DebugMode {
|
if boil.DebugMode {
|
||||||
fmt.Fprintln(boil.DebugWriter, cache.query)
|
fmt.Fprintln(boil.DebugWriter, cache.query)
|
||||||
fmt.Fprintln(boil.DebugWriter, vals)
|
fmt.Fprintln(boil.DebugWriter, vals)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{{if .UseLastInsertID -}}
|
||||||
|
{{- $canLastInsertID := .Table.CanLastInsertID -}}
|
||||||
|
{{if $canLastInsertID -}}
|
||||||
result, err := exec.Exec(cache.query, vals...)
|
result, err := exec.Exec(cache.query, vals...)
|
||||||
|
{{else -}}
|
||||||
|
_, err = exec.Exec(cache.query, vals...)
|
||||||
|
{{- end}}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
|
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
|
||||||
}
|
}
|
||||||
|
|
||||||
var lastID int64
|
{{if $canLastInsertID -}}
|
||||||
|
var lastID int64
|
||||||
|
{{- end}}
|
||||||
var identifierCols []interface{}
|
var identifierCols []interface{}
|
||||||
|
|
||||||
if len(cache.retMapping) == 0 {
|
if len(cache.retMapping) == 0 {
|
||||||
goto CacheNoHooks
|
goto CacheNoHooks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{{if $canLastInsertID -}}
|
||||||
lastID, err = result.LastInsertId()
|
lastID, err = result.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrSyncFail
|
return ErrSyncFail
|
||||||
}
|
}
|
||||||
|
|
||||||
{{- $colName := index .Table.PKey.Columns 0 -}}
|
{{$colName := index .Table.PKey.Columns 0 -}}
|
||||||
{{- $col := .Table.GetColumn $colName -}}
|
{{- $col := .Table.GetColumn $colName -}}
|
||||||
{{- $colTitled := $colName | singular | titleCase}}
|
{{- $colTitled := $colName | titleCase}}
|
||||||
{{if eq 1 (len .Table.PKey.Columns)}}
|
|
||||||
{{$cnames := .Table.Columns | filterColumnsByDefault true | columnNames}}
|
|
||||||
{{if setInclude $colName $cnames}}
|
|
||||||
o.{{$colTitled}} = {{$col.Type}}(lastID)
|
o.{{$colTitled}} = {{$col.Type}}(lastID)
|
||||||
identifierCols = []interface{}{lastID}
|
if lastID != 0 && len(cache.retMapping) == 1 && cache.retMapping[0] == {{$varNameSingular}}Mapping["{{$colTitled}}"] {
|
||||||
{{end}}
|
goto CacheNoHooks
|
||||||
{{else}}
|
}
|
||||||
|
{{- end}}
|
||||||
|
|
||||||
identifierCols = []interface{}{
|
identifierCols = []interface{}{
|
||||||
{{range .Table.PKey.Columns -}}
|
{{range .Table.PKey.Columns -}}
|
||||||
o.{{. | singular | titleCase}},
|
o.{{. | titleCase}},
|
||||||
{{end -}}
|
{{end -}}
|
||||||
}
|
}
|
||||||
{{end}}
|
|
||||||
|
|
||||||
if lastID == 0 || len(cache.retMapping) != 1 || cache.retMapping[0] == {{$varNameSingular}}Mapping["{{$colTitled}}"] {
|
if boil.DebugMode {
|
||||||
if boil.DebugMode {
|
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
|
||||||
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
|
fmt.Fprintln(boil.DebugWriter, identifierCols...)
|
||||||
fmt.Fprintln(boil.DebugWriter, identifierCols...)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...)
|
err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}")
|
return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
{{else}}
|
{{else}}
|
||||||
if len(cache.retMapping) != 0 {
|
if len(cache.retMapping) != 0 {
|
||||||
|
@ -136,15 +141,11 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
|
||||||
_, err = exec.Exec(cache.query, vals...)
|
_, err = exec.Exec(cache.query, vals...)
|
||||||
}
|
}
|
||||||
|
|
||||||
if boil.DebugMode {
|
|
||||||
fmt.Fprintln(boil.DebugWriter, cache.query)
|
|
||||||
fmt.Fprintln(boil.DebugWriter, vals)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
|
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
|
||||||
}
|
}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{if .UseLastInsertID -}}
|
{{if .UseLastInsertID -}}
|
||||||
CacheNoHooks:
|
CacheNoHooks:
|
||||||
{{- end}}
|
{{- end}}
|
||||||
|
|
|
@ -117,7 +117,7 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, {{if ne .DriverName
|
||||||
}
|
}
|
||||||
|
|
||||||
value := reflect.Indirect(reflect.ValueOf(o))
|
value := reflect.Indirect(reflect.ValueOf(o))
|
||||||
values := queries.ValuesFromMapping(value, cache.valueMapping)
|
vals := queries.ValuesFromMapping(value, cache.valueMapping)
|
||||||
var returns []interface{}
|
var returns []interface{}
|
||||||
if len(cache.retMapping) != 0 {
|
if len(cache.retMapping) != 0 {
|
||||||
returns = queries.PtrsFromMapping(value, cache.retMapping)
|
returns = queries.PtrsFromMapping(value, cache.retMapping)
|
||||||
|
@ -125,59 +125,64 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, {{if ne .DriverName
|
||||||
|
|
||||||
if boil.DebugMode {
|
if boil.DebugMode {
|
||||||
fmt.Fprintln(boil.DebugWriter, cache.query)
|
fmt.Fprintln(boil.DebugWriter, cache.query)
|
||||||
fmt.Fprintln(boil.DebugWriter, values)
|
fmt.Fprintln(boil.DebugWriter, vals)
|
||||||
}
|
}
|
||||||
|
|
||||||
{{- if .UseLastInsertID}}
|
{{if .UseLastInsertID -}}
|
||||||
result, err := exec.Exec(cache.query, values...)
|
{{- $canLastInsertID := .Table.CanLastInsertID -}}
|
||||||
|
{{if $canLastInsertID -}}
|
||||||
|
result, err := exec.Exec(cache.query, vals...)
|
||||||
|
{{else -}}
|
||||||
|
_, err = exec.Exec(cache.query, vals...)
|
||||||
|
{{- end}}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
|
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{{if $canLastInsertID -}}
|
||||||
var lastID int64
|
var lastID int64
|
||||||
|
{{- end}}
|
||||||
var identifierCols []interface{}
|
var identifierCols []interface{}
|
||||||
|
|
||||||
if len(cache.retMapping) == 0 {
|
if len(cache.retMapping) == 0 {
|
||||||
goto CacheNoHooks
|
goto CacheNoHooks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{{if $canLastInsertID -}}
|
||||||
lastID, err = result.LastInsertId()
|
lastID, err = result.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrSyncFail
|
return ErrSyncFail
|
||||||
}
|
}
|
||||||
|
|
||||||
{{- $colName := index .Table.PKey.Columns 0 -}}
|
{{$colName := index .Table.PKey.Columns 0 -}}
|
||||||
{{- $col := .Table.GetColumn $colName -}}
|
{{- $col := .Table.GetColumn $colName -}}
|
||||||
{{- $colTitled := $colName | singular | titleCase}}
|
{{- $colTitled := $colName | titleCase}}
|
||||||
{{if eq 1 (len .Table.PKey.Columns)}}
|
|
||||||
{{$cnames := .Table.Columns | filterColumnsByDefault true | columnNames}}
|
|
||||||
{{if setInclude $colName $cnames}}
|
|
||||||
o.{{$colTitled}} = {{$col.Type}}(lastID)
|
o.{{$colTitled}} = {{$col.Type}}(lastID)
|
||||||
identifierCols = []interface{}{lastID}
|
if lastID != 0 && len(cache.retMapping) == 1 && cache.retMapping[0] == {{$varNameSingular}}Mapping["{{$colTitled}}"] {
|
||||||
{{end}}
|
goto CacheNoHooks
|
||||||
{{else}}
|
}
|
||||||
|
{{- end}}
|
||||||
|
|
||||||
identifierCols = []interface{}{
|
identifierCols = []interface{}{
|
||||||
{{range .Table.PKey.Columns -}}
|
{{range .Table.PKey.Columns -}}
|
||||||
o.{{. | singular | titleCase}},
|
o.{{. | titleCase}},
|
||||||
{{end -}}
|
{{end -}}
|
||||||
}
|
}
|
||||||
{{end}}
|
|
||||||
|
|
||||||
if lastID == 0 || len(cache.retMapping) != 1 || cache.retMapping[0] == {{$varNameSingular}}Mapping["{{$colTitled}}"] {
|
if boil.DebugMode {
|
||||||
if boil.DebugMode {
|
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
|
||||||
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
|
fmt.Fprintln(boil.DebugWriter, identifierCols...)
|
||||||
fmt.Fprintln(boil.DebugWriter, identifierCols...)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(returns...)
|
err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(returns...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}")
|
return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
{{- else}}
|
{{- else}}
|
||||||
if len(cache.retMapping) != 0 {
|
if len(cache.retMapping) != 0 {
|
||||||
err = exec.QueryRow(cache.query, values...).Scan(returns...)
|
err = exec.QueryRow(cache.query, vals...).Scan(returns...)
|
||||||
} else {
|
} else {
|
||||||
_, err = exec.Exec(cache.query, values...)
|
_, err = exec.Exec(cache.query, vals...)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
|
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
|
||||||
|
|
Loading…
Reference in a new issue