sql: Refactor use of tables
This commit is contained in:
parent
85fe5b5c5a
commit
73d821e320
19 changed files with 191 additions and 83 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue