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) }