From fdcf2e98067ec3501f90ae15483341dc6738dfd4 Mon Sep 17 00:00:00 2001 From: Uzziah <120019273+uzziahlin@users.noreply.github.com> Date: Sun, 2 Jul 2023 15:31:44 +0800 Subject: [PATCH] fix: modify InsertOrUpdate method (#5269) * fix: modify InsertOrUpdate method, Remove the isMulti variable and its associated code * fix: Delete unnecessary judgment branches * fix: add modify record into CHANGELOG --- CHANGELOG.md | 1 + client/orm/db.go | 12 +----------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7323e8b5..d75ac02a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - [fix: use of ioutil package](https://github.com/beego/beego/pull/5261) - [cache/redis: support skipEmptyPrefix option ](https://github.com/beego/beego/pull/5264) - [fix: refactor InsertValue method](https://github.com/beego/beego/pull/5267) +- [fix: modify InsertOrUpdate method, Remove the isMulti variable and its associated code](https://github.com/beego/beego/pull/5269) ## ORM refactoring - [introducing internal/models pkg](https://github.com/beego/beego/pull/5238) diff --git a/client/orm/db.go b/client/orm/db.go index ddb9e589..596d1667 100644 --- a/client/orm/db.go +++ b/client/orm/db.go @@ -552,7 +552,6 @@ func (d *dbBase) InsertOrUpdate(ctx context.Context, q dbQuerier, mi *models.Mod } } - isMulti := false names := make([]string, 0, len(mi.Fields.DBcols)-1) Q := d.ins.TableQuote() values, _, err := d.collectValues(mi, ind, mi.Fields.DBcols, true, true, &names, a.TZ) @@ -598,23 +597,14 @@ func (d *dbBase) InsertOrUpdate(ctx context.Context, q dbQuerier, mi *models.Mod qupdates := strings.Join(updates, ", ") columns := strings.Join(names, sep) - multi := len(values) / len(names) - - if isMulti { - qmarks = strings.Repeat(qmarks+"), (", multi-1) + qmarks - } // conflitValue maybe is a int,can`t use fmt.Sprintf query := fmt.Sprintf("INSERT INTO %s%s%s (%s%s%s) VALUES (%s) %s "+qupdates, Q, mi.Table, Q, Q, columns, Q, qmarks, iouStr) d.ins.ReplaceMarks(&query) - if isMulti || !d.ins.HasReturningID(mi, &query) { + if !d.ins.HasReturningID(mi, &query) { res, err := q.ExecContext(ctx, query, values...) if err == nil { - if isMulti { - return res.RowsAffected() - } - lastInsertId, err := res.LastInsertId() if err != nil { logs.DebugLog.Println(ErrLastInsertIdUnavailable, ':', err)