Documentation ¶
Overview ¶
Package db contains common database abstractions for our infra.
Index ¶
- Constants
- Variables
- func ConfiguredMaxIdleConns(cfg map[string]string) (int, bool)
- func ConfiguredMaxOpenConns(cfg map[string]string) (int, bool)
- func DeleteInTx(ctx context.Context, db Sqler, delFunc func(tx *sql.Tx) (sql.Result, error)) (int, error)
- func DoInTx(ctx context.Context, db Sqler, action func(context.Context, *sql.Tx) error) error
- func ErrToMetricLabel(err error) string
- func NewDataError(msg common.ErrMsg, err error, logCtx ...interface{}) error
- func NewInputDataError(msg common.ErrMsg, err error, logCtx ...interface{}) error
- func NewReadError(msg common.ErrMsg, err error, logCtx ...interface{}) error
- func NewSqlite(path string, schema string, schemaVersion int) (*sql.DB, error)
- func NewTxError(msg common.ErrMsg, err error, logCtx ...interface{}) error
- func NewWriteError(msg common.ErrMsg, err error, logCtx ...interface{}) error
- func SetConnLimits(cfg LimitConfig, db LimitSetter)
- func ValidateConfigLimits(cfg map[string]string) error
- type LimitConfig
- type LimitSetter
- type Sqler
Constants ¶
const ( // MaxOpenConnsKey is the configuration key for max open connections. MaxOpenConnsKey = "maxopenconns" // MaxIdleConnsKey is the configuration key for max idle connections. MaxIdleConnsKey = "maxidleconns" )
Variables ¶
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 ¶
ConfiguredMaxIdleConns returns the configured max idle connections in the config map and returns true if the limit was set.
func ConfiguredMaxOpenConns ¶
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 ¶
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 ¶
ErrToMetricLabel classifies the error into a label that can be used in metrics.
func NewInputDataError ¶
func NewSqlite ¶
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 SetConnLimits ¶
func SetConnLimits(cfg LimitConfig, db LimitSetter)
SetConnLimits sets the configured limits on the database.
func ValidateConfigLimits ¶
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 ¶
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.