Documentation
¶
Index ¶
- Constants
- type DB
- type DBLogger
- func (d DBLogger) Begin() (tx *sql.Tx, err error)
- func (d DBLogger) BeginTx(ctx context.Context, opts *sql.TxOptions) (tx *sql.Tx, err error)
- func (d DBLogger) ExecContext(ctx context.Context, query string, args ...interface{}) (res sql.Result, err error)
- func (d DBLogger) PrepareContext(ctx context.Context, query string) (stmt *sql.Stmt, err error)
- func (d DBLogger) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error)
- func (d DBLogger) QueryRowContext(ctx context.Context, query string, args ...interface{}) (row *sql.Row)
- type DBLoggerOption
- type DBTxPropagator
- func (d DBTxPropagator) Begin() (*sql.Tx, error)
- func (d DBTxPropagator) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
- func (d DBTxPropagator) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (d DBTxPropagator) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
- func (d DBTxPropagator) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (d DBTxPropagator) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- type Transaction
- type TxFactory
Constants ¶
const TxDriver persistence.TxDriver = "sql"
TxDriver is a type alias for the persistence.TxDriver used in the context of a DBTxPropagator.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB interface {
// Begin starts a transaction. The default isolation level is dependent on
// the driver.
//
// Begin uses [context.Background] internally; to specify the context, use
// [DB.BeginTx].
Begin() (*sql.Tx, error)
// BeginTx starts a transaction.
//
// The provided context is used until the transaction is committed or rolled back.
// If the context is canceled, the sql package will roll back
// the transaction. [sql.Tx.Commit] will return an error if the context provided to
// BeginTx is canceled.
//
// The provided [sql.TxOptions] is optional and may be nil if defaults should be used.
// If a non-default isolation level is used that the driver doesn't support,
// an error will be returned.
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
// QueryContext executes a query that returns rows, typically a SELECT.
// The args are for any placeholder parameters in the query.
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
// QueryRowContext executes a query that is expected to return at most one row.
// QueryRowContext always returns a non-nil value. Errors are deferred until
// [sql.Row]'s Scan method is called.
// If the query selects no rows, the [*sql.Row.Scan] will return [sql.ErrNoRows].
// Otherwise, [*sql.Row.Scan] scans the first selected row and discards
// the rest.
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
// ExecContext executes a query without returning any rows.
// The args are for any placeholder parameters in the query.
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
// PrepareContext creates a prepared statement for later queries or executions.
// Multiple queries or executions may be run concurrently from the
// returned statement.
// The caller must call the statement's [*sql.Stmt.Close] method
// when the statement is no longer needed.
//
// The provided context is used for the preparation of the statement, not for the
// execution of the statement.
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}
DB represents a SQL database client based on stdlib sql.DB.
The intention of this interface is to avoid persistence components to depend on a concrete implementation (sql.DB). By not depending, testing/mocking is easier and also, it allows consumers to implement patterns like chain-of-responsibility to adhere additional functionality without affecting the final concrete implementation (e.g. logging, tracing, transaction management).
type DBLogger ¶
type DBLogger struct {
// contains filtered or unexported fields
}
DBLogger is an interceptor component adhering logging capabilities to an existing DB.
func NewDBLogger ¶
func NewDBLogger(parent DB, logger *slog.Logger, opts ...DBLoggerOption) DBLogger
NewDBLogger allocates a new DBLogger.
func (DBLogger) ExecContext ¶
func (DBLogger) PrepareContext ¶
func (DBLogger) QueryContext ¶
type DBLoggerOption ¶
type DBLoggerOption func(*dbLoggerOptions)
DBLoggerOption is a routine used to set up DBLogger optional configuration.
func WithLogLevel ¶
func WithLogLevel(lvl slog.Level) DBLoggerOption
WithLogLevel sets the base log level to use for a DBLogger.
type DBTxPropagator ¶
type DBTxPropagator struct {
// contains filtered or unexported fields
}
DBTxPropagator is an interceptor component adhering transaction propagation to all operations of an existing DB, using transaction contexts.
func NewDBTxPropagator ¶
func NewDBTxPropagator(parent DB) DBTxPropagator
NewDBTxPropagator allocates a new DBTxPropagator.
func (DBTxPropagator) ExecContext ¶
func (DBTxPropagator) PrepareContext ¶
func (DBTxPropagator) QueryContext ¶
func (DBTxPropagator) QueryRowContext ¶
type Transaction ¶
Transaction is the adapter structure of persistence.Transaction for sql.
type TxFactory ¶
type TxFactory struct {
// contains filtered or unexported fields
}
TxFactory is the concrete implementation of persistence.TxFactory for sql.
func NewTxFactory ¶
NewTxFactory creates a new instance of TxFactory with the provided DB client and transaction options.
func (TxFactory) Driver ¶
func (t TxFactory) Driver() persistence.TxDriver
func (TxFactory) NewTx ¶
func (t TxFactory) NewTx(ctx context.Context) (persistence.Transaction, error)