Add an example call to the Transaction function on README

pull/2/head
Vinícius Garcia 2021-03-08 16:11:59 -03:00
parent 1972ccd989
commit 3d14ed0296
2 changed files with 54 additions and 0 deletions

View File

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

View File

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