forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx.go
46 lines (38 loc) · 959 Bytes
/
tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package pop
import (
"context"
"math/rand"
"time"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// Tx stores a transaction with an ID to keep track.
type Tx struct {
ID int
*sqlx.Tx
}
func newTX(ctx context.Context, db *dB) (*Tx, error) {
t := &Tx{
ID: rand.Int(),
}
tx, err := db.BeginTxx(ctx, nil)
t.Tx = tx
return t, errors.Wrap(err, "could not create new transaction")
}
// TransactionContext simply returns the current transaction,
// this is defined so it implements the `Store` interface.
func (tx *Tx) TransactionContext(ctx context.Context) (*Tx, error) {
return tx, nil
}
// Transaction simply returns the current transaction,
// this is defined so it implements the `Store` interface.
func (tx *Tx) Transaction() (*Tx, error) {
return tx, nil
}
// Close does nothing. This is defined so it implements the `Store` interface.
func (tx *Tx) Close() error {
return nil
}