Documentation ¶
Index ¶
- Variables
- func Exec(ctx TxContext, stmt string, args ...interface{}) error
- func ExecAffected(ctx TxContext, stmt string, args ...interface{}) (int, error)
- func FirstOrNil[T any](ctx TxContext, query string, args ...interface{}) (*T, error)
- func Get[T any](ctx TxContext, query string, args ...interface{}) (*T, error)
- func InAnyTransaction[R any](ctx context.Context, db TxStarter, fun func(ctx TxContext) (R, error)) (R, error)
- func InExistingTransaction[R any](ctx context.Context, fun func(ctx TxContext) (R, error)) (R, error)
- func InNewTransaction[R any](ctx context.Context, db TxStarter, fun func(ctx TxContext) (R, error)) (R, error)
- func NoRollback(err error) error
- func Select[T any](ctx TxContext, query string, args ...interface{}) ([]T, error)
- type Action
- type Hooks
- type QueryIter
- type Tx
- type TxContext
- type TxStarter
Constants ¶
This section is empty.
Variables ¶
var ErrNoTransaction = errors.New("no transaction in context")
var ErrTransactionExistInContext = errors.New("transaction exists in context")
Functions ¶
func ExecAffected ¶
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 FirstOrNil ¶
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 ¶
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[R any](ctx context.Context, db TxStarter, fun func(ctx TxContext) (R, error)) (R, error)
InAnyTransaction checks the context for an existing transaction created by InNewTransaction. 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 InNewTransaction regarding error handling.
func InExistingTransaction ¶
func InExistingTransaction[R any](ctx context.Context, fun func(ctx TxContext) (R, error)) (R, error)
InExistingTransaction 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 InNewTransaction.
This function will not rollback the transaction on error.
func InNewTransaction ¶
func InNewTransaction[R any](ctx context.Context, db TxStarter, fun func(ctx TxContext) (R, error)) (R, error)
InNewTransaction 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 ¶
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 }
type Tx ¶
type Tx interface { sqlx.ExecerContext sqlx.QueryerContext }
Tx describes a simple transaction