-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransaction.go
More file actions
50 lines (40 loc) · 864 Bytes
/
transaction.go
File metadata and controls
50 lines (40 loc) · 864 Bytes
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
47
48
49
50
package goquery
import (
"context"
"database/sql"
"errors"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jmoiron/sqlx"
)
type TransactionFunction func(Tx)
var NoTx *Tx = nil
type Tx struct {
tx interface{}
}
func (t Tx) PgxTx() *pgxpool.Tx {
return t.tx.(*pgxpool.Tx)
}
func (t Tx) SqlXTx() *sqlx.Tx {
return t.tx.(*sqlx.Tx)
}
func (t Tx) SqlTx() *sql.Tx {
return t.tx.(*sql.Tx)
}
func (t Tx) Rollback() error {
switch t.tx.(type) {
case *sqlx.Tx:
return t.tx.(*sqlx.Tx).Rollback()
case *pgxpool.Tx:
return t.tx.(*pgxpool.Tx).Rollback(context.Background())
}
return errors.New("invalid transaction type")
}
func (t Tx) Commit() error {
switch t.tx.(type) {
case *sqlx.Tx:
return t.tx.(*sqlx.Tx).Commit()
case *pgxpool.Tx:
return t.tx.(*pgxpool.Tx).Commit(context.Background())
}
return errors.New("invalid transaction type")
}