2026-10-07 16:51:44 -06:00
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
2026-10-20 15:16:21 -06:00
|
|
|
"context"
|
2026-10-09 15:38:08 -06:00
|
|
|
"strings"
|
2026-10-07 16:51:44 -06:00
|
|
|
|
|
|
|
|
"code.destrealm.net/zancarius/justusbot/internal/settings"
|
2026-10-09 15:38:08 -06:00
|
|
|
"github.com/jmoiron/sqlx"
|
2026-10-07 16:51:44 -06:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2026-10-14 13:07:43 -06:00
|
|
|
"go.destrealm.net/errors"
|
2026-10-08 15:38:41 -06:00
|
|
|
"go.destrealm.net/migratory"
|
|
|
|
|
"go.destrealm.net/vfs"
|
2026-10-07 16:51:44 -06:00
|
|
|
)
|
|
|
|
|
|
2026-10-14 23:56:48 -06:00
|
|
|
var ErrNoRows = errors.NewError("no rows")
|
2026-10-14 13:07:43 -06:00
|
|
|
var ErrUniqueConstraint = errors.NewError("unique constraint failed")
|
|
|
|
|
|
2026-10-08 15:38:41 -06:00
|
|
|
type Database struct {
|
2026-10-20 15:16:21 -06:00
|
|
|
ctx context.Context
|
2026-10-09 15:38:08 -06:00
|
|
|
db *sqlx.DB
|
2026-10-08 15:38:41 -06:00
|
|
|
conf *settings.Settings
|
2026-10-20 15:34:03 -06:00
|
|
|
|
2026-10-30 14:28:38 -06:00
|
|
|
announcements *AnnouncementModel
|
|
|
|
|
birthdays *BirthdayModel
|
|
|
|
|
chats *ChatModel
|
|
|
|
|
verses *VerseModel
|
2026-10-08 15:38:41 -06:00
|
|
|
}
|
|
|
|
|
|
2026-10-20 15:16:21 -06:00
|
|
|
func OpenDatabase(ctx context.Context, conf *settings.Settings) *Database {
|
2026-10-08 15:38:41 -06:00
|
|
|
return &Database{
|
2026-10-20 15:16:21 -06:00
|
|
|
ctx: ctx,
|
2026-10-08 15:38:41 -06:00
|
|
|
conf: conf,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-09 15:38:08 -06:00
|
|
|
func (d *Database) DB() *sqlx.DB {
|
|
|
|
|
return d.db
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 15:38:41 -06:00
|
|
|
func (d *Database) Open() error {
|
2026-10-09 15:38:08 -06:00
|
|
|
// Examine go/migratory to see why this isn't necessary over there.
|
|
|
|
|
db, err := sqlx.Open("sqlite3", strings.Replace(d.conf.System.DSN, "sqlite3:///", "", 1))
|
2026-10-07 18:57:26 -06:00
|
|
|
if err != nil {
|
2026-10-09 15:38:08 -06:00
|
|
|
return err
|
2026-10-07 18:57:26 -06:00
|
|
|
}
|
2026-10-08 15:38:41 -06:00
|
|
|
d.db = db
|
2026-10-07 16:51:44 -06:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-10-08 15:38:41 -06:00
|
|
|
|
|
|
|
|
func (d *Database) Migrate(migrations vfs.VFS) error {
|
|
|
|
|
mg, err := migratory.New(d.conf.System.DSN)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
mg.UsingVFS(migrations)
|
|
|
|
|
migrator, err := mg.Migrate("migrations/sqlite")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return migrator.Up()
|
|
|
|
|
}
|
2026-10-20 15:16:21 -06:00
|
|
|
|
2026-10-30 14:28:38 -06:00
|
|
|
func (d *Database) Announcements() *AnnouncementModel {
|
|
|
|
|
if d.announcements == nil {
|
|
|
|
|
d.announcements = NewAnnouncementModel(d.ctx, d.db)
|
|
|
|
|
}
|
|
|
|
|
return d.announcements
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-23 21:28:16 -06:00
|
|
|
func (d *Database) Birthdays() *BirthdayModel {
|
|
|
|
|
if d.birthdays == nil {
|
|
|
|
|
d.birthdays = NewBirthdayModel(d.ctx, d.db)
|
|
|
|
|
}
|
|
|
|
|
return d.birthdays
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 15:16:21 -06:00
|
|
|
func (d *Database) Chats() *ChatModel {
|
2026-10-20 15:34:03 -06:00
|
|
|
if d.chats == nil {
|
2026-10-23 21:28:38 -06:00
|
|
|
d.chats = NewChatModel(d.ctx, d.db)
|
2026-10-20 15:34:03 -06:00
|
|
|
}
|
|
|
|
|
return d.chats
|
2026-10-20 15:16:21 -06:00
|
|
|
}
|
|
|
|
|
|
2026-10-20 15:34:03 -06:00
|
|
|
func (d *Database) Verses() *VerseModel {
|
|
|
|
|
if d.verses == nil {
|
|
|
|
|
d.verses = NewVerseModel(d.ctx, d.db)
|
|
|
|
|
}
|
|
|
|
|
return d.verses
|
2026-10-20 15:16:21 -06:00
|
|
|
}
|