db

package
v0.0.0-...-a7177cb Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2023 License: BSD-2-Clause Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const ErrSchemaAlreadyExists = errors.Sentinel("a schema with that name already has migrations added")
View Source
const ErrUnknownEvent = errors.Sentinel("unknown event type")
View Source
const Reschedule = errors.Sentinel("reschedule event")

Return Reschedule if the event should be rescheduled (offset by the duration returned from Offset)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bot

type Bot struct {
	DB    *DB
	State *state.State
}

type Config

type Config interface {
	GetGuildString(ctx context.Context, guildID discord.GuildID, key string) (val string, err error)
	GetGuildBool(ctx context.Context, guildID discord.GuildID, key string) (val bool, err error)
	GetGuildInt(ctx context.Context, guildID discord.GuildID, key string) (val int64, err error)

	SetGuildString(ctx context.Context, guildID discord.GuildID, key, val string) error
	SetGuildBool(ctx context.Context, guildID discord.GuildID, key string, val bool) error
	SetGuildInt(ctx context.Context, guildID discord.GuildID, key string, val int64) error
	DeleteGuildKey(ctx context.Context, guildID discord.GuildID, key string) error

	GetUserString(ctx context.Context, userID discord.UserID, key string) (val string, err error)
	GetUserBool(ctx context.Context, userID discord.UserID, key string) (val bool, err error)
	GetUserInt(ctx context.Context, userID discord.UserID, key string) (val int64, err error)

	SetUserString(ctx context.Context, userID discord.UserID, key, val string) error
	SetUserBool(ctx context.Context, userID discord.UserID, key string, val bool) error
	SetUserInt(ctx context.Context, userID discord.UserID, key string, val int64) error
	DeleteUserKey(ctx context.Context, userID discord.UserID, key string) error

	GetGuildUserString(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key string) (val string, err error)
	GetGuildUserBool(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key string) (val bool, err error)
	GetGuildUserInt(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key string) (val int64, err error)

	SetGuildUserString(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key, val string) error
	SetGuildUserBool(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key string, val bool) error
	SetGuildUserInt(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key string, val int64) error
	DeleteGuildUserKey(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key string) error
}

type DB

type DB struct {
	*pgxpool.Pool
	// contains filtered or unexported fields
}

func New

func New(dsn string) (*DB, error)

func (*DB) DeleteGuildKey

func (db *DB) DeleteGuildKey(ctx context.Context, guildID discord.GuildID, key string) error

func (*DB) DeleteGuildUserKey

func (db *DB) DeleteGuildUserKey(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key string) error

func (*DB) DeleteUserKey

func (db *DB) DeleteUserKey(ctx context.Context, userID discord.UserID, key string) error

func (*DB) Get

func (db *DB) Get(v any, sql string, args ...any) error

func (*DB) GetContext

func (db *DB) GetContext(ctx context.Context, v any, sql string, args ...any) error

func (*DB) GetGuildBool

func (db *DB) GetGuildBool(ctx context.Context, guildID discord.GuildID, key string) (val bool, err error)

func (*DB) GetGuildInt

func (db *DB) GetGuildInt(ctx context.Context, guildID discord.GuildID, key string) (val int64, err error)

func (*DB) GetGuildString

func (db *DB) GetGuildString(ctx context.Context, guildID discord.GuildID, key string) (val string, err error)

func (*DB) GetGuildUserBool

func (db *DB) GetGuildUserBool(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key string) (val bool, err error)

func (*DB) GetGuildUserInt

func (db *DB) GetGuildUserInt(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key string) (val int64, err error)

func (*DB) GetGuildUserString

func (db *DB) GetGuildUserString(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key string) (val string, err error)

func (*DB) GetUserBool

func (db *DB) GetUserBool(ctx context.Context, userID discord.UserID, key string) (val bool, err error)

func (*DB) GetUserInt

func (db *DB) GetUserInt(ctx context.Context, userID discord.UserID, key string) (val int64, err error)

func (*DB) GetUserString

func (db *DB) GetUserString(ctx context.Context, userID discord.UserID, key string) (val string, err error)

func (*DB) Migrator

func (db *DB) Migrator() MigratorInterface

func (*DB) Scheduler

func (db *DB) Scheduler() SchedulerInterface

func (*DB) Select

func (db *DB) Select(v any, sql string, args ...any) error

func (*DB) SelectContext

func (db *DB) SelectContext(ctx context.Context, v any, sql string, args ...any) error

func (*DB) SetGuildBool

func (db *DB) SetGuildBool(ctx context.Context, guildID discord.GuildID, key string, val bool) error

func (*DB) SetGuildInt

func (db *DB) SetGuildInt(ctx context.Context, guildID discord.GuildID, key string, val int64) error

func (*DB) SetGuildString

func (db *DB) SetGuildString(ctx context.Context, guildID discord.GuildID, key, val string) error

func (*DB) SetGuildUserBool

func (db *DB) SetGuildUserBool(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key string, val bool) error

func (*DB) SetGuildUserInt

func (db *DB) SetGuildUserInt(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key string, val int64) error

func (*DB) SetGuildUserString

func (db *DB) SetGuildUserString(ctx context.Context, guildID discord.GuildID, userID discord.UserID, key, val string) error

func (*DB) SetUserBool

func (db *DB) SetUserBool(ctx context.Context, userID discord.UserID, key string, val bool) error

func (*DB) SetUserInt

func (db *DB) SetUserInt(ctx context.Context, userID discord.UserID, key string, val int64) error

func (*DB) SetUserString

func (db *DB) SetUserString(ctx context.Context, userID discord.UserID, key, val string) error

type Event

type Event interface {
	Execute(ctx context.Context, id int64, bot *Bot) error
	Offset() time.Duration
}

Event is any event that can be scheduled. Execute is called when the event is due to fire, Offset is called to determine how much to move the event.

type Migrator

type Migrator struct {
	// contains filtered or unexported fields
}

Migrator manages database migrations. Each module should use its own separate database schema, to avoid conflicting with other modules. All modules should use the database independently from each other (no cross-schema relations), even though they use the same connection pool.

func (*Migrator) AddMigrations

func (m *Migrator) AddMigrations(schema string, src migrate.MigrationSource) error

func (*Migrator) Migrate

func (m *Migrator) Migrate() error

type MigratorInterface

type MigratorInterface interface {
	AddMigrations(schema string, src migrate.MigrationSource) error
	Migrate() error
}

type Scheduler

type Scheduler struct {
	*pgxpool.Pool
	// contains filtered or unexported fields
}

func NewScheduler

func NewScheduler(p *pgxpool.Pool) *Scheduler

func (*Scheduler) Add

func (s *Scheduler) Add(t time.Time, v Event) (id int64, err error)

func (*Scheduler) AddType

func (s *Scheduler) AddType(events ...Event)

AddType adds event types. These should be *pointers*, anything else will panic, even if it implements Event!

func (*Scheduler) Remove

func (s *Scheduler) Remove(id int64) error

func (*Scheduler) Reschedule

func (s *Scheduler) Reschedule(id int64, dur time.Duration) error

func (*Scheduler) Start

func (s *Scheduler) Start(bot *Bot)

Start starts the scheduler. *This function is blocking!*

type SchedulerInterface

type SchedulerInterface interface {
	AddType(events ...Event)
	Add(t time.Time, v Event) (id int64, err error)
	Remove(id int64) error
	Reschedule(id int64, dur time.Duration) error
}

Jump to

Keyboard shortcuts

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