db

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package db contains common database abstractions for our infra.

Index

Constants

View Source
const (
	// MaxOpenConnsKey is the configuration key for max open connections.
	MaxOpenConnsKey = "maxopenconns"
	// MaxIdleConnsKey is the configuration key for max idle connections.
	MaxIdleConnsKey = "maxidleconns"
)

Variables

View Source
var (
	// ErrInvalidInputData indicates invalid data was tried to input in the DB.
	ErrInvalidInputData = serrors.New("db: input data invalid")
	// ErrDataInvalid indicates invalid data is stored in the DB.
	ErrDataInvalid = serrors.New("db: db data invalid")
	// ErrReadFailed indicates that reading from the DB failed.
	ErrReadFailed = serrors.New("db: read failed")
	// ErrWriteFailed indicates that writing to the DB failed.
	ErrWriteFailed = serrors.New("db: write failed")
	// ErrTx indicates a transaction error.
	ErrTx = serrors.New("db: transaction error")
)

Functions

func ConfiguredMaxIdleConns

func ConfiguredMaxIdleConns(cfg map[string]string) (int, bool)

ConfiguredMaxIdleConns returns the configured max idle connections in the config map and returns true if the limit was set.

func ConfiguredMaxOpenConns

func ConfiguredMaxOpenConns(cfg map[string]string) (int, bool)

ConfiguredMaxOpenConns returns the configured max open connections in the config map and returns true if the limit was set.

func DeleteInTx

func DeleteInTx(ctx context.Context, db Sqler,
	delFunc func(tx *sql.Tx) (sql.Result, error)) (int, error)

DeleteInTx executes delFunc in a transaction and returns the affected rows.

func DoInTx

func DoInTx(ctx context.Context, db Sqler, action func(context.Context, *sql.Tx) error) error

DoInTx executes the given action in a transaction. If db is already a transaction the action is executed in the existing transaction, the transaction is not modified. If db is a "normal" db, a transaction is created and action is executed in it. If action errors the created transaction is rollbacked, otherwise it is committed.

func ErrToMetricLabel

func ErrToMetricLabel(err error) string

ErrToMetricLabel classifies the error into a label that can be used in metrics.

func NewDataError

func NewDataError(msg common.ErrMsg, err error, logCtx ...interface{}) error

func NewInputDataError

func NewInputDataError(msg common.ErrMsg, err error, logCtx ...interface{}) error

func NewReadError

func NewReadError(msg common.ErrMsg, err error, logCtx ...interface{}) error

func NewSqlite

func NewSqlite(path string, schema string, schemaVersion int) (*sql.DB, error)

NewSqlite returns a new SQLite backend opening a database at the given path. If no database exists a new database is be created. If the schema version of the stored database is different from schemaVersion, an error is returned.

func NewTxError

func NewTxError(msg common.ErrMsg, err error, logCtx ...interface{}) error

func NewWriteError

func NewWriteError(msg common.ErrMsg, err error, logCtx ...interface{}) error

func SetConnLimits

func SetConnLimits(cfg LimitConfig, db LimitSetter)

SetConnLimits sets the configured limits on the database.

func ValidateConfigLimits

func ValidateConfigLimits(cfg map[string]string) error

ValidateConfigLimits validates connection limits on the given config map.

Types

type LimitConfig

type LimitConfig interface {
	// MaxOpenConns returns the max open connection count and true if the limit
	// was configured.
	MaxOpenConns() (int, bool)
	// MaxIdleConns returns the max idle connection count and true if the limit
	// was configured.
	MaxIdleConns() (int, bool)
}

LimitConfig is a configuration of database limits.

type LimitSetter

type LimitSetter interface {
	SetMaxOpenConns(maxOpenConns int)
	SetMaxIdleConns(maxIdleConns int)
}

LimitSetter allows setting the database connection limits.

type Sqler

type Sqler interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}

Sqler contains the common functions of *sql.DB and *sql.Tx.

Jump to

Keyboard shortcuts

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