Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrDatabaseNotOpen = errors.New("database not opened") ErrDatabaseAlreadyOpen = errors.New("database already opened") ErrDatabaseClosed = errors.New("database closed") ErrCantResetMaintenance = errors.New("can't reset maintenance ticker") ErrInvalidDuration = errors.New("invalid duration for maintenance ticker") MinMaintenanceInterval = 1 * time.Minute )
Functions ¶
This section is empty.
Types ¶
type DBHandle ¶
type DBHandle[T comparable] struct { Ctx context.Context DB *sql.DB Driver DriverName DSN DSNGenerator // contains filtered or unexported fields }
func (*DBHandle[T]) Close ¶
Close will be called when the context passed to Open() is cancelled. It can also be called manually to release resources. It will close the database handle and any prepared statements, and stop any maintenance jobs.
func (*DBHandle[T]) Maintenance ¶
func (s *DBHandle[T]) Maintenance(d time.Duration, f MaintenanceFunction) error
Pass a maintenance function and a duration to run it at. The maintenance function will be called with the context and the database handle. If the function returns an error, the ticker will be stopped. If the duration is 0 or less than a second, an error will be returned. It is possible to set up multiple maintenance functions. The Maintenance ticker will be stopped when the done channel receives a message or is closed. The done channel will be closed when this DBHandle is closed.
type DSNGenerator ¶
type DSNGenerator func() string
DSNGenerator is a function that returns a DSN string.
type MaintenanceFunction ¶
MaintenanceFunction is a function that can be called periodically to perform maintenance on the database. It's passed the context and current database handle. Returning an error will stop the maintenance ticker.
type MaterialDB ¶
func NewDB ¶
func NewDB(dsnF DSNGenerator) *MaterialDB