Skip to Content
fusion-orm
API Reference

Clause types

The clause.Interface contract, every clause struct with its Name() string, the canonical build order, and golden renderings.

The clause package holds pure SQL clause builders: they never perform I/O and never import the schema or session layers; they only append bytes through Builder, which binds vars and quotes identifiers via the dialect.

Interfaces

clause.Interface

type Interface interface {
	Name() string
	Build(Builder)
}

A named, orderable SQL clause. Name() selects the builder's slot in the canonical order; Build appends SQL.

clause.Builder

type Builder interface {
	WriteString(string)
	AppendByte(byte)
	WriteQuoted(string)
	AddVar(v any)
	Dialector() dialect.Dialector
}

The append/bind surface given to clauses. WriteQuoted quotes an identifier for the dialect; AddVar binds a variable and appends the placeholder (? or $1).

clause.Expression

type Expression struct {
	SQL  string
	Vars []any
}

func Expr(sql string, vars ...any) Expression

A SQL fragment with positional ? placeholders and vars. Expr creates one: each ? in sql consumes the next var. Build appends the expression, replacing ? with dialect placeholders; a ? beyond the available vars renders nothing.

clause.Condition

type Condition struct {
	Join string // "AND" (default) or "OR"
	// Conds renders a parenthesized nested group — used to keep OR groups
	// intact when another predicate is appended after them. When set, Exprs
	// is ignored.
	Conds []Condition
	Exprs []Expression
}

One joined group of a WHERE clause. Helpers mark the next group:

func Or(exprs ...Expression) Condition { return Condition{Join: "OR", Exprs: exprs} }
func And(exprs ...Expression) Condition { return Condition{Join: "AND", Exprs: exprs} }

Safe appends

Appending a predicate to a OR b yields (a OR b) AND x, never a OR b AND x — a condition holding multiple expressions is parenthesized unless it is the only one.

Canonical build order

Standard clauses are stored in 16 fixed slots; rendering follows this exact order, custom clauses after:

with, select, insert, update, delete, from, join, set, values,
on_conflict, where, order, limit, offset, locking, returning

The order is what makes one registry valid for all four statement shapes: a SELECT uses select/from/where…, an UPDATE uses update/set, an INSERT uses insert/values/on_conflict, and a DELETE uses delete — all from the same slot array.

Clause types

From clause/query.go

TypeName()FieldsRenders
SelectselectColumns []string, Distinct boolSELECT [DISTINCT] columns — empty columns render *; column expressions containing ( ) pass through unquoted.
FromfromTable stringFROM "table"
WherewhereConds []ConditionWHERE … — groups joined by AND/OR.
OrderByorderColumns []stringORDER BY … — each column may carry DESC/ ASC.
LimitlimitN intLIMIT ? — negative N renders nothing.
OffsetoffsetN intOFFSET ? — N ≤ 0 renders nothing.
LockinglockingEnabled boolFOR UPDATE when enabled.
WithwithCTEName string, Subquery stringWITH "cte" AS (subquery) — empty CTEName renders nothing.

From clause/write.go

TypeName()FieldsRenders
InsertinsertTable string, Columns []stringINSERT INTO "table" ("col", …)
ValuesvaluesRows []ExpressionVALUES (?, …), (…)
Assignment—Column string, Value ExpressionOne SET pair (used inside Set).
SetsetAssignments []AssignmentSET "col" = expr, …
OnConflicton_conflictColumns []string, DoNothing bool, UpdateColumns []string, UpdateAll boolON CONFLICT (cols) DO NOTHING / DO UPDATE SET "col" = EXCLUDED."col" — gated on the dialect's InsertOnConflict feature.
ReturningreturningColumns []stringRETURNING "col", … — gated on the dialect's Returning feature.
UpdateupdateTable stringUPDATE "table"
DeletedeleteTable stringDELETE FROM "table"

On OnConflict: UpdateAll expands to every non-primary-key column of the model; Statement.BuildClauses fills UpdateColumns and falls back to the primary key as the conflict target. An empty target renders no parentheses — never ON CONFLICT ().

From clause/join.go

TypeName()FieldsRenders
JoinjoinType string, Table string, Alias string, On []Expression, Raw Expression, HasRaw boolINNER/LEFT/RIGHT JOIN "table" "alias" ON … — relation joins set Table/Alias/On; raw joins set Raw (rendered verbatim). Empty Type defaults to INNER.
JoinsjoinItems []JoinThe set of JOIN clauses; the registry stores one instance so multiple joins keep their position in the canonical build order.

Golden renderings

From the clause test suite (clause/clause_test.go:26). Note the placeholder styles: SQLite uses ?, PostgreSQL uses $1:

WHERE "age" > ? OR "name" = ?
ON CONFLICT ("email") DO UPDATE SET "name" = EXCLUDED."name"
LEFT JOIN "profiles" "profile" ON "users"."id" = "profile"."user_id"

Next

On this page