wrapper

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package wrapper provides driver-agnostic wrappers around database/sql.Stmt and database/sql.Tx that implement the types.Statement and types.Tx interfaces. Extracted from byte-identical code in database/postgresql/ and database/oracle/ so a future vendor (or a behavior change to the wrapping logic) lives in one place.

The wrapped sql.Stmt/sql.Tx values are stored unexported. Callers should construct via NewStatement/NewTransaction; both vendor packages re-export the wrapper types via type aliases to preserve their public API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	DB             *sql.DB
	Config         *config.DatabaseConfig
	Logger         logger.Logger
	MetricsCleanup func()
	Name           string // vendor display name, e.g. "PostgreSQL", "Oracle"
}

Connection holds the byte-identical fields and delegation methods that previously lived in both postgresql.Connection and oracle.Connection. Vendor packages embed this struct (typically by pointer) and add only the vendor-specific bits (DatabaseType / MigrationTable / CreateMigrationTable DDL / dialer plumbing).

Fields are exported so vendor packages can construct via struct literal in their NewConnection, and so the metrics-registration callback can flip MetricsCleanup after wiring it up. The Name field flows into Close()'s "Closing X database connection" log message — the only previously vendor-specific bit in those nine methods.

func (*Connection) Begin

func (c *Connection) Begin(ctx context.Context) (types.Tx, error)

Begin starts a transaction with default options.

func (*Connection) BeginTx

func (c *Connection) BeginTx(ctx context.Context, opts *sql.TxOptions) (types.Tx, error)

BeginTx starts a transaction with the given options.

func (*Connection) Close

func (c *Connection) Close() error

Close closes the underlying *sql.DB, unregisters the metrics callback (if any), and logs the shutdown. The Name field is used to render the vendor name into the log message.

func (*Connection) Exec

func (c *Connection) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

Exec executes a query without returning any rows.

func (*Connection) Health

func (c *Connection) Health(ctx context.Context) error

Health checks database connectivity with a 5s timeout. The caller's context is honored if it has a shorter deadline.

func (*Connection) Prepare

func (c *Connection) Prepare(ctx context.Context, query string) (types.Statement, error)

Prepare creates a prepared statement for later queries or executions.

func (*Connection) Query

func (c *Connection) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows.

func (*Connection) QueryRow

func (c *Connection) QueryRow(ctx context.Context, query string, args ...any) types.Row

QueryRow executes a query that returns at most one row.

func (*Connection) Stats

func (c *Connection) Stats() (map[string]any, error)

Stats returns database connection statistics in a map suitable for logging or metrics tracking. When Config is non-nil, the configured idle-connection target is included for visibility (Go's sql.DB doesn't enforce a minimum idle count; this is purely a configured target).

type Statement

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

Statement wraps sql.Stmt to implement types.Statement.

func NewStatement

func NewStatement(stmt *sql.Stmt) *Statement

NewStatement wraps a sql.Stmt as a types.Statement implementation.

func (*Statement) Close

func (s *Statement) Close() error

Close closes the prepared statement.

func (*Statement) Exec

func (s *Statement) Exec(ctx context.Context, args ...any) (sql.Result, error)

Exec executes a prepared statement with arguments.

func (*Statement) Query

func (s *Statement) Query(ctx context.Context, args ...any) (*sql.Rows, error)

Query executes a prepared query with arguments.

func (*Statement) QueryRow

func (s *Statement) QueryRow(ctx context.Context, args ...any) types.Row

QueryRow executes a prepared query that returns a single row.

type Transaction

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

Transaction wraps sql.Tx to implement types.Tx.

func NewTransaction

func NewTransaction(tx *sql.Tx) *Transaction

NewTransaction wraps a sql.Tx as a types.Tx implementation.

func (*Transaction) Commit

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

Commit commits the transaction. Note: database/sql's Tx.Commit doesn't accept context; it's atomic and non-cancellable. The context parameter maintains interface consistency for databases that support cancellable commit (if a future vendor adds one).

func (*Transaction) Exec

func (t *Transaction) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

Exec executes a query without returning rows within the transaction.

func (*Transaction) Prepare

func (t *Transaction) Prepare(ctx context.Context, query string) (types.Statement, error)

Prepare creates a prepared statement within the transaction.

func (*Transaction) Query

func (t *Transaction) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

Query executes a query within the transaction.

func (*Transaction) QueryRow

func (t *Transaction) QueryRow(ctx context.Context, query string, args ...any) types.Row

QueryRow executes a query that returns a single row within the transaction.

func (*Transaction) Rollback

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

Rollback rolls back the transaction. Note: database/sql's Tx.Rollback doesn't accept context; it's atomic and non-cancellable. The context parameter maintains interface consistency.

Jump to

Keyboard shortcuts

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