github.com/extrame/fabric-ca@v2.0.0-alpha+incompatible/lib/server/db/tx.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package db 8 9 import ( 10 "database/sql" 11 "time" 12 13 "github.com/jmoiron/sqlx" 14 ) 15 16 //go:generate counterfeiter -o mocks/fabricCATX.go -fake-name FabricCATx . FabricCATx 17 18 // FabricCATx is the interface with functions implemented by sqlx.Tx 19 // object that are used by Fabric CA server 20 type FabricCATx interface { 21 Select(funcName string, dest interface{}, query string, args ...interface{}) error 22 Exec(funcName, query string, args ...interface{}) (sql.Result, error) 23 Queryx(funcName, query string, args ...interface{}) (*sqlx.Rows, error) 24 Get(funcName string, dest interface{}, query string, args ...interface{}) error 25 Rebind(query string) string 26 Commit(funcName string) error 27 Rollback(funcName string) error 28 } 29 30 //go:generate counterfeiter -o mocks/sqlxTx.go -fake-name SqlxTx . SqlxTx 31 32 // SqlxTx is the contract with sqlx 33 type SqlxTx interface { 34 Queryx(query string, args ...interface{}) (*sqlx.Rows, error) 35 Get(dest interface{}, query string, args ...interface{}) error 36 Select(dest interface{}, query string, args ...interface{}) error 37 Rebind(query string) string 38 Exec(query string, args ...interface{}) (sql.Result, error) 39 Commit() error 40 Rollback() error 41 } 42 43 type record interface { 44 recordMetric(startTime time.Time, funcName, dbapiName string) 45 } 46 47 // TX is the database transaction 48 type TX struct { 49 TX SqlxTx 50 Record record 51 } 52 53 // Select performs select sql statement 54 func (tx *TX) Select(funcName string, dest interface{}, query string, args ...interface{}) error { 55 startTime := time.Now() 56 err := tx.TX.Select(dest, query, args...) 57 tx.Record.recordMetric(startTime, funcName, "Select") 58 return err 59 } 60 61 // Exec executes query 62 func (tx *TX) Exec(funcName, query string, args ...interface{}) (sql.Result, error) { 63 startTime := time.Now() 64 res, err := tx.TX.Exec(query, args...) 65 tx.Record.recordMetric(startTime, funcName, "Exec") 66 return res, err 67 } 68 69 // Get executes query 70 func (tx *TX) Get(funcName string, dest interface{}, query string, args ...interface{}) error { 71 startTime := time.Now() 72 err := tx.TX.Get(dest, query, args...) 73 tx.Record.recordMetric(startTime, funcName, "Get") 74 return err 75 } 76 77 // Queryx executes query 78 func (tx *TX) Queryx(funcName, query string, args ...interface{}) (*sqlx.Rows, error) { 79 startTime := time.Now() 80 rows, err := tx.TX.Queryx(query, args...) 81 tx.Record.recordMetric(startTime, funcName, "Queryx") 82 return rows, err 83 } 84 85 // Rebind rebinds the query 86 func (tx *TX) Rebind(query string) string { 87 return tx.TX.Rebind(query) 88 } 89 90 // Commit commits the transaction 91 func (tx *TX) Commit(funcName string) error { 92 startTime := time.Now() 93 err := tx.TX.Commit() 94 tx.Record.recordMetric(startTime, funcName, "Commit") 95 return err 96 } 97 98 // Rollback roll backs the transaction 99 func (tx *TX) Rollback(funcName string) error { 100 startTime := time.Now() 101 err := tx.TX.Rollback() 102 tx.Record.recordMetric(startTime, funcName, "Rollback") 103 return err 104 }