deal with oracle's special handling of last inserted id

This commit is contained in:
Jim Lambert 2020-02-12 10:37:13 -05:00
parent 4061744821
commit e56cd82358

View File

@ -1,7 +1,9 @@
package gorm
import (
"database/sql"
"fmt"
"sort"
"strings"
)
@ -144,6 +146,22 @@ func createCallback(scope *Scope) {
return
}
fmt.Println(scope.Dialect().GetName())
// deal with oracle special case handling of last insert id
oraModules := []string{"godror", "oci8", "ora"} // must be an asc sorted slice
insertAt := sort.SearchStrings(oraModules, scope.Dialect().GetName())
if insertAt < len(oraModules) && oraModules[insertAt] == scope.Dialect().GetName() {
var id uint32
scope.SQLVars = append(scope.SQLVars, sql.Out{Dest: &id})
scope.SQL = fmt.Sprintf("%s returning id into :%d", scope.SQL, len(scope.SQLVars))
if result, err := scope.SQLDB().Exec(scope.SQL, scope.SQLVars...); scope.Err(err) == nil {
scope.db.RowsAffected, _ = result.RowsAffected()
scope.Err(primaryField.Set(id))
}
return
}
// execute create sql: lastInsertID implemention for majority of dialects
if lastInsertIDReturningSuffix == "" && lastInsertIDOutputInterstitial == "" {
if result, err := scope.SQLDB().Exec(scope.SQL, scope.SQLVars...); scope.Err(err) == nil {