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
This commit is contained in:
Uzziah 2023-07-02 15:31:44 +08:00 committed by GitHub
parent 20a5030842
commit fdcf2e9806
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 11 deletions

View File

@ -8,6 +8,7 @@
- [fix: use of ioutil package](https://github.com/beego/beego/pull/5261) - [fix: use of ioutil package](https://github.com/beego/beego/pull/5261)
- [cache/redis: support skipEmptyPrefix option ](https://github.com/beego/beego/pull/5264) - [cache/redis: support skipEmptyPrefix option ](https://github.com/beego/beego/pull/5264)
- [fix: refactor InsertValue method](https://github.com/beego/beego/pull/5267) - [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 ## ORM refactoring
- [introducing internal/models pkg](https://github.com/beego/beego/pull/5238) - [introducing internal/models pkg](https://github.com/beego/beego/pull/5238)

View File

@ -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) names := make([]string, 0, len(mi.Fields.DBcols)-1)
Q := d.ins.TableQuote() Q := d.ins.TableQuote()
values, _, err := d.collectValues(mi, ind, mi.Fields.DBcols, true, true, &names, a.TZ) 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, ", ") qupdates := strings.Join(updates, ", ")
columns := strings.Join(names, sep) 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 // 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) 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) d.ins.ReplaceMarks(&query)
if isMulti || !d.ins.HasReturningID(mi, &query) { if !d.ins.HasReturningID(mi, &query) {
res, err := q.ExecContext(ctx, query, values...) res, err := q.ExecContext(ctx, query, values...)
if err == nil { if err == nil {
if isMulti {
return res.RowsAffected()
}
lastInsertId, err := res.LastInsertId() lastInsertId, err := res.LastInsertId()
if err != nil { if err != nil {
logs.DebugLog.Println(ErrLastInsertIdUnavailable, ':', err) logs.DebugLog.Println(ErrLastInsertIdUnavailable, ':', err)