Skip to content

Commit

Permalink
Add brief example in README
Browse files Browse the repository at this point in the history
  • Loading branch information
dhui committed Dec 15, 2018
1 parent 36bd5ea commit 49a73c8
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,56 @@

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

## Example usage

```golang
package main

import (
"context"
"database/sql"
"github.com/dhui/satomic"
"github.com/dhui/satomic/savepointers/postgres"
)

// Error handling omitted for brevity. Actual code should handle errors
func main() {
ctx := context.Background()
var db *sql.DB // Use an actual db

// savepointer should match the db driver used
q, _ := satomic.NewQuerier(ctx, db, postgres.Savepointer{}, sql.TxOptions{})

// In transaction
q.Atomic(func(ctx context.Context, q satomic.Querier) error {
var dummy int
q.QueryRowContext(ctx, "SELECT 1;").Scan(&dummy)

// In first savepoint
q.Atomic(func(ctx context.Context, q satomic.Querier) error {
return q.QueryRowContext(ctx, "SELECT 2;").Scan(&dummy)
})

// In second savepoint
q.Atomic(func(ctx context.Context, q satomic.Querier) error {
q.QueryRowContext(ctx, "SELECT 3;").Scan(&dummy)

// In third savepoint
q.Atomic(func(ctx context.Context, q satomic.Querier) error {
q.QueryRowContext(ctx, "SELECT 4;").Scan(&dummy)
return nil
})

return nil
})

return nil
})
}
```

A more complete example can be found in the [GoDoc](https://godoc.org/github.com/dhui/satomic)

## What's with the name?
Go **S**QL **atomic** => satomic

Expand Down

0 comments on commit 49a73c8

Please sign in to comment.