database

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConnectionNotFound is returned when a named connection does not exist.
	ErrConnectionNotFound = errors.New("connection not found")

	// ErrConnectionFailed is returned when a connection cannot be established.
	ErrConnectionFailed = errors.New("database connection failed")

	// ErrDriverNotFound is returned when a driver is not registered for the given name.
	ErrDriverNotFound = errors.New("database driver not found")

	// ErrNoDefault is returned when no default connection has been set.
	ErrNoDefault = errors.New("no default connection set")

	// ErrTransactionFailed is returned when a transaction operation (begin, commit, or rollback) fails.
	ErrTransactionFailed = errors.New("transaction failed")
)

Sentinel errors for the database connection manager. Defined locally to avoid circular dependencies with pkg/migrator.

Functions

func WithTransaction

func WithTransaction(db *sql.DB, fn TxFunc) error

WithTransaction begins a transaction, calls fn, commits on nil return, and rolls back on error. If commit fails, it attempts a rollback and returns a descriptive error.

Types

type ConnectionConfig

type ConnectionConfig struct {
	Driver          string            `yaml:"driver" json:"driver"`
	Host            string            `yaml:"host" json:"host"`
	Port            int               `yaml:"port" json:"port"`
	Database        string            `yaml:"database" json:"database"`
	Username        string            `yaml:"username" json:"username"`
	Password        string            `yaml:"password" json:"password"`
	MaxOpenConns    int               `yaml:"max_open_conns" json:"max_open_conns"`
	MaxIdleConns    int               `yaml:"max_idle_conns" json:"max_idle_conns"`
	ConnMaxLifetime time.Duration     `yaml:"conn_max_lifetime" json:"conn_max_lifetime"`
	Options         map[string]string `yaml:"options" json:"options"`
}

ConnectionConfig holds the configuration for a database connection.

type Driver

type Driver interface {
	Open(config ConnectionConfig) (*sql.DB, error)
	Name() string
}

Driver defines the contract for a database driver that can open connections. This is a local alias to avoid requiring callers to import the drivers sub-package when working with the Manager directly.

type Manager

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

Manager manages multiple named database connections with lazy initialization and connection pool configuration.

func NewManager

func NewManager() *Manager

NewManager creates a new connection Manager with empty registries.

func (*Manager) AddConnection

func (m *Manager) AddConnection(name string, config ConnectionConfig) error

AddConnection stores a named connection configuration. The actual database connection is opened lazily on the first call to Connection(). If this is the first connection added, it becomes the default.

func (*Manager) Close

func (m *Manager) Close() error

Close closes all open database connections and releases pool resources.

func (*Manager) Connection

func (m *Manager) Connection(name string) (*sql.DB, error)

Connection returns an active database connection for the given name. Connections are opened lazily on first request and cached for reuse.

func (*Manager) Default

func (m *Manager) Default() (*sql.DB, error)

Default returns the default database connection.

func (*Manager) RegisterDriver

func (m *Manager) RegisterDriver(name string, driver Driver)

RegisterDriver registers a database driver by name (e.g. "postgres", "mysql", "sqlite3").

func (*Manager) SetDefault

func (m *Manager) SetDefault(name string) error

SetDefault sets the default connection name. The named connection must already be registered via AddConnection.

type TxFunc

type TxFunc func(tx *sql.Tx) error

TxFunc is a function that executes within a database transaction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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