Skip to content

Commit 49a73c8

Browse files
committed
Add brief example in README
1 parent 36bd5ea commit 49a73c8

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,56 @@
44

55
satomic is a Golang package that makes managing nested SQL transactions/savepoints easier
66

7+
## Example usage
8+
9+
```golang
10+
package main
11+
12+
import (
13+
"context"
14+
"database/sql"
15+
"github.com/dhui/satomic"
16+
"github.com/dhui/satomic/savepointers/postgres"
17+
)
18+
19+
// Error handling omitted for brevity. Actual code should handle errors
20+
func main() {
21+
ctx := context.Background()
22+
var db *sql.DB // Use an actual db
23+
24+
// savepointer should match the db driver used
25+
q, _ := satomic.NewQuerier(ctx, db, postgres.Savepointer{}, sql.TxOptions{})
26+
27+
// In transaction
28+
q.Atomic(func(ctx context.Context, q satomic.Querier) error {
29+
var dummy int
30+
q.QueryRowContext(ctx, "SELECT 1;").Scan(&dummy)
31+
32+
// In first savepoint
33+
q.Atomic(func(ctx context.Context, q satomic.Querier) error {
34+
return q.QueryRowContext(ctx, "SELECT 2;").Scan(&dummy)
35+
})
36+
37+
// In second savepoint
38+
q.Atomic(func(ctx context.Context, q satomic.Querier) error {
39+
q.QueryRowContext(ctx, "SELECT 3;").Scan(&dummy)
40+
41+
// In third savepoint
42+
q.Atomic(func(ctx context.Context, q satomic.Querier) error {
43+
q.QueryRowContext(ctx, "SELECT 4;").Scan(&dummy)
44+
return nil
45+
})
46+
47+
return nil
48+
})
49+
50+
return nil
51+
})
52+
}
53+
```
54+
55+
A more complete example can be found in the [GoDoc](https://godoc.org/github.com/dhui/satomic)
56+
757
## What's with the name?
858
Go **S**QL **atomic** => satomic
959

0 commit comments

Comments
 (0)