From 81592af770fa9637cad3976763def778eec44be4 Mon Sep 17 00:00:00 2001 From: gamray Date: Mon, 13 May 2024 00:32:54 +0200 Subject: [PATCH] Using transaction instead of normal handle to do multiple queries --- db/savedata.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/db/savedata.go b/db/savedata.go index 2f1e23f..1f407c9 100644 --- a/db/savedata.go +++ b/db/savedata.go @@ -166,6 +166,11 @@ func RetrieveAccountEggs(uuid []byte) ([]defs.EggData, error) { } func UpdateAccountEggs(uuid []byte, eggs []defs.EggData) error { + tx, err := handle.Begin() + if err != nil { + return err + } + for _, egg := range eggs { // TODO: find a fix to enforce encoding from body to EggData only if // it respects the EggData struct so we can get rid of the test @@ -173,7 +178,7 @@ func UpdateAccountEggs(uuid []byte, eggs []defs.EggData) error { continue } - _, err = handle.Exec(`INSERT INTO eggs (uuid, owner, gachaType, hatchWaves, timestamp) + _, err = tx.Exec(`INSERT INTO eggs (uuid, owner, gachaType, hatchWaves, timestamp) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE hatchWaves = ?`, egg.Id, uuid, egg.GachaType, egg.HatchWaves, egg.Timestamp, egg.HatchWaves) @@ -182,6 +187,11 @@ func UpdateAccountEggs(uuid []byte, eggs []defs.EggData) error { } } + err = tx.Commit() + if err != nil { + return err + } + return nil }