codegen

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package codegen renders a relalg.Expr (logical plan) into dialect-specific, fully parameterized SQL. User values always bind to placeholders — never string-interpolated — so generated SQL is inject-proof (invariant I3).

A Renderer holds a dialect.Dialect. Compile produces a Compiled value carrying the SQL, args, read-only flag, affected tables, and (when the caller supplies identity keys via WithPrimaryKey or WithIdentityKeys) an IsKeyPoint flag for the cost gate's whitelist and write guard.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupportedExpr = errors.New("codegen: unsupported expression")

ErrUnsupportedExpr is returned when the renderer encounters an expression it cannot render.

Functions

This section is empty.

Types

type CompileOption

type CompileOption func(*compileConfig)

CompileOption configures Compile.

func WithIdentityKeys added in v0.1.11

func WithIdentityKeys(keys ...[]string) CompileOption

WithIdentityKeys supplies the column sets that identify at most one row (the primary key and qualifying unique keys) so Compile can detect a point lookup on any of them; none means no lookup is a point. Without it the primary key is the only one.

func WithMaxINCardinality

func WithMaxINCardinality(max int) CompileOption

WithMaxINCardinality sets the maximum accepted IN/NOT IN list size. Values must be positive; invalid values fail compilation rather than disabling the bound.

func WithPrimaryKey

func WithPrimaryKey(cols ...string) CompileOption

WithPrimaryKey supplies the primary-key columns: returned by an insert that supports RETURNING, and the identity key unless WithIdentityKeys says otherwise.

type Compiled

type Compiled struct {
	Expr           relalg.Expr
	Kind           Kind
	SQL            string
	Args           []any
	IsKeyPoint     bool     // every column of an identity key matched by equality (gate whitelist)
	PrimaryKey     []string // primary-key columns, when supplied via WithPrimaryKey
	ReadOnly       bool
	AffectedTables []string // tables touched by a write (cache invalidation)
}

Compiled is the physical, executable product of a logical plan: the rendered SQL, bound args, and metadata consumed by the cost gate, cache, and store.

type Kind

type Kind string

Kind identifies the execution class of a compiled statement.

const (
	KindRead      Kind = "read"
	KindAggregate Kind = "aggregate"
	KindWrite     Kind = "write"
	KindCall      Kind = "call"
)

type Renderer

type Renderer struct {
	Dialect dialect.Dialect
}

Renderer renders relalg expressions for a single dialect.

func (Renderer) Compile

func (r Renderer) Compile(e relalg.Expr, opts ...CompileOption) (Compiled, error)

Compile renders e into a Compiled value. It applies inline lightweight transforms (IsKeyPoint detection) and capability-driven rendering (e.g. RETURNING). It does not run a separate optimizer: the IR is deliberately limited (no join/subquery), so predicate pushdown has nothing to push over.

Jump to

Keyboard shortcuts

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