database

package
v0.0.0-...-4a11b79 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(c context.Context) *sql.DB

Get returns the database pointer embedded in the current context. The database pointer can be embedded in the current context using With.

func With

func With(c context.Context, database *sql.DB) context.Context

With installs a database pointer into the given context. It can be retrieved later using Get.

func WithMiddleware

func WithMiddleware(c *router.Context, next router.Handler)

WithMiddleware is middleware which installs a database pointer into the given context. It can be retrieved later in the middleware chain using Get.

Types

type ExecerContext

type ExecerContext interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

ExecerContext defines an interface which supports ExecContext calls. Implemented by sql.Conn, sql.DB, sql.Stmt, sql.Tx.

type QueryerContext

type QueryerContext interface {
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}

QueryerContext defines an interface which supports QueryContext calls. Implemented by sql.Conn, sql.DB, sql.Stmt, sql.Tx.

type RollbackTx

type RollbackTx struct {
	*sql.Tx
	// contains filtered or unexported fields
}

RollbackTx is a sql.Tx which supports automatic rollback. defer a call to MaybeRollback(), then call (*RollbackTx).Commit() instead of (*sql.Tx).Commit() to avoid having the transaction rolled back by the defer.

func Begin

func Begin(c context.Context) (*RollbackTx, error)

Begin begins a transaction using the database pointer embedded in the current context.

func (*RollbackTx) Commit

func (r *RollbackTx) Commit() error

Commit commits the transaction.

func (*RollbackTx) MaybeRollback

func (r *RollbackTx) MaybeRollback(c context.Context) error

MaybeRollback rolls back the transaction if (*RollbackTx).Commit() hasn't been called. Logs the error if the rollback itself fails.

Jump to

Keyboard shortcuts

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