dbtxn

package
v0.9.5 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2020 License: MIT Imports: 5 Imported by: 0

README

DBTXN

Help database transaction

In Repository layer

func (r *RepoImpl) Delete(ctx context.Context) (int64, error) {
    // Use dbtxn for potential transaction usecase
    txn, err := dbtxn.Use(ctx, r.DB)
    if err != nil {
        return -1, err
    }
    
    // Get the transaction object 
    db := txn.DB

    // result, err := ...

    if err != nil {
        // Set the error when failed
        txn.SetError(err)
        return -1, err
    }

    return result.RowsAffected()
}

In Service layer

func (s *SvcImpl) SomeOperation(ctx context.Context) error{
    // To begin the transaction and commit or rollback in end function
    defer dbtxn.Begin(&ctx)()

    // ...
}

Documentation

Index

Constants

View Source
const ContextKey key = iota

ContextKey to get transaction

Variables

This section is empty.

Functions

func Error

func Error(ctx context.Context) error

Error of transaction

Types

type CommitFn

type CommitFn func() error

CommitFn is commit function to close the transaction

func Begin

func Begin(parent *context.Context) CommitFn

Begin transaction

type Context

type Context struct {
	Tx  Tx
	Err error
}

Context of transaction

func Find added in v0.9.1

func Find(ctx context.Context) *Context

Find transaction context

func (*Context) Commit

func (c *Context) Commit() error

Commit if no error

type Handler

type Handler struct {
	DB      sq.BaseRunner
	Context *Context
}

Handler responsible to handle transaction

func Use

func Use(ctx context.Context, db *sql.DB) (*Handler, error)

Use transaction if possible

func (*Handler) SetError

func (t *Handler) SetError(err error) bool

SetError to set error to txn context

type Tx

type Tx interface {
	sq.BaseRunner
	Rollback() error
	Commit() error
}

Tx is interface for database transaction

Jump to

Keyboard shortcuts

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