518927c395
Query Update * baselineupdate * baseupdateplusmodelupdates * passtests * somemoresubmittesting * testinnerfunctions * readmeandinstall * cleanerstartup * readmeplusdeps * tidyatrighttime * validatetests * normalizedontvalidate * abitofzenormalizationcleanup * addunitstestforhelperfuns * normalizeactiveversiontestas
151 lines
3.1 KiB
Go
151 lines
3.1 KiB
Go
package query
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"queryorchestration/internal/database"
|
|
"queryorchestration/internal/database/repository"
|
|
queryprocessor "queryorchestration/internal/query/processor"
|
|
contextfull "queryorchestration/internal/query/types/contextFull"
|
|
jsonextractor "queryorchestration/internal/query/types/jsonExtractor"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func (s *Service) Create(ctx context.Context, entity *queryprocessor.Create) (uuid.UUID, error) {
|
|
err := s.normalizeCreate(ctx, entity)
|
|
if err != nil {
|
|
return uuid.Nil, err
|
|
}
|
|
|
|
id, err := s.submitCreate(ctx, entity)
|
|
if err != nil {
|
|
return uuid.Nil, err
|
|
}
|
|
|
|
return id, err
|
|
}
|
|
|
|
func (s *Service) normalizeCreate(ctx context.Context, entity *queryprocessor.Create) error {
|
|
err := s.normalizeQueryIDs(ctx, entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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)
|
|
}()
|
|
|
|
qtx := s.db.Queries.WithTx(tx)
|
|
|
|
dbID, err := qtx.CreateQuery(ctx, query.Type)
|
|
if err != nil {
|
|
return uuid.Nil, err
|
|
}
|
|
|
|
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 uuid.Nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
if query.Config != nil && string(*query.Config) != "" {
|
|
err = qtx.AddQueryConfig(ctx, &repository.AddQueryConfigParams{
|
|
Queryid: dbID,
|
|
Config: *query.Config,
|
|
Addedversion: 1,
|
|
})
|
|
if err != nil {
|
|
return uuid.Nil, err
|
|
}
|
|
}
|
|
|
|
err = tx.Commit(ctx)
|
|
if err != nil {
|
|
return uuid.Nil, err
|
|
}
|
|
|
|
id := database.MustToUUID(dbID)
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
return &createQuery{
|
|
Type: t,
|
|
RequiredQueryIDs: reqIDs,
|
|
Config: cfg,
|
|
}, nil
|
|
}
|