sqlh

package
v0.11.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Rollback is used to rollback a transaction without returning an error.
	Rollback = errors.New("Just rollback")
)

Functions

func WithTx

func WithTx(ctx context.Context, db *sql.DB, fn func(context.Context, *sql.Tx) error) (err error)

WithTx starts a transaction and run fn. If no error is returned, the transaction is committed. Otherwise it is rollbacked and the error is returned to the caller (except returning Rollback, which will rollback the transaction but not return error).

Inside one can use OnCommitted/OnFinalised to add functions to be called after tx end.

Types

type Queryer

type Queryer interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}

Queryer abstracts sql.DB/sql.Conn/sql.Tx .

type TxContext

type TxContext struct {
	// contains filtered or unexported fields
}

TxContext contains transaction context.

func CurTxContext

func CurTxContext(ctx context.Context) *TxContext

CurTxContext returns current TxContext in transaction.

func MustCurTxContext

func MustCurTxContext(ctx context.Context) *TxContext

MustCurTxContext is the `must` version of CurTxContext.

func (*TxContext) DB

func (txCtx *TxContext) DB() *sql.DB

DB returns the original *sql.DB that starts the transaction.

func (*TxContext) Local

func (txCtx *TxContext) Local(key interface{}) interface{}

Local gets tx local variable.

func (*TxContext) OnCommitted

func (txCtx *TxContext) OnCommitted(fn func())

OnCommitted adds a function which will be only called after the transaction has been successful committed. The invocation order of OnCommitted functions just like defer functions.

func (*TxContext) OnFinalised

func (txCtx *TxContext) OnFinalised(fn func())

OnFinalised adds a function which will be called after the transaction ended (either committed or rollback). The invocation order of OnFinalised functions just like defer functions.

func (*TxContext) SetLocal

func (txCtx *TxContext) SetLocal(key, val interface{})

SetLocal sets tx local variable.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL