Skip to content

Commit

Permalink
Merge pull request #631 from upper/experimental-check-record-before-c…
Browse files Browse the repository at this point in the history
…reate

experimental: check if record exists before update
  • Loading branch information
xiam authored Jun 29, 2021
2 parents 35edd0b + a813cb0 commit 2158231
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions internal/sqladapter/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,27 +361,30 @@ func (sess *session) Save(record db.Record) error {
}

store := record.Store(sess)

if saver, ok := store.(db.StoreSaver); ok {
return saver.Save(record)
}

_, fields, err := recordPrimaryKeyFieldValues(store, record)
id := db.Cond{}
keys, values, err := recordPrimaryKeyFieldValues(store, record)
if err != nil {
return err
}
isCreate := true
for i := range fields {
if fields[i] != reflect.Zero(reflect.TypeOf(fields[i])).Interface() {
isCreate = false
break
for i := range values {
if values[i] != reflect.Zero(reflect.TypeOf(values[i])).Interface() {
id[keys[i]] = values[i]
}
}

if isCreate {
return recordCreate(store, record)
if len(id) > 0 && len(id) == len(values) {
// check if record exists before updating it
exists, _ := store.Find(id).Count()
if exists > 0 {
return recordUpdate(store, record)
}
}
return recordUpdate(store, record)

return recordCreate(store, record)
}

func (sess *session) Delete(record db.Record) error {
Expand Down

0 comments on commit 2158231

Please sign in to comment.