Documentation ¶
Index ¶
- Variables
- func MaybeRollbackContext(context SqlContext) error
- func WithSavepointContext[T *any](parent SqlContext, f func(*SavepointContext) (T, error)) (T, error)
- func WithSubcontext[T *any](context SqlContext, f func(SqlContext) (T, error)) (T, error)
- func WithTxContext[T *any](db *sql.DB, f func(*TxContext) (T, error)) (T, error)
- type SavepointContext
- func (c *SavepointContext) Commit() error
- func (c *SavepointContext) Completed() bool
- func (c *SavepointContext) Ctx() context.Context
- func (c *SavepointContext) Exec(query string, args ...any) (sql.Result, error)
- func (c *SavepointContext) Parent() SqlContext
- func (c *SavepointContext) Prepare(query string) (*sql.Stmt, error)
- func (c *SavepointContext) Query(query string, args ...any) (*sql.Rows, error)
- func (c *SavepointContext) QueryRow(query string, args ...any) (*sql.Row, error)
- func (c *SavepointContext) Rollback() error
- func (c *SavepointContext) Stmt(stmt *sql.Stmt) (*sql.Stmt, error)
- func (c *SavepointContext) Subcontext() (SqlContext, error)
- func (c *SavepointContext) Tx() *sql.Tx
- type SqlContext
- type TxContext
- func (c *TxContext) Commit() error
- func (c *TxContext) Completed() bool
- func (c *TxContext) Ctx() context.Context
- func (c *TxContext) Exec(query string, args ...any) (sql.Result, error)
- func (c *TxContext) Prepare(query string) (*sql.Stmt, error)
- func (c *TxContext) Query(query string, args ...any) (*sql.Rows, error)
- func (c *TxContext) QueryRow(query string, args ...any) (*sql.Row, error)
- func (c *TxContext) Rollback() error
- func (c *TxContext) Stmt(stmt *sql.Stmt) (*sql.Stmt, error)
- func (c *TxContext) Subcontext() (SqlContext, error)
- func (c *TxContext) Tx() *sql.Tx
Constants ¶
This section is empty.
Variables ¶
var ErrOperatingOnCompletedContext = fmt.Errorf("tried to operate on completed context")
Functions ¶
func MaybeRollbackContext ¶
func MaybeRollbackContext(context SqlContext) error
Rollback a context if it hasn't already been completed (rolled back or committed)
func WithSavepointContext ¶
func WithSavepointContext[T *any](parent SqlContext, f func(*SavepointContext) (T, error)) (T, error)
WithSavepointContext runs a function with a new SavepointContext created as a child of the given context. If the function doesn't commit or rollback the new subcontext, it will be automatically rolled back. You should probably use WithSubcontext instead.
func WithSubcontext ¶
func WithSubcontext[T *any](context SqlContext, f func(SqlContext) (T, error)) (T, error)
WithSubcontext runs a function with a subcontext of the given context. If the function doesn't commit or rollback the subcontext, it will be automatically rolled back.
func WithTxContext ¶
WithTxContext runs a function with a new TxContext created by wrapping a sql.Tx database transaction. If the function doesn't commit or rollback the transaction, it will be automatically rolled back.
Types ¶
type SavepointContext ¶
type SavepointContext struct {
// contains filtered or unexported fields
}
func NewSavepointContext ¶
func NewSavepointContext(parent SqlContext) (*SavepointContext, error)
NewSavepointContext creates a new SavepointContext as a child of another context. You should probably use [Context.Subcontext] instead.
func (*SavepointContext) Commit ¶
func (c *SavepointContext) Commit() error
func (*SavepointContext) Completed ¶
func (c *SavepointContext) Completed() bool
func (*SavepointContext) Ctx ¶
func (c *SavepointContext) Ctx() context.Context
func (*SavepointContext) Parent ¶
func (c *SavepointContext) Parent() SqlContext
func (*SavepointContext) Prepare ¶
func (c *SavepointContext) Prepare(query string) (*sql.Stmt, error)
func (*SavepointContext) Rollback ¶
func (c *SavepointContext) Rollback() error
func (*SavepointContext) Subcontext ¶
func (c *SavepointContext) Subcontext() (SqlContext, error)
func (*SavepointContext) Tx ¶
func (c *SavepointContext) Tx() *sql.Tx
type SqlContext ¶
type SqlContext interface { // Ctx returns the [context.Context] attached to this SqlContext Ctx() context.Context // Tx returns the [sql.Tx] attached to this SqlContext. This Tx object might // be fetched through many layers of contexts, so be careful rolling it back // or committing it. Tx() *sql.Tx // Completed returns hether this context has been rolled back or committed Completed() bool // Exec executes a query that doesn't return rows. See [database/sql] // documentation for more details. Exec(query string, args ...any) (sql.Result, error) // Prepare creates a prepared statement for use within a transaction. See // [database/sql] documentation for more details. Prepare(query string) (*sql.Stmt, error) // Query executes a query that returns rows. See [database/sql] documentation // for more details. Query(query string, args ...any) (*sql.Rows, error) // QueryRow executes a query that is expected to return at most one row. See // [database/sql] documentation for more details. QueryRow(query string, args ...any) (*sql.Row, error) // Rollback executes this context's rollback behavior, reverting any INSERTs // and UPDATEs made. This may abort a database transaction, or perform some // other behavior. Rollback() error // Commit executes this context's commit behavior, making any INSERTs and // UPDATEs visible to higher up contexts. This may commit a database // transaction, perform some other behavior, or do nothing at all. Commit() error // Stmt returns a transaction-specific prepared statement from an existing // statement. See [database/sql] documentation for more details. Stmt(stmt *sql.Stmt) (*sql.Stmt, error) // Subcontext returns a context below this context. Complete this new // subcontext (by using [Commit] or [Rollback]) before performing any more // operations on this context, including from other goroutines. Subcontext() (SqlContext, error) }
type TxContext ¶
type TxContext struct {
// contains filtered or unexported fields
}
func BeginTx ¶
BeginTx creates a new database transaction and returns a TxContext corresponding to this new transaction.
func NewContextFromTx ¶
NewContextFromTx creates a TxContext wrapping a sql.Tx database transaction.
func NewContextFromTxAndCtx ¶
NewContextFromTxAndCtx creates a TxContext wrapping a sql.Tx database transaction with a context.Context which can be used to interrupt queries and statements.
func (*TxContext) Subcontext ¶
func (c *TxContext) Subcontext() (SqlContext, error)