forked from hyperledger/fabric-ca
/
tx.go
104 lines (87 loc) · 2.89 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package db
import (
"database/sql"
"time"
"github.com/jmoiron/sqlx"
)
//go:generate counterfeiter -o mocks/fabricCATX.go -fake-name FabricCATx . FabricCATx
// FabricCATx is the interface with functions implemented by sqlx.Tx
// object that are used by Fabric CA server
type FabricCATx interface {
Select(funcName string, dest interface{}, query string, args ...interface{}) error
Exec(funcName, query string, args ...interface{}) (sql.Result, error)
Queryx(funcName, query string, args ...interface{}) (*sqlx.Rows, error)
Get(funcName string, dest interface{}, query string, args ...interface{}) error
Rebind(query string) string
Commit(funcName string) error
Rollback(funcName string) error
}
//go:generate counterfeiter -o mocks/sqlxTx.go -fake-name SqlxTx . SqlxTx
// SqlxTx is the contract with sqlx
type SqlxTx interface {
Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
Get(dest interface{}, query string, args ...interface{}) error
Select(dest interface{}, query string, args ...interface{}) error
Rebind(query string) string
Exec(query string, args ...interface{}) (sql.Result, error)
Commit() error
Rollback() error
}
type record interface {
recordMetric(startTime time.Time, funcName, dbapiName string)
}
// TX is the database transaction
type TX struct {
TX SqlxTx
Record record
}
// Select performs select sql statement
func (tx *TX) Select(funcName string, dest interface{}, query string, args ...interface{}) error {
startTime := time.Now()
err := tx.TX.Select(dest, query, args...)
tx.Record.recordMetric(startTime, funcName, "Select")
return err
}
// Exec executes query
func (tx *TX) Exec(funcName, query string, args ...interface{}) (sql.Result, error) {
startTime := time.Now()
res, err := tx.TX.Exec(query, args...)
tx.Record.recordMetric(startTime, funcName, "Exec")
return res, err
}
// Get executes query
func (tx *TX) Get(funcName string, dest interface{}, query string, args ...interface{}) error {
startTime := time.Now()
err := tx.TX.Get(dest, query, args...)
tx.Record.recordMetric(startTime, funcName, "Get")
return err
}
// Queryx executes query
func (tx *TX) Queryx(funcName, query string, args ...interface{}) (*sqlx.Rows, error) {
startTime := time.Now()
rows, err := tx.TX.Queryx(query, args...)
tx.Record.recordMetric(startTime, funcName, "Queryx")
return rows, err
}
// Rebind rebinds the query
func (tx *TX) Rebind(query string) string {
return tx.TX.Rebind(query)
}
// Commit commits the transaction
func (tx *TX) Commit(funcName string) error {
startTime := time.Now()
err := tx.TX.Commit()
tx.Record.recordMetric(startTime, funcName, "Commit")
return err
}
// Rollback roll backs the transaction
func (tx *TX) Rollback(funcName string) error {
startTime := time.Now()
err := tx.TX.Rollback()
tx.Record.recordMetric(startTime, funcName, "Rollback")
return err
}