Documentation
¶
Index ¶
- Variables
- func WithTransaction(db *sql.DB, fn TxFunc) error
- type ConnectionConfig
- type Driver
- type Manager
- func (m *Manager) AddConnection(name string, config ConnectionConfig) error
- func (m *Manager) Close() error
- func (m *Manager) Connection(name string) (*sql.DB, error)
- func (m *Manager) Default() (*sql.DB, error)
- func (m *Manager) RegisterDriver(name string, driver Driver)
- func (m *Manager) SetDefault(name string) error
- type TxFunc
Constants ¶
This section is empty.
Variables ¶
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 ¶
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) Connection ¶
Connection returns an active database connection for the given name. Connections are opened lazily on first request and cached for reuse.
func (*Manager) RegisterDriver ¶
RegisterDriver registers a database driver by name (e.g. "postgres", "mysql", "sqlite3").
func (*Manager) SetDefault ¶
SetDefault sets the default connection name. The named connection must already be registered via AddConnection.