-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsqlxtxmocks.go
74 lines (63 loc) · 1.79 KB
/
sqlxtxmocks.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
package sqlxtx
import (
"database/sql"
"log"
"github.com/jmoiron/sqlx"
)
// SQLxTx is wrapper around sqlx.Tx, to change behavior in runtime between sqlx.DB and sqlx.Tx
type SQLxTx struct {
DB *sqlx.Tx
}
// Select within a transaction.
func (sdt *SQLxTx) Select(dest interface{}, query string, args ...interface{}) error {
return sdt.DB.Select(dest, query, args...)
}
// Get within a transaction.
// Any placeholder parameters are replaced with supplied args.
// An error is returned if the result set is empty.
func (sdt *SQLxTx) Get(dest interface{}, query string, args ...interface{}) error {
return sdt.DB.Get(dest, query, args...)
}
// NamedExec within a transaction.
// Any named placeholder parameters are replaced with fields from arg.
func (sdt *SQLxTx) NamedExec(query string, args interface{}) (sql.Result, error) {
return sdt.DB.NamedExec(query, args)
}
// Exec executes a query that doesn't return rows.
// For example: an INSERT and UPDATE.
func (sdt *SQLxTx) Exec(query string, args ...interface{}) (sql.Result, error) {
return sdt.DB.Exec(query, args...)
}
// Rollback aborts the transaction.
func (sdt *SQLxTx) Rollback() error {
return sdt.DB.Rollback()
}
// Commit commits the transaction.
func (sdt *SQLxTx) Commit() error {
return sdt.DB.Commit()
}
// TxEnd Finish a transaction
func (sdt *SQLxTx) TxEnd(txFn func() error, config Config) error {
var err error
tx := sdt
defer func() {
if p := recover(); p != nil {
if config.Verbose {
log.Println("Transaction Error, Exec Rollback:", p)
}
tx.Rollback()
} else if err != nil {
if config.Verbose {
log.Println("Transaction Error, Exec Rollback:", err)
}
tx.Rollback()
} else {
if config.Verbose {
log.Println("Transaction Commited:")
}
err = tx.Commit()
}
}()
err = txFn()
return err
}