Documentation
¶
Overview ¶
Package eval provides T-SQL expression evaluation for xolu FSM guard conditions and set clauses.
It is a surgical extraction of the ExpressionEvaluator from github.com/ha1tch/aulsql/pkg/tsqlruntime. The Interpreter, ExecutionContext, TempTableManager, CursorManager, and all database machinery have been removed. Only the expression evaluator, the Value type system, ToValue, and the built-in function registry are retained.
Entry points ¶
EvalGuard evaluates a boolean guard expression against machine variables and a walk payload:
ok, err := eval.EvalGuard(e, "payload.result = 'pass' AND @retries < 3",
map[string]interface{}{"retries": 2},
map[string]interface{}{"result": "pass", "technician": "alice"})
EvalSet evaluates an arithmetic set-clause expression and returns the new variable value:
val, err := eval.EvalSet(e, "@retries + 1", map[string]interface{}{"retries": 2})
Payload binding convention ¶
Payload fields are bound into the variable map with the prefix "payload." so that the expression `payload.result` resolves to the payload field named "result". This prefix is flattened — nested payload objects are not supported in guard expressions; only top-level string/number/bool fields are accessible.
Machine variables are bound without prefix. A variable declared as `@retries INTEGER` is bound as key "retries" (the @ is stripped by SetVariable). Guard expressions reference it as `@retries`.
Generator functions ¶
The four stateless generators are registered on every Evaluator so they are available in FSM set clauses:
SET @id = UUID_V4() SET @ref = CUID()
Package tsqlruntime provides a runtime interpreter for T-SQL, enabling execution of dynamic SQL at runtime in Go applications.
Index ¶
- func ClearErrorContext()
- func ContainsNextValueFor(expr string) (bool, error)
- func EvalGuard(e *Evaluator, expr string, vars map[string]interface{}, ...) (bool, error)
- func EvalGuardWithQuery(e *Evaluator, expr string, vars, payload, query map[string]interface{}) (bool, error)
- func EvalSet(e *Evaluator, expr string, vars map[string]interface{}) (interface{}, error)
- func EvalSetWithSeq(e *Evaluator, expr string, vars map[string]interface{}) (interface{}, error)
- func ForceTop1(query string) (string, error)
- func FromValue(v Value) interface{}
- func ParseGuard(expr string) (ast.Expression, error)
- func SetErrorContext(errNum int, msg string, line int, proc string, state int, severity int)
- type DataType
- type Evaluator
- func (e *Evaluator) BindPayload(payload map[string]interface{})
- func (e *Evaluator) BindQuery(cols map[string]interface{})
- func (e *Evaluator) BindVars(vars map[string]interface{})
- func (e *Evaluator) RegisterFunc(name string, fn func(args []Value) (Value, error))
- func (e *Evaluator) SetSeqIncrementor(fn SeqIncrementor)
- type ExclusivityResult
- type ExpressionEvaluator
- type Function
- type FunctionRegistry
- type GuardExpr
- type SeqIncrementor
- type Value
- func Cast(v Value, targetType DataType, precision, scale, maxLen int) (Value, error)
- func Convert(v Value, targetType DataType, precision, scale, maxLen int, style int) (Value, error)
- func NewBigInt(v int64) Value
- func NewBinary(v []byte) Value
- func NewBit(v bool) Value
- func NewChar(v string, length int) Value
- func NewDate(v time.Time) Value
- func NewDateTime(v time.Time) Value
- func NewDecimal(v decimal.Decimal, precision, scale int) Value
- func NewDecimalFromString(s string, precision, scale int) (Value, error)
- func NewFloat(v float64) Value
- func NewInt(v int64) Value
- func NewMoney(v decimal.Decimal) Value
- func NewNVarChar(v string, maxLen int) Value
- func NewReal(v float32) Value
- func NewSmallInt(v int16) Value
- func NewTime(v time.Time) Value
- func NewTinyInt(v uint8) Value
- func NewVarBinary(v []byte, maxLen int) Value
- func NewVarChar(v string, maxLen int) Value
- func Null(dt DataType) Value
- func ToValue(v interface{}) Value
- func (v Value) Add(other Value) Value
- func (v Value) And(other Value) Value
- func (v Value) AsBool() bool
- func (v Value) AsDecimal() decimal.Decimal
- func (v Value) AsFloat() float64
- func (v Value) AsInt() int64
- func (v Value) AsString() string
- func (v Value) AsTime() time.Time
- func (v Value) BitwiseAnd(other Value) Value
- func (v Value) BitwiseNot() Value
- func (v Value) BitwiseOr(other Value) Value
- func (v Value) BitwiseXor(other Value) Value
- func (v Value) Clone() Value
- func (v Value) Compare(other Value) int
- func (v Value) Div(other Value) Value
- func (v Value) Equals(other Value) Value
- func (v Value) GreaterThan(other Value) Value
- func (v Value) GreaterThanOrEqual(other Value) Value
- func (v Value) IsTruthy() bool
- func (v Value) LessThan(other Value) Value
- func (v Value) LessThanOrEqual(other Value) Value
- func (v Value) Mod(other Value) Value
- func (v Value) Mul(other Value) Value
- func (v Value) Neg() Value
- func (v Value) Not() Value
- func (v Value) NotEquals(other Value) Value
- func (v Value) Or(other Value) Value
- func (v Value) Power(exp Value) Value
- func (v Value) Sub(other Value) Value
- func (v Value) ToInterface() interface{}
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContainsNextValueFor ¶
ContainsNextValueFor reports whether a set-clause expression string references NEXT VALUE FOR. The walk runtime uses this to decide whether a set clause must run inside the sequence-aware path.
func EvalGuard ¶
func EvalGuard(e *Evaluator, expr string, vars map[string]interface{}, payload map[string]interface{}) (bool, error)
EvalGuard parses and evaluates a boolean guard expression. Returns true if the guard passes, false if it fails, and an error if the expression cannot be parsed or evaluated.
vars and payload are bound before evaluation; they do not persist between calls. Create a new Evaluator per walk step.
func EvalGuardWithQuery ¶
func EvalGuardWithQuery(e *Evaluator, expr string, vars, payload, query map[string]interface{}) (bool, error)
EvalGuardWithQuery evaluates a guard with vars, payload, and transition pre-query result columns (bound under the "query." prefix) all in scope.
func EvalSet ¶
EvalSet parses and evaluates a set-clause expression and returns the result as a Go value suitable for storing back into machine variables.
vars is bound before evaluation. The expression should be an arithmetic or string expression (e.g. "@retries + 1", "UPPER(@status)").
func EvalSetWithSeq ¶
EvalSetWithSeq evaluates a set-clause expression, resolving any NEXT VALUE FOR references via the installed incrementor before evaluation. If the expression contains no NEXT VALUE FOR it behaves exactly like EvalSet. If it contains NEXT VALUE FOR and no incrementor is installed, it returns an error.
func ForceTop1 ¶
ForceTop1 parses a SELECT query, forces TOP 1 onto it (replacing any existing TOP), and returns the rewritten query text. It returns an error if the query does not parse or is not a single SELECT statement. ORDER BY, WHERE, and all other clauses are preserved; only the row-count bound is imposed.
func FromValue ¶
func FromValue(v Value) interface{}
FromValue converts a runtime Value to a Go value
func ParseGuard ¶
func ParseGuard(expr string) (ast.Expression, error)
ParseGuard parses a guard expression string and returns the AST node. Use this at FSM definition creation time to validate guard syntax without evaluating. Returns a non-nil error if the expression is syntactically invalid.
Types ¶
type DataType ¶
type DataType int
DataType represents T-SQL data types
const ( TypeUnknown DataType = iota // Integer types TypeBit TypeTinyInt TypeSmallInt TypeInt TypeBigInt // Exact numeric TypeDecimal TypeNumeric TypeMoney TypeSmallMoney // Approximate numeric TypeFloat TypeReal // Date/time TypeDate TypeTime TypeDateTime TypeDateTime2 TypeSmallDateTime TypeDateTimeOffset // String TypeChar TypeVarChar TypeNChar TypeNVarChar TypeText TypeNText // Binary TypeBinary TypeVarBinary // Other TypeUniqueIdentifier TypeXML TypeTable )
func ParseDataType ¶
ParseDataType parses a T-SQL type name into DataType with precision/scale/maxlen
func (DataType) IsDateTime ¶
IsDateTime returns true if the type is a date/time type
type Evaluator ¶
type Evaluator struct {
// contains filtered or unexported fields
}
Evaluator wraps ExpressionEvaluator with the xolu-specific API. Create one per FSM definition validation or per walk execution. Evaluators are not safe for concurrent use.
func New ¶
func New() *Evaluator
New creates a new Evaluator with the built-in function registry seeded with the four stateless generators.
func (*Evaluator) BindPayload ¶
BindPayload binds walk payload fields into the evaluator under the "payload." prefix. A payload field "result" is accessible in expressions as `payload.result`. Only top-level string, number, and bool fields are bound; nested objects are skipped.
func (*Evaluator) BindQuery ¶
BindQuery binds transition pre-query result columns under the "query." prefix, so a guard or set clause can read query.<column>. Like BindPayload, only top-level scalar columns are bound.
func (*Evaluator) BindVars ¶
BindVars binds machine variable values into the evaluator. The @ prefix is stripped from keys; `@retries` and `retries` both bind as "retries". Values are converted via ToValue.
func (*Evaluator) RegisterFunc ¶
RegisterFunc registers a custom function on this Evaluator. name is normalised to uppercase. Use this to register @SEQ, @GEN, or domain-specific functions needed in a specific FSM definition.
func (*Evaluator) SetSeqIncrementor ¶
func (e *Evaluator) SetSeqIncrementor(fn SeqIncrementor)
SetSeqIncrementor installs the sequence incrementor used by EvalSetWithSeq. When nil (the default), a NEXT VALUE FOR in a set clause produces an error rather than silently evaluating to nil.
type ExclusivityResult ¶
ExclusivityResult is the recognizer's verdict.
func CheckExclusivity ¶
func CheckExclusivity(guards []GuardExpr) ExclusivityResult
CheckExclusivity reports whether guards are provably pairwise mutually exclusive. Empty or single-guard sets are trivially exclusive.
type ExpressionEvaluator ¶
type ExpressionEvaluator struct {
// contains filtered or unexported fields
}
ExpressionEvaluator evaluates T-SQL expressions at runtime
func NewExpressionEvaluator ¶
func NewExpressionEvaluator() *ExpressionEvaluator
NewExpressionEvaluator creates a new expression evaluator
func (*ExpressionEvaluator) Evaluate ¶
func (e *ExpressionEvaluator) Evaluate(expr ast.Expression) (Value, error)
Evaluate evaluates an AST expression and returns its value
func (*ExpressionEvaluator) GetVariable ¶
func (e *ExpressionEvaluator) GetVariable(name string) (Value, bool)
GetVariable gets a variable value
func (*ExpressionEvaluator) SetVariable ¶
func (e *ExpressionEvaluator) SetVariable(name string, value Value)
SetVariable sets a variable value
func (*ExpressionEvaluator) SetVariables ¶
func (e *ExpressionEvaluator) SetVariables(vars map[string]interface{})
SetVariables sets multiple variables from a map
type FunctionRegistry ¶
type FunctionRegistry struct {
// contains filtered or unexported fields
}
FunctionRegistry holds all registered functions
func NewFunctionRegistry ¶
func NewFunctionRegistry() *FunctionRegistry
NewFunctionRegistry creates a new function registry with built-in functions
func (*FunctionRegistry) Call ¶
func (r *FunctionRegistry) Call(name string, args []Value) (Value, error)
Call invokes a function by name
func (*FunctionRegistry) Has ¶
func (r *FunctionRegistry) Has(name string) bool
Has returns true if the function exists
func (*FunctionRegistry) Register ¶
func (r *FunctionRegistry) Register(name string, fn Function)
Register adds a function to the registry
type GuardExpr ¶
type GuardExpr struct {
Source string
AST ast.Expression
}
GuardExpr pairs a guard's source text with its parsed AST.
type SeqIncrementor ¶
SeqIncrementor increments the named sequence and returns the new value. It is expected to run on the caller's transaction so the increment is atomic with the surrounding walk. A non-nil error aborts the set clause.
type Value ¶
type Value struct {
Type DataType
IsNull bool
Precision int // For decimal/numeric
Scale int // For decimal/numeric
MaxLen int // For string/binary types
// contains filtered or unexported fields
}
Value represents a runtime T-SQL value with type information
func NewDecimal ¶
NewDecimal creates a decimal value
func NewDecimalFromString ¶
NewDecimalFromString creates a decimal from a string
func NewNVarChar ¶
NewNVarChar creates an nvarchar value
func NewVarBinary ¶
NewVarBinary creates a new varbinary value
func (Value) BitwiseAnd ¶
BitwiseAnd performs bitwise AND
func (Value) BitwiseXor ¶
BitwiseXor performs bitwise XOR
func (Value) GreaterThan ¶
GreaterThan compares values
func (Value) GreaterThanOrEqual ¶
GreaterThanOrEqual compares values
func (Value) IsTruthy ¶
IsTruthy returns true if the value is considered "true" in a boolean context
func (Value) LessThanOrEqual ¶
LessThanOrEqual compares values
func (Value) ToInterface ¶
func (v Value) ToInterface() interface{}
ToInterface converts the Value to a Go interface{} for use with JSON/XML