database

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func (s *DBHandle[T]) Close() error

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.

func (*DBHandle[T]) Open

func (s *DBHandle[T]) Open(ctx context.Context) error

func (DBHandle[T]) Ping

func (s DBHandle[T]) Ping() error

func (*DBHandle[T]) Statement

func (s *DBHandle[T]) Statement(key T, generator StatementGenerator) (*sql.Stmt, error)

type DSNGenerator

type DSNGenerator func() string

DSNGenerator is a function that returns a DSN string.

type DriverName

type DriverName string
const (
	SQLite DriverName = "sqlite3"
)

type MaintenanceFunction

type MaintenanceFunction func(ctx context.Context, db *sql.DB, t time.Time) error

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

type MaterialDB struct {
	DBHandle[string]
}

func NewDB

func NewDB(dsnF DSNGenerator) *MaterialDB

type StatementGenerator

type StatementGenerator func(ctx context.Context, db *sql.DB) (*sql.Stmt, error)

StatementGenerator is a function that returns a prepared statement. The DBHandle holds a map of prepared statements, and will clean them up when closing.

Jump to

Keyboard shortcuts

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