databaser

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package databaser provides SQLite database connection and operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InTransaction

func InTransaction(ctx context.Context, db *DB, f func(tx *sqlx.Tx) error) error

InTransaction executes the given function within a database transaction.

func SaveManyEventsTx

func SaveManyEventsTx(ctx context.Context, tx *sqlx.Tx, events []*Event) error

SaveManyEventsTx stores multiple events in the database within a transaction.

func SaveManyHolidaysTx added in v0.0.12

func SaveManyHolidaysTx(ctx context.Context, tx *sqlx.Tx, holidays []Holiday) error

SaveManyHolidaysTx stores multiple holidays in the database within a transaction.

Types

type DB

type DB struct {
	*sqlx.DB
}

DB wraps sqlx.DB for database operations.

func New

func New(ctx context.Context, path string) (*DB, error)

New creates a new database connection.

func (*DB) Close

func (db *DB) Close() error

Close closes the database connection.

func (*DB) GetAllEvents added in v0.0.13

func (db *DB) GetAllEvents(ctx context.Context, limit, offset int) ([]Event, error)

GetAllEvents retrieves all events with pagination.

func (*DB) GetEvents

func (db *DB) GetEvents(ctx context.Context, period time.Duration) ([]Event, error)

GetEvents retrieves events to the current time minus the given period.

func (*DB) GetHolidays added in v0.0.12

func (db *DB) GetHolidays(ctx context.Context, year int, location *time.Location) ([]Holiday, error)

GetHolidays retrieves holidays for the specified year and location.

func (*DB) Init

func (db *DB) Init(ctx context.Context) error

Init initializes the database schema.

func (*DB) SaveEvent

func (db *DB) SaveEvent(ctx context.Context, event Event) error

SaveEvent stores an event in the database.

func (*DB) SaveManyEvents

func (db *DB) SaveManyEvents(ctx context.Context, events []Event) error

SaveManyEvents stores multiple events in the database.

type DateOnly added in v0.0.12

type DateOnly time.Time

DateOnly is a custom type that stores only the date part of time.Time.

func (*DateOnly) After added in v0.0.12

func (d *DateOnly) After(u *DateOnly) bool

func (*DateOnly) Before added in v0.0.12

func (d *DateOnly) Before(u *DateOnly) bool

func (*DateOnly) Date added in v0.0.12

func (d *DateOnly) Date() (year int, month time.Month, day int)

func (*DateOnly) EndOfYear added in v0.0.12

func (d *DateOnly) EndOfYear() string

func (*DateOnly) Format added in v0.0.12

func (d *DateOnly) Format(layout string) string

Format formats the DateOnly using the provided layout.

func (*DateOnly) Scan added in v0.0.12

func (d *DateOnly) Scan(value any) error

Scan implements sql.Scanner interface.

func (*DateOnly) SetLocation added in v0.0.12

func (d *DateOnly) SetLocation(loc *time.Location)

func (*DateOnly) StartOfYear added in v0.0.12

func (d *DateOnly) StartOfYear() string

func (*DateOnly) String added in v0.0.12

func (d *DateOnly) String() string

func (*DateOnly) Time added in v0.0.12

func (d *DateOnly) Time() time.Time

func (*DateOnly) Value added in v0.0.12

func (d *DateOnly) Value() (driver.Value, error)

Value implements driver.Valuer interface.

type Event

type Event struct {
	Timestamp time.Time `db:"timestamp"`
	Load      uint8     `db:"load"`
	Predict   float64   `db:"-"`
}

Event represents a load event with a timestamp and load percentage.

func NewEventFromCSVRecord

func NewEventFromCSVRecord(record []string, location *time.Location) (*Event, error)

NewEventFromCSVRecord creates an Event from a CSV record.

func (*Event) FloatLoad added in v0.0.10

func (e *Event) FloatLoad() float64

FloatLoad returns the load as a float64.

func (*Event) LogValue

func (e *Event) LogValue() slog.Value

LogValue implements slog.LogValuer for Event.

type Holiday added in v0.0.12

type Holiday struct {
	Day   *DateOnly `db:"day"`
	Title string    `db:"title"`
}

Holiday represents a holiday with a date and title.

func (*Holiday) LogValue added in v0.0.12

func (h *Holiday) LogValue() slog.Value

LogValue implements slog.LogValuer for Event.

Jump to

Keyboard shortcuts

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