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 tx.Rollback(ctx)
|
|
|
|
|
|
|
|
|
|
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{
|
|
|
|
|
Queryid: dbID,
|
|
|
|
|
Requiredqueryid: reqQuery,
|
|
|
|
|
Addedversion: 1,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return uuid.Nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if query.Config != nil {
|
|
|
|
|
err = qtx.CreateQueryConfig(ctx, repository.CreateQueryConfigParams{
|
|
|
|
|
Queryid: dbID,
|
|
|
|
|
Config: query.Config,
|
|
|
|
|
Addedversion: 1,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return uuid.Nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reqIDs := make([]pgtype.UUID, len(q.RequiredQueryIDs))
|
|
|
|
|
for index, id := range q.RequiredQueryIDs {
|
|
|
|
|
reqIDs[index] = database.MustToDBUUID(id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &createQuery{
|
|
|
|
|
Type: t,
|
|
|
|
|
RequiredQueryIDs: reqIDs,
|
|
|
|
|
Config: []byte(q.Config),
|
|
|
|
|
}, nil
|
|
|
|
|
}
|