-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtx.go
297 lines (231 loc) · 5.98 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package hermes
import (
"context"
"database/sql"
"errors"
"github.com/jmoiron/sqlx"
)
var (
// ErrBadContext returned when the caller attempts to reset the context.
ErrBadContext = errors.New("context mismatch")
// ErrTxRolledBack returned by calls to the transaction if it has been
// rolled back.
ErrTxRolledBack = errors.New("transaction rolled back")
// ErrTxCommitted returned if the caller tries to rollback then
// commit a transaction in the same function.
ErrTxCommitted = errors.New("already committed")
)
const (
_pending = iota
_rollback
_commit
)
// Tx wraps a sqlx.Tx transaction. Tracks context.
type Tx struct {
db *DB
ctx context.Context
internal *sqlx.Tx
current int // current state
history []int // past states
rollback bool // is the transaction being rolled back?
timer *txTimer // if TxTimeout is set, reports when Tx existence exceeds timeout
}
// BaseDB returns the base database connection.
func (tx *Tx) BaseDB() *sqlx.DB {
return tx.db.BaseDB()
}
// BaseTx returns the internal sqlx transaction.
func (tx *Tx) BaseTx() *sqlx.Tx {
return tx.internal
}
// Context returns the context associated with this transaction.
func (tx *Tx) Context() context.Context {
return tx.ctx
}
// Begin a new transaction. Returns a Conn wrapping the transaction
// (*sqlx.Tx).
func (tx *Tx) Begin() (Conn, error) {
if tx.rollback {
return nil, ErrTxRolledBack
}
tx.push()
return tx, nil
}
// BeginCtx begins a new transaction in context. The Conn will have the context
// associated with it and use it for all subsequent commands.
func (tx *Tx) BeginCtx(ctx context.Context) (Conn, error) {
if tx.rollback {
return nil, ErrTxRolledBack
}
if tx.ctx != nil && tx.ctx != ctx {
return nil, ErrBadContext
}
tx.ctx = ctx
tx.push()
return tx, nil
}
// Exec executes a database statement with no results..
func (tx *Tx) Exec(query string, args ...interface{}) (sql.Result, error) {
if err := tx.ok(); err != nil {
return nil, err
}
var res sql.Result
var err error
if tx.ctx != nil {
res, err = tx.internal.ExecContext(tx.ctx, query, args...)
} else {
res, err = tx.internal.Exec(query, args...)
}
return res, tx.check(err)
}
// Query the database.
func (tx *Tx) Query(query string, args ...interface{}) (*sqlx.Rows, error) {
if err := tx.ok(); err != nil {
return nil, err
}
var rows *sqlx.Rows
var err error
if tx.ctx != nil {
rows, err = tx.internal.QueryxContext(tx.ctx, query, args...)
} else {
rows, err = tx.internal.Queryx(query, args...)
}
return rows, tx.check(err)
}
// Row queries the databsae for a single row.
func (tx *Tx) Row(query string, args ...interface{}) (*sqlx.Row, error) {
if err := tx.ok(); err != nil {
return nil, err
}
var row *sqlx.Row
if tx.ctx != nil {
row = tx.internal.QueryRowxContext(tx.ctx, query, args...)
} else {
row = tx.internal.QueryRowx(query, args...)
}
if row.Err() != nil {
return nil, tx.check(row.Err())
}
return row, nil
}
// Prepare a database query.
func (tx *Tx) Prepare(query string) (*sqlx.Stmt, error) {
if err := tx.ok(); err != nil {
return nil, err
}
// TODO: No PreparexContext?
//
// if tx.ctx != nil {
// return tx.internal.PreparexContext(tx.ctx, query, args...)
// }
stmt, err := tx.internal.Preparex(query)
return stmt, tx.check(err)
}
// Get a single record from the database, e.g. "SELECT ... LIMIT 1".
func (tx *Tx) Get(dest interface{}, query string, args ...interface{}) error {
if err := tx.ok(); err != nil {
return err
}
if tx.ctx != nil {
return tx.check(tx.internal.GetContext(tx.ctx, dest, query, args...))
}
return tx.check(tx.internal.Get(dest, query, args...))
}
// Select a collection record from the database.
func (tx *Tx) Select(dest interface{}, query string, args ...interface{}) error {
if err := tx.ok(); err != nil {
return err
}
if tx.ctx != nil {
return tx.check(tx.internal.SelectContext(tx.ctx, dest, query, args...))
}
return tx.check(tx.internal.Select(dest, query, args...))
}
// Commit the current transaction. Returns ErrTxRolledBack if the transaction
// was already rolled back, or ErrTxCommitted if it was committed.
func (tx *Tx) Commit() error {
if tx.rollback {
return ErrTxRolledBack
}
if tx.current == _commit {
return ErrTxCommitted
}
if len(tx.history) == 0 {
if err := tx.internal.Commit(); err != nil {
return tx.check(err)
}
}
tx.current = _commit
return nil
}
// Rollback the transaction. Ignored if the transaction is already in a
// rollback. Returns ErrTxCommitted if the transaction was committed.
func (tx *Tx) Rollback() error {
if tx.rollback {
return nil
}
if tx.current == _commit {
return ErrTxCommitted
}
if err := tx.internal.Rollback(); err != nil {
return tx.check(err)
}
tx.current = _rollback
tx.rollback = true
tx.pop()
return nil
}
// Close will automatically rollback a transaction if it hasn't been committed.
func (tx *Tx) Close() error {
if tx.current == _rollback || tx.current == _commit {
tx.pop()
return nil
}
if err := tx.internal.Rollback(); err != nil {
tx.pop()
if err == sql.ErrTxDone {
tx.current = _rollback
return nil
}
return tx.check(err)
}
tx.current = _rollback
tx.rollback = true
tx.pop()
return nil
}
// RolledBack returns true if the transaction was rolled back.
func (tx *Tx) RolledBack() bool {
return tx.rollback
}
// Name returns the datasource name for this connection
func (tx *Tx) Name() string {
return tx.db.name
}
// Confirm the transaction is viable before executing a query.
func (tx *Tx) ok() error {
if tx.rollback {
return ErrTxRolledBack
}
if tx.current == _commit {
return ErrTxCommitted
}
return nil
}
func (tx *Tx) push() {
tx.history = append(tx.history, tx.current)
tx.current = _pending
}
func (tx *Tx) pop() {
if len(tx.history) == 0 {
if tx.timer != nil {
tx.timer.stop()
tx.timer = nil
}
return
}
tx.current, tx.history = tx.history[len(tx.history)-1], tx.history[:len(tx.history)-1]
}
func (tx *Tx) check(err error) error {
return tx.db.check(err)
}