Skip to content

Commit

Permalink
feat: ajust if logic
Browse files Browse the repository at this point in the history
  • Loading branch information
daheige committed Oct 9, 2021
1 parent 8f8809f commit 6001a47
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions callbacks/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,41 @@ func BeforeUpdate(db *gorm.DB) {
}

func Update(db *gorm.DB) {
if db.Error == nil {
if db.Statement.Schema != nil && !db.Statement.Unscoped {
for _, c := range db.Statement.Schema.UpdateClauses {
db.Statement.AddClause(c)
}
}

if db.Statement.SQL.String() == "" {
db.Statement.SQL.Grow(180)
db.Statement.AddClauseIfNotExists(clause.Update{})
set := ConvertToAssignments(db.Statement)
if len(set) == 0 {
return
}
if db.Error != nil {
return
}

db.Statement.AddClause(set)
db.Statement.Build(db.Statement.BuildClauses...)
if db.Statement.Schema != nil && !db.Statement.Unscoped {
for _, c := range db.Statement.Schema.UpdateClauses {
db.Statement.AddClause(c)
}
}

if _, ok := db.Statement.Clauses["WHERE"]; !db.AllowGlobalUpdate && !ok {
db.AddError(gorm.ErrMissingWhereClause)
if db.Statement.SQL.String() == "" {
db.Statement.SQL.Grow(180)
db.Statement.AddClauseIfNotExists(clause.Update{})
set := ConvertToAssignments(db.Statement)
if len(set) == 0 {
return
}

if !db.DryRun && db.Error == nil {
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
if err != nil {
db.AddError(err)
return
}
db.Statement.AddClause(set)
db.Statement.Build(db.Statement.BuildClauses...)
}

if _, ok := db.Statement.Clauses["WHERE"]; !db.AllowGlobalUpdate && !ok {
db.AddError(gorm.ErrMissingWhereClause)
return
}

db.RowsAffected, _ = result.RowsAffected()
if !db.DryRun && db.Error == nil {
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
if err != nil {
db.AddError(err)
return
}

db.RowsAffected, _ = result.RowsAffected()
}
}

Expand Down

0 comments on commit 6001a47

Please sign in to comment.