dbconn
This commit is contained in:
@@ -4,10 +4,13 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
contextfull "queryorchestration/internal/contextFull"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
jsonextractor "queryorchestration/internal/jsonExtractor"
|
||||
queryprocessor "queryorchestration/internal/queryProcessor"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func (s *Service) Create(ctx context.Context, entity *queryprocessor.Create) (uuid.UUID, error) {
|
||||
@@ -21,10 +24,59 @@ func (s *Service) Create(ctx context.Context, entity *queryprocessor.Create) (uu
|
||||
return uuid.Nil, err
|
||||
}
|
||||
|
||||
// TODO - submit create - type, requiredids, config
|
||||
id := uuid.New()
|
||||
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)
|
||||
|
||||
return id, nil
|
||||
|
||||
}
|
||||
|
||||
func (s *Service) getCreator(qType queryprocessor.Type) (queryprocessor.Creator, error) {
|
||||
@@ -37,3 +89,27 @@ func (s *Service) getCreator(qType queryprocessor.Type) (queryprocessor.Creator,
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user