From 3d14ed0296c6b6366334b30d28d2c230e352918e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Garcia?= Date: Mon, 8 Mar 2021 16:11:59 -0300 Subject: [PATCH] Add an example call to the Transaction function on README --- README.md | 27 +++++++++++++++++++++++++++ examples/crud/crud.go | 27 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/README.md b/README.md index 445b5fe..9f14b9b 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,33 @@ func main() { if err != nil { panic(err.Error()) } + + // Making transactions: + err = db.Transaction(ctx, func(db kisssql.SQLProvider) error { + var cris2 User + err = db.QueryOne(ctx, &cris2, "SELECT * FROM users WHERE id = ?", cris.ID) + if err != nil { + // This will cause an automatic rollback: + return err + } + + err = db.Update(ctx, PartialUpdateUser{ + ID: cris2.ID, + Age: nullable.Int(29), + }) + if err != nil { + // This will also cause an automatic rollback and then panic again + // so that we don't hide the panic inside the KissSQL library + panic(err.Error()) + } + + // Commits the transaction + return nil + }) + if err != nil { + panic(err.Error()) + } + fmt.Printf("Users: %#v\n", users) } ``` diff --git a/examples/crud/crud.go b/examples/crud/crud.go index 555e313..9822dcd 100644 --- a/examples/crud/crud.go +++ b/examples/crud/crud.go @@ -132,5 +132,32 @@ func main() { if err != nil { panic(err.Error()) } + + // Making transactions: + err = db.Transaction(ctx, func(db kisssql.SQLProvider) error { + var cris2 User + err = db.QueryOne(ctx, &cris2, "SELECT * FROM users WHERE id = ?", cris.ID) + if err != nil { + // This will cause an automatic rollback: + return err + } + + err = db.Update(ctx, PartialUpdateUser{ + ID: cris2.ID, + Age: nullable.Int(29), + }) + if err != nil { + // This will also cause an automatic rollback and then panic again + // so that we don't hide the panic inside the KissSQL library + panic(err.Error()) + } + + // Commits the transaction + return nil + }) + if err != nil { + panic(err.Error()) + } + fmt.Printf("Users: %#v\n", users) }