Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
Andrew Thornton 2023-03-14 21:03:48 +00:00
parent b4467b717a
commit 4bf7c4d738
No known key found for this signature in database
GPG Key ID: 3CDE74631F13A748
2 changed files with 9 additions and 7 deletions

View File

@ -94,6 +94,9 @@ func (statement *Statement) GenUpsertSQL(doUpdate bool, columns []string, args [
for _, column := range updateColumns[1:] {
write(", ", column, " = VALUES(", column, ")")
}
if len(table.AutoIncrement) > 0 {
write(", ", quote(table.AutoIncrement), " = LAST_INSERT_ID(", quote(table.AutoIncrement), ")")
}
}
default:
return "", nil, fmt.Errorf("unimplemented") // FIXME: UPSERT
@ -282,7 +285,7 @@ func (statement *Statement) GenUpsertMapSQL(doUpdate bool, columns []string, arg
write(", ", column, " = VALUES(", column, ")")
}
if len(table.AutoIncrement) > 0 {
write(", ", quote(table.AutoIncrement), " = ", quote(table.AutoIncrement))
write(", ", quote(table.AutoIncrement), " = LAST_INSERT_ID(", quote(table.AutoIncrement), ")")
}
}
default:

View File

@ -212,8 +212,6 @@ func (session *Session) upsertStruct(doUpdate bool, bean interface{}) (int64, er
session.incrVersionFieldValue(verValue)
}
}
id, iderr := res.LastInsertId()
n, err := res.RowsAffected()
if err != nil || n == 0 {
return 0, err
@ -224,14 +222,15 @@ func (session *Session) upsertStruct(doUpdate bool, bean interface{}) (int64, er
n = 1
}
if iderr != nil || id <= 0 {
return n, err
}
if table.AutoIncrement == "" {
return n, nil
}
id, err := res.LastInsertId()
if err != nil || id <= 0 {
return n, err
}
aiValue, err := table.AutoIncrColumn().ValueOf(bean)
if err != nil {
session.engine.logger.Errorf("%v", err)