Fix more LastInsertId calls (#101892)

* Fix more places where LastInsertId is used without proper directive for Spanner.

* Fix tests.
This commit is contained in:
Peter Štibraný
2025-03-11 09:03:03 +01:00
committed by GitHub
parent 11a95a0b80
commit fa809ac417
3 changed files with 13 additions and 7 deletions
+5 -4
View File
@@ -101,19 +101,20 @@ func (gtx *SessionTx) ExecWithReturningId(ctx context.Context, query string, arg
}
func execWithReturningId(ctx context.Context, driverName string, query string, sess Session, args ...any) (int64, error) {
supported := false
var id int64
if driverName == "postgres" {
query = fmt.Sprintf("%s RETURNING id", query)
supported = true
}
if supported {
err := sess.Get(ctx, &id, query, args...)
if err != nil {
return id, err
}
return id, nil
} else {
if driverName == "spanner" {
// LastInsertId requires THEN RETURN directive.
query = fmt.Sprintf("%s THEN RETURN id", query)
}
res, err := sess.Exec(ctx, query, args...)
if err != nil {
return id, err