db

package
v1.0.814-3002709 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2021 License: MIT Imports: 12 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNop         = o11y.NewWarning("no update or results")
	ErrConstrained = errors.New("violates constraints")
	ErrException   = errors.New("exception")
	ErrCanceled    = o11y.NewWarning("statement canceled")
)

Functions

func EscapeLike

func EscapeLike(s string) string

func New

func New(ctx context.Context, dbName, appName string, options Config) (db *sqlx.DB, err error)

New connects to a database. The context passed in is expected to carry an o11y provider and is only used for reporting (not for cancellation),

func Span

func Span(ctx context.Context, entity, queryName string) (context.Context, o11y.Span)

Types

type Config

type Config struct {
	Host string
	Port int
	User string
	Pass secret.String
	Name string
	SSL  bool

	// If these are unset, then defaults will be chosen
	ConnMaxLifetime time.Duration
	MaxOpenConns    int
	MaxIdleConns    int
}

type HealthCheck

type HealthCheck struct {
	Name string
	DB   *sqlx.DB
}

func (*HealthCheck) Gauges

func (h *HealthCheck) Gauges(_ context.Context) map[string]float64

func (*HealthCheck) HealthChecks

func (h *HealthCheck) HealthChecks() (name string, ready, live func(ctx context.Context) error)

func (*HealthCheck) MetricName

func (h *HealthCheck) MetricName() string

type Querier

type Querier interface {

	// ExecContext executes the query with placeholder parameters that match the args.
	// Use this if the query does not use named parameters (for that use NamedExecContext),
	// and you do not care about the data the query generates.
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

	// GetContext expects placeholder parameters in the query and will bind args to them.
	// A single row result will be mapped to dest which must be a pointer to a struct.
	// In the case of no result the error returned will be sql.ErrNoRows.
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

	// NamedExecContext expect a query with named parameters, fields from the arg struct will be mapped
	// to the named parameters. Use this if you do not care about the data the query generates, and
	// you don't want to use placeholder parameters (see ExecContext)
	NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)

	// SelectContext expects placeholder parameters in the query and will bind args to them.
	// Each resultant row will be scanned into dest, which must be a slice.
	// (If you expect (or want) a single row in the response use GetContext instead.)
	// This method never returns sql.ErrNoRows, instead the dest slice will be empty.
	SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}

Querier can either be a *sqlx.DB or *sqlx.Tx

type TxManager

type TxManager struct {
	DB *sqlx.DB
	// This is only for testing purposes
	TestQuerier func(Querier) Querier
}

func NewTxManager

func NewTxManager(db *sqlx.DB) *TxManager

func (*TxManager) WithTransaction

func (s *TxManager) WithTransaction(ctx context.Context, f func(context.Context, Querier) error) (err error)

Jump to

Keyboard shortcuts

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