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"
|
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-06 12:26:28 +00:00
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
2025-01-03 13:41:07 +00:00
|
|
|
)
|
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
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-03 13:41:07 +00:00
|
|
|
return jsonextractor.NewCreator(s.db), nil
|
2025-01-29 11:52:37 +00:00
|
|
|
case resultprocessor.TypeContextFull:
|
2025-01-03 13:41:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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-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
|
|
|
|
|
}
|