forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
162 lines (135 loc) · 2.72 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package pg
import "os"
// When true Tx does not issue BEGIN, COMMIT, and ROLLBACK.
// It is primarily useful for testing and can be enabled with
// GO_PG_NO_TX environment variable.
var noTx bool
func init() {
noTx = os.Getenv("GO_PG_NO_TX") != ""
}
// Not thread-safe.
type Tx struct {
db *DB
_cn *conn
err error
done bool
}
func (db *DB) Begin() (*Tx, error) {
cn, err := db.conn()
if err != nil {
return nil, err
}
tx := &Tx{
db: db,
_cn: cn,
}
if !noTx {
if _, err := tx.Exec("BEGIN"); err != nil {
tx.close()
return nil, err
}
}
return tx, nil
}
// RunInTransaction runs a function in a transaction. If function
// returns an error transaction is rollbacked, otherwise transaction
// is committed.
func (db *DB) RunInTransaction(fn func(*Tx) error) error {
tx, err := db.Begin()
if err != nil {
return err
}
if err := fn(tx); err != nil {
tx.Rollback()
return err
}
return tx.Commit()
}
func (tx *Tx) conn() *conn {
tx._cn.SetReadTimeout(tx.db.opt.ReadTimeout)
tx._cn.SetWriteTimeout(tx.db.opt.WriteTimeout)
return tx._cn
}
// TODO(vmihailenco): track and close prepared statements
func (tx *Tx) Prepare(q string) (*Stmt, error) {
if tx.done {
return nil, errTxDone
}
cn := tx.conn()
return prepare(tx.db, cn, q)
}
func (tx *Tx) Exec(q string, args ...interface{}) (Result, error) {
if tx.done {
return nil, errTxDone
}
cn := tx.conn()
res, err := simpleQuery(cn, q, args...)
if err != nil {
tx.setErr(err)
return nil, err
}
return res, nil
}
func (tx *Tx) ExecOne(q string, args ...interface{}) (Result, error) {
res, err := tx.Exec(q, args...)
if err != nil {
return nil, err
}
return assertOneAffected(res, nil)
}
func (tx *Tx) Query(coll interface{}, q string, args ...interface{}) (Result, error) {
if tx.done {
return nil, errTxDone
}
cn := tx.conn()
res, err := simpleQueryData(cn, coll, q, args...)
if err != nil {
tx.setErr(err)
return nil, err
}
return res, nil
}
func (tx *Tx) QueryOne(record interface{}, q string, args ...interface{}) (Result, error) {
coll := &singleRecordCollection{record: record}
res, err := tx.Query(coll, q, args...)
if err != nil {
return nil, err
}
return assertOneAffected(res, coll)
}
func (tx *Tx) Commit() (err error) {
if tx.done {
return errTxDone
}
if !noTx {
_, err = tx.Exec("COMMIT")
if err != nil {
tx.setErr(err)
}
}
tx.close()
return err
}
func (tx *Tx) Rollback() (err error) {
if tx.done {
return errTxDone
}
if !noTx {
_, err = tx.Exec("ROLLBACK")
if err != nil {
tx.setErr(err)
}
}
tx.close()
return err
}
func (tx *Tx) setErr(e error) {
tx.err = e
}
func (tx *Tx) close() error {
if tx.done {
return nil
}
tx.done = true
return tx.db.freeConn(tx._cn, tx.err)
}