MCPcopy
hub / github.com/go-gorm/gorm / Save

Method Save

finisher_api.go:85–127  ·  view source on GitHub ↗

Save updates value in database. If value doesn't contain a matching primary key, value is inserted.

(value interface{})

Source from the content-addressed store, hash-verified

83
84// Save updates value in database. If value doesn't contain a matching primary key, value is inserted.
85func (db *DB) Save(value interface{}) (tx *DB) {
86 tx = db.getInstance()
87 tx.Statement.Dest = value
88
89 reflectValue := reflect.Indirect(reflect.ValueOf(value))
90 for reflectValue.Kind() == reflect.Ptr || reflectValue.Kind() == reflect.Interface {
91 reflectValue = reflect.Indirect(reflectValue)
92 }
93
94 switch reflectValue.Kind() {
95 case reflect.Slice, reflect.Array:
96 if _, ok := tx.Statement.Clauses["ON CONFLICT"]; !ok {
97 tx = tx.Clauses(clause.OnConflict{UpdateAll: true})
98 }
99 tx = tx.callbacks.Create().Execute(tx.Set("gorm:update_track_time", true))
100 case reflect.Struct:
101 if err := tx.Statement.Parse(value); err == nil && tx.Statement.Schema != nil {
102 for _, pf := range tx.Statement.Schema.PrimaryFields {
103 if _, isZero := pf.ValueOf(tx.Statement.Context, reflectValue); isZero {
104 return tx.callbacks.Create().Execute(tx)
105 }
106 }
107 }
108
109 fallthrough
110 default:
111 selectedUpdate := len(tx.Statement.Selects) != 0
112 // when updating, use all fields including those zero-value fields
113 if !selectedUpdate {
114 tx.Statement.Selects = append(tx.Statement.Selects, "*")
115 }
116
117 updateTx := tx.callbacks.Update().Execute(tx.Session(&Session{Initialized: true}))
118
119 if updateTx.Error == nil && updateTx.RowsAffected == 0 && !updateTx.DryRun && !selectedUpdate {
120 return tx.Session(&Session{SkipHooks: true}).Clauses(clause.OnConflict{UpdateAll: true}).Create(value)
121 }
122
123 return updateTx
124 }
125
126 return
127}
128
129// First finds the first record ordered by primary key, matching given conditions conds
130func (db *DB) First(dest interface{}, conds ...interface{}) (tx *DB) {

Callers 15

TestTransactionFunction · 0.80
TestTransactionWithBlockFunction · 0.80
TestMigrateSerialColumnFunction · 0.80
TestMigrateViewFunction · 0.80
TestRunCallbacksFunction · 0.80
TestCallbacksWithErrorsFunction · 0.80
TestConnPoolWrapperFunction · 0.80
TestUpsertWithSaveFunction · 0.80
TestFindOrInitializeFunction · 0.80
TestUpdateWithMissWhereFunction · 0.80

Calls 8

getInstanceMethod · 0.95
ClausesMethod · 0.80
ExecuteMethod · 0.80
ParseMethod · 0.80
SessionMethod · 0.80
CreateMethod · 0.65
SetMethod · 0.65
UpdateMethod · 0.65

Tested by 15

TestTransactionFunction · 0.64
TestTransactionWithBlockFunction · 0.64
TestMigrateSerialColumnFunction · 0.64
TestMigrateViewFunction · 0.64
TestRunCallbacksFunction · 0.64
TestCallbacksWithErrorsFunction · 0.64
TestConnPoolWrapperFunction · 0.64
TestUpsertWithSaveFunction · 0.64
TestFindOrInitializeFunction · 0.64
TestUpdateWithMissWhereFunction · 0.64