databaser

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 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

View Source
var ErrUserNotFound = errors.New("user not found")

ErrUserNotFound is returned when a user operation fails because the user doesn't exist.

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, threads uint8) (*DB, error)

New creates a new database connection.

func (*DB) ApproveUser added in v0.1.0

func (db *DB) ApproveUser(ctx context.Context, userID int64) error

ApproveUser sets the approved flag to true for a user by ID.

func (*DB) Close

func (db *DB) Close() error

Close closes the database connection.

func (*DB) DeleteUser added in v0.1.0

func (db *DB) DeleteUser(ctx context.Context, userID int64) error

DeleteUser removes a user by ID from the database.

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) GetApprovedUsers added in v0.1.0

func (db *DB) GetApprovedUsers(ctx context.Context) ([]User, error)

GetApprovedUsers retrieves all approved users from the database.

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) GetPendingUsers added in v0.1.0

func (db *DB) GetPendingUsers(ctx context.Context) ([]User, error)

GetPendingUsers retrieves all pending users from the database.

func (*DB) GetUser added in v0.1.0

func (db *DB) GetUser(ctx context.Context, userID int64) (*User, error)

GetUser retrieves a user by ID from the database.

func (*DB) GetUsers added in v0.1.0

func (db *DB) GetUsers(ctx context.Context) ([]User, error)

GetUsers retrieves all users from the database.

func (*DB) Init

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

Init initializes the database schema.

func (*DB) RejectUser added in v0.1.0

func (db *DB) RejectUser(ctx context.Context, userID int64) error

RejectUser sets the approved flag to false for a user by ID.

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

After reports whether d is after u.

func (*DateOnly) Before added in v0.0.12

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

Before reports whether d is before u.

func (*DateOnly) Date added in v0.0.12

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

Date returns the year, month, and day components.

func (*DateOnly) EndOfYear added in v0.0.12

func (d *DateOnly) EndOfYear() string

EndOfYear returns the last day of the year in "2006-01-02" format.

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)

SetLocation sets the time zone location while preserving the date.

func (*DateOnly) StartOfYear added in v0.0.12

func (d *DateOnly) StartOfYear() string

StartOfYear returns the first day of the year in "2006-01-02" format.

func (*DateOnly) String added in v0.0.12

func (d *DateOnly) String() string

String returns the date in "2006-01-02" format.

func (*DateOnly) Time added in v0.0.12

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

Time converts DateOnly to 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 {
	Created time.Time `db:"created"`
	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.

type User added in v0.1.0

type User struct {
	Created   time.Time `db:"created"`
	Updated   time.Time `db:"updated"`
	Username  string    `db:"username"`
	FirstName string    `db:"first_name"`
	LastName  string    `db:"last_name"`
	ID        int64     `db:"id"`
	Status    uint8     `db:"status"`
}

User represents a user in the database.

func GetOrCreateUser added in v0.1.0

func GetOrCreateUser(ctx context.Context, tx *sqlx.Tx, id int64, username, firstName, lastName string) (*User, error)

GetOrCreateUser retrieves a user by ID or creates a new one if not found.

func (*User) IsApproved added in v0.1.0

func (user *User) IsApproved() bool

IsApproved checks if the user is approved.

func (*User) IsPending added in v0.1.0

func (user *User) IsPending() bool

IsPending checks if the user is pending.

func (*User) IsRejected added in v0.1.0

func (user *User) IsRejected() bool

IsRejected checks if the user is rejected.

func (*User) LogValue added in v0.1.0

func (user *User) LogValue() slog.Value

LogValue implements slog.LogValuer for User.

func (*User) String added in v0.1.0

func (user *User) String() string

String implements stringer for User.

Jump to

Keyboard shortcuts

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