dbpgx

package
v0.0.0-...-cbbd1a2 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DbErrRuntimeEnvironment        = errx.NewKind("DbErrRuntimeEnvironment")
	DbErrInternalAppError          = errx.NewKind("DbErrInternalAppError")
	DbErrConnectionException       = errx.NewKind("DbErrConnectionException")
	DbErrConstraintViolation       = errx.NewKind("DbErrConstraintViolation")
	DbErrUniqueViolation           = errx.NewKind("DbErrUniqueViolation", DbErrConstraintViolation)
	DbErrInsufficientResources     = errx.NewKind("DbErrInsufficientResources", DbErrRuntimeEnvironment)
	DbErrOperatorIntervention      = errx.NewKind("DbErrOperatorIntervention", DbErrRuntimeEnvironment)
	DbErrExternalSystemError       = errx.NewKind("DbErrExternalSystemError", DbErrRuntimeEnvironment)
	DbErrEngineError               = errx.NewKind("DbErrEngineError", DbErrRuntimeEnvironment)
	DbErrRecordNotFound            = errx.NewKind("DbErrRecordNotFound")
	DbErrUnexpectedMultipleRecords = errx.NewKind("DbErrUnexpectedMultipleRecords", DbErrInternalAppError)
)

Functions

func ClassifyError

func ClassifyError(err error) *errx.Kind

ClassifyError returns an database-related *errx.Kind that corresponds to err.

func DeferredRollback

func DeferredRollback(ctx context.Context, tx pgx.Tx)

func GetCtxPool

func GetCtxPool(ctx context.Context) (*pgxpool.Pool, error)

func GetCtxTx

func GetCtxTx(ctx context.Context) (pgx.Tx, error)

func ReadMany

func ReadMany[R any](
	ctx context.Context,
	tx pgx.Tx,
	mainSql string,
	limit int,
	offset int,
	args ...any,
) ([]R, error)

ReadMany reads an array of records from the database. `mainSql` is the main query string, which is appended with optional limit and offset clauses. If limit or offset is negative then the corresponding clause is not appended.

func ReadSingle

func ReadSingle[R any, F any](
	ctx context.Context,
	tx pgx.Tx,
	tableName string,
	fieldName string,
	fieldValue F,
	record *R,
) error

ReadSingle reads a single record from a table.

func SflWithTransaction

func SflWithTransaction[R, S, T any](
	db Db,
	block func(ctx context.Context, tx pgx.Tx, reqCtx R, in S) (T, error),
) func(ctx context.Context, reqCtx R, in S) (T, error)

func SqlState

func SqlState(err error) string

SqlState returns the the pgx SQLState() of err if err is wraps a *pgconn.PgError, returns the empty string otherwise.

func WithTransaction

func WithTransaction[T any](
	db Db,
	ctx context.Context,
	block func(ctx context.Context, tx pgx.Tx) (T, error),
) (T, error)

Types

type CtxPgx

type CtxPgx struct {
	Pool *pgxpool.Pool
}

func (CtxPgx) BeginTx

func (p CtxPgx) BeginTx(ctx context.Context) (context.Context, error)

func (CtxPgx) Commit

func (p CtxPgx) Commit(ctx context.Context) (context.Context, error)

func (CtxPgx) DeferredRollback

func (p CtxPgx) DeferredRollback(ctx context.Context)

func (CtxPgx) Rollback

func (p CtxPgx) Rollback(ctx context.Context) (context.Context, error)

func (CtxPgx) SetPool

func (p CtxPgx) SetPool(ctx context.Context) (context.Context, error)

type CtxPgxPoolKeyT

type CtxPgxPoolKeyT struct{}
var CtxPgxPoolKey CtxPgxPoolKeyT = struct{}{}

type CtxPgxTxKeyT

type CtxPgxTxKeyT struct{}
var CtxPgxTxKey CtxPgxTxKeyT = struct{}{}

type Db

type Db struct {
	Pool *pgxpool.Pool
}

func (Db) Acquire

func (s Db) Acquire(ctx context.Context) (*pgxpool.Conn, error)

func (Db) BeginTx

func (s Db) BeginTx(ctx context.Context) (pgx.Tx, error)

type RecCtx

type RecCtx[E any] struct {
	//Id        uint
	CreatedAt time.Time
	UpdatedAt time.Time
}

RecCtx is a type that holds platform-specific database record context information, e.g., an optimistic locking token and/or a record Id. DAFs may accept this type as a parameter or return this type, together with domain entity types. This type is parameterized to provide type safety, i.e., to prevent passing a RecCtx[U] on a call that involves entity type V.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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