2025-01-03 13:41:07 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2025-03-05 12:05:46 +00:00
|
|
|
|
2025-01-06 12:26:28 +00:00
|
|
|
"queryorchestration/internal/database/repository"
|
2025-01-29 11:52:37 +00:00
|
|
|
resultprocessor "queryorchestration/internal/query/result/processor"
|
2025-01-17 12:00:32 +00:00
|
|
|
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-29 11:52:37 +00:00
|
|
|
func (s *Service) Create(ctx context.Context, entity *resultprocessor.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
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-29 11:52:37 +00:00
|
|
|
func (s *Service) normalizeCreate(ctx context.Context, entity *resultprocessor.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-02-14 10:56:24 +00:00
|
|
|
err = s.NormalizeConfig(entity)
|
2025-01-20 13:31:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2025-01-29 11:52:37 +00:00
|
|
|
func (s *Service) submitCreate(ctx context.Context, entity *resultprocessor.Create) (uuid.UUID, error) {
|
2025-01-06 12:26:28 +00:00
|
|
|
query, err := parseCreateQuery(entity)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return uuid.Nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
var id uuid.UUID
|
2025-01-31 13:43:55 +00:00
|
|
|
err = s.cfg.ExecuteDBTransaction(ctx, func(ctx context.Context, qtx *repository.Queries) error {
|
2025-03-20 11:06:41 +00:00
|
|
|
id, err = qtx.CreateQuery(ctx, query.Type)
|
2025-01-23 14:56:20 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-01-06 12:26:28 +00:00
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
version, err := qtx.AddLatestQueryVersion(ctx, id)
|
2025-02-14 18:43:26 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = qtx.AddActiveQueryVersion(ctx, &repository.AddActiveQueryVersionParams{
|
2025-03-20 11:06:41 +00:00
|
|
|
Queryid: id,
|
2025-02-14 18:43:26 +00:00
|
|
|
Versionid: version,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 14:56:20 +00:00
|
|
|
if query.RequiredQueryIDs != nil {
|
|
|
|
|
for _, reqQuery := range *query.RequiredQueryIDs {
|
|
|
|
|
err = qtx.AddRequiredQuery(ctx, &repository.AddRequiredQueryParams{
|
2025-03-20 11:06:41 +00:00
|
|
|
Queryid: id,
|
2025-01-23 14:56:20 +00:00
|
|
|
Requiredqueryid: reqQuery,
|
2025-02-14 18:43:26 +00:00
|
|
|
Addedversion: version,
|
2025-01-23 14:56:20 +00:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-06 12:26:28 +00:00
|
|
|
|
2025-02-14 18:43:26 +00:00
|
|
|
if query.Config != nil {
|
2025-02-17 14:30:37 +00:00
|
|
|
err = qtx.SetQueryConfig(ctx, &repository.SetQueryConfigParams{
|
2025-03-20 11:06:41 +00:00
|
|
|
Queryid: id,
|
2025-01-23 14:56:20 +00:00
|
|
|
Config: *query.Config,
|
2025-02-14 18:43:26 +00:00
|
|
|
Addedversion: version,
|
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-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
|
|
|
}
|
|
|
|
|
|
2025-01-29 11:52:37 +00:00
|
|
|
func (s *Service) getCreator(qType resultprocessor.Type) (resultprocessor.Creator, error) {
|
2025-01-03 13:41:07 +00:00
|
|
|
switch qType {
|
2025-01-29 11:52:37 +00:00
|
|
|
case resultprocessor.TypeJsonExtractor:
|
2025-01-31 13:43:55 +00:00
|
|
|
return jsonextractor.NewCreator(), nil
|
2025-01-29 11:52:37 +00:00
|
|
|
case resultprocessor.TypeContextFull:
|
2025-01-31 13:43:55 +00:00
|
|
|
return contextfull.NewCreator(), nil
|
2025-01-03 13:41:07 +00:00
|
|
|
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-03-20 11:06:41 +00:00
|
|
|
RequiredQueryIDs *[]uuid.UUID
|
2025-01-20 13:31:48 +00:00
|
|
|
Config *[]byte
|
2025-01-06 12:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-29 11:52:37 +00:00
|
|
|
func parseCreateQuery(q *resultprocessor.Create) (*createQuery, error) {
|
|
|
|
|
t, err := resultprocessor.ToDBQueryType(q.Type)
|
2025-01-06 12:26:28 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
var reqIDs *[]uuid.UUID
|
2025-01-20 13:31:48 +00:00
|
|
|
if q.RequiredQueryIDs != nil {
|
2025-03-20 11:06:41 +00:00
|
|
|
tIDs := *q.RequiredQueryIDs
|
2025-01-20 13:31:48 +00:00
|
|
|
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
|
|
|
|
|
}
|