sql

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package sql compiles validated policy conditions into provider-rendered, parameterized SQL predicate fragments. It owns traversal, relation correlation, alias allocation, and bind ordering; dialects own leaf syntax and physical value codecs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RebasePlaceholders added in v0.2.0

func RebasePlaceholders(text string, offset int, provider ir.Provider) string

Types

type Binder

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

Binder is the only path from a dialect leaf to statement arguments.

func (*Binder) Integer

func (binder *Binder) Integer(value int64) string

func (*Binder) Text

func (binder *Binder) Text(value string) string

func (*Binder) Value

func (binder *Binder) Value(value ir.Value, typ ir.TypeRef) (string, error)

type BoundValue

type BoundValue struct {
	Value     ir.Value
	Type      ir.TypeRef
	EnumWires []string
}

BoundValue carries a typed immutable operand plus any descriptor-resolved enum wire values needed by physical storage. Dialects never guess persisted enum labels from stable identity bytes.

type CapabilityProof

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

CapabilityProof is an immutable snapshot of capabilities measured for the active provider instance. It is separate from static schema declarations.

func NewCapabilityProof

func NewCapabilityProof(provider ir.Provider, fingerprint [32]byte, capabilities ...ir.Capability) (CapabilityProof, error)

func (CapabilityProof) Has

func (proof CapabilityProof) Has(capability ir.Capability) bool

func (CapabilityProof) Provider

func (proof CapabilityProof) Provider() ir.Provider

func (CapabilityProof) SchemaFingerprint

func (proof CapabilityProof) SchemaFingerprint() [32]byte

type Column

type Column struct {
	Alias physical.PhysicalName
	Name  physical.PhysicalName
}

type Correlation

type Correlation struct {
	Parent ir.FieldID
	Child  ir.FieldID
}

type Dialect

type Dialect interface {
	Provider() ir.Provider
	Quote(physical.PhysicalName) string
	Table(Model) string
	Placeholder(position int) string
	Encode(BoundValue) (any, error)
	Supports(ir.OperatorID) bool
	RenderScalar(ScalarLeaf, *Binder) (string, error)
	RenderList(ListLeaf, *Binder) (string, error)
	RenderJSON(JSONLeaf, *Binder) (string, error)
}

Dialect is deliberately high-level: it cannot alter logical traversal or relation quantifier semantics. A dialect renders only closed leaf families, quotes descriptor names, and encodes typed values.

type Error

type Error struct {
	Code       ErrorCode
	ModelID    ir.ModelID
	FieldID    ir.FieldID
	OperatorID ir.OperatorID
	Detail     string
	Cause      error
}

func (*Error) Error

func (err *Error) Error() string

func (*Error) Unwrap

func (err *Error) Unwrap() error

type ErrorCode

type ErrorCode string
const (
	CodeInput      ErrorCode = "P2_SQL_INPUT"
	CodeSchema     ErrorCode = "P2_SQL_SCHEMA"
	CodeProvider   ErrorCode = "P2_SQL_PROVIDER"
	CodeCapability ErrorCode = "P2_SQL_CAPABILITY"
	CodeRender     ErrorCode = "P2_SQL_RENDER"
)

type Field

type Field struct {
	Model    ir.ModelID
	ID       ir.FieldID
	Column   physical.PhysicalName
	Type     ir.TypeRef
	Nullable bool
}

type Fragment

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

Fragment is an immutable predicate and its positional arguments. SQL is produced only by the shared walker and a closed provider dialect.

func Compile

func Compile(request Request) (Fragment, error)

func CompileWithPolicyAliasAllocator added in v0.1.0

func CompileWithPolicyAliasAllocator(request Request, aliases *PolicyAliasAllocator) (Fragment, error)

CompileWithPolicyAliasAllocator compiles one fragment within an outer renderer-owned statement allocation scope. The allocator must be fresh for that statement and shared by every fragment that contributes SQL to it.

func (Fragment) Args

func (fragment Fragment) Args() []any

func (Fragment) PolicyRelationAliases added in v0.1.0

func (fragment Fragment) PolicyRelationAliases() []PolicyRelationAliasFact

PolicyRelationAliases returns allocation-ordered, caller-owned facts for aliases allocated by this policy fragment. The outer renderer owns and registers the root alias separately.

func (Fragment) SQL

func (fragment Fragment) SQL() string

type JSONLeaf

type JSONLeaf struct {
	Column   Column
	Field    Field
	Operator ir.OperatorID
	Mode     ir.ComparisonMode
	Path     ir.JSONPath
	Operand  ir.Operand
}

type ListLeaf

type ListLeaf struct {
	Column   Column
	Field    Field
	Operator ir.OperatorID
	Operand  ir.Operand
}

type Model

type Model struct {
	ID        ir.ModelID
	Namespace physical.PhysicalName
	Table     physical.PhysicalName
}

type PolicyAliasAllocator added in v0.1.0

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

PolicyAliasAllocator is a single-render ownership token. The ordinary SQL renderer shares one allocator across every independently compiled policy fragment in a statement so aliases remain globally unique even across nested scopes. It exposes no counter or allocated identifier.

func NewPolicyAliasAllocator added in v0.1.0

func NewPolicyAliasAllocator() *PolicyAliasAllocator

type PolicyRelationAliasFact added in v0.1.0

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

PolicyRelationAliasFact is one renderer-owned policy traversal identity. It deliberately retains no SQL, predicate, bind, actor, table, column, or provider object name. The opaque alias can only be compared with a provider plan alias; it cannot be extracted and reused as an identifier.

func (PolicyRelationAliasFact) Matches added in v0.1.0

func (fact PolicyRelationAliasFact) Matches(candidate string) bool

Matches compares an untrusted provider alias with the exact renderer-owned token. Zero or incomplete facts never match.

func (PolicyRelationAliasFact) ModelID added in v0.1.0

func (fact PolicyRelationAliasFact) ModelID() ir.ModelID

func (PolicyRelationAliasFact) RelationID added in v0.1.0

func (fact PolicyRelationAliasFact) RelationID() ir.RelationID

type Relation

type Relation struct {
	Model       ir.ModelID
	Field       ir.FieldID
	ID          ir.RelationID
	Target      ir.ModelID
	Cardinality ir.RelationCardinality
	Pairs       []Correlation
}

type Request

type Request struct {
	Condition        ir.Condition
	Provider         ir.Provider
	Resolver         Resolver
	Dialect          Dialect
	Capabilities     CapabilityProof
	BoundFingerprint [32]byte
	RootAlias        physical.PhysicalName
}

type Resolver

type Resolver interface {
	Providers() ir.ProviderSet
	SchemaFingerprint() [32]byte
	Model(provider ir.Provider, model ir.ModelID) (Model, bool)
	Field(provider ir.Provider, model ir.ModelID, field ir.FieldID) (Field, bool)
	Relation(model ir.ModelID, field ir.FieldID, relation ir.RelationID) (Relation, bool)
	EnumWire(enum ir.EnumID, value ir.EnumValueID) (string, bool)
	Capability(provider ir.Provider, capability ir.Capability) bool
}

Resolver publishes only descriptor-derived identities and physical names. Implementations must reject wrong-owner field identities.

func SchemaResolver

func SchemaResolver(registry *schema.Registry) Resolver

type ScalarLeaf

type ScalarLeaf struct {
	Column   Column
	Field    Field
	Operator ir.OperatorID
	Mode     ir.ComparisonMode
	Operand  ir.Operand
}

Jump to

Keyboard shortcuts

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