generic

package
v0.0.0-...-bdb8432 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package generic implements a generic Transacter which is compatible with a wide range of executors for different data sources.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Executer

type Executer[Remote any] interface {
	Execute(context.Context, func(Remote) error) error
}

Executer models the handler for the remote specific transaction logic. A call to execute should: - Open a new transaction - Run the provided function - On error roll back the transaction - On success commit the transaction The provided context is not passed down to the actual function that is executed, changes or additions to the context in Execute are not propagated.

type SQLRemote

type SQLRemote interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

SQLRemote is a subset of the shared methods of sql.DB and sql.Tx To push context based development purposefully only the Context variants are used.

type SQLXRemote

type SQLXRemote interface {
	BindNamed(query string, arg any) (string, []any, error)
	DriverName() string
	GetContext(ctx context.Context, dest any, query string, args ...any) error
	MustExecContext(ctx context.Context, query string, args ...any) sql.Result
	NamedExecContext(ctx context.Context, query string, arg any) (sql.Result, error)
	NamedQuery(query string, arg any) (*sqlx.Rows, error)
	NamedStmtContext(ctx context.Context, stmt *sqlx.NamedStmt) *sqlx.NamedStmt
	PrepareNamedContext(ctx context.Context, query string) (*sqlx.NamedStmt, error)
	PreparexContext(ctx context.Context, query string) (*sqlx.Stmt, error)
	QueryRowxContext(ctx context.Context, query string, args ...any) *sqlx.Row
	QueryxContext(ctx context.Context, query string, args ...any) (*sqlx.Rows, error)
	Rebind(query string) string
	SelectContext(ctx context.Context, dest any, query string, args ...any) error
}

SQLXRemote is a subset of the shared methods of sqlx.DB and sqlx.Tx To push context based development purposefully only the Context variants are used. The exception to this rule is NamedQuery, as the sqlx project apparently refuses to include a contextualized version on sqlx.Tx (see: https://github.com/jmoiron/sqlx/issues/447)

type Session

type Session[Remote any] struct {
	Tx Remote
}

Session models all info passed from transacter through context to other nested Transact calls.

type Transacter

type Transacter[Remote any, Resources any] struct {
	// contains filtered or unexported fields
}

Transacter implements the Transacter interface for sqlx compatible databases. It flattens statements on nested uses of the Transact method into one sqlx transaction.

func NewTransacter

func NewTransacter[Remote any, Resources any](
	executer Executer[Remote],
	createResources func(
		ctx context.Context,
		transacter *Transacter[Remote, Resources],
		tx Remote,
	) (Resources, error),
	opts ...TransacterOption[Remote, Resources],
) Transacter[Remote, Resources]

NewTransacter creates a new Transacter. Remote is the type passed to the createResources function, ie a sql.DB or sql.Tx object (depending on the chosen executor). The executor must match the chosen remote type. It is useful to use an interface as remote like the SQLRemote or SQLXRemote interfaces, as it permits the use of eg repositories with both sql.DB and sql.Tx.

By default sets:

createResources is supposed to do any setup or new instantiation of members of Resources, ie create new repositories using the provided Remote. For an example view see [example.Example].

func (Transacter[Remote, Resources]) Transact

func (transacter Transacter[Remote, Resources]) Transact(
	ctx context.Context,
	run func(context.Context, Resources) error,
) (err error)

Transact will run run in a sqlx Session. If a session is present in ctx at atomic.SessionContextKey it will use the existing session, else it will create a new session and insert it into the context. It does not support nested transactions, rather all statements are flattened into a single transaction. Using the Session it executes the resource creation function which is provided on creation of the transacter to produce the resources for calling run. If an error is returned from run the outermost call of Transact will handle the error with the provided retry function.

type TransacterOption

type TransacterOption[Remote any, Resources any] func(*Transacter[Remote, Resources])

TransacterOption is used to configure a Transacter.

func WithBackOffDelays

func WithBackOffDelays[Remote any, Resources any](
	backoffs ...time.Duration,
) TransacterOption[Remote, Resources]

WithBackOffDelays sets the backoffs to use on retry. The maximum amount of retries is determined by the length of backoffs.

func WithBackOffRetry

func WithBackOffRetry[Remote any, Resources any](
	retry func(backoffs []time.Duration, run func() error) error,
) TransacterOption[Remote, Resources]

WithBackOffRetry sets the retry function which manages automatic retries on errors.

Directories

Path Synopsis
Package crdb implements [generic.Executer] for cockroachdb
Package crdb implements [generic.Executer] for cockroachdb
Package sql implements [generic.Executer] for the stdlib sql db
Package sql implements [generic.Executer] for the stdlib sql db
Package sqlx implements [generic.Executer] for the sqlx databases
Package sqlx implements [generic.Executer] for the sqlx databases

Jump to

Keyboard shortcuts

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