Files
query-orchestration/internal/query/create.go
T

147 lines
3.1 KiB
Go
Raw Normal View History

2025-01-03 13:41:07 +00:00
package query
import (
"context"
"fmt"
2025-01-06 12:26:28 +00:00
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
queryprocessor "queryorchestration/internal/query/processor"
contextfull "queryorchestration/internal/query/types/contextFull"
jsonextractor "queryorchestration/internal/query/types/jsonExtractor"
2025-01-03 13:41:07 +00:00
"github.com/google/uuid"
2025-01-06 12:26:28 +00:00
"github.com/jackc/pgx/v5/pgtype"
2025-01-03 13:41:07 +00:00
)
func (s *Service) Create(ctx context.Context, entity *queryprocessor.Create) (uuid.UUID, error) {
2025-01-20 13:31:48 +00:00
err := s.normalizeCreate(ctx, entity)
2025-01-03 13:41:07 +00:00
if err != nil {
return uuid.Nil, err
}
2025-01-20 13:31:48 +00:00
id, err := s.submitCreate(ctx, entity)
2025-01-03 13:41:07 +00:00
if err != nil {
return uuid.Nil, err
}
2025-01-20 13:31:48 +00:00
return id, err
}
func (s *Service) normalizeCreate(ctx context.Context, entity *queryprocessor.Create) error {
2025-01-23 14:56:20 +00:00
err := s.NormalizeQueryIDs(ctx, entity)
2025-01-06 12:26:28 +00:00
if err != nil {
2025-01-20 13:31:48 +00:00
return err
2025-01-06 12:26:28 +00:00
}
2025-01-20 13:31:48 +00:00
err = s.normalizeConfig(entity)
if err != nil {
return err
}
validator, err := s.getCreator(entity.Type)
if err != nil {
return err
}
err = validator.Validate(ctx, entity)
if err != nil {
return err
}
return nil
2025-01-06 12:26:28 +00:00
}
func (s *Service) submitCreate(ctx context.Context, entity *queryprocessor.Create) (uuid.UUID, error) {
query, err := parseCreateQuery(entity)
if err != nil {
return uuid.Nil, err
}
2025-01-23 14:56:20 +00:00
var dbID pgtype.UUID
err = database.ExecuteTransaction(ctx, s.db, func(ctx context.Context, qtx *repository.Queries) error {
dbID, err = qtx.CreateQuery(ctx, query.Type)
if err != nil {
return err
}
2025-01-06 12:26:28 +00:00
2025-01-23 14:56:20 +00:00
if query.RequiredQueryIDs != nil {
for _, reqQuery := range *query.RequiredQueryIDs {
err = qtx.AddRequiredQuery(ctx, &repository.AddRequiredQueryParams{
Queryid: dbID,
Requiredqueryid: reqQuery,
Addedversion: 1,
})
if err != nil {
return err
}
}
}
2025-01-06 12:26:28 +00:00
2025-01-23 14:56:20 +00:00
if query.Config != nil && string(*query.Config) != "" {
err = qtx.AddQueryConfig(ctx, &repository.AddQueryConfigParams{
Queryid: dbID,
Config: *query.Config,
Addedversion: 1,
2025-01-20 13:31:48 +00:00
})
if err != nil {
2025-01-23 14:56:20 +00:00
return err
2025-01-20 13:31:48 +00:00
}
2025-01-06 12:26:28 +00:00
}
2025-01-23 14:56:20 +00:00
return nil
})
if err != nil {
return uuid.Nil, err
2025-01-06 12:26:28 +00:00
}
2025-01-23 14:56:20 +00:00
id, err := database.ToUUID(dbID)
2025-01-07 16:12:18 +00:00
if err != nil {
return uuid.Nil, err
}
2025-01-03 13:41:07 +00:00
return id, nil
2025-01-06 12:26:28 +00:00
2025-01-03 13:41:07 +00:00
}
func (s *Service) getCreator(qType queryprocessor.Type) (queryprocessor.Creator, error) {
switch qType {
case queryprocessor.TypeJsonExtractor:
return jsonextractor.NewCreator(s.db), nil
case queryprocessor.TypeContextFull:
return contextfull.NewCreator(s.db), nil
default:
return nil, fmt.Errorf("attempting to process invalid query type")
}
}
2025-01-06 12:26:28 +00:00
type createQuery struct {
Type repository.Querytype
2025-01-20 13:31:48 +00:00
RequiredQueryIDs *[]pgtype.UUID
Config *[]byte
2025-01-06 12:26:28 +00:00
}
func parseCreateQuery(q *queryprocessor.Create) (*createQuery, error) {
t, err := queryprocessor.ToDBQueryType(q.Type)
if err != nil {
return nil, err
}
2025-01-20 13:31:48 +00:00
var reqIDs *[]pgtype.UUID
if q.RequiredQueryIDs != nil {
tIDs := database.MustToDBUUIDArray(*q.RequiredQueryIDs)
reqIDs = &tIDs
}
var cfg *[]byte
if q.Config != nil {
tC := []byte(*q.Config)
cfg = &tC
}
2025-01-06 12:26:28 +00:00
return &createQuery{
Type: t,
RequiredQueryIDs: reqIDs,
2025-01-20 13:31:48 +00:00
Config: cfg,
2025-01-06 12:26:28 +00:00
}, nil
}