ql

package
v2.3.32 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoTransaction             = errors.New("no transaction in context")
	ErrTransactionExistInContext = errors.New("transaction exists in context")
)

Functions

func ContextWithoutTx added in v2.3.11

func ContextWithoutTx(ctx context.Context) context.Context

ContextWithoutTx returns a new transaction with any existing txContext masked.

func Exec

func Exec(ctx TxContext, stmt string, args ...any) error

Exec just execute the given statement in the provided transaction.

func ExecAffected

func ExecAffected(ctx TxContext, stmt string, args ...any) (int, error)

ExecAffected executes the given statement and returns the number of rows that were affected by the statement. This is especially useful with an `UPDATE` statement.

func ExecNamed added in v2.2.111

func ExecNamed(ctx TxContext, stmt string, args any) error

ExecNamed just execute the given statement in the provided transaction.

func ExecNamedAffected added in v2.2.124

func ExecNamedAffected(ctx TxContext, stmt string, args any) (int, error)

ExecNamedAffected executes the given statement and returns the number of rows that were affected by the statement.

func FirstOrNil

func FirstOrNil[T any](ctx TxContext, query string, args ...any) (*T, error)

FirstOrNil is similar to Get. It will only scan the first row of the result. If the query does not return any row, this method returns a value of nil and no error.

func Get

func Get[T any](ctx TxContext, query string, args ...interface{}) (*T, error)

Get runs the given query and parses the result into an object of type T. If not object can be found the method will return sql.ErrNoRows and a value of nil.

func InAnyTransaction

func InAnyTransaction(ctx context.Context, db TxStarter, fun func(ctx TxContext) error) error

InAnyTransaction calls InAnyTransactionWithResult without returning a result.

func InAnyTransactionWithResult added in v2.2.113

func InAnyTransactionWithResult[R any](ctx context.Context, db TxStarter, fun func(ctx TxContext) (R, error)) (R, error)

InAnyTransactionWithResult checks the context for an existing transaction created by InNewTransactionWithResult. If a transaction exists it will run the given operation in the transaction context. If no transaction exists, a new transaction will be created.

See InNewTransactionWithResult regarding error handling.

func InExistingTransaction

func InExistingTransaction(ctx context.Context, fun func(ctx TxContext) error) error

InExistingTransaction calls InExistingTransactionWithResult without returning the error.

func InExistingTransactionWithResult added in v2.2.113

func InExistingTransactionWithResult[R any](ctx context.Context, fun func(ctx TxContext) (R, error)) (R, error)

InExistingTransactionWithResult runs the given operation in the transaction that is hidden in the provided Context instance. If the context does not contain any transaction, ErrNoTransaction will be returned. The context must contain a transaction created by InNewTransactionWithResult.

This function will not rollback the transaction on error.

func InNewTransaction

func InNewTransaction(ctx context.Context, db TxStarter, fun func(ctx TxContext) error) error

InNewTransaction calls InNewTransactionWithResult without returning a result

func InNewTransactionWithResult added in v2.2.114

func InNewTransactionWithResult[R any](ctx context.Context, db TxStarter, fun func(ctx TxContext) (R, error)) (R, error)

InNewTransactionWithResult creates a new transaction and executes the given function within that transaction. The method will automatically roll back the transaction if an error is returned or otherwise commit it. This excludes the 'ErrNoRows' error. This error never triggers a rollback.

This behaviour can be overwritten by wrapping the error into NoRollback. The transaction will be committed in spite of the error present.

The caller can also trigger a rollback with no error present by simply calling Rollback on the transaction.

If the context already contains a transaction then ErrTransactionExistInContext will be returned as error and the actual operation will not be executed.

func NoRollback

func NoRollback(err error) error

func Select

func Select[T any](ctx TxContext, query string, args ...any) ([]T, error)

Select scans the result into a slice of element type T using sqlx.SelectContext.

func TryTxFromContext added in v2.3.10

func TryTxFromContext(ctx TxContext) *sqlx.Tx

Types

type Action

type Action func()

An Action can be schedule to run after a commit or after a rollback of a transaction to execute some (infallible) side effects.

type Hooks

type Hooks interface {
	// OnCommit schedules some side effect that is only run if the transaction
	// commits successfully. The Action is run after the transaction is committed and must
	// not access the database again.
	OnCommit(action Action)
}

type QueryIter

type QueryIter[T any] struct {
	// contains filtered or unexported fields
}

func Iter

func Iter[T any](ctx TxContext, query string, args ...any) (*QueryIter[T], error)

Iter returns a typed iterator over the rows of the given query. It is the callers responsibility to close the returned iterator. Most of the time, you want to use Select. Only use this, if you are expecting millions of rows.

func (*QueryIter[T]) Close

func (q *QueryIter[T]) Close() error

func (*QueryIter[T]) ForEach

func (q *QueryIter[T]) ForEach(consumer func(row T) error) error

func (*QueryIter[T]) HasNext

func (q *QueryIter[T]) HasNext() bool

func (*QueryIter[T]) Next

func (q *QueryIter[T]) Next() (T, error)

type StringArray added in v2.2.120

type StringArray []string

func NewStringArray added in v2.2.120

func NewStringArray(value []string) StringArray

func (*StringArray) Scan added in v2.2.120

func (s *StringArray) Scan(src any) error

type Tx

type Tx interface {
	sqlx.ExtContext
}

Tx describes a simple transaction

type TxContext

type TxContext interface {
	context.Context
	Tx
	Hooks
	sqlx.PreparerContext

	// WithContext returns a new TxContext with the given "real" context.
	WithContext(ctx context.Context) TxContext

	// CommitAndChain performs a commit, runs all OnCommit hooks and creates a new transaction
	// using the postgres `COMMIT AND CHAIN` command.
	CommitAndChain() error
}

func TxContextFromContext added in v2.3.6

func TxContextFromContext(ctx context.Context) TxContext

func WithCancel added in v2.2.120

func WithCancel(parent TxContext) (TxContext, context.CancelFunc)

WithCancel is a wrapper around context.WithCancel

func WithDeadline added in v2.2.120

func WithDeadline(parent TxContext, deadline time.Time) (TxContext, context.CancelFunc)

WithDeadline is a wrapper around context.WithDeadline

func WithTimeout added in v2.2.120

func WithTimeout(parent TxContext, timeout time.Duration) (TxContext, context.CancelFunc)

WithTimeout is a wrapper around context.WithTimeout

func WithValue added in v2.2.120

func WithValue(parent TxContext, key any, value any) TxContext

WithValue is a wrapper around context.WithValue

type TxStarter

type TxStarter interface {
	BeginTxx(ctx context.Context, opts *sql.TxOptions) (*sqlx.Tx, error)
}

Jump to

Keyboard shortcuts

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