-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
77 lines (63 loc) · 1.97 KB
/
transaction.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
package sage
import (
"context"
"database/sql"
)
// Transaction represents a database transaction
type Transaction struct {
tx *sql.Tx
}
// Commit commits the transaction
func (t *Transaction) Commit() error {
return t.tx.Commit()
}
// Rollback aborts the transaction
func (t *Transaction) Rollback() error {
return t.tx.Rollback()
}
// Exec executes a query without returning any rows
func (t *Transaction) Exec(query string, args ...interface{}) (sql.Result, error) {
return t.tx.Exec(query, args...)
}
// ExecContext executes a query without returning any rows
func (t *Transaction) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
return t.tx.ExecContext(ctx, query, args...)
}
// Query executes a query that returns rows
func (t *Transaction) Query(query string, args ...interface{}) (*sql.Rows, error) {
return t.tx.Query(query, args...)
}
// QueryContext executes a query that returns rows
func (t *Transaction) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
return t.tx.QueryContext(ctx, query, args...)
}
// QueryRow executes a query that returns a single row
func (t *Transaction) QueryRow(query string, args ...interface{}) *sql.Row {
return t.tx.QueryRow(query, args...)
}
// QueryRowContext executes a query that returns a single row
func (t *Transaction) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
return t.tx.QueryRowContext(ctx, query, args...)
}
// WithTransaction runs a function within a transaction
func (c *Connection) WithTransaction(ctx context.Context, fn func(*Transaction) error) error {
tx, err := c.BeginTx(ctx, nil)
if err != nil {
return err
}
defer func() {
if p := recover(); p != nil {
// Rollback on panic
_ = tx.Rollback()
panic(p) // Re-throw panic after rollback
} else if err != nil {
// Rollback on error
_ = tx.Rollback()
}
}()
err = fn(tx)
if err != nil {
return err
}
return tx.Commit()
}