Skip to Content
fusion-orm
Core Concepts

The Statement

Per-chain state, the typed 16-slot clause registry, and the canonical build order.

A Statement belongs to exactly one chain instance; chainable methods clone it before mutating. It carries clauses, model/destination, vars, the connection pool, the execution context, SQL captured for raw/DryRun execution, and the plugin settings store.

// Statement carries per-chain state: clauses, model/destination, vars, the
// connection pool, execution context, SQL captured for raw/DryRun execution,
// and the plugin settings store. A Statement belongs to exactly one chain
// instance; chainable methods clone it before mutating.

The fields

type Statement struct {
	// DB is the chain instance that owns this statement.
	DB *DB
	// ConnPool is the pool queries run against: the root pool, or the
	// current transaction once Begin has swapped it in.
	ConnPool ConnPool
	// Context is propagated to every I/O call made through this statement.
	Context context.Context
	// SQL holds the assembled or raw statement text.
	SQL []byte
	// Vars holds the bound variables for SQL.
	Vars []any
	// Settings is a per-statement key/value store for plugins. It is
	// copied entry-by-entry on clone so chains never share settings.
	Settings *sync.Map

	// Model is the struct/slice used to derive table and columns.
	Model any
	// Dest is the destination passed to a finisher.
	Dest any
	// Table overrides the model-derived table name.
	Table string
	// Clauses holds custom (non-standard) clause builders by name,
	// allocated only when a plugin or Clauses() call registers one.
	Clauses map[string]clause.Interface
	// Order is the insertion order of custom clauses only.
	Order []string

	// RaiseErrorOnNotFound makes First/Take/Last return ErrRecordNotFound.
	RaiseErrorOnNotFound bool
	// SkipHooks suppresses model hooks for this statement.
	SkipHooks bool
	// SkipTimestamps suppresses automatic UpdatedAt maintenance
	// (UpdateColumn/UpdateColumns).
	SkipTimestamps bool
	// CreateBatchSize bounds rows per INSERT statement (0 = all rows).
	CreateBatchSize int
	// Preloads holds requested relation paths; each path is loaded in one
	// batched query, with nested paths loaded after their parent.
	Preloads []*preloadEntry
	// Joins holds explicit raw joins and relation names to LEFT JOIN.
	Joins []joinEntry
}

The 16-slot clause registry

Standard clauses don't live in a map. They ride in a fixed array indexed by canonical build order, with a bitmask recording presence — Add, Remove, Clause, and clone are allocation-free for the hot names.

// slotNames is the canonical clause build order. Index i in this array is
// the slot index for that clause; custom clauses follow after the slots.
var slotNames = [...]string{
	"with", "select", "insert", "update", "delete", "from", "join", "set", "values",
	"on_conflict", "where", "order", "limit", "offset", "locking", "returning",
}

The canonical build order (statement.go:73-76):

SlotClause name
0with
1select
2insert
3update
4delete
5from
6join
7set
8values
9on_conflict
10where
11order
12limit
13offset
14locking
15returning

BuildClauses walks the slots in this order, appends each present clause to a reused []byte buffer, then renders custom clauses in Order. This is why chaining order never changes output — Where before Select and Select before Where compile identically.

The registry API

// AddClause registers or replaces c. Standard clause names land in fixed
// slots (no allocation); custom names use the fallback map and keep their
// first-seen build order after the standard clauses.
func (stmt *Statement) AddClause(c clause.Interface)
// RemoveClause drops the named clause from the statement.
func (stmt *Statement) RemoveClause(name string)
// Clause returns the named clause, if present.
func (stmt *Statement) Clause(name string) clause.Interface

Two more you'll see in callbacks and plugins:

  • stmt.QuoteTo(ident) (statement.go:169) — quotes an identifier through the active dialect; useful when composing raw SQL strings.
  • stmt.BuildClauses() (statement.go:200) — renders all registered clauses in order, binds vars, and stores the result in SQL. Each build starts with a fresh var list.

Settings: the plugin key/value store

Settings is a *sync.Map for per-statement plugin state. It is copied entry-by-entry on clone, so chains never share settings — a value you set on a session cannot leak into a sibling chain.

// Set stores a plugin/step value on the statement settings.
func (stmt *Statement) Set(key string, value any)

// Get retrieves a statement setting.
func (stmt *Statement) Get(key string) (any, bool)

// Delete removes a statement setting.
func (stmt *Statement) Delete(key string)

A callback reads a setting the same way: stmt.Get("myplugin:key") inside a registered func(*DB) — the statement is reachable as db.Statement.

Seeing a statement without executing

DryRun captures the assembled SQL and vars on Statement instead of executing — this is the same mechanism ToSQL builds on:

tx := db.Session(&Session{DryRun: true}).Exec("SELECT * FROM users WHERE id = ?", 7)
// tx.Statement.SQL  == "SELECT * FROM users WHERE id = ?"
// tx.Statement.Vars == []any{7}
// the root config is untouched: db.DryRun == false

Order independence

Because standard clauses have fixed slots, the build order is a property of the registry, not your call order. Order on the Statement tracks the insertion order of custom clauses only.

Next

On this page