sql: Refactor use of tables

This commit is contained in:
Tom Wiesing 2023-01-09 10:24:24 +01:00
parent 85fe5b5c5a
commit 73d821e320
No known key found for this signature in database
19 changed files with 191 additions and 83 deletions

View file

@ -5,6 +5,8 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/tkw1536/goprogram/lib/reflectx"
)
// Component meta is responsible for managing metadata per WissKI Instance
@ -20,8 +22,16 @@ type Meta struct {
var (
_ component.Provisionable = (*Meta)(nil)
_ component.Table = (*Meta)(nil)
)
func (*Meta) TableInfo() component.TableInfo {
return component.TableInfo{
Model: reflectx.TypeOf[models.Metadatum](),
Name: models.MetadataTable,
}
}
// Storage returns a Storage for the instance with the given slug.
// When slug is nil, returns a global storage.
func (meta *Meta) Storage(slug string) *Storage {
@ -40,8 +50,9 @@ func (meta *Meta) Storage(slug string) *Storage {
// create a new storage
meta.sc[slug] = &Storage{
Slug: slug,
sql: meta.Dependencies.SQL,
Slug: slug,
sql: meta.Dependencies.SQL,
table: meta,
}
return meta.sc[slug]
}

View file

@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/tkw1536/goprogram/lib/collection"
@ -20,13 +21,15 @@ var ErrMetadatumNotSet = errors.New("metadatum not set")
// Storage manages metadata for either the entire distillery, or a single slug
type Storage struct {
Slug string
sql *sql.SQL
table component.Table
sql *sql.SQL
}
// Get retrieves metadata with the provided key and deserializes the first one into target.
// If no metadatum exists, returns [ErrMetadatumNotSet].
func (s Storage) Get(ctx context.Context, key Key, target any) error {
table, err := s.sql.QueryTable(ctx, true, models.MetadataTable)
table, err := s.sql.QueryTable(ctx, s.table)
if err != nil {
return err
}
@ -55,7 +58,7 @@ func (s Storage) Get(ctx context.Context, key Key, target any) error {
//
// When no metadatum exists, targets is not called, and nil error is returned.
func (s Storage) GetAll(ctx context.Context, key Key, target func(index, total int) any) error {
table, err := s.sql.QueryTable(ctx, true, models.MetadataTable)
table, err := s.sql.QueryTable(ctx, s.table)
if err != nil {
return err
}
@ -82,7 +85,7 @@ func (s Storage) GetAll(ctx context.Context, key Key, target func(index, total i
// Delete deletes all metadata with the provided key.
func (s Storage) Delete(ctx context.Context, key Key) error {
table, err := s.sql.QueryTable(ctx, true, models.MetadataTable)
table, err := s.sql.QueryTable(ctx, s.table)
if err != nil {
return err
}
@ -98,7 +101,7 @@ func (s Storage) Delete(ctx context.Context, key Key) error {
// Set serializes value and stores it with the provided key.
// Any other metadata with the same key is deleted.
func (s Storage) Set(ctx context.Context, key Key, value any) error {
table, err := s.sql.QueryTable(ctx, true, models.MetadataTable)
table, err := s.sql.QueryTable(ctx, s.table)
if err != nil {
return err
}
@ -133,7 +136,7 @@ func (s Storage) Set(ctx context.Context, key Key, value any) error {
// Set serializes values and stores them with the provided key.
// Any other metadata with the same key is deleted.
func (s Storage) SetAll(ctx context.Context, key Key, values ...any) error {
table, err := s.sql.QueryTable(ctx, true, models.MetadataTable)
table, err := s.sql.QueryTable(ctx, s.table)
if err != nil {
return err
}
@ -167,7 +170,7 @@ func (s Storage) SetAll(ctx context.Context, key Key, values ...any) error {
// Purge removes all metadata, regardless of key.
func (s Storage) Purge(ctx context.Context) error {
table, err := s.sql.QueryTable(ctx, true, models.MetadataTable)
table, err := s.sql.QueryTable(ctx, s.table)
if err != nil {
return err
}