make withTx part of SQL

This commit is contained in:
Victor Shyba 2021-05-20 15:14:56 -03:00
parent 070938e12a
commit 13c6be981c

View file

@ -698,14 +698,8 @@ type txFunc func(tx *sql.Tx) error
// withTx wraps a function in an sql transaction. the transaction is committed if there's no error, or rolled back if there is one.
// if dbOrTx is an sql.DB, a new transaction is started
func withTx(dbOrTx interface{}, f txFunc) (err error) {
var tx *sql.Tx
switch t := dbOrTx.(type) {
case *sql.Tx:
tx = t
case *sql.DB:
tx, err = t.Begin()
func (s *SQL) withTx(f txFunc) (err error) {
tx, err := s.conn.Begin()
if err != nil {
return err
}
@ -723,9 +717,6 @@ func withTx(dbOrTx interface{}, f txFunc) (err error) {
err = errors.Err(tx.Commit())
}
}()
default:
return errors.Err("db or tx required")
}
return f(tx)
}