transaction

package
v0.0.0-...-14d380b Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package transaction provides a client for a live transaction, and interfaces for some relevant sql types. The transaction client automatically performs rollbacks on failures. The use of this package simplifies testing for callers by making the underlying transaction mock-able.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client provides a way to interact with the underlying sql transaction.

func NewClient

func NewClient(tx SQLTx) *Client

NewClient returns a Client with the given transaction assigned.

func (*Client) Cancel

func (c *Client) Cancel() error

Cancel rollbacks the transaction without wrapping an error. This only needs to be called if Client has not returned an error yet or has not committed. Otherwise, transaction has already rolled back, or in the case of Commit() it is too late.

func (*Client) Commit

func (c *Client) Commit() error

Commit commits the transaction and then unlocks the database.

func (*Client) Exec

func (c *Client) Exec(stmt string, args ...any) error

Exec uses the sqlTX Exec() with the given stmt and args. The transaction will be automatically rolled back if Exec() returns an error.

func (*Client) Stmt

func (c *Client) Stmt(stmt *sql.Stmt) Stmt

Stmt adds the given sql.Stmt to the client's transaction and then returns a Stmt. An interface is being returned here to aid in testing callers by providing a way to configure the statement's behavior.

func (*Client) StmtExec

func (c *Client) StmtExec(stmt Stmt, args ...any) error

StmtExec Execs the given statement with the given args. It assumes the stmt has been added to the transaction. The transaction is rolled back if Stmt.Exec() returns an error.

type SQLTx

type SQLTx interface {
	Exec(query string, args ...any) (sql.Result, error)
	Stmt(stmt *sql.Stmt) *sql.Stmt
	Commit() error
	Rollback() error
}

SQLTx represents a sql transaction

type Stmt

type Stmt interface {
	Exec(args ...any) (sql.Result, error)
	Query(args ...any) (*sql.Rows, error)
	QueryContext(ctx context.Context, args ...any) (*sql.Rows, error)
}

Stmt represents a sql stmt. It is used as a return type to offer some testability over returning sql's Stmt type because we are able to mock its outputs and do not need an actual connection.

Jump to

Keyboard shortcuts

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