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) ExpressionA 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, returningThe 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
| Type | Name() | Fields | Renders |
|---|---|---|---|
Select | select | Columns []string, Distinct bool | SELECT [DISTINCT] columns — empty columns render *; column expressions containing ( ) pass through unquoted. |
From | from | Table string | FROM "table" |
Where | where | Conds []Condition | WHERE … — groups joined by AND/OR. |
OrderBy | order | Columns []string | ORDER BY … — each column may carry DESC/ ASC. |
Limit | limit | N int | LIMIT ? — negative N renders nothing. |
Offset | offset | N int | OFFSET ? — N ≤ 0 renders nothing. |
Locking | locking | Enabled bool | FOR UPDATE when enabled. |
With | with | CTEName string, Subquery string | WITH "cte" AS (subquery) — empty CTEName renders nothing. |
From clause/write.go
| Type | Name() | Fields | Renders |
|---|---|---|---|
Insert | insert | Table string, Columns []string | INSERT INTO "table" ("col", …) |
Values | values | Rows []Expression | VALUES (?, …), (…) |
Assignment | — | Column string, Value Expression | One SET pair (used inside Set). |
Set | set | Assignments []Assignment | SET "col" = expr, … |
OnConflict | on_conflict | Columns []string, DoNothing bool, UpdateColumns []string, UpdateAll bool | ON CONFLICT (cols) DO NOTHING / DO UPDATE SET "col" = EXCLUDED."col" — gated on the dialect's InsertOnConflict feature. |
Returning | returning | Columns []string | RETURNING "col", … — gated on the dialect's Returning feature. |
Update | update | Table string | UPDATE "table" |
Delete | delete | Table string | DELETE 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
| Type | Name() | Fields | Renders |
|---|---|---|---|
Join | join | Type string, Table string, Alias string, On []Expression, Raw Expression, HasRaw bool | INNER/LEFT/RIGHT JOIN "table" "alias" ON … — relation joins set Table/Alias/On; raw joins set Raw (rendered verbatim). Empty Type defaults to INNER. |
Joins | join | Items []Join | The 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"