mirror of https://github.com/go-gorm/gorm.git
Refactor calc associations onConflictOption
This commit is contained in:
parent
74746211b8
commit
3a8c250180
|
@ -310,33 +310,22 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func onConflictOption(stmt *gorm.Statement, s *schema.Schema, selectColumns map[string]bool, restricted bool, defaultUpdatingColumns []string) clause.OnConflict {
|
func onConflictOption(stmt *gorm.Statement, s *schema.Schema, selectColumns map[string]bool, restricted bool, defaultUpdatingColumns []string) (onConflict clause.OnConflict) {
|
||||||
if stmt.DB.FullSaveAssociations {
|
if len(defaultUpdatingColumns) > 0 || stmt.DB.FullSaveAssociations {
|
||||||
defaultUpdatingColumns = make([]string, 0, len(s.DBNames))
|
onConflict.Columns = make([]clause.Column, 0, len(s.PrimaryFieldDBNames))
|
||||||
for _, dbName := range s.DBNames {
|
|
||||||
if v, ok := selectColumns[dbName]; (ok && !v) || (!ok && restricted) || !s.FieldsByDBName[dbName].Updatable {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !s.LookUpField(dbName).PrimaryKey {
|
|
||||||
defaultUpdatingColumns = append(defaultUpdatingColumns, dbName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(defaultUpdatingColumns) > 0 {
|
|
||||||
columns := make([]clause.Column, 0, len(s.PrimaryFieldDBNames))
|
|
||||||
for _, dbName := range s.PrimaryFieldDBNames {
|
for _, dbName := range s.PrimaryFieldDBNames {
|
||||||
columns = append(columns, clause.Column{Name: dbName})
|
onConflict.Columns = append(onConflict.Columns, clause.Column{Name: dbName})
|
||||||
}
|
}
|
||||||
|
|
||||||
return clause.OnConflict{
|
onConflict.UpdateAll = stmt.DB.FullSaveAssociations
|
||||||
Columns: columns,
|
if !onConflict.UpdateAll {
|
||||||
DoUpdates: clause.AssignmentColumns(defaultUpdatingColumns),
|
onConflict.DoUpdates = clause.AssignmentColumns(defaultUpdatingColumns)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
onConflict.DoNothing = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return clause.OnConflict{DoNothing: true}
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveAssociations(db *gorm.DB, rel *schema.Relationship, values interface{}, selectColumns map[string]bool, restricted bool, defaultUpdatingColumns []string) error {
|
func saveAssociations(db *gorm.DB, rel *schema.Relationship, values interface{}, selectColumns map[string]bool, restricted bool, defaultUpdatingColumns []string) error {
|
||||||
|
|
Loading…
Reference in New Issue