mirror of https://github.com/go-gorm/gorm.git
Refactor if logic (#4683)
* adjust code for preload * adjust code for Create
This commit is contained in:
parent
c170af11e9
commit
4c8810a848
|
@ -65,21 +65,33 @@ func Create(config *Config) func(db *gorm.DB) {
|
|||
db.Statement.Build(db.Statement.BuildClauses...)
|
||||
}
|
||||
|
||||
if !db.DryRun && db.Error == nil {
|
||||
isDryRun := !db.DryRun && db.Error == nil
|
||||
if !isDryRun {
|
||||
return
|
||||
}
|
||||
|
||||
if ok, mode := hasReturning(db, supportReturning); ok {
|
||||
ok, mode := hasReturning(db, supportReturning)
|
||||
if ok {
|
||||
if c, ok := db.Statement.Clauses["ON CONFLICT"]; ok {
|
||||
if onConflict, _ := c.Expression.(clause.OnConflict); onConflict.DoNothing {
|
||||
mode |= gorm.ScanOnConflictDoNothing
|
||||
}
|
||||
}
|
||||
if rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...); db.AddError(err) == nil {
|
||||
|
||||
rows, err := db.Statement.ConnPool.QueryContext(
|
||||
db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...,
|
||||
)
|
||||
if db.AddError(err) == nil {
|
||||
gorm.Scan(rows, db, mode)
|
||||
rows.Close()
|
||||
}
|
||||
} else {
|
||||
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
result, err := db.Statement.ConnPool.ExecContext(
|
||||
db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...,
|
||||
)
|
||||
if err != nil {
|
||||
db.AddError(err)
|
||||
return
|
||||
|
@ -87,8 +99,15 @@ func Create(config *Config) func(db *gorm.DB) {
|
|||
|
||||
db.RowsAffected, _ = result.RowsAffected()
|
||||
if db.RowsAffected != 0 && db.Statement.Schema != nil &&
|
||||
db.Statement.Schema.PrioritizedPrimaryField != nil && db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue {
|
||||
if insertID, err := result.LastInsertId(); err == nil && insertID > 0 {
|
||||
db.Statement.Schema.PrioritizedPrimaryField != nil &&
|
||||
db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue {
|
||||
insertID, err := result.LastInsertId()
|
||||
insertOk := err == nil && insertID > 0
|
||||
if !insertOk {
|
||||
db.AddError(err)
|
||||
return
|
||||
}
|
||||
|
||||
switch db.Statement.ReflectValue.Kind() {
|
||||
case reflect.Slice, reflect.Array:
|
||||
if config.LastInsertIDReversed {
|
||||
|
@ -118,15 +137,11 @@ func Create(config *Config) func(db *gorm.DB) {
|
|||
}
|
||||
}
|
||||
case reflect.Struct:
|
||||
if _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.ReflectValue); isZero {
|
||||
_, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.ReflectValue)
|
||||
if isZero {
|
||||
db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.ReflectValue, insertID)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
db.AddError(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,11 +26,20 @@ func BeforeDelete(db *gorm.DB) {
|
|||
func DeleteBeforeAssociations(db *gorm.DB) {
|
||||
if db.Error == nil && db.Statement.Schema != nil {
|
||||
selectColumns, restricted := db.Statement.SelectAndOmitColumns(true, false)
|
||||
if !restricted {
|
||||
return
|
||||
}
|
||||
|
||||
if restricted {
|
||||
for column, v := range selectColumns {
|
||||
if v {
|
||||
if rel, ok := db.Statement.Schema.Relationships.Relations[column]; ok {
|
||||
if !v {
|
||||
continue
|
||||
}
|
||||
|
||||
rel, ok := db.Statement.Schema.Relationships.Relations[column]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
switch rel.Type {
|
||||
case schema.HasOne, schema.HasMany:
|
||||
queryConds := rel.ToQueryConditions(db.Statement.ReflectValue)
|
||||
|
@ -46,8 +55,8 @@ func DeleteBeforeAssociations(db *gorm.DB) {
|
|||
for _, s := range db.Statement.Selects {
|
||||
if s == clause.Associations {
|
||||
selects = append(selects, s)
|
||||
} else if strings.HasPrefix(s, column+".") {
|
||||
selects = append(selects, strings.TrimPrefix(s, column+"."))
|
||||
} else if columnPrefix := column + "."; strings.HasPrefix(s, columnPrefix) {
|
||||
selects = append(selects, strings.TrimPrefix(s, columnPrefix))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,11 +72,9 @@ func DeleteBeforeAssociations(db *gorm.DB) {
|
|||
}
|
||||
}
|
||||
|
||||
if !withoutConditions {
|
||||
if db.AddError(tx.Clauses(clause.Where{Exprs: queryConds}).Delete(modelValue).Error) != nil {
|
||||
if !withoutConditions && db.AddError(tx.Clauses(clause.Where{Exprs: queryConds}).Delete(modelValue).Error) != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
case schema.Many2Many:
|
||||
var (
|
||||
queryConds = make([]clause.Expression, 0, len(rel.References))
|
||||
|
@ -99,9 +106,7 @@ func DeleteBeforeAssociations(db *gorm.DB) {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -145,7 +145,13 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
|||
fieldValues[idx], _ = field.ValueOf(elem)
|
||||
}
|
||||
|
||||
if datas, ok := identityMap[utils.ToStringKey(fieldValues...)]; ok {
|
||||
datas, ok := identityMap[utils.ToStringKey(fieldValues...)]
|
||||
if !ok {
|
||||
db.AddError(fmt.Errorf("failed to assign association %#v, make sure foreign fields exists",
|
||||
elem.Interface()))
|
||||
continue
|
||||
}
|
||||
|
||||
for _, data := range datas {
|
||||
reflectFieldValue := rel.Field.ReflectValueOf(data)
|
||||
if reflectFieldValue.Kind() == reflect.Ptr && reflectFieldValue.IsNil() {
|
||||
|
@ -155,7 +161,7 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
|||
reflectFieldValue = reflect.Indirect(reflectFieldValue)
|
||||
switch reflectFieldValue.Kind() {
|
||||
case reflect.Struct:
|
||||
rel.Field.Set(data, reflectResults.Index(i).Interface())
|
||||
rel.Field.Set(data, elem.Interface())
|
||||
case reflect.Slice, reflect.Array:
|
||||
if reflectFieldValue.Type().Elem().Kind() == reflect.Ptr {
|
||||
rel.Field.Set(data, reflect.Append(reflectFieldValue, elem).Interface())
|
||||
|
@ -164,8 +170,5 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
|||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
db.AddError(fmt.Errorf("failed to assign association %#v, make sure foreign fields exists", elem.Interface()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue