Documentation
¶
Index ¶
- Variables
- func Begin(ctx context.Context, q DBQ) (out *Tx, fn LeaveFn, err error)
- func WrapDriver(name string, originalDriver driver.Driver) error
- type DBQ
- type LeaveFn
- type Options
- type Root
- type RootQuerier
- type Savepoint
- type Tx
- func (tx *Tx) Begin(ctx context.Context) (*Tx, error)
- func (tx *Tx) Commit() error
- func (tx *Tx) Error() (err error)
- func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (out sql.Result, errOut error)
- func (tx *Tx) PrepareContext(ctx context.Context, query string) (out *sql.Stmt, errOut error)
- func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (out *sql.Rows, errOut error)
- func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) (out *sql.Row)
- func (tx *Tx) Rollback() error
- type TxController
- type TxOptionSetter
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotSupported = errors.New("operation is not support") ErrInProgress = errors.New("nested transaction in progress") )
var ( // OptionReadOnly sets the transaction to read-only. OptionReadOnly TxOptionSetter = func(txOpts *sql.TxOptions) { txOpts.ReadOnly = true } // OptionIsolationReadUncommitted sets transaction isolation level [sql.LevelReadUncommitted]. OptionIsolationReadUncommitted = OptionIsolation(sql.LevelReadUncommitted) // OptionIsolationReadCommitted sets transaction isolation level [sql.LevelReadCommitted]. OptionIsolationReadCommitted = OptionIsolation(sql.LevelReadCommitted) // OptionIsolationWriteCommitted sets transaction isolation level [sql.LevelWriteCommitted]. OptionIsolationWriteCommitted = OptionIsolation(sql.LevelWriteCommitted) // OptionIsolationRepeatableRead sets the transaction isolation level to [sql.LevelRepeatableRead]. OptionIsolationRepeatableRead = OptionIsolation(sql.LevelRepeatableRead) // OptionIsolationSnapshot sets the transaction isolation level to [sql.LevelSnapshot]. OptionIsolationSnapshot = OptionIsolation(sql.LevelSnapshot) // OptionIsolationSerializable sets the transaction isolation level to [sql.LevelSerializable]. OptionIsolationSerializable = OptionIsolation(sql.LevelSerializable) // OptionIsolationLinearizable sets the transaction isolation level to [sql.LevelLinearizable]. OptionIsolationLinearizable = OptionIsolation(sql.LevelLinearizable) )
Functions ¶
func Begin ¶
Begin starts a transaction or savepoint.
The returned LeaveFn function must be called, typically within a defer statement, to commit or rollback the transaction. The returned error is ErrNotSupported if the given DBQ does not support transactions or any error returned by the Begin method of the DBQ.
Types ¶
type DBQ ¶
type DBQ interface {
ExecContext(context.Context, string, ...any) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...any) *sql.Row
}
DBQ provides querying access to a database.
type LeaveFn ¶
type LeaveFn func(*error)
LeaveFn should be deferred to handle transaction committing or rolling back.
It's returned by the Begin function. The function accepts a pointer to an error that will be inspected for any errors that occurred during the transaction. If the error is not nil, the transaction will be rolled back. If the error is nil, the transaction will be committed.
type Options ¶
type Options struct {
// contains filtered or unexported fields
}
func WithOptions ¶
func WithOptions(db *sql.DB, opts ...TxOptionSetter) *Options
WithOptions creates a new Options instance.
type Root ¶
type Root struct {
DBQ
// contains filtered or unexported fields
}
func (*Root) NextSavepointID ¶
NextSavepointID provides a sequential ID intended for savepoint naming.
type RootQuerier ¶
type Savepoint ¶
type Savepoint struct {
// contains filtered or unexported fields
}
Savepoint represents a savepoint within a transaction.
func NewSavepoint ¶
NewSavepoint creates a new savepoint.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
func NewTransaction ¶
func NewTransaction(root RootQuerier, txc TxController, pt *Tx) *Tx
NewTransaction constructs a new transaction handle.
func (*Tx) ExecContext ¶
func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (out sql.Result, errOut error)
ExecContext executes a query that doesn't return rows within the transaction.
See sql.Tx.ExecContext <https://pkg.go.dev/database/sql#Tx.ExecContext> for more information.
func (*Tx) PrepareContext ¶
PrepareContext creates a prepared statement for use within a transaction.
See sql.Tx.PrepareContext <https://pkg.go.dev/database/sql#Tx.PrepareContext> for more information.
func (*Tx) QueryContext ¶
func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (out *sql.Rows, errOut error)
QueryContext executes a query that returns rows, typically a SELECT, within the transaction.
See sql.Tx.QueryContext <https://pkg.go.dev/database/sql#Tx.QueryContext> for more information.
func (*Tx) QueryRowContext ¶
QueryRowContext executes a query, that is expected to return at most one row, within the transaction.
See sql.Tx.QueryRowContext <https://pkg.go.dev/database/sql#Tx.QueryRowContext> for more information.
type TxController ¶
TxController can roll back transactions.
type TxOptionSetter ¶
TxOptionSetter supplies options for a transaction.
func OptionIsolation ¶
func OptionIsolation(level sql.IsolationLevel) TxOptionSetter
OptionIsolation sets the transaction isolation level.