diff --git a/callback_create.go b/callback_create.go index c4d25f37..76c1b4de 100644 --- a/callback_create.go +++ b/callback_create.go @@ -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 {