db

package
v0.0.0-...-a050769 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoQuerrier = errors.New("Querrier missing from context")

Functions

func Exec

func Exec(ctx context.Context, query string, params ...interface{}) (sql.Result, error)

func Get

func Get(ctx context.Context, dest interface{}, query string, params ...interface{}) error

func Select

func Select(ctx context.Context, dest interface{}, query string, params ...interface{}) error

func WithQuerier

func WithQuerier(ctx context.Context, q Querier) context.Context

Types

type Conn

type Conn interface {
	Querier

	// Begin is an alternative way of starting a transaction if you need more
	// control over it. It is strongly recommended that you use `defer
	// tx.Rollback()` directly after acquiring the transaction to ensure it is
	// closed even in the case of a panic.
	Begin(context.Context) (Tx, error)

	// Tx is wraps a transaction. If no error is returned from the callback, the
	// transaction will be committed, otherwise it will be aborted.
	Tx(context.Context, func(context.Context, Querier) error) error
}

Conn is a stripped down convenience wrapper around sqlx (which is, in-turn, a wrapper around database/sql) meant to make DB access more convenient and easier to fall into the pit of success.

func Open

func Open(driver string, connInfo string) (Conn, error)

type Querier

type Querier interface {
	// Exec is meant for running queries which don't read data, such as INSERTs
	// or UPDATES.
	Exec(ctx context.Context, query string, params ...interface{}) (sql.Result, error)

	// Get is meant for SELECT queries returning a single row. It will return an
	// error if no rows are returned.
	Get(ctx context.Context, dest interface{}, query string, params ...interface{}) error

	// Select is meant for SELECT queries returning an unknown number of rows
	// but which should easily fit into memory.
	Select(ctx context.Context, dest interface{}, query string, params ...interface{}) error

	// Query is meant for SELECT queries which return an unknown number of rows
	// and may not all fit into memory. The Rows object can be used to scan the
	// results one row at a time.
	Query(ctx context.Context, query string, params ...interface{}) (*Rows, error)
}

Querier is the common interface between a DB and a Tx.

func GetQuerrier

func GetQuerrier(ctx context.Context) (Querier, bool)

type Rows

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

func Query

func Query(ctx context.Context, query string, params ...interface{}) (*Rows, error)

func (*Rows) Close

func (r *Rows) Close() error

func (*Rows) Err

func (r *Rows) Err() error

func (*Rows) Next

func (r *Rows) Next() bool

func (*Rows) Scan

func (r *Rows) Scan(dest interface{}) error

type Tx

type Tx interface {
	Querier

	// Rollback will roll back the transaction.
	Rollback() error

	// Commit will commit the transaction.
	Commit() error
}

Tx is a Querier representing a transaction rather than a connection. The first call to either Rollback or Commit will end the transaction any calls afterwords will be dropped and an error returned.

Jump to

Keyboard shortcuts

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