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

120 lines
2.6 KiB
Go
Raw Normal View History

2025-01-03 13:41:07 +00:00
package query
import (
"context"
"fmt"
contextfull "queryorchestration/internal/contextFull"
2025-01-06 12:26:28 +00:00
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
2025-01-03 13:41:07 +00:00
jsonextractor "queryorchestration/internal/jsonExtractor"
queryprocessor "queryorchestration/internal/queryProcessor"
"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) {
validator, err := s.getCreator(entity.Type)
if err != nil {
return uuid.Nil, err
}
2025-01-03 16:00:36 +00:00
err = validator.Validate(ctx, entity)
2025-01-03 13:41:07 +00:00
if err != nil {
return uuid.Nil, err
}
2025-01-06 12:26:28 +00:00
id, err := s.submitCreate(ctx, entity)
if err != nil {
return uuid.Nil, err
}
return id, err
}
func (s *Service) submitCreate(ctx context.Context, entity *queryprocessor.Create) (uuid.UUID, error) {
query, err := parseCreateQuery(entity)
if err != nil {
return uuid.Nil, err
}
tx, err := s.db.Pool.Begin(ctx)
if err != nil {
return uuid.Nil, err
}
defer func() {
_ = tx.Rollback(ctx)
}()
2025-01-06 12:26:28 +00:00
qtx := s.db.Queries.WithTx(tx)
dbID, err := qtx.CreateQuery(ctx, query.Type)
if err != nil {
return uuid.Nil, err
}
for _, reqQuery := range query.RequiredQueryIDs {
err = qtx.CreateRequiredQuery(ctx, &repository.CreateRequiredQueryParams{
2025-01-06 12:26:28 +00:00
Queryid: dbID,
Requiredqueryid: reqQuery,
Addedversion: 1,
})
if err != nil {
return uuid.Nil, err
}
}
2025-01-06 16:29:38 +00:00
if query.Config != nil && string(query.Config) != "" {
err = qtx.CreateQueryConfig(ctx, &repository.CreateQueryConfigParams{
2025-01-06 12:26:28 +00:00
Queryid: dbID,
Config: query.Config,
Addedversion: 1,
})
if err != nil {
return uuid.Nil, err
}
}
2025-01-07 16:12:18 +00:00
err = tx.Commit(ctx)
if err != nil {
return uuid.Nil, err
}
2025-01-06 12:26:28 +00:00
id := database.MustToUUID(dbID)
2025-01-03 16:00:36 +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
}
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
RequiredQueryIDs []pgtype.UUID
Config []byte
}
func parseCreateQuery(q *queryprocessor.Create) (*createQuery, error) {
t, err := queryprocessor.ToDBQueryType(q.Type)
if err != nil {
return nil, err
}
2025-01-06 15:31:39 +00:00
reqIDs := database.MustToDBUUIDArray(q.RequiredQueryIDs)
2025-01-06 12:26:28 +00:00
return &createQuery{
Type: t,
RequiredQueryIDs: reqIDs,
Config: []byte(q.Config),
}, nil
}