wrapper

package
v0.66.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2026 License: MIT Imports: 9 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

func AppendPoolFields added in v0.41.0

func AppendPoolFields(ev logger.LogEvent, cfg *config.DatabaseConfig) logger.LogEvent

AppendPoolFields adds the effective connection-pool settings to a log event so operators can confirm what the pool actually uses after defaulting. Shared by the PostgreSQL and Oracle connection layers so the logged field set cannot drift between vendors.

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) OpenSession added in v0.65.0

func (c *Connection) OpenSession(ctx context.Context, vendor string) (types.Session, error)

OpenSession acquires a dedicated physical connection from the pool via (*sql.DB).Conn, pinned for the caller until Close.

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 cap is included for visibility (Go's sql.DB treats the idle setting as a cap, not a floor — it does not pre-warm or maintain a minimum idle count).

type Session added in v0.65.0

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

Session wraps a *sql.Conn pinned to a single physical database connection, so every statement runs on the same backend. See types.Session for the error and concurrency contract.

func (*Session) Begin added in v0.65.0

func (s *Session) Begin(ctx context.Context) (types.Tx, error)

Begin starts a transaction on the pinned connection with default options.

func (*Session) BeginTx added in v0.65.0

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

BeginTx starts a transaction on the pinned connection with explicit options.

func (*Session) Close added in v0.65.0

func (s *Session) Close() error

Close releases the pinned physical connection back to the pool. A connection already shown dead is discarded instead: (*sql.Conn).Close releases with a nil error, which would return it to the pool, while Raw releasing with driver.ErrBadConn makes database/sql drop it.

func (*Session) DatabaseType added in v0.65.0

func (s *Session) DatabaseType() string

DatabaseType returns the vendor identifier this Session was opened with.

func (*Session) Exec added in v0.65.0

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

Exec executes a statement on the pinned connection.

func (*Session) Query added in v0.65.0

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

Query executes a query on the pinned connection.

func (*Session) QueryRow added in v0.65.0

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

QueryRow executes a query expected to return at most one row on the pinned connection.

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