Update update hook
(config *Config)
| 54 | |
| 55 | // Update update hook |
| 56 | func Update(config *Config) func(db *gorm.DB) { |
| 57 | supportReturning := utils.Contains(config.UpdateClauses, "RETURNING") |
| 58 | |
| 59 | return func(db *gorm.DB) { |
| 60 | if db.Error != nil { |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | if db.Statement.Schema != nil { |
| 65 | for _, c := range db.Statement.Schema.UpdateClauses { |
| 66 | db.Statement.AddClause(c) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if db.Statement.SQL.Len() == 0 { |
| 71 | db.Statement.SQL.Grow(180) |
| 72 | db.Statement.AddClauseIfNotExists(clause.Update{}) |
| 73 | if _, ok := db.Statement.Clauses["SET"]; !ok { |
| 74 | if set := ConvertToAssignments(db.Statement); len(set) != 0 { |
| 75 | defer delete(db.Statement.Clauses, "SET") |
| 76 | db.Statement.AddClause(set) |
| 77 | } else { |
| 78 | return |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | db.Statement.Build(db.Statement.BuildClauses...) |
| 83 | } |
| 84 | |
| 85 | checkMissingWhereConditions(db) |
| 86 | |
| 87 | if !db.DryRun && db.Error == nil { |
| 88 | if ok, mode := hasReturning(db, supportReturning); ok { |
| 89 | if rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...); db.AddError(err) == nil { |
| 90 | defer func() { |
| 91 | if err := rows.Close(); err != nil { |
| 92 | _ = db.AddError(err) |
| 93 | } |
| 94 | }() |
| 95 | |
| 96 | dest := db.Statement.Dest |
| 97 | db.Statement.Dest = db.Statement.ReflectValue.Addr().Interface() |
| 98 | gorm.Scan(rows, db, mode) |
| 99 | db.Statement.Dest = dest |
| 100 | |
| 101 | if db.Statement.Result != nil { |
| 102 | db.Statement.Result.RowsAffected = db.RowsAffected |
| 103 | } |
| 104 | } |
| 105 | } else { |
| 106 | result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...) |
| 107 | |
| 108 | if db.AddError(err) == nil { |
| 109 | db.RowsAffected, _ = result.RowsAffected() |
| 110 | } |
| 111 | |
| 112 | if db.Statement.Result != nil { |
| 113 | db.Statement.Result.Result = result |
no test coverage detected