sqlh

package
v0.0.0-...-d6dd603 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2021 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Package sqlh contains some helpers for database/sql.

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 by fn, the transaction will be committed. Otherwise it is rollbacked and the error is returned to the caller (except returning Rollback, which will rollback the transaction but not returning error).

func WithTxOpts

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

WithTxOpts is similar to WithTx with extra options.

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 TxOptions

type TxOptions struct {
	sql.TxOptions

	// BeforeTx will be called (if not nil) before transaction starts.
	// `conn` is the db session used to start transaction.
	// If the callback returns an error, WithTxOpts returns that error and the transaction
	// will not start.
	BeforeTx func(ctx context.Context, conn *sql.Conn) error

	// AfterTx will be called (if not nil) after transaction finished with the commit status.
	// `conn` is the db session used to start transaction.
	AfterTx func(ctx context.Context, conn *sql.Conn, committed bool)
}

TxOptions contains extra options for a db transaction.

Directories

Path Synopsis
Package mysqlh contains some helpers for MySQL dialet.
Package mysqlh contains some helpers for MySQL dialet.

Jump to

Keyboard shortcuts

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