sql

package
v0.1.19 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
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) Begin

func (d DBLogger) Begin() (tx *sql.Tx, err error)

func (DBLogger) BeginTx

func (d DBLogger) BeginTx(ctx context.Context, opts *sql.TxOptions) (tx *sql.Tx, err error)

func (DBLogger) ExecContext

func (d DBLogger) ExecContext(ctx context.Context, query string, args ...interface{}) (res sql.Result, err error)

func (DBLogger) PrepareContext

func (d DBLogger) PrepareContext(ctx context.Context, query string) (stmt *sql.Stmt, err error)

func (DBLogger) QueryContext

func (d DBLogger) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error)

func (DBLogger) QueryRowContext

func (d DBLogger) QueryRowContext(ctx context.Context, query string, args ...interface{}) (row *sql.Row)

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) Begin

func (d DBTxPropagator) Begin() (*sql.Tx, error)

func (DBTxPropagator) BeginTx

func (d DBTxPropagator) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

func (DBTxPropagator) ExecContext

func (d DBTxPropagator) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (DBTxPropagator) PrepareContext

func (d DBTxPropagator) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

func (DBTxPropagator) QueryContext

func (d DBTxPropagator) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (DBTxPropagator) QueryRowContext

func (d DBTxPropagator) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

type Transaction

type Transaction struct {
	Parent *sql.Tx
}

Transaction is the adapter structure of persistence.Transaction for sql.

func (Transaction) Commit

func (t Transaction) Commit(_ context.Context) error

func (Transaction) Rollback

func (t Transaction) Rollback(_ context.Context) error

type TxFactory

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

TxFactory is the concrete implementation of persistence.TxFactory for sql.

func NewTxFactory

func NewTxFactory(client DB, txOpts *sql.TxOptions) TxFactory

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

Jump to

Keyboard shortcuts

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