executor

package
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package executor provides query and mutation execution logic.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is returned when a "one" type query finds no rows.

View Source
var ErrUnsupportedMutationType = errors.New("unsupported mutation type")

ErrUnsupportedMutationType is returned when mutation type is not supported.

View Source
var ErrUnsupportedQueryType = errors.New("unsupported query type")

ErrUnsupportedQueryType is returned when query type is not supported.

Functions

func CompileMock

func CompileMock(m *config.Mock, opts CompileTransformOptions) (mock.Source, error)

CompileMock compiles a mock source from config.Mock.

func WrapMockError

func WrapMockError(err error) error

WrapMockError wraps an error as a mock-transform error.

func WrapPostError

func WrapPostError(err error) error

WrapPostError wraps an error as a post-transform error.

func WrapPreError

func WrapPreError(err error) error

WrapPreError wraps an error as a pre-transform error.

Types

type BaseExecutor

type BaseExecutor[R any] struct {
	DB         *sqlx.DB
	SQL        string
	OpType     config.OpType
	Entity     config.EntityType
	Transforms *Transforms
	MockSource mock.Source
	Builder    ResultBuilder[R]
}

BaseExecutor contains common executor logic.

func (*BaseExecutor[R]) ExecuteBase

func (e *BaseExecutor[R]) ExecuteBase(reqCtx context.Context, params map[string]any, opts Options, dbExec DBExecutor[R]) (*R, error)

ExecuteBase runs the common execution flow.

func (*BaseExecutor[R]) ProcessManyResult

func (e *BaseExecutor[R]) ProcessManyResult(ctx, originalParams map[string]any, output any, tc *js.TransformContext) (*R, error)

ProcessManyResult processes multiple row results.

func (*BaseExecutor[R]) ProcessNoneResult

func (e *BaseExecutor[R]) ProcessNoneResult(ctx, originalParams map[string]any, tc *js.TransformContext) (*R, error)

ProcessNoneResult processes type: none result (no data returned).

func (*BaseExecutor[R]) ProcessOneResult

func (e *BaseExecutor[R]) ProcessOneResult(ctx, originalParams map[string]any, output any, tc *js.TransformContext) (*R, error)

ProcessOneResult processes a single row result.

type CompileTransformOptions

type CompileTransformOptions struct {
	ConfigDir   string
	Helpers     *js.CompiledHelpers
	ValueParser *mock.ValueParser
}

CompileTransformOptions contains options for compiling transforms.

type DBExecutor

type DBExecutor[R any] interface {
	ExecuteDB(reqCtx context.Context, ec *ExecContext[R]) (*R, error)
}

DBExecutor executes database operations.

type ExecContext

type ExecContext[R any] struct {
	Base           *BaseExecutor[R]
	Ctx            map[string]any
	SQL            string
	Params         map[string]any
	OriginalParams map[string]any
	Opts           Options
	TC             *js.TransformContext
}

ExecContext holds the execution context for DB operations.

type ExecuteResult

type ExecuteResult struct {
	Output         any         // The response data
	Status         int         // HTTP status code (0 means default)
	ResponseHeader http.Header // Response headers to set
}

ExecuteResult holds the result of an execution including response metadata.

type MutationExecutor

type MutationExecutor struct {
	// contains filtered or unexported fields
}

MutationExecutor executes mutations (mock or DB).

func NewMutationExecutor

func NewMutationExecutor(db *sqlx.DB, mutation config.Mutation, opts CompileTransformOptions) (*MutationExecutor, error)

NewMutationExecutor creates a new MutationExecutor with pre-compiled transforms.

func (*MutationExecutor) Execute

func (e *MutationExecutor) Execute(reqCtx context.Context, params map[string]any, opts Options) (*MutationResult, error)

Execute runs the mutation with the given parameters.

func (*MutationExecutor) ExecuteDB

ExecuteDB implements DBExecutor interface.

type MutationResult

type MutationResult struct {
	Data           any
	NoContent      bool
	Status         int
	ResponseHeader http.Header
}

MutationResult holds the result of a mutation execution.

type Options

type Options struct {
	Recorder    Recorder
	HTTPRequest *http.Request // HTTP request for transform context
}

Options contains optional settings for executors.

type PhaseError

type PhaseError struct {
	Phase string // "pre", "mock", or "post"
	Err   error
}

PhaseError wraps an error with the phase (pre/mock/post) it occurred in.

func (*PhaseError) Error

func (e *PhaseError) Error() string

func (*PhaseError) Unwrap

func (e *PhaseError) Unwrap() error

type QueryExecutor

type QueryExecutor struct {
	// contains filtered or unexported fields
}

QueryExecutor executes queries (mock or DB).

func NewQueryExecutor

func NewQueryExecutor(db *sqlx.DB, query config.Query, opts CompileTransformOptions) (*QueryExecutor, error)

NewQueryExecutor creates a new QueryExecutor with pre-compiled transforms.

func (*QueryExecutor) Execute

func (e *QueryExecutor) Execute(reqCtx context.Context, params map[string]any, opts Options) (*ExecuteResult, error)

Execute runs the query with the given parameters.

func (*QueryExecutor) ExecuteDB

func (e *QueryExecutor) ExecuteDB(reqCtx context.Context, ec *ExecContext[ExecuteResult]) (*ExecuteResult, error)

ExecuteDB implements DBExecutor interface.

type Record

type Record struct {
	Type   config.OpType  // OpTypeOne, OpTypeMany, or OpTypeNone
	IsMock bool           // true if mock execution
	SQL    string         // executed SQL (bound for DB, original for mock)
	Params map[string]any // named parameters (for mock)
	Args   []any          // positional arguments (for DB)
}

Record represents an executed query/mutation record for testing/logging.

type Recorder

type Recorder func(record Record)

Recorder is called when a query/mutation is executed.

type ResultBuilder

type ResultBuilder[R any] interface {
	// BuildResult creates the result from output and transform context.
	BuildResult(output any, tc *js.TransformContext) *R
	// OnEmptyOne handles empty result for "one" type. Returns (result, error).
	// If error is non-nil, it's returned. If result is non-nil, it's returned.
	OnEmptyOne(tc *js.TransformContext) (*R, error)
}

ResultBuilder builds the final result from processed output.

type Transforms

type Transforms struct {
	Pre      *js.Transformer
	PostEach *js.Transformer
	PostAll  *js.Transformer
}

Transforms holds pre-compiled transforms.

func CompileTransforms

func CompileTransforms(t *config.Transform, opts CompileTransformOptions) (*Transforms, error)

CompileTransforms compiles all transforms from a config.Transform. Note: mock is now compiled separately via CompileMock.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL