Documentation ¶
Overview ¶
Simple wrapper around sqlx.Tx to provide error handling and automatic commit/rollback
Index ¶
- func DBGWithTx(ctx context.Context, dbWrap DBGetter, fn func(tx *TxWrap) error) (rtnErr error)
- func IsTxWrapContext(ctx context.Context) bool
- func WithTx(ctx context.Context, db *sqlx.DB, fn func(tx *TxWrap) error) error
- type DBGetter
- type SimpleDBGetter
- type TxWrap
- func (tx *TxWrap) Context() context.Context
- func (tx *TxWrap) Exec(query string, args ...interface{}) sql.Result
- func (tx *TxWrap) Exists(query string, args ...interface{}) bool
- func (tx *TxWrap) Get(dest interface{}, query string, args ...interface{}) bool
- func (tx *TxWrap) GetBool(query string, args ...interface{}) bool
- func (tx *TxWrap) GetInt(query string, args ...interface{}) int
- func (tx *TxWrap) GetInt64(query string, args ...interface{}) int64
- func (tx *TxWrap) GetMap(query string, args ...interface{}) map[string]interface{}
- func (tx *TxWrap) GetString(query string, args ...interface{}) string
- func (tx *TxWrap) NamedExec(query string, arg interface{}) sql.Result
- func (tx *TxWrap) Run(fn func() error)
- func (tx *TxWrap) Select(dest interface{}, query string, args ...interface{})
- func (tx *TxWrap) SelectMaps(query string, args ...interface{}) []map[string]interface{}
- func (tx *TxWrap) SelectStrings(query string, args ...interface{}) []string
- func (tx *TxWrap) SetErr(err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DBGWithTx ¶
Main transaction wrapper. If any database call fails, or an error is returned from 'fn' then the transation will be rolled back and the first error will be returned. Otherwise the transaction will be committed and WithTx will return nil.
Note that WithTx *can* be nested. If there is already an error WithTx will immediately return that error. Otherwise it will use the existing outer TxWrap object. Note that this will *not* run a nested DB transation. Begin and Commit/Rollback will only be called once for the *outer* transaction.
func IsTxWrapContext ¶
Checks to see if the given Context is running a TxWrap transaction
func WithTx ¶
Simple transaction wrapper. If any database call fails, or an error is returned from 'fn' then the transation will be rolled back and the first error will be returned. Otherwise the transaction will be committed and WithTx will return nil.
Calls DBGWithTx with 'db' wrapped in SimpleDBGetter
Types ¶
type DBGetter ¶
Allows for custom database get/release logic If GetDB returns a DB, ReleaseDB is guaranteed to be called once the transaction has been committed or rolledback.
type SimpleDBGetter ¶
Simple implementation of DBGetter for a sqlx.DB
func (*SimpleDBGetter) ReleaseDB ¶
func (db *SimpleDBGetter) ReleaseDB(instance *sqlx.DB)
type TxWrap ¶
Main TxWrap data-structure. Wraps the sqlx.Tx interface. Once an error is returned from one of the DB calls, no future calls will run and the transaction will be rolled back.
Notes: * Can get the raw sqlx.Tx or Err directly from the struct * TxWrap is not thread-safe, must be synchronized externally to be used by multiple go-routines * If you use sqlx.Rows, sqlx.Row, or sqlx.Stmt directly you'll have to implement and return your errors manually.
func (*TxWrap) Context ¶
Returns the TxWrap Context (with the txWrapKey). Must use this Context for nested calls to TxWrap
func (*TxWrap) Exists ¶
Returns false if there is an error or the query returns sql.ErrNoRows. Otherwise if there is at least 1 matching row, returns true.
func (*TxWrap) Get ¶
If there is an error or sql.ErrNoRows will return false, otherwise true. Note that sql.ErrNoRows will *not* error out the TxWrap.