style: prepose error judgement

This commit is contained in:
riverchu 2021-09-03 23:09:20 +08:00 committed by Jinzhu
parent a89d4d8fd5
commit 1d9e563023
1 changed files with 26 additions and 24 deletions

View File

@ -51,37 +51,39 @@ func BeforeUpdate(db *gorm.DB) {
} }
func Update(db *gorm.DB) { func Update(db *gorm.DB) {
if db.Error == nil { if db.Error != nil {
if db.Statement.Schema != nil && !db.Statement.Unscoped { return
for _, c := range db.Statement.Schema.UpdateClauses { }
db.Statement.AddClause(c)
}
}
if db.Statement.SQL.String() == "" { if db.Statement.Schema != nil && !db.Statement.Unscoped {
db.Statement.SQL.Grow(180) for _, c := range db.Statement.Schema.UpdateClauses {
db.Statement.AddClauseIfNotExists(clause.Update{}) db.Statement.AddClause(c)
if set := ConvertToAssignments(db.Statement); len(set) != 0 {
db.Statement.AddClause(set)
} else {
return
}
db.Statement.Build(db.Statement.BuildClauses...)
} }
}
if _, ok := db.Statement.Clauses["WHERE"]; !db.AllowGlobalUpdate && !ok { if db.Statement.SQL.String() == "" {
db.AddError(gorm.ErrMissingWhereClause) db.Statement.SQL.Grow(180)
db.Statement.AddClauseIfNotExists(clause.Update{})
if set := ConvertToAssignments(db.Statement); len(set) != 0 {
db.Statement.AddClause(set)
} else {
return return
} }
db.Statement.Build(db.Statement.BuildClauses...)
}
if !db.DryRun && db.Error == nil { if _, ok := db.Statement.Clauses["WHERE"]; !db.AllowGlobalUpdate && !ok {
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...) db.AddError(gorm.ErrMissingWhereClause)
return
}
if err == nil { if !db.DryRun && db.Error == nil {
db.RowsAffected, _ = result.RowsAffected() result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
} else {
db.AddError(err) if err == nil {
} db.RowsAffected, _ = result.RowsAffected()
} else {
db.AddError(err)
} }
} }
} }